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.

840 lines
19 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "kernelModuleKafka.h"
  2. // #include "mojoKernel.h"
  3. #include "../data/json/json_tool.h"
  4. #include "../hardware/soc/general/3DLRF.h"
  5. #include <json/reader.h>
  6. #include <json/value.h>
  7. #include <json/writer.h>
  8. #include <strings.h>
  9. #define LOG(X) kernelModule::m_pLog->Log("MOD_KAFKA", this, X)
  10. #ifdef DEBUG
  11. #define LOG_ERROR(X) \
  12. { \
  13. char _tBfrErrMSG2w34[256]; \
  14. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  15. kernelModule::m_pLog->Log("MOD_KAFKA", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  16. }
  17. #else
  18. #define LOG_ERROR(X) kernelModule::m_pLog->Log("MOD_KAFKA", this, _LOG_ERROR, X);
  19. #endif
  20. kernelModuleKafka::kernelModuleKafka(uint32_t ModuleID) :
  21. kernelModule(ModuleID, KERNEL_MODULE_KAFKA)
  22. {
  23. m_pProtocol = NULL;
  24. m_bConsumerEnabled = false;
  25. m_pEventHeap = basicEventHeap::getInstance();
  26. partition = 0; // RdKafka::Topic::PARTITION_UA;
  27. // start_offset = RdKafka::Topic::OFFSET_END;// OFFSET_BEGINNING;
  28. start_offset = 0; // RdKafka::Topic::OFFSET_BEGINNING;
  29. // start_offset = RdKafka::Topic::OFFSET_STORED;
  30. memset(m_sServerIP, 0, 48);
  31. CSTR TVAL = NULL; // m_pKernel->getServerIp();
  32. if (TVAL)
  33. {
  34. strncpy(m_sServerIP, TVAL, 48);
  35. free(TVAL);
  36. }
  37. else
  38. {
  39. LOG_ERROR("Error Get Argus Server IP!");
  40. strncpy(m_sServerIP, "192.168.0.127", 48);
  41. };
  42. memset(m_sServerName, 0, 48);
  43. TVAL = NULL; // m_pKernel->getServerName();
  44. if (TVAL)
  45. {
  46. strncpy(m_sServerName, TVAL, 48);
  47. free(TVAL);
  48. }
  49. else
  50. {
  51. LOG_ERROR("Error Get Argus Server Name!");
  52. strncpy(m_sServerName, "Argus Server", 48);
  53. };
  54. m_pEvtTemplates = (KAFKA_TEMPLATE ***) calloc(EVT_CLASS_COUNT, sizeof(KAFKA_TEMPLATE **));
  55. if (m_pEvtTemplates != NULL)
  56. {
  57. memset(m_pEvtTemplates, 0, EVT_CLASS_COUNT * sizeof(KAFKA_TEMPLATE **));
  58. }
  59. else
  60. {
  61. LOG_ERROR("EVT Templates Memory Allocation Error!");
  62. }
  63. /*
  64. Do it in Kernel!!
  65. if ( m_pKernel!= NULL ) {
  66. argusHardwareManager *tHWM = m_pKernel->getHardwareManager();
  67. if( tHWM != NULL ) {
  68. DBC_INT_LIST *rIDS = tHWM->requestResourcesByType( ARGUS_HARDWARE_PROVIDER_NETWORK );
  69. if( rIDS != NULL ) {
  70. for( DBC_INT_LIST::iterator idx = rIDS->begin(); idx < rIDS->end(); idx++ ){
  71. argusHardwareResourceNetwork *tRES = (argusHardwareResourceNetwork *) tHWM->requestResource( ARGUS_HARDWARE_PROVIDER_NETWORK, *idx );
  72. if( tRES != NULL ) {
  73. m_pServerIP = tRES->getAddress();
  74. break;
  75. } else {
  76. LOG_ERROR("Cannot Get Resource!");
  77. };
  78. };
  79. delete( rIDS );
  80. } else {
  81. LOG("No Networks Detected!");
  82. m_bEnabled = false;
  83. };
  84. } else {
  85. LOG_ERROR("No HW Manager");
  86. };
  87. } else {
  88. LOG_ERROR("No Kernel");
  89. };
  90. */
  91. }
  92. kernelModuleKafka::~kernelModuleKafka()
  93. {
  94. m_bEnabled = false;
  95. waitThreadEnd(true);
  96. if (m_pProtocol != NULL)
  97. {
  98. delete(m_pProtocol);
  99. };
  100. if (m_pEvtTemplates != NULL)
  101. {
  102. for (int i = 0; i < EVT_CLASS_COUNT; i++)
  103. {
  104. if (m_pEvtTemplates[i] != NULL)
  105. {
  106. for (int j = 0; j < EVT_COUNT; j++)
  107. {
  108. if (m_pEvtTemplates[i][j] != NULL)
  109. {
  110. if (m_pEvtTemplates[i][j]->tpl)
  111. {
  112. free(m_pEvtTemplates[i][j]->tpl);
  113. }
  114. free(m_pEvtTemplates[i][j]);
  115. };
  116. };
  117. free(m_pEvtTemplates[i]);
  118. };
  119. };
  120. free(m_pEvtTemplates);
  121. };
  122. }
  123. /* Periodical Job Handler */
  124. void kernelModuleKafka::m_fThreadProc(void)
  125. {
  126. #ifdef SIMULATOR
  127. float sim_Az = 0.f;
  128. float sim_El = 20.f;
  129. float sim_Ran = 80.35f;
  130. float tr_Az_step = 0.3f;
  131. uint32_t src = 128;
  132. #endif
  133. #ifdef DEBUG
  134. LOG("Pereodical Jobs Started");
  135. #endif
  136. m_bThreadStatus = TH_WORKING;
  137. if (! m_bEnabled)
  138. {
  139. m_bThreadStatus = TH_ENDED;
  140. #ifdef DEBUG
  141. LOG("Module is Disabled!");
  142. #endif
  143. };
  144. while (m_bThreadStatus < TH_ENDED)
  145. {
  146. m_llTicks++; //= getMilliseconds();
  147. if (m_llTicks % 20 == 0)
  148. { // each 2 seconds
  149. #ifdef SIMULATOR
  150. sim_Az += tr_Az_step;
  151. for (int i = 0; i < EVT_CLASS_COUNT; i++)
  152. {
  153. if (m_pEvtTemplates[i] != NULL)
  154. {
  155. for (int j = 0; j < EVT_COUNT; j++)
  156. {
  157. if (m_pEvtTemplates[i][j] != NULL)
  158. {
  159. CSTR tpl;
  160. CSTR ts;
  161. CSTR uuid;
  162. char LON[10];
  163. char LAT[10];
  164. char AZ[10];
  165. char EL[10];
  166. char RN[10];
  167. float lat = 32.906153575029926;
  168. float lon = 35.09629379621815;
  169. sprintf(AZ, "%.3f", sim_Az + (0.6 / RAND_MAX) * rand());
  170. sprintf(EL, "%.3f", sim_El + (0.6 / RAND_MAX) * rand());
  171. sprintf(RN, "%.3f", sim_Ran + (0.6 / RAND_MAX) * rand());
  172. sprintf(LAT, "%.10f", lat + (0.00006 / RAND_MAX) * rand());
  173. sprintf(LON, "%.10f", lon + (0.00006 / RAND_MAX) * rand());
  174. uuid = getUniqID_UUID();
  175. ts = getTimeStamp();
  176. src = i;
  177. tpl = buildTemplate(m_pEvtTemplates[i][j]->tpl, src, ts, uuid, AZ, RN, EL, LAT, LON);
  178. if (tpl)
  179. {
  180. m_pProtocol->issuePost(m_pEvtTemplates[i][j]->topic, tpl, strlen(tpl));
  181. free(tpl);
  182. };
  183. if (uuid)
  184. {
  185. free(uuid);
  186. }
  187. if (ts)
  188. {
  189. free(ts);
  190. }
  191. };
  192. };
  193. };
  194. };
  195. #endif
  196. };
  197. if (m_bConsumerEnabled)
  198. {
  199. for (uint8_t idx = 0; idx < EVT_COUNT; idx++)
  200. {
  201. if (m_pEvtTemplates[EVT_CLASS_EXTERNAL_CMD][idx] != NULL)
  202. {
  203. char BUF[1024];
  204. int len;
  205. if (m_pProtocol->receivePost(m_pEvtTemplates[EVT_CLASS_EXTERNAL_CMD][idx]->topic, partition, start_offset, BUF, len))
  206. {
  207. if (len > 0)
  208. {
  209. start_offset++;
  210. #ifdef DEBUG
  211. char ttt[64];
  212. LOG(" HAVE MESSAGE ");
  213. sprintf(ttt, " MSG Len: %d", len);
  214. LOG(ttt);
  215. #endif
  216. if (len > 0)
  217. {
  218. LOG(BUF);
  219. try
  220. {
  221. Json::Value root;
  222. #ifdef OLD_WAY_JSON
  223. Json::Reader reader;
  224. if (! reader.parse(BUF, root))
  225. {
  226. LOG_ERROR("Error parsing the string");
  227. }
  228. else
  229. {
  230. #else
  231. JSONCPP_STRING err;
  232. Json::CharReaderBuilder builder;
  233. const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
  234. if (! reader->parse(BUF, BUF + len, &root, &err))
  235. {
  236. LOG_ERROR("Error parsing the string");
  237. LOG_ERROR(err.c_str());
  238. }
  239. else
  240. {
  241. #endif
  242. if (root.size())
  243. {
  244. FH_CTRL_DATA * tData;
  245. basicEventTypeID evtID = EVT_NOINIT;
  246. Json::Value tVal;
  247. tData = (FH_CTRL_DATA *) malloc(sizeof(FH_CTRL_DATA));
  248. if (tData)
  249. {
  250. memset(tData, 0, sizeof(FH_CTRL_DATA));
  251. tVal = root["id"];
  252. if (! tVal.isNull())
  253. {
  254. #ifdef DEBUG
  255. LOG(tVal.asString().c_str());
  256. #endif
  257. sprintf(tData->cmdUUID, "%s", tVal.asString().c_str());
  258. };
  259. tVal = root["action"];
  260. if (! tVal.isNull())
  261. {
  262. #ifdef DEBUG
  263. LOG(tVal.asString().c_str());
  264. #endif
  265. if (strcasecmp(tVal.asString().c_str(), "intercept_target") == 0)
  266. {
  267. evtID = EVT_FH_MISSION_BEGIN;
  268. };
  269. if (strcasecmp(tVal.asString().c_str(), "jam_target") == 0)
  270. {
  271. tData->param = atoi(tVal.asString().c_str() + 10);
  272. if (tData->param == 0)
  273. {
  274. tData->param = FH_DEFAULT_JAMING_TIME;
  275. };
  276. evtID = EVT_FH_JAMING_BEGIN;
  277. };
  278. if (strcasecmp(tVal.asString().c_str(), "cancel") == 0)
  279. {
  280. evtID = EVT_FH_CANCEL_ACTION;
  281. };
  282. };
  283. tVal = root["event_id"];
  284. if (! tVal.isNull())
  285. {
  286. #ifdef DEBUG
  287. LOG(tVal.asString().c_str());
  288. #endif
  289. sprintf(tData->evtUUID, "%s", tVal.asString().c_str());
  290. };
  291. tVal = root["target_id"];
  292. if (! tVal.isNull())
  293. {
  294. #ifdef DEBUG
  295. LOG(tVal.asString().c_str());
  296. #endif
  297. sprintf(tData->targetUUID, "%s", tVal.asString().c_str());
  298. };
  299. if (evtID)
  300. {
  301. EVENT_CONTAINER * tEVT;
  302. tEVT = createBasicEvent(0, EVT_CLASS_EXTERNAL_CMD, evtID, NULL, NULL, tData);
  303. if (tEVT)
  304. {
  305. m_pEventHeap->broadcast(tEVT);
  306. }
  307. else
  308. {
  309. free(tData);
  310. };
  311. }
  312. else
  313. {
  314. free(tData);
  315. };
  316. };
  317. };
  318. };
  319. }
  320. catch (exception e)
  321. {
  322. LOG_ERROR("EXCEPTION");
  323. };
  324. };
  325. };
  326. };
  327. };
  328. };
  329. };
  330. SLEEP(100);
  331. };
  332. }
  333. bool kernelModuleKafka::startService()
  334. {
  335. if (! m_bEnabled)
  336. {
  337. return false;
  338. }
  339. if (reloadSettings())
  340. {
  341. if (m_pSettings != NULL)
  342. {
  343. m_pProtocol = new protocolKAFKA(m_pSettings);
  344. if (m_pProtocol->init())
  345. {
  346. return init();
  347. };
  348. };
  349. };
  350. return false;
  351. }
  352. bool kernelModuleKafka::stopService(bool force)
  353. {
  354. waitThreadEnd(true);
  355. return true;
  356. }
  357. bool kernelModuleKafka::reloadSettings()
  358. {
  359. if (m_pDBC == NULL)
  360. {
  361. return false;
  362. }
  363. if (! m_bEnabled)
  364. {
  365. return false;
  366. }
  367. m_pSettings = m_pDBC->getSettingsKAFKA(m_nDBAUXID);
  368. if (m_pSettings != NULL)
  369. {
  370. if (m_pEvtTemplates != NULL)
  371. {
  372. // Clear Templates
  373. for (int i = 0; i < EVT_CLASS_COUNT; i++)
  374. {
  375. for (int j = 0; j < EVT_COUNT; j++)
  376. {
  377. if (m_pEvtTemplates[i] != NULL)
  378. {
  379. if (m_pEvtTemplates[i][j] != NULL)
  380. {
  381. if (m_pEvtTemplates[i][j]->tpl)
  382. {
  383. free(m_pEvtTemplates[i][j]->tpl);
  384. }
  385. free(m_pEvtTemplates[i][j]);
  386. m_pEvtTemplates[i][j] = NULL;
  387. };
  388. };
  389. };
  390. free(m_pEvtTemplates[i]);
  391. m_pEvtTemplates[i] = NULL;
  392. };
  393. for (int i = 0; i < EVT_CLASS_COUNT; i++)
  394. {
  395. for (int j = 0; j < EVT_COUNT; j++)
  396. {
  397. KAFKA_TEMPLATE * tT;
  398. tT = m_pDBC->getKafkaTemplateForEvent(m_nDBAUXID, i, j);
  399. if (tT != NULL)
  400. {
  401. if (m_pEvtTemplates[i] == NULL)
  402. {
  403. m_pEvtTemplates[i] = (KAFKA_TEMPLATE **) calloc(EVT_COUNT, sizeof(KAFKA_TEMPLATE *));
  404. memset(m_pEvtTemplates[i], 0, EVT_COUNT * sizeof(KAFKA_TEMPLATE *));
  405. if (i == EVT_CLASS_EXTERNAL_CMD)
  406. {
  407. m_bConsumerEnabled = true;
  408. };
  409. };
  410. m_pEvtTemplates[i][j] = tT;
  411. };
  412. };
  413. };
  414. };
  415. return true;
  416. };
  417. return false;
  418. }
  419. CSTR kernelModuleKafka::buildTemplate(CSTR tpl, uint32_t source_id, CSTR ts, CSTR uuid, CSTR az, CSTR range, CSTR width, CSTR el, CSTR lat, CSTR lon, CSTR alt, CSTR extID, CSTR src_Name, CSTR src_IP, CSTR src_lat, CSTR src_lon, CSTR src_alt,
  420. CSTR src_rot_x, CSTR src_rot_y, CSTR src_rot_z)
  421. {
  422. CSTR ct1, ct2;
  423. char sid[64];
  424. if (tpl)
  425. {
  426. sprintf(sid, "%d", source_id);
  427. ct1 = strReplace(tpl, KAFKA_TPL_SUBST_ID, sid);
  428. if (ct1)
  429. {
  430. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_TS, ts);
  431. if (ct2)
  432. {
  433. free(ct1);
  434. ct1 = ct2;
  435. };
  436. if (uuid)
  437. {
  438. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_UUID, uuid);
  439. if (ct2)
  440. {
  441. free(ct1);
  442. ct1 = ct2;
  443. };
  444. };
  445. if (az)
  446. {
  447. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_AZ, az);
  448. if (ct2)
  449. {
  450. free(ct1);
  451. ct1 = ct2;
  452. };
  453. };
  454. if (range)
  455. {
  456. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_RANGE, range);
  457. if (ct2)
  458. {
  459. free(ct1);
  460. ct1 = ct2;
  461. };
  462. };
  463. if (width)
  464. {
  465. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_WIDTH, width);
  466. if (ct2)
  467. {
  468. free(ct1);
  469. ct1 = ct2;
  470. };
  471. };
  472. if (el)
  473. {
  474. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_EL, el);
  475. if (ct2)
  476. {
  477. free(ct1);
  478. ct1 = ct2;
  479. };
  480. };
  481. if (lat)
  482. {
  483. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_LAT, lat);
  484. if (ct2)
  485. {
  486. free(ct1);
  487. ct1 = ct2;
  488. };
  489. };
  490. if (lon)
  491. {
  492. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_LON, lon);
  493. if (ct2)
  494. {
  495. free(ct1);
  496. ct1 = ct2;
  497. };
  498. };
  499. if (alt)
  500. {
  501. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_ALT, alt);
  502. if (ct2)
  503. {
  504. free(ct1);
  505. ct1 = ct2;
  506. };
  507. };
  508. if (extID)
  509. {
  510. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_EXEVTD, extID);
  511. if (ct2)
  512. {
  513. free(ct1);
  514. ct1 = ct2;
  515. };
  516. };
  517. if (src_Name)
  518. {
  519. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_SRC_NAME, src_Name);
  520. if (ct2)
  521. {
  522. free(ct1);
  523. ct1 = ct2;
  524. };
  525. };
  526. if (src_IP)
  527. {
  528. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_SRC_IP, src_IP);
  529. if (ct2)
  530. {
  531. free(ct1);
  532. ct1 = ct2;
  533. };
  534. };
  535. if (src_lat)
  536. {
  537. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_SRC_LAT, src_lat);
  538. if (ct2)
  539. {
  540. free(ct1);
  541. ct1 = ct2;
  542. };
  543. };
  544. if (src_lon)
  545. {
  546. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_SRC_LON, src_lon);
  547. if (ct2)
  548. {
  549. free(ct1);
  550. ct1 = ct2;
  551. };
  552. };
  553. if (src_alt)
  554. {
  555. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_SRC_ALT, src_alt);
  556. if (ct2)
  557. {
  558. free(ct1);
  559. ct1 = ct2;
  560. };
  561. };
  562. if (src_rot_x)
  563. {
  564. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_SRC_ROT_X, src_rot_x);
  565. if (ct2)
  566. {
  567. free(ct1);
  568. ct1 = ct2;
  569. };
  570. };
  571. if (src_rot_y)
  572. {
  573. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_SRC_ROT_Y, src_rot_y);
  574. if (ct2)
  575. {
  576. free(ct1);
  577. ct1 = ct2;
  578. };
  579. };
  580. if (src_rot_z)
  581. {
  582. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_SRC_ROT_Z, src_rot_z);
  583. if (ct2)
  584. {
  585. free(ct1);
  586. ct1 = ct2;
  587. };
  588. };
  589. // Finalaze Message by System Wide Data
  590. // UUID
  591. CSTR TVAL = getUniqID_UUID();
  592. if (TVAL != NULL)
  593. {
  594. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_MSG_UUID, TVAL);
  595. if (ct2)
  596. {
  597. free(ct1);
  598. ct1 = ct2;
  599. };
  600. free(TVAL);
  601. }
  602. else
  603. {
  604. LOG_ERROR("Error Generate Message UUID!");
  605. };
  606. // Server IP
  607. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_SRV_IP, m_sServerIP);
  608. if (ct2)
  609. {
  610. free(ct1);
  611. ct1 = ct2;
  612. };
  613. // Server Name
  614. ct2 = strReplace(ct1, KAFKA_TPL_SUBST_SRV_NAME, m_sServerName);
  615. if (ct2)
  616. {
  617. free(ct1);
  618. ct1 = ct2;
  619. };
  620. return ct1;
  621. };
  622. }
  623. return NULL;
  624. }
  625. bool kernelModuleKafka::dispatchEvent(EVENT_CONTAINER * Evt)
  626. {
  627. if (Evt == NULL)
  628. {
  629. return false;
  630. }
  631. if (! m_bEnabled)
  632. {
  633. return true;
  634. }
  635. #ifdef DEBUG
  636. char tBuffer[256];
  637. sprintf(tBuffer, "Event Class %d Type %d ", Evt->m_eClass, Evt->m_eType);
  638. LOG(tBuffer);
  639. #endif
  640. if (m_pEvtTemplates[Evt->m_eClass] != NULL)
  641. {
  642. // We have defined Channel for that class Events
  643. // Skip Event Handling by flags
  644. if (! m_bEvtFlags[Evt->m_eType])
  645. {
  646. #ifdef DEBUG
  647. // LOG("Disabled By FLAG!");
  648. #endif
  649. // return;
  650. }
  651. // Hadle Event
  652. CSTR tDATA = NULL;
  653. if (m_pEvtTemplates[Evt->m_eClass][Evt->m_eType] != NULL)
  654. {
  655. switch (Evt->m_eType)
  656. {
  657. case EVT_TARGET_2D_BLACK:
  658. case EVT_TARGET_2D_WHITE:
  659. {
  660. TARGET_DATA * tTARGET = (TARGET_DATA *) Evt->m_pEvtData;
  661. if (tTARGET != NULL)
  662. {
  663. char AZ[10];
  664. char RA[10];
  665. char WID[10];
  666. char LAT[10];
  667. char LON[10];
  668. char ALT[10];
  669. char SLAT[10];
  670. char SLON[10];
  671. char SALT[10];
  672. char SROT_X[10];
  673. char SROT_Y[10];
  674. char SROT_Z[10];
  675. char SNAME[64];
  676. char TS[64];
  677. sprintf(AZ, "%0.3f", tTARGET->local_target.fi);
  678. sprintf(RA, "%0.3f", tTARGET->local_target.r);
  679. sprintf(WID, "%0.3f", tTARGET->area);
  680. sprintf(LAT, "%0.6f", tTARGET->global_target.lat);
  681. sprintf(LON, "%0.6f", tTARGET->global_target.lon);
  682. sprintf(ALT, "%0.6f", tTARGET->global_target.alt);
  683. sprintf(TS, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", Evt->sTS.tm_year + 1900, Evt->sTS.tm_mon + 1, Evt->sTS.tm_mday, Evt->sTS.tm_hour, Evt->sTS.tm_min, Evt->sTS.tm_sec, Evt->m_UMSTS % 1000);
  684. sprintf(SLAT, "%0.6f", tTARGET->source_position.lat);
  685. sprintf(SLON, "%0.6f", tTARGET->source_position.lon);
  686. sprintf(SALT, "%0.6f", tTARGET->source_position.alt);
  687. sprintf(SROT_X, "%0.6f", tTARGET->source_orient.rot_x);
  688. sprintf(SROT_Y, "%0.6f", tTARGET->source_orient.rot_y);
  689. sprintf(SROT_Z, "%0.6f", tTARGET->source_orient.rot_z);
  690. CSTR tN = m_pDBC->getNodeName(Evt->m_nSourceNodeID);
  691. if (tN)
  692. {
  693. strncpy(SNAME, tN, 64);
  694. free(tN);
  695. }
  696. else
  697. {
  698. strncpy(SNAME, "Noname-Laser-Detector", 64);
  699. };
  700. tDATA = buildTemplate(m_pEvtTemplates[Evt->m_eClass][Evt->m_eType]->tpl, Evt->m_nSourceNodeID, TS, Evt->m_UUID, AZ, RA, WID, NULL, LAT, LON, ALT, NULL, SNAME, m_sServerIP, SLAT, SLON, SALT, SROT_X, SROT_Y, SROT_Z);
  701. };
  702. break;
  703. };
  704. case EVT_TARGET_3D_BLACK:
  705. case EVT_TARGET_3D_WHITE:
  706. {
  707. TARGET_DATA * tTARGET = (TARGET_DATA *) Evt->m_pEvtData;
  708. if (tTARGET != NULL)
  709. {
  710. char AZ[10];
  711. char EL[10];
  712. char RA[10];
  713. char WID[10];
  714. char LAT[10];
  715. char LON[10];
  716. char ALT[10];
  717. char SLAT[10];
  718. char SLON[10];
  719. char SALT[10];
  720. char SROT_X[10];
  721. char SROT_Y[10];
  722. char SROT_Z[10];
  723. char SIP[16];
  724. char SNAME[64];
  725. char TS[64];
  726. sprintf(AZ, "%0.3f", tTARGET->local_target.fi);
  727. sprintf(EL, "%0.3f", tTARGET->local_target.tetta);
  728. sprintf(RA, "%0.3f", tTARGET->local_target.r);
  729. sprintf(WID, "%0.3f", tTARGET->area);
  730. sprintf(LAT, "%0.6f", tTARGET->global_target.lat);
  731. sprintf(LON, "%0.6f", tTARGET->global_target.lon);
  732. sprintf(ALT, "%0.6f", tTARGET->global_target.alt);
  733. sprintf(TS, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", Evt->sTS.tm_year + 1900, Evt->sTS.tm_mon + 1, Evt->sTS.tm_mday, Evt->sTS.tm_hour, Evt->sTS.tm_min, Evt->sTS.tm_sec, Evt->m_UMSTS % 1000);
  734. sprintf(SLAT, "%0.6f", tTARGET->source_position.lat);
  735. sprintf(SLON, "%0.6f", tTARGET->source_position.lon);
  736. sprintf(SALT, "%0.6f", tTARGET->source_position.alt);
  737. sprintf(SROT_X, "%0.6f", tTARGET->source_orient.rot_x);
  738. sprintf(SROT_Y, "%0.6f", tTARGET->source_orient.rot_y);
  739. sprintf(SROT_Z, "%0.6f", tTARGET->source_orient.rot_z);
  740. CSTR tN = m_pDBC->getNodeName(Evt->m_nSourceNodeID);
  741. if (tN)
  742. {
  743. strncpy(SNAME, tN, 64);
  744. free(tN);
  745. }
  746. else
  747. {
  748. strncpy(SNAME, "Noname-Laser-Detector", 64);
  749. };
  750. if (0)
  751. {
  752. strncpy(SIP, tN, 16);
  753. free(tN);
  754. }
  755. else
  756. {
  757. strncpy(SIP, "192.168.1.24", 16);
  758. };
  759. tDATA = buildTemplate(m_pEvtTemplates[Evt->m_eClass][Evt->m_eType]->tpl, Evt->m_nSourceNodeID, TS, Evt->m_UUID, AZ, RA, WID, EL, LAT, LON, ALT, NULL, SNAME, SIP, SLAT, SLON, SALT, SROT_X, SROT_Y, SROT_Z);
  760. };
  761. break;
  762. };
  763. case EVT_FH_JAMER_ON:
  764. case EVT_FH_JAMER_OFF:
  765. case EVT_FH_FLY_TO:
  766. case EVT_FH_WAYPOINT:
  767. case EVT_FH_INTERCEPT_OK:
  768. case EVT_FH_INTERCEPT_NOK:
  769. case EVT_FH_BUSY:
  770. case EVT_FH_LANDED:
  771. {
  772. FH_MISSION_DATA * tPOINT = (FH_MISSION_DATA *) Evt->m_pEvtData;
  773. if (tPOINT)
  774. {
  775. char TS[64];
  776. char LAT[10];
  777. char LON[10];
  778. char ALT[10];
  779. sprintf(LAT, "%0.6f", tPOINT->location.lat);
  780. sprintf(LON, "%0.6f", tPOINT->location.lon);
  781. sprintf(ALT, "%0.6f", tPOINT->location.alt);
  782. sprintf(TS, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", Evt->sTS.tm_year + 1900, Evt->sTS.tm_mon + 1, Evt->sTS.tm_mday, Evt->sTS.tm_hour, Evt->sTS.tm_min, Evt->sTS.tm_sec, Evt->m_UMSTS % 1000);
  783. tDATA = buildTemplate(m_pEvtTemplates[Evt->m_eClass][Evt->m_eType]->tpl, Evt->m_nSourceNodeID, TS, Evt->m_UUID, NULL, NULL, NULL, NULL, LAT, LON, ALT, tPOINT->missionID);
  784. };
  785. break;
  786. };
  787. default:
  788. {
  789. break;
  790. };
  791. };
  792. if (tDATA != NULL)
  793. {
  794. #ifdef KAFKA_DUMMY_SEND
  795. LOG(tDATA);
  796. #else
  797. m_pProtocol->issuePost(m_pEvtTemplates[Evt->m_eClass][Evt->m_eType]->topic, tDATA, strlen(tDATA));
  798. #endif
  799. }
  800. else
  801. {
  802. LOG_ERROR("Cant Build Template!");
  803. };
  804. };
  805. };
  806. return true;
  807. }
  808. #undef LOG
  809. #undef LOG_ERROR