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.

530 lines
11 KiB

7 years ago
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
4 years ago
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
4 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) :
  6. protocolPTZ(),
  7. m_mInquiryMutex()
  8. {
  9. m_pLog = basicLogCollection::getInstance();
  10. m_bLastCommandIssued = CMD_PTZ_NOOP;
  11. m_pSettings = Settings;
  12. m_bAuthMode = 0;
  13. #ifdef USE_PONVIF
  14. m_pOnVif = NULL;
  15. m_pProfile = NULL;
  16. #else
  17. m_pDevice = NULL;
  18. m_pPTZ = NULL;
  19. #endif
  20. memset(m_pDeviceURI, 0, MAX_PATH_LEN);
  21. memset(m_pUser, 0, MAX_CRED_LEN);
  22. memset(m_pPassword, 0, MAX_CRED_LEN);
  23. }
  24. protocolPTZ_ONVIF::protocolPTZ_ONVIF(CSTR ip, uint16_t port, CSTR username, CSTR password, uint8_t AuthMode) :
  25. protocolPTZ(),
  26. m_mInquiryMutex()
  27. {
  28. m_pLog = basicLogCollection::getInstance();
  29. m_bLastCommandIssued = CMD_PTZ_NOOP;
  30. m_pSettings = NULL;
  31. #ifdef USE_PONVIF
  32. m_pOnVif = NULL;
  33. m_pProfile = NULL;
  34. #else
  35. m_pDevice = NULL;
  36. m_pPTZ = NULL;
  37. #endif
  38. LOG(ip);
  39. LOG(username);
  40. LOG(password);
  41. m_bAuthMode = AuthMode;
  42. sprintf(m_pDeviceURI, "Auth Mode: [%d]", m_bAuthMode);
  43. LOG(m_pDeviceURI);
  44. if (port == 80)
  45. {
  46. sprintf(m_pDeviceURI, "http://%s/onvif/device_service", ip);
  47. }
  48. else
  49. {
  50. sprintf(m_pDeviceURI, "http://%s:%d/onvif/device_service", ip, port);
  51. }
  52. LOG(m_pDeviceURI);
  53. strncpy(m_pUser, username, MAX_CRED_LEN);
  54. strncpy(m_pPassword, password, MAX_CRED_LEN);
  55. }
  56. protocolPTZ_ONVIF::protocolPTZ_ONVIF(CSTR ctl_url, CSTR username, CSTR password, uint8_t AuthMode) :
  57. protocolPTZ(),
  58. m_mInquiryMutex()
  59. {
  60. m_pLog = basicLogCollection::getInstance();
  61. m_bLastCommandIssued = CMD_PTZ_NOOP;
  62. m_pSettings = NULL;
  63. #ifdef USE_PONVIF
  64. m_pOnVif = NULL;
  65. m_pProfile = NULL;
  66. #else
  67. m_pDevice = NULL;
  68. m_pPTZ = NULL;
  69. #endif
  70. LOG(ctl_url);
  71. LOG(username);
  72. LOG(password);
  73. m_bAuthMode = AuthMode;
  74. sprintf(m_pDeviceURI, "Auth Mode: [%d]", m_bAuthMode);
  75. LOG(m_pDeviceURI);
  76. strncpy(m_pDeviceURI, ctl_url, 250);
  77. strncpy(m_pUser, username, MAX_CRED_LEN);
  78. strncpy(m_pPassword, password, MAX_CRED_LEN);
  79. }
  80. protocolPTZ_ONVIF::~protocolPTZ_ONVIF() {}
  81. bool protocolPTZ_ONVIF::setIPAddress(CSTR ip)
  82. {
  83. if (ip == NULL)
  84. {
  85. return false;
  86. }
  87. if (strlen(ip) == 0)
  88. {
  89. return false;
  90. }
  91. sprintf(m_pDeviceURI, "http://%s/onvif/device_service", ip);
  92. // strncpy( m_pDeviceURI, ip, MAX_PATH_LEN );
  93. return true;
  94. }
  95. bool protocolPTZ_ONVIF::setUsername(CSTR username)
  96. {
  97. if (username == NULL)
  98. {
  99. return false;
  100. }
  101. if (strlen(username) == 0)
  102. {
  103. return false;
  104. }
  105. strncpy(m_pUser, username, MAX_CRED_LEN);
  106. return true;
  107. }
  108. bool protocolPTZ_ONVIF::setPassword(CSTR password)
  109. {
  110. if (password == NULL)
  111. {
  112. return false;
  113. }
  114. if (strlen(password) == 0)
  115. {
  116. return false;
  117. }
  118. strncpy(m_pPassword, password, MAX_CRED_LEN);
  119. return true;
  120. }
  121. bool protocolPTZ_ONVIF::setAuthMode(uint8_t Mode)
  122. {
  123. m_bAuthMode = Mode;
  124. return true;
  125. }
  126. bool protocolPTZ_ONVIF::create(void)
  127. {
  128. if (! protocolPTZ::create())
  129. {
  130. return false;
  131. }
  132. if (m_pSettings != NULL)
  133. {
  134. config_setting_t * pSetting;
  135. pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_USER);
  136. if (pSetting == NULL)
  137. {
  138. return false;
  139. }
  140. sprintf(m_pUser, "%s", config_setting_get_string(pSetting));
  141. pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_PASS);
  142. if (pSetting == NULL)
  143. {
  144. return false;
  145. }
  146. sprintf(m_pPassword, "%s", config_setting_get_string(pSetting));
  147. pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_DEV_URI);
  148. if (pSetting == NULL)
  149. {
  150. return false;
  151. }
  152. sprintf(m_pDeviceURI, "%s", config_setting_get_string(pSetting));
  153. };
  154. #ifdef DEBUG
  155. LOG(m_pDeviceURI);
  156. #endif
  157. if (strlen(m_pDeviceURI) < 4)
  158. {
  159. LOG_ERROR("No Device URI Is Set!");
  160. return false;
  161. };
  162. #ifdef USE_PONVIF
  163. m_pOnVif = new pOnvif();
  164. if (m_pOnVif == NULL)
  165. {
  166. LOG_ERROR("pOnvif NEW FAILURE!");
  167. m_bStatus = 0;
  168. return false;
  169. };
  170. m_pOnVif->setDeviceUri(m_pDeviceURI);
  171. m_pOnVif->setUsername(m_pUser);
  172. m_pOnVif->setPassword(m_pPassword);
  173. m_pOnVif->setAuthMode(m_bAuthMode);
  174. LOG("PROTOCREATE");
  175. if (m_pOnVif->create())
  176. {
  177. m_pProfile = m_pOnVif->getProfiles()->at(0);
  178. LOG("DONE");
  179. m_bStatus = 1;
  180. return true;
  181. }
  182. else
  183. {
  184. LOG_ERROR("pOnvif CREATE FAILURE!");
  185. delete(m_pOnVif);
  186. m_pOnVif = NULL;
  187. m_bStatus = 0;
  188. return false;
  189. };
  190. #else
  191. std::string A(m_pUser);
  192. std::string B(m_pPassword);
  193. std::string C(m_pDeviceURI);
  194. m_pDevice = new OnvifClientDevice(C, A, B);
  195. if (m_pDevice != NULL)
  196. {
  197. m_pPTZ = new OnvifClientPTZ(*m_pDevice);
  198. if (m_pPTZ != NULL)
  199. {
  200. _tptz__GetNodesResponse tNodes;
  201. int RetVal;
  202. RetVal = m_pPTZ->GetNodes(tNodes);
  203. if (RetVal == 0)
  204. {
  205. OnvifClientMedia media(*m_pDevice);
  206. _trt__GetProfilesResponse profiles;
  207. media.GetProfiles(profiles);
  208. m_pPTZToken = profiles.Profiles.at(0)->token; // PTZConfiguration->token;
  209. // m_pPTZToken = tNodes.PTZNode.at(0)->token;
  210. //_tptz__GetConfigurationOptionsResponse COResp;
  211. // int RetVal = m_pPTZ->GetConfigurationOptions(COResp, m_pToken);
  212. //_tptz__GetConfigurationResponse configResponce;
  213. // configResponce.PTZConfiguration = new tt__PTZConfiguration();
  214. // m_pPTZ->GetConfiguration(configResponce);
  215. // m_pToken = configResponce.PTZConfiguration->token;
  216. setConnectionStatus(CS_CONNECTED);
  217. m_bStatus = 1;
  218. return true;
  219. };
  220. };
  221. };
  222. #endif
  223. return false;
  224. }
  225. #ifndef USE_PONVIF
  226. bool protocolPTZ_ONVIF::initCamera(void)
  227. {
  228. if (m_bStatus == 0)
  229. {
  230. return false;
  231. }
  232. // if (m_bReplyRequired) if ( m_bLastCommandIssued != CMD_PTZ_NOOP ) return false;
  233. m_bLastCommandIssued = CMD_PTZ_INIT;
  234. m_pPTZ->Initialize();
  235. return true;
  236. }
  237. #endif
  238. bool protocolPTZ_ONVIF::move(uint32_t MoveMatrix)
  239. {
  240. // if( m_bStatus == 0 ) return false;
  241. #ifdef USE_PONVIF
  242. if (m_pProfile == NULL)
  243. {
  244. return false;
  245. }
  246. // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  247. float Speed_x, Speed_y, Speed_z;
  248. if (MoveMatrix > 0)
  249. {
  250. if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) == 0)
  251. {
  252. Speed_x = 0.0f;
  253. }
  254. else if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0)
  255. {
  256. Speed_x = -m_dPanSpeed;
  257. }
  258. else
  259. {
  260. Speed_x = m_dPanSpeed;
  261. };
  262. if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) == 0)
  263. {
  264. Speed_y = 0.0;
  265. }
  266. else if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0)
  267. {
  268. Speed_y = m_dTiltSpeed;
  269. }
  270. else
  271. {
  272. Speed_y = -m_dTiltSpeed;
  273. };
  274. if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0)
  275. {
  276. Speed_z = 0.0;
  277. }
  278. else if ((MoveMatrix & PTZ_DRIVE_ZOOM_WIDE) != 0)
  279. {
  280. Speed_z = m_dZoomSpeed;
  281. }
  282. else
  283. {
  284. Speed_z = -m_dZoomSpeed;
  285. };
  286. bool res = true;
  287. if ((MoveMatrix & (PTZ_DRIVE_PAN_MASK | PTZ_DRIVE_TILT_MASK)) != 0)
  288. {
  289. res = m_pOnVif->ptz_ContinuousMove(m_pProfile->profiletoken, Speed_x, Speed_y);
  290. };
  291. if (res)
  292. {
  293. if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) != 0)
  294. {
  295. res = m_pOnVif->ptz_ContinuousMoveZoom(m_pProfile->profiletoken, Speed_z);
  296. };
  297. };
  298. m_bStatus = (res) ? 1 : 0;
  299. }
  300. else
  301. {
  302. m_bStatus = (m_pOnVif->ptz_Stop(m_pProfile->profiletoken, true, true)) ? 1 : 0;
  303. };
  304. return (m_bStatus > 0);
  305. #else
  306. if (m_bStatus == 0)
  307. {
  308. return false;
  309. }
  310. // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  311. _tptz__ContinuousMoveResponse ContMoveResponse;
  312. tt__PTZSpeed tSpeed;
  313. tSpeed.PanTilt = new tt__Vector2D();
  314. tSpeed.Zoom = new tt__Vector1D();
  315. LONG64 to = 1000;
  316. int RetVal;
  317. if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) == 0)
  318. {
  319. tSpeed.PanTilt->x = 0.0;
  320. }
  321. else if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0)
  322. {
  323. tSpeed.PanTilt->x = -m_dPanSpeed;
  324. }
  325. else
  326. {
  327. tSpeed.PanTilt->x = m_dPanSpeed;
  328. };
  329. if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) == 0)
  330. {
  331. tSpeed.PanTilt->y = 0.0;
  332. }
  333. else if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0)
  334. {
  335. tSpeed.PanTilt->y = m_dTiltSpeed;
  336. }
  337. else
  338. {
  339. tSpeed.PanTilt->y = -m_dTiltSpeed;
  340. };
  341. if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0)
  342. {
  343. tSpeed.Zoom->x = 0.0;
  344. }
  345. else if ((MoveMatrix & PTZ_DRIVE_ZOOM_WIDE) != 0)
  346. {
  347. tSpeed.Zoom->x = m_dZoomSpeed;
  348. }
  349. else
  350. {
  351. tSpeed.Zoom->x = -m_dZoomSpeed;
  352. };
  353. RetVal = m_pPTZ->ContinuousMove(ContMoveResponse, tSpeed, to, m_pPTZToken);
  354. delete(tSpeed.PanTilt);
  355. delete(tSpeed.Zoom);
  356. if (RetVal)
  357. {
  358. return false;
  359. }
  360. return true;
  361. #endif
  362. }
  363. #include <math.h>
  364. bool protocolPTZ_ONVIF::waitMoveIsDone(void)
  365. {
  366. ptzStatus tVal;
  367. uint8_t TO = 12; // 3.6 second max
  368. while (m_pOnVif->ptz_GetStatus(m_pProfile->profiletoken, &tVal) && TO)
  369. {
  370. if (tVal.hasMoveStatus)
  371. {
  372. if (tVal.PanTiltIdle && tVal.ZoomIdle)
  373. {
  374. break;
  375. }
  376. }
  377. else
  378. {
  379. // if( ( fabs(tVal.pan - dPan) < 0.0005 ) && ( fabs( tVal.tilt - dTilt) < 0.0005 ) ) break;//&& ( fabs( tVal.zoom - dZoom ) < 0.05 ) ) break;
  380. };
  381. SLEEP(333);
  382. TO--;
  383. };
  384. if (TO)
  385. {
  386. #ifdef DEBUG_EXTRA
  387. LOG("Finished Move!");
  388. #endif
  389. return true;
  390. #ifdef DEBUG_EXTRA
  391. }
  392. else
  393. {
  394. LOG_ERROR("Error Fetching PTZ Status!");
  395. #endif
  396. };
  397. return false;
  398. }
  399. bool protocolPTZ_ONVIF::goAbsolute(float dPan, float dTilt, float dZoom)
  400. {
  401. #ifdef USE_PONVIF
  402. // if (m_bStatus == 0) return false;
  403. #ifdef DEBUG_EXTRA
  404. LOG("ABS Move");
  405. #endif
  406. if (m_pProfile == NULL)
  407. {
  408. LOG_ERROR("No PTZ Profile!!");
  409. return false;
  410. };
  411. if (dZoom < 0.0f)
  412. {
  413. dZoom = 0.0f;
  414. }
  415. if (m_pOnVif->ptz_AbsoluteMove(m_pProfile->profiletoken, dPan, dTilt, dZoom))
  416. {
  417. };
  418. return true;
  419. #else
  420. if (m_bStatus == 0)
  421. {
  422. return false;
  423. }
  424. // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  425. _tptz__AbsoluteMoveResponse AbsMoveResponse;
  426. tt__PTZSpeed tSpeed;
  427. tt__PTZVector tPos;
  428. tSpeed.PanTilt = new tt__Vector2D();
  429. tSpeed.Zoom = new tt__Vector1D();
  430. tSpeed.PanTilt->x = 1.f;
  431. tSpeed.PanTilt->y = 1.f;
  432. tSpeed.Zoom->x = 1.f;
  433. tPos.PanTilt = new tt__Vector2D();
  434. tPos.Zoom = new tt__Vector1D();
  435. tPos.PanTilt->x = dPan;
  436. tPos.PanTilt->y = dTilt;
  437. tPos.Zoom->x = dZoom;
  438. int RetVal;
  439. RetVal = m_pPTZ->AbsoluteMove(AbsMoveResponse, tSpeed, tPos, m_pPTZToken);
  440. delete(tSpeed.PanTilt);
  441. delete(tSpeed.Zoom);
  442. delete(tPos.PanTilt);
  443. delete(tPos.Zoom);
  444. return true;
  445. #endif
  446. }
  447. bool protocolPTZ_ONVIF::getCameraPresets(void)
  448. {
  449. LOG("GET PRESETS!");
  450. return m_pOnVif->ptz_GetPresets(m_pProfile->profiletoken);
  451. }
  452. bool protocolPTZ_ONVIF::goPreset(char * PresetToken)
  453. {
  454. #ifdef USE_PONVIF
  455. // if (m_bStatus == 0) return false;
  456. #ifdef DEBUG_EXTRA
  457. LOG("Preset Move");
  458. #endif
  459. if (m_pProfile == NULL)
  460. {
  461. LOG_ERROR("No PTZ Profile!!");
  462. return false;
  463. };
  464. if (! m_pOnVif->ptz_GotoPreset(m_pProfile->profiletoken, PresetToken, 1.0f, 1.0f, 1.0f))
  465. {
  466. m_bStatus = 0;
  467. }
  468. return (m_bStatus > 0);
  469. #else
  470. if (m_bStatus == 0)
  471. {
  472. return false;
  473. }
  474. // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  475. /*
  476. _tptz__AbsoluteMoveResponse AbsMoveResponse;
  477. tt__PTZSpeed tSpeed;
  478. tt__PTZVector tPos;
  479. tSpeed.PanTilt = new tt__Vector2D();
  480. tSpeed.Zoom = new tt__Vector1D();
  481. tSpeed.PanTilt->x = 1.f;
  482. tSpeed.PanTilt->y = 1.f;
  483. tSpeed.Zoom->x = 1.f;
  484. tPos.PanTilt = new tt__Vector2D();
  485. tPos.Zoom = new tt__Vector1D();
  486. tPos.PanTilt->x = dPan;
  487. tPos.PanTilt->y = dTilt;
  488. tPos.Zoom->x = dZoom;
  489. int RetVal;
  490. RetVal = m_pPTZ->AbsoluteMove(AbsMoveResponse, tSpeed, tPos, m_pPTZToken);
  491. delete(tSpeed.PanTilt);
  492. delete(tSpeed.Zoom);
  493. delete(tPos.PanTilt);
  494. delete(tPos.Zoom);
  495. */
  496. return true;
  497. #endif
  498. }
  499. bool protocolPTZ_ONVIF::validate()
  500. {
  501. return (m_bStatus > 0);
  502. }
  503. #undef LOG
  504. #undef LOG_ERROR