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.

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