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.

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