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.

370 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. if( m_pOnVif->create() ){
  125. m_pProfile = m_pOnVif->getProfiles()->at(0);
  126. return true;
  127. } else {
  128. LOG_ERROR("pOnvif CREATE FAILURE!");
  129. delete( m_pOnVif );
  130. m_pOnVif = NULL;
  131. return false;
  132. };
  133. #else
  134. std::string A(m_pUser);
  135. std::string B(m_pPassword);
  136. std::string C(m_pDeviceURI);
  137. m_pDevice = new OnvifClientDevice(C, A, B);
  138. if (m_pDevice != NULL){
  139. m_pPTZ = new OnvifClientPTZ(*m_pDevice);
  140. if (m_pPTZ!=NULL){
  141. _tptz__GetNodesResponse tNodes;
  142. int RetVal;
  143. RetVal = m_pPTZ->GetNodes(tNodes);
  144. if (RetVal == 0){
  145. OnvifClientMedia media(*m_pDevice);
  146. _trt__GetProfilesResponse profiles;
  147. media.GetProfiles(profiles);
  148. m_pPTZToken = profiles.Profiles.at(0)->token;// PTZConfiguration->token;
  149. //m_pPTZToken = tNodes.PTZNode.at(0)->token;
  150. //_tptz__GetConfigurationOptionsResponse COResp;
  151. //int RetVal = m_pPTZ->GetConfigurationOptions(COResp, m_pToken);
  152. //_tptz__GetConfigurationResponse configResponce;
  153. //configResponce.PTZConfiguration = new tt__PTZConfiguration();
  154. //m_pPTZ->GetConfiguration(configResponce);
  155. //m_pToken = configResponce.PTZConfiguration->token;
  156. setConnectionStatus(CS_CONNECTED);
  157. m_bStatus = 1;
  158. return true;
  159. };
  160. };
  161. };
  162. #endif
  163. return false;
  164. };
  165. #ifndef USE_PONVIF
  166. bool protocolPTZ_ONVIF::initCamera(void){
  167. if (m_bStatus == 0) return false;
  168. //if (m_bReplyRequired) if ( m_bLastCommandIssued != CMD_PTZ_NOOP ) return false;
  169. m_bLastCommandIssued = CMD_PTZ_INIT;
  170. m_pPTZ->Initialize();
  171. return true;
  172. };
  173. #endif
  174. bool protocolPTZ_ONVIF::move( uint32_t MoveMatrix ){
  175. //if( m_bStatus == 0 ) return false;
  176. #ifdef USE_PONVIF
  177. if( m_pProfile == NULL ) return false;
  178. //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  179. float Speed_x, Speed_y, Speed_z;
  180. if( MoveMatrix > 0 ) {
  181. if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) == 0){
  182. Speed_x = 0.0f;
  183. } else if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0){
  184. Speed_x = -m_dPanSpeed;
  185. } else {
  186. Speed_x = m_dPanSpeed;
  187. };
  188. if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) == 0){
  189. Speed_y = 0.0;
  190. } else if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0){
  191. Speed_y = m_dTiltSpeed;
  192. } else {
  193. Speed_y = -m_dTiltSpeed;
  194. };
  195. if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0){
  196. Speed_z = 0.0;
  197. } else if ((MoveMatrix & PTZ_DRIVE_ZOOM_WIDE) != 0){
  198. Speed_z = m_dZoomSpeed;
  199. } else {
  200. Speed_z = -m_dZoomSpeed;
  201. };
  202. bool res = true;
  203. if( ( MoveMatrix & ( PTZ_DRIVE_PAN_MASK | PTZ_DRIVE_TILT_MASK ) ) != 0 ) {
  204. res = m_pOnVif->ptz_ContinuousMove( m_pProfile->profiletoken, Speed_x , Speed_y );
  205. };
  206. if( res ){
  207. if(( MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0 ) {
  208. return true;
  209. };
  210. return m_pOnVif->ptz_ContinuousMoveZoom( m_pProfile->profiletoken, Speed_z );
  211. };
  212. } else {
  213. return m_pOnVif->ptz_Stop( m_pProfile->profiletoken, true, true );
  214. };
  215. return false;
  216. #else
  217. if (m_bStatus == 0) return false;
  218. //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  219. _tptz__ContinuousMoveResponse ContMoveResponse;
  220. tt__PTZSpeed tSpeed;
  221. tSpeed.PanTilt = new tt__Vector2D();
  222. tSpeed.Zoom = new tt__Vector1D();
  223. LONG64 to = 1000;
  224. int RetVal;
  225. if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) == 0){
  226. tSpeed.PanTilt->x = 0.0;
  227. } else if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0){
  228. tSpeed.PanTilt->x = -m_dPanSpeed;
  229. } else {
  230. tSpeed.PanTilt->x = m_dPanSpeed;
  231. };
  232. if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) == 0){
  233. tSpeed.PanTilt->y = 0.0;
  234. } else if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0){
  235. tSpeed.PanTilt->y = m_dTiltSpeed;
  236. } else {
  237. tSpeed.PanTilt->y = -m_dTiltSpeed;
  238. };
  239. if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0){
  240. tSpeed.Zoom->x = 0.0;
  241. } else if ((MoveMatrix & PTZ_DRIVE_ZOOM_WIDE) != 0){
  242. tSpeed.Zoom->x = m_dZoomSpeed;
  243. } else {
  244. tSpeed.Zoom->x = -m_dZoomSpeed;
  245. };
  246. RetVal = m_pPTZ->ContinuousMove(ContMoveResponse, tSpeed, to, m_pPTZToken);
  247. delete(tSpeed.PanTilt);
  248. delete(tSpeed.Zoom);
  249. if (RetVal) return false;
  250. return true;
  251. #endif
  252. };
  253. #include <math.h>
  254. bool protocolPTZ_ONVIF::goAbsolute( float dPan, float dTilt, float dZoom ){
  255. #ifdef USE_PONVIF
  256. //if (m_bStatus == 0) return false;
  257. #ifdef DEBUG_EXTRA
  258. LOG("ABS Move");
  259. #endif
  260. if( m_pProfile == NULL ){
  261. LOG_ERROR("No PTZ Profile!!");
  262. return false;
  263. };
  264. if ( dZoom < 0.0f ) dZoom = 0.0f;
  265. uint8_t TO = 10; // 3.6 second max
  266. if ( m_pOnVif->ptz_AbsoluteMove( m_pProfile->profiletoken, dPan, dTilt, dZoom ) ){
  267. SLEEP( 333 );
  268. ptzStatus tVal;
  269. while( m_pOnVif->ptz_GetStatus( m_pProfile->profiletoken, &tVal ) && TO ) {
  270. if( tVal.hasMoveStatus ) {
  271. if( tVal.PanTiltIdle && tVal.ZoomIdle ) break;
  272. } else {
  273. if( ( fabs(tVal.pan - dPan) < 0.0005 ) && ( fabs( tVal.tilt - dTilt) < 0.0005 ) ) break;//&& ( fabs( tVal.zoom - dZoom ) < 0.05 ) ) break;
  274. };
  275. SLEEP( 333 );
  276. TO--;
  277. };
  278. if( TO ) {
  279. #ifdef DEBUG_EXTRA
  280. LOG("Finished Move!");
  281. #endif
  282. return true;
  283. } else LOG_ERROR("Error Fetching PTZ Status!");
  284. };
  285. return true;
  286. #else
  287. if (m_bStatus == 0) return false;
  288. //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  289. _tptz__AbsoluteMoveResponse AbsMoveResponse;
  290. tt__PTZSpeed tSpeed;
  291. tt__PTZVector tPos;
  292. tSpeed.PanTilt = new tt__Vector2D();
  293. tSpeed.Zoom = new tt__Vector1D();
  294. tSpeed.PanTilt->x = 1.f;
  295. tSpeed.PanTilt->y = 1.f;
  296. tSpeed.Zoom->x = 1.f;
  297. tPos.PanTilt = new tt__Vector2D();
  298. tPos.Zoom = new tt__Vector1D();
  299. tPos.PanTilt->x = dPan;
  300. tPos.PanTilt->y = dTilt;
  301. tPos.Zoom->x = dZoom;
  302. int RetVal;
  303. RetVal = m_pPTZ->AbsoluteMove(AbsMoveResponse, tSpeed, tPos, m_pPTZToken);
  304. delete(tSpeed.PanTilt);
  305. delete(tSpeed.Zoom);
  306. delete(tPos.PanTilt);
  307. delete(tPos.Zoom);
  308. return true;
  309. #endif
  310. };
  311. bool protocolPTZ_ONVIF::goPreset( char *PresetToken ){
  312. #ifdef USE_PONVIF
  313. //if (m_bStatus == 0) return false;
  314. #ifdef DEBUG_EXTRA
  315. LOG("Preset Move");
  316. #endif
  317. if( m_pProfile == NULL ){
  318. LOG_ERROR("No PTZ Profile!!");
  319. return false;
  320. };
  321. return m_pOnVif->ptz_GotoPreset( m_pProfile->profiletoken, PresetToken, 1.0f, 1.0f, 1.0f );
  322. #else
  323. if (m_bStatus == 0) return false;
  324. //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  325. /*
  326. _tptz__AbsoluteMoveResponse AbsMoveResponse;
  327. tt__PTZSpeed tSpeed;
  328. tt__PTZVector tPos;
  329. tSpeed.PanTilt = new tt__Vector2D();
  330. tSpeed.Zoom = new tt__Vector1D();
  331. tSpeed.PanTilt->x = 1.f;
  332. tSpeed.PanTilt->y = 1.f;
  333. tSpeed.Zoom->x = 1.f;
  334. tPos.PanTilt = new tt__Vector2D();
  335. tPos.Zoom = new tt__Vector1D();
  336. tPos.PanTilt->x = dPan;
  337. tPos.PanTilt->y = dTilt;
  338. tPos.Zoom->x = dZoom;
  339. int RetVal;
  340. RetVal = m_pPTZ->AbsoluteMove(AbsMoveResponse, tSpeed, tPos, m_pPTZToken);
  341. delete(tSpeed.PanTilt);
  342. delete(tSpeed.Zoom);
  343. delete(tPos.PanTilt);
  344. delete(tPos.Zoom);
  345. */
  346. return true;
  347. #endif
  348. };
  349. #undef LOG
  350. #undef LOG_ERROR