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.

295 lines
9.1 KiB

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