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.

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