You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

379 lines
11 KiB

7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
  1. #include "protocolPTZ_ONVIF.h"
  2. #include "../../hardware/uart/serialPort.h"
  3. #define LOG( X ) m_pLog->Log( "ONVIF", this, X )
  4. #define LOG_ERROR( X ) m_pLog->Log( "ONVIF", this, _LOG_ERROR, X )
  5. protocolPTZ_ONVIF::protocolPTZ_ONVIF( config_setting_t *Settings ) :protocolPTZ(), m_mInquiryMutex(){
  6. m_pLog = basicLogCollection::getInstance();
  7. m_bLastCommandIssued = CMD_PTZ_NOOP;
  8. m_pSettings = Settings;
  9. m_bAuthMode = 0;
  10. #ifdef USE_PONVIF
  11. m_pOnVif = NULL;
  12. m_pProfile = NULL;
  13. #else
  14. m_pDevice = NULL;
  15. m_pPTZ = NULL;
  16. #endif
  17. memset( m_pDeviceURI, 0, MAX_PATH_LEN );
  18. memset( m_pUser, 0, MAX_CRED_LEN );
  19. memset( m_pPassword, 0, MAX_CRED_LEN );
  20. };
  21. protocolPTZ_ONVIF::protocolPTZ_ONVIF( CSTR ip, uint16_t port, CSTR username, CSTR password, uint8_t AuthMode ) :protocolPTZ(), m_mInquiryMutex(){
  22. m_pLog = basicLogCollection::getInstance();
  23. m_bLastCommandIssued = CMD_PTZ_NOOP;
  24. m_pSettings = NULL;
  25. #ifdef USE_PONVIF
  26. m_pOnVif = NULL;
  27. m_pProfile = NULL;
  28. #else
  29. m_pDevice = NULL;
  30. m_pPTZ = NULL;
  31. #endif
  32. LOG( ip );
  33. LOG( username );
  34. LOG( password );
  35. m_bAuthMode = AuthMode;
  36. sprintf( m_pDeviceURI, "Auth Mode: [%d]", m_bAuthMode );
  37. LOG( m_pDeviceURI );
  38. if ( port == 80 ) {
  39. sprintf(m_pDeviceURI, "http://%s/onvif/device_service", ip );
  40. } else {
  41. sprintf(m_pDeviceURI, "http://%s:%d/onvif/device_service", ip, port );
  42. }
  43. LOG( m_pDeviceURI );
  44. strncpy( m_pUser, username, MAX_CRED_LEN );
  45. strncpy( m_pPassword, password, MAX_CRED_LEN );
  46. };
  47. protocolPTZ_ONVIF::protocolPTZ_ONVIF( CSTR ctl_url, CSTR username, CSTR password, uint8_t AuthMode ) :protocolPTZ(), m_mInquiryMutex(){
  48. m_pLog = basicLogCollection::getInstance();
  49. m_bLastCommandIssued = CMD_PTZ_NOOP;
  50. m_pSettings = NULL;
  51. #ifdef USE_PONVIF
  52. m_pOnVif = NULL;
  53. m_pProfile = NULL;
  54. #else
  55. m_pDevice = NULL;
  56. m_pPTZ = NULL;
  57. #endif
  58. LOG( ctl_url );
  59. LOG( username );
  60. LOG( password );
  61. m_bAuthMode = AuthMode;
  62. sprintf( m_pDeviceURI, "Auth Mode: [%d]", m_bAuthMode );
  63. LOG( m_pDeviceURI );
  64. strncpy( m_pDeviceURI, ctl_url, 250 );
  65. strncpy( m_pUser, username, MAX_CRED_LEN );
  66. strncpy( m_pPassword, password, MAX_CRED_LEN );
  67. };
  68. protocolPTZ_ONVIF::~protocolPTZ_ONVIF(){
  69. };
  70. bool protocolPTZ_ONVIF::setIPAddress( CSTR ip ){
  71. if( ip== NULL ) return false;
  72. if( strlen(ip) == 0 ) return false;
  73. sprintf( m_pDeviceURI, "http://%s/onvif/device_service", ip );
  74. //strncpy( m_pDeviceURI, ip, MAX_PATH_LEN );
  75. return true;
  76. };
  77. bool protocolPTZ_ONVIF::setUsername( CSTR username ){
  78. if( username == NULL ) return false;
  79. if( strlen(username) == 0 ) return false;
  80. strncpy( m_pUser, username, MAX_CRED_LEN );
  81. return true;
  82. };
  83. bool protocolPTZ_ONVIF::setPassword( CSTR password ){
  84. if( password == NULL ) return false;
  85. if( strlen(password) == 0 ) return false;
  86. strncpy( m_pPassword, password, MAX_CRED_LEN );
  87. return true;
  88. };
  89. bool protocolPTZ_ONVIF::setAuthMode( uint8_t Mode ){
  90. m_bAuthMode = Mode;
  91. return true;
  92. };
  93. bool protocolPTZ_ONVIF::create( void ){
  94. if (!protocolPTZ::create()) return false;
  95. if( m_pSettings!= NULL ) {
  96. config_setting_t *pSetting;
  97. pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_USER);
  98. if (pSetting == NULL) return false;
  99. sprintf( m_pUser, "%s", config_setting_get_string(pSetting));
  100. pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_PASS);
  101. if (pSetting == NULL) return false;
  102. sprintf( m_pPassword, "%s", config_setting_get_string(pSetting));
  103. pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_DEV_URI);
  104. if (pSetting == NULL) return false;
  105. sprintf( m_pDeviceURI, "%s", config_setting_get_string(pSetting));
  106. };
  107. #ifdef DEBUG
  108. LOG( m_pDeviceURI );
  109. #endif
  110. if( strlen( m_pDeviceURI ) < 4 ){
  111. LOG_ERROR("No Device URI Is Set!");
  112. return false;
  113. };
  114. #ifdef USE_PONVIF
  115. m_pOnVif = new pOnvif();
  116. if( m_pOnVif == NULL ){
  117. LOG_ERROR("pOnvif NEW FAILURE!");
  118. return false;
  119. };
  120. m_pOnVif->setDeviceUri( m_pDeviceURI );
  121. m_pOnVif->setUsername( m_pUser );
  122. m_pOnVif->setPassword( m_pPassword );
  123. m_pOnVif->setAuthMode( m_bAuthMode );
  124. LOG("PROTOCREATE");
  125. if( m_pOnVif->create() ){
  126. m_pProfile = m_pOnVif->getProfiles()->at(0);
  127. LOG("DONE");
  128. return true;
  129. } else {
  130. LOG_ERROR("pOnvif CREATE FAILURE!");
  131. delete( m_pOnVif );
  132. m_pOnVif = NULL;
  133. return false;
  134. };
  135. #else
  136. std::string A(m_pUser);
  137. std::string B(m_pPassword);
  138. std::string C(m_pDeviceURI);
  139. m_pDevice = new OnvifClientDevice(C, A, B);
  140. if (m_pDevice != NULL){
  141. m_pPTZ = new OnvifClientPTZ(*m_pDevice);
  142. if (m_pPTZ!=NULL){
  143. _tptz__GetNodesResponse tNodes;
  144. int RetVal;
  145. RetVal = m_pPTZ->GetNodes(tNodes);
  146. if (RetVal == 0){
  147. OnvifClientMedia media(*m_pDevice);
  148. _trt__GetProfilesResponse profiles;
  149. media.GetProfiles(profiles);
  150. m_pPTZToken = profiles.Profiles.at(0)->token;// PTZConfiguration->token;
  151. //m_pPTZToken = tNodes.PTZNode.at(0)->token;
  152. //_tptz__GetConfigurationOptionsResponse COResp;
  153. //int RetVal = m_pPTZ->GetConfigurationOptions(COResp, m_pToken);
  154. //_tptz__GetConfigurationResponse configResponce;
  155. //configResponce.PTZConfiguration = new tt__PTZConfiguration();
  156. //m_pPTZ->GetConfiguration(configResponce);
  157. //m_pToken = configResponce.PTZConfiguration->token;
  158. setConnectionStatus(CS_CONNECTED);
  159. m_bStatus = 1;
  160. return true;
  161. };
  162. };
  163. };
  164. #endif
  165. return false;
  166. };
  167. #ifndef USE_PONVIF
  168. bool protocolPTZ_ONVIF::initCamera(void){
  169. if (m_bStatus == 0) return false;
  170. //if (m_bReplyRequired) if ( m_bLastCommandIssued != CMD_PTZ_NOOP ) return false;
  171. m_bLastCommandIssued = CMD_PTZ_INIT;
  172. m_pPTZ->Initialize();
  173. return true;
  174. };
  175. #endif
  176. bool protocolPTZ_ONVIF::move( uint32_t MoveMatrix ){
  177. //if( m_bStatus == 0 ) return false;
  178. #ifdef USE_PONVIF
  179. if( m_pProfile == NULL ) return false;
  180. //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  181. float Speed_x, Speed_y, Speed_z;
  182. if( MoveMatrix > 0 ) {
  183. if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) == 0){
  184. Speed_x = 0.0f;
  185. } else if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0){
  186. Speed_x = -m_dPanSpeed;
  187. } else {
  188. Speed_x = m_dPanSpeed;
  189. };
  190. if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) == 0){
  191. Speed_y = 0.0;
  192. } else if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0){
  193. Speed_y = m_dTiltSpeed;
  194. } else {
  195. Speed_y = -m_dTiltSpeed;
  196. };
  197. if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0){
  198. Speed_z = 0.0;
  199. } else if ((MoveMatrix & PTZ_DRIVE_ZOOM_WIDE) != 0){
  200. Speed_z = m_dZoomSpeed;
  201. } else {
  202. Speed_z = -m_dZoomSpeed;
  203. };
  204. bool res = true;
  205. if( ( MoveMatrix & ( PTZ_DRIVE_PAN_MASK | PTZ_DRIVE_TILT_MASK ) ) != 0 ) {
  206. res = m_pOnVif->ptz_ContinuousMove( m_pProfile->profiletoken, Speed_x , Speed_y );
  207. };
  208. if( res ){
  209. if(( MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0 ) {
  210. return true;
  211. };
  212. return m_pOnVif->ptz_ContinuousMoveZoom( m_pProfile->profiletoken, Speed_z );
  213. };
  214. } else {
  215. return m_pOnVif->ptz_Stop( m_pProfile->profiletoken, true, true );
  216. };
  217. return false;
  218. #else
  219. if (m_bStatus == 0) return false;
  220. //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  221. _tptz__ContinuousMoveResponse ContMoveResponse;
  222. tt__PTZSpeed tSpeed;
  223. tSpeed.PanTilt = new tt__Vector2D();
  224. tSpeed.Zoom = new tt__Vector1D();
  225. LONG64 to = 1000;
  226. int RetVal;
  227. if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) == 0){
  228. tSpeed.PanTilt->x = 0.0;
  229. } else if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0){
  230. tSpeed.PanTilt->x = -m_dPanSpeed;
  231. } else {
  232. tSpeed.PanTilt->x = m_dPanSpeed;
  233. };
  234. if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) == 0){
  235. tSpeed.PanTilt->y = 0.0;
  236. } else if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0){
  237. tSpeed.PanTilt->y = m_dTiltSpeed;
  238. } else {
  239. tSpeed.PanTilt->y = -m_dTiltSpeed;
  240. };
  241. if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0){
  242. tSpeed.Zoom->x = 0.0;
  243. } else if ((MoveMatrix & PTZ_DRIVE_ZOOM_WIDE) != 0){
  244. tSpeed.Zoom->x = m_dZoomSpeed;
  245. } else {
  246. tSpeed.Zoom->x = -m_dZoomSpeed;
  247. };
  248. RetVal = m_pPTZ->ContinuousMove(ContMoveResponse, tSpeed, to, m_pPTZToken);
  249. delete(tSpeed.PanTilt);
  250. delete(tSpeed.Zoom);
  251. if (RetVal) return false;
  252. return true;
  253. #endif
  254. };
  255. #include <math.h>
  256. bool protocolPTZ_ONVIF::waitMoveIsDone( void ){
  257. ptzStatus tVal;
  258. uint8_t TO = 10; // 3.6 second max
  259. SLEEP( 333 );
  260. while( m_pOnVif->ptz_GetStatus( m_pProfile->profiletoken, &tVal ) && TO ) {
  261. if( tVal.hasMoveStatus ) {
  262. if( tVal.PanTiltIdle && tVal.ZoomIdle ) break;
  263. } else {
  264. //if( ( fabs(tVal.pan - dPan) < 0.0005 ) && ( fabs( tVal.tilt - dTilt) < 0.0005 ) ) break;//&& ( fabs( tVal.zoom - dZoom ) < 0.05 ) ) break;
  265. };
  266. SLEEP( 333 );
  267. TO--;
  268. };
  269. if( TO ) {
  270. #ifdef DEBUG_EXTRA
  271. LOG("Finished Move!");
  272. #endif
  273. return true;
  274. }; //else LOG_ERROR("Error Fetching PTZ Status!");
  275. return false;
  276. };
  277. bool protocolPTZ_ONVIF::goAbsolute( float dPan, float dTilt, float dZoom ){
  278. #ifdef USE_PONVIF
  279. //if (m_bStatus == 0) return false;
  280. #ifdef DEBUG_EXTRA
  281. LOG("ABS Move");
  282. #endif
  283. if( m_pProfile == NULL ){
  284. LOG_ERROR("No PTZ Profile!!");
  285. return false;
  286. };
  287. if ( dZoom < 0.0f ) dZoom = 0.0f;
  288. if ( m_pOnVif->ptz_AbsoluteMove( m_pProfile->profiletoken, dPan, dTilt, dZoom ) ){
  289. };
  290. return true;
  291. #else
  292. if (m_bStatus == 0) return false;
  293. //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  294. _tptz__AbsoluteMoveResponse AbsMoveResponse;
  295. tt__PTZSpeed tSpeed;
  296. tt__PTZVector tPos;
  297. tSpeed.PanTilt = new tt__Vector2D();
  298. tSpeed.Zoom = new tt__Vector1D();
  299. tSpeed.PanTilt->x = 1.f;
  300. tSpeed.PanTilt->y = 1.f;
  301. tSpeed.Zoom->x = 1.f;
  302. tPos.PanTilt = new tt__Vector2D();
  303. tPos.Zoom = new tt__Vector1D();
  304. tPos.PanTilt->x = dPan;
  305. tPos.PanTilt->y = dTilt;
  306. tPos.Zoom->x = dZoom;
  307. int RetVal;
  308. RetVal = m_pPTZ->AbsoluteMove(AbsMoveResponse, tSpeed, tPos, m_pPTZToken);
  309. delete(tSpeed.PanTilt);
  310. delete(tSpeed.Zoom);
  311. delete(tPos.PanTilt);
  312. delete(tPos.Zoom);
  313. return true;
  314. #endif
  315. };
  316. bool protocolPTZ_ONVIF::goPreset( char *PresetToken ){
  317. #ifdef USE_PONVIF
  318. //if (m_bStatus == 0) return false;
  319. #ifdef DEBUG_EXTRA
  320. LOG("Preset Move");
  321. #endif
  322. if( m_pProfile == NULL ){
  323. LOG_ERROR("No PTZ Profile!!");
  324. return false;
  325. };
  326. return m_pOnVif->ptz_GotoPreset( m_pProfile->profiletoken, PresetToken, 1.0f, 1.0f, 1.0f );
  327. #else
  328. if (m_bStatus == 0) return false;
  329. //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  330. /*
  331. _tptz__AbsoluteMoveResponse AbsMoveResponse;
  332. tt__PTZSpeed tSpeed;
  333. tt__PTZVector tPos;
  334. tSpeed.PanTilt = new tt__Vector2D();
  335. tSpeed.Zoom = new tt__Vector1D();
  336. tSpeed.PanTilt->x = 1.f;
  337. tSpeed.PanTilt->y = 1.f;
  338. tSpeed.Zoom->x = 1.f;
  339. tPos.PanTilt = new tt__Vector2D();
  340. tPos.Zoom = new tt__Vector1D();
  341. tPos.PanTilt->x = dPan;
  342. tPos.PanTilt->y = dTilt;
  343. tPos.Zoom->x = dZoom;
  344. int RetVal;
  345. RetVal = m_pPTZ->AbsoluteMove(AbsMoveResponse, tSpeed, tPos, m_pPTZToken);
  346. delete(tSpeed.PanTilt);
  347. delete(tSpeed.Zoom);
  348. delete(tPos.PanTilt);
  349. delete(tPos.Zoom);
  350. */
  351. return true;
  352. #endif
  353. };
  354. #undef LOG
  355. #undef LOG_ERROR