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.

317 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "kernelModuleBosch.h"
  2. // #include "mojoKernel.h"
  3. #define LOG(X) kernelModule::m_pLog->Log("MOD_BVMS", this, X)
  4. #ifdef DEBUG
  5. #define LOG_ERROR(X) \
  6. { \
  7. char _tBfrErrMSG2w34[256]; \
  8. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  9. kernelModule::m_pLog->Log("MOD_BVMS", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  10. }
  11. #else
  12. #define LOG_ERROR(X) kernelModule::m_pLog->Log("MOD_BVMS", this, _LOG_ERROR, X);
  13. #endif
  14. const CSTR bmvs_zones[] = { "C2", "C3", "D1", "D2", "E1", "E2", "E3", "F1", "F2", "G1", "G2", "G3", "H1", "H2", "C1", "B2", "B1", "A3", "A2", "A1", "" };
  15. const uint8_t bmvs_zones_count = 20;
  16. kernelModuleBosch::kernelModuleBosch(uint32_t ModuleID) :
  17. kernelModule(ModuleID, KERNEL_MODULE_BOSCH_REST){
  18. m_pCURL = NULL;
  19. m_pHttpAccessManager = NULL;
  20. m_pEventHeap = basicEventHeap::getInstance();
  21. }
  22. kernelModuleBosch::~kernelModuleBosch(){
  23. m_bEnabled = false;
  24. if ( m_pCURL ){
  25. curl_easy_cleanup(m_pCURL);
  26. m_pCURL = NULL;
  27. };
  28. }
  29. bool kernelModuleBosch::startService(){
  30. m_pHttpAccessManager = curlRequestsManager::getInstance();
  31. if( !m_bEnabled ) return false;
  32. if ( m_pCURL ) return true;
  33. if ( reloadSettings() ) return true;
  34. return false;
  35. }
  36. bool kernelModuleBosch::stopService(bool force){
  37. m_bEnabled = false;
  38. return true;
  39. }
  40. bool kernelModuleBosch::reloadSettings(){
  41. if (m_pDBC == NULL) return false;
  42. if (! m_bEnabled) return false;
  43. REST_SETTINGS * tData = m_pDBC->getBoschData( m_nDBAUXID );
  44. if ( tData != NULL ){
  45. memcpy( &m_sRestSettings, tData, sizeof(REST_SETTINGS));
  46. free( tData );
  47. return true;
  48. };
  49. return false;
  50. }
  51. bool kernelModuleBosch::_send_request(CSTR url, CSTR post_string ){
  52. #ifdef CURL_MANAGER
  53. bool RetVal = false;
  54. if (m_pHttpAccessManager){
  55. m_mResponseProtector.lock();
  56. curlRequest * tRC;
  57. tRC = new curlRequest( url, (CSTR) "Content-Type: application/json; charset=utf-8;");
  58. if (tRC != NULL){
  59. #ifdef DEBUG_BVMS_TRAFFIC
  60. FILE * tf;
  61. tf = fopen("Traffic_bvms.txt", "a+");
  62. fprintf(tf, "--------------------\r\n");
  63. if ( post_string != NULL ) {
  64. fprintf( tf, "REQ: %s (%d)\n", url, strlen(post_string));
  65. fprintf( tf, ">>>>> %s\n", post_string );
  66. } else fprintf(tf, "REQ: %s\n", url );
  67. fprintf(tf,"Creds: [%s]:[%s]\n", m_sRestSettings.access_user, m_sRestSettings.access_pass );
  68. fprintf(tf, "--------------------\r\n");
  69. fclose(tf);
  70. //bool mustReturnData = true; // In Debug Allways Read Return
  71. #endif
  72. tRC->setCredentials( m_sRestSettings.access_user, m_sRestSettings.access_pass, m_sRestSettings.auth_mode );
  73. if( post_string != NULL ){
  74. tRC->setPOST(post_string, strlen(post_string));
  75. };
  76. RetVal = m_pHttpAccessManager->accomplish( tRC );
  77. } else {
  78. LOG_ERROR("Cannot Create curlRequest!");
  79. };
  80. m_mResponseProtector.unlock();
  81. };
  82. return RetVal;
  83. #else
  84. char tBuffer[CTEXT];
  85. if (m_pCURL == NULL)
  86. {
  87. if (m_pLastResponse == NULL)
  88. {
  89. m_pLastResponse = (CSTR) malloc(DEFAULT_BUFFER_SIZE);
  90. };
  91. m_pCURL = curl_easy_init();
  92. if (m_pProxyHost != NULL && m_nProxyPort != 0)
  93. {
  94. curl_easy_setopt(m_pCURL, CURLOPT_PROXY, m_pProxyHost);
  95. curl_easy_setopt(m_pCURL, CURLOPT_PROXYPORT, m_nProxyPort);
  96. if (m_pProxyUsername != NULL && m_pProxyPassword != NULL)
  97. {
  98. sprintf(tBuffer, "%s:%s", m_pProxyUsername, m_pProxyPassword);
  99. curl_easy_setopt(m_pCURL, CURLOPT_PROXYUSERPWD, tBuffer);
  100. };
  101. };
  102. curl_easy_setopt(m_pCURL, CURLOPT_CONNECTTIMEOUT, 1);
  103. curl_easy_setopt(m_pCURL, CURLOPT_TIMEOUT, 2);
  104. curl_easy_setopt(m_pCURL, CURLOPT_SSL_VERIFYPEER, 0);
  105. curl_easy_setopt(m_pCURL, CURLOPT_SSL_VERIFYHOST, 0);
  106. curl_easy_setopt(m_pCURL, CURLOPT_VERBOSE, 0);
  107. curl_easy_setopt(m_pCURL, CURLOPT_POST, 1);
  108. // curl_easy_setopt( m_pCURL, CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS, 0 );
  109. // curl_easy_setopt( m_pCURL, CURLOPT_NOPROGRESS, 1 );
  110. // curl_easy_setopt( m_pCURL, CURLOPT_NOSIGNAL, 1 );
  111. curl_easy_setopt(m_pCURL, CURLOPT_TCP_FASTOPEN, 1);
  112. curl_easy_setopt(m_pCURL, CURLOPT_TCP_NODELAY, 1);
  113. // curl_easy_setopt( m_pCURL, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
  114. curl_easy_setopt(m_pCURL, CURLOPT_WRITEFUNCTION, pOnvif::_writer);
  115. curl_easy_setopt(m_pCURL, CURLOPT_WRITEDATA, this);
  116. };
  117. if (m_pCURL != NULL)
  118. {
  119. curl_easy_setopt(m_pCURL, CURLOPT_URL, url);
  120. struct curl_slist * sList = NULL;
  121. // Content-Type: application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/ContinuousMove"
  122. // Connection: Close
  123. sList = curl_slist_append(sList, "Content-Type: text/xml; charset=utf-8");
  124. sprintf(tBuffer, "Content-Length: %d", strlen(post_string));
  125. sList = curl_slist_append(sList, tBuffer);
  126. curl_easy_setopt(m_pCURL, CURLOPT_HTTPHEADER, sList);
  127. curl_easy_setopt(m_pCURL, CURLOPT_POSTFIELDS, post_string);
  128. sprintf(tBuffer, "%s:%s", m_pUsername, m_pPassword);
  129. curl_easy_setopt(m_pCURL, CURLOPT_USERPWD, tBuffer);
  130. switch (m_bAuthMode)
  131. {
  132. case 0: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); break;
  133. case 1: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); break;
  134. case 2: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST_IE); break;
  135. case 3: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); break;
  136. case 4: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NEGOTIATE); break;
  137. case 5: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); break;
  138. case 6: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM_WB); break;
  139. case 7: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANY); break;
  140. case 8: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE); break;
  141. case 9:
  142. curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ONLY);
  143. break;
  144. // case 10: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_AWS_SIGV4 ); break;
  145. };
  146. #ifdef DEBUG
  147. char tErrorBuffer[CURL_ERROR_SIZE];
  148. curl_easy_setopt(m_pCURL, CURLOPT_ERRORBUFFER, tErrorBuffer);
  149. #endif
  150. m_pLastIndex = 0;
  151. if (curl_easy_perform(m_pCURL))
  152. {
  153. LOG_ERROR("CURL Failure!");
  154. #ifdef DEBUG
  155. LOG_ERROR(tErrorBuffer);
  156. #endif
  157. }
  158. else
  159. {
  160. m_pLastResponse[m_pLastIndex] = (char) 0;
  161. return true;
  162. };
  163. m_pLastIndex = 0;
  164. };
  165. return false;
  166. #endif
  167. }
  168. bool kernelModuleBosch::buildTargetEventMsg( CSTR Buffer, float az, float range, float lat, float lon, float alt, uint64_t extID, CSTR src_Name ){
  169. if ( Buffer == NULL ) return false;
  170. char tTS[64];
  171. time_t LocalTime;
  172. time(&LocalTime);
  173. tm gmtt;
  174. tm * gmt_time = &gmtt;
  175. if( gmtime_r( &LocalTime, &gmtt ) == NULL) return false;
  176. CSTR pp = strstr( src_Name, "_" );
  177. uint8_t zid = bmvs_zones_count;
  178. if( pp!= NULL ) zid = atoi( pp+1 )-1;
  179. if ( zid > bmvs_zones_count ) zid = bmvs_zones_count;
  180. sprintf(tTS, "%04d-%02d-%02d %02d:%02d:%02d", 1900 + gmt_time->tm_year, 1 + gmt_time->tm_mon, gmt_time->tm_mday, gmt_time->tm_hour, gmt_time->tm_min, gmt_time->tm_sec);
  181. //sprintf(tTS, "%04d-%02d-%02dT%02d:%02d:%02dZ", 1900 + gmt_time->tm_year, 1 + gmt_time->tm_mon, gmt_time->tm_mday, gmt_time->tm_hour, gmt_time->tm_min, gmt_time->tm_sec);
  182. sprintf( Buffer, "{\"externalAlarmId\":%d,\"data\":[%0.2f,%0.1f,%0.12f,%0.12f,%0.3f,10,\"%s\",\"%s\",\"%s\",\"Detection Event\"]}",
  183. m_sRestSettings.detection_src,
  184. az, range,
  185. lat, lon, alt,
  186. src_Name,
  187. bmvs_zones[zid],
  188. tTS
  189. );
  190. //sprintf( Buffer, "{\"azimuth\": %0.2f,\"distance\": %0.2f,\"name\": \"RADAR1_DFSL\",\"position\": {\"alt\": %0.2f,\"lat\": %0.8f,\"lon\": %0.8f},\"target_coordinates\": {\"alt\": %0.2f,\"lat\": %0.8f,\"lon\": %0.8f},\"timestamp\": \"%s\"}",
  191. // az, range,
  192. // alt, lat, lon,
  193. // alt, lat, lon,
  194. // tTS
  195. //);
  196. return true;
  197. }
  198. bool kernelModuleBosch::buildCameraEventMsg( CSTR Buffer, CSTR CamName, uint8_t Status ){
  199. if ( Buffer == NULL ) return false;
  200. char tTS[64];
  201. time_t LocalTime;
  202. time(&LocalTime);
  203. tm gmtt;
  204. tm * gmt_time = &gmtt;
  205. if( gmtime_r( &LocalTime, &gmtt ) == NULL) return false;
  206. sprintf( tTS, "%04d-%02d-%02d %02d:%02d:%02d", 1900 + gmt_time->tm_year, 1 + gmt_time->tm_mon, gmt_time->tm_mday, gmt_time->tm_hour, gmt_time->tm_min, gmt_time->tm_sec );
  207. sprintf( Buffer, "{\"externalAlarmId\":%d,\"data\":[\"%s\",%d,\"%s\",\"Camera Event\"]}",
  208. m_sRestSettings.camera_src,
  209. CamName,
  210. Status,
  211. tTS
  212. );
  213. return true;
  214. }
  215. bool kernelModuleBosch::dispatchEvent(EVENT_CONTAINER * Evt){
  216. if( Evt == NULL ) return false;
  217. if( !m_bEnabled ) return true;
  218. char tBuffer[1024];
  219. char tURL[256];
  220. #ifdef DEBUG_EXTRA
  221. sprintf(tBuffer, "Event Class %d Type %d ", Evt->m_eClass, Evt->m_eType);
  222. LOG(tBuffer);
  223. #endif
  224. switch (Evt->m_eType){
  225. case EVT_ROI_ACTIVE:
  226. case EVT_TARGET_2D_BLACK:
  227. case EVT_TARGET_2D_WHITE:{
  228. if ( m_sRestSettings.detection_enabled ) {
  229. TARGET_DATA * tTARGET = (TARGET_DATA *) Evt->m_pEvtData;
  230. if (tTARGET != NULL){
  231. CSTR tN = m_pDBC->getNodeName( Evt->m_nSourceNodeID );
  232. if ( tN ) {
  233. if ( buildTargetEventMsg(
  234. tBuffer,
  235. tTARGET->local_target.fi,
  236. tTARGET->local_target.r,
  237. tTARGET->global_target.lat,
  238. tTARGET->global_target.lon,
  239. tTARGET->global_target.alt,
  240. Evt->m_nSourceNodeID,
  241. tN
  242. ) ) {
  243. if ( m_sRestSettings.use_ssl ) {
  244. sprintf(tURL, "https://%s:%d%s", m_sRestSettings.server_ip, m_sRestSettings.port, m_sRestSettings.rest_api);
  245. } else {
  246. sprintf(tURL, "http://%s:%d%s", m_sRestSettings.server_ip, m_sRestSettings.port, m_sRestSettings.rest_api);
  247. };
  248. _send_request( tURL ,tBuffer );
  249. };
  250. free( tN );
  251. };
  252. };
  253. };
  254. break;
  255. };
  256. case EVT_CAMERA_MOVE_TO:{
  257. // Evt->m_nSourceNodeID
  258. //LOG("CAM MOVE!");
  259. break;
  260. };
  261. case EVT_CAMERA_ON_POSITION:{
  262. if ( m_sRestSettings.camera_enabled ){
  263. DBC_SNAPSHOT_DATA * tData = (DBC_SNAPSHOT_DATA *) Evt->m_pEvtData;
  264. if( tData ){
  265. CSTR tN = m_pDBC->getNodeName( Evt->m_nSourceNodeID );
  266. if ( tN ) {
  267. if ( buildCameraEventMsg( tBuffer, tN, 1 ) ) {
  268. if ( m_sRestSettings.use_ssl ) {
  269. sprintf(tURL, "https://%s:%d%s", m_sRestSettings.server_ip, m_sRestSettings.port, m_sRestSettings.rest_api);
  270. } else {
  271. sprintf(tURL, "http://%s:%d%s", m_sRestSettings.server_ip, m_sRestSettings.port, m_sRestSettings.rest_api);
  272. };
  273. _send_request( tURL ,tBuffer );
  274. };
  275. free( tN );
  276. };
  277. };
  278. };
  279. break;
  280. };
  281. default: return true;
  282. };
  283. return true; // Never return false -> to propogate event to next dispatcher.
  284. }
  285. bool kernelModuleBosch::validate(){
  286. //check settings
  287. REST_SETTINGS * tData = m_pDBC->getBoschData( m_nDBAUXID );
  288. if( tData ){
  289. if( memcmp( &m_sRestSettings, tData, sizeof(REST_SETTINGS) ) != 0 ){
  290. memcpy( &m_sRestSettings, tData, sizeof(REST_SETTINGS) );
  291. };
  292. free( tData );
  293. return true;
  294. };
  295. return false;
  296. }
  297. #undef LOG
  298. #undef LOG_ERROR