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.

504 lines
21 KiB

6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "rgooseModuleKafka.h"
  2. #include "../hardware/soc/general/3DLRF.h"
  3. #include "../data/json/json_tool.h"
  4. #include <strings.h>
  5. #include <json/value.h>
  6. #include <json/writer.h>
  7. #include <json/reader.h>
  8. #define LOG( X ) argusKernelModule::m_pLog->Log( "MOD_KAFKA", this, X )
  9. #ifdef DEBUG
  10. #define LOG_ERROR( X ) {\
  11. char _tBfrErrMSG2w34[256];\
  12. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  13. argusKernelModule::m_pLog->Log( "MOD_KAFKA", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  14. }
  15. #else
  16. #define LOG_ERROR( X ) argusKernelModule::m_pLog->Log( "MOD_KAFKA", this, _LOG_ERROR, X );
  17. #endif
  18. argusModuleKafka::argusModuleKafka( uint32_t ModuleID ): argusKernelModule( ModuleID, ARGUS_MODULE_KAFKA ){
  19. m_pProtocol = NULL;
  20. m_bConsumerEnabled = false;
  21. m_pEventHeap = basicEventHeap::getInstance();
  22. partition = 0; //RdKafka::Topic::PARTITION_UA;
  23. //start_offset = RdKafka::Topic::OFFSET_END;// OFFSET_BEGINNING;
  24. start_offset = 0;//RdKafka::Topic::OFFSET_BEGINNING;
  25. //start_offset = RdKafka::Topic::OFFSET_STORED;
  26. m_pEvtTemplates = (KAFKA_TEMPLATE***) calloc(EVT_CLASS_COUNT, sizeof(KAFKA_TEMPLATE**));
  27. if( m_pEvtTemplates!= NULL ) {
  28. memset( m_pEvtTemplates, 0, EVT_CLASS_COUNT * sizeof(KAFKA_TEMPLATE**));
  29. } else LOG_ERROR("EVT Templates Memory Allocation Error!");
  30. };
  31. argusModuleKafka::~argusModuleKafka(){
  32. m_bEnabled = false;
  33. waitThreadEnd( true );
  34. if( m_pProtocol!= NULL ) {
  35. delete( m_pProtocol );
  36. };
  37. if( m_pEvtTemplates!= NULL ) {
  38. for(int i=0; i < EVT_CLASS_COUNT; i++ ) {
  39. if( m_pEvtTemplates[i] != NULL ){
  40. for( int j=0; j< EVT_COUNT; j++ ) {
  41. if( m_pEvtTemplates[i][j] != NULL ){
  42. if( m_pEvtTemplates[i][j]->tpl ) free( m_pEvtTemplates[i][j]->tpl );
  43. free( m_pEvtTemplates[i][j] );
  44. };
  45. };
  46. free( m_pEvtTemplates[i] );
  47. };
  48. };
  49. free( m_pEvtTemplates );
  50. };
  51. };
  52. /* Periodical Job Handler */
  53. void argusModuleKafka::m_fThreadProc( void ){
  54. #ifdef SIMULATOR
  55. float sim_Az = 0.f;
  56. float sim_El = 20.f;
  57. float sim_Ran = 80.35f;
  58. float tr_Az_step = 0.3f;
  59. uint32_t src = 128;
  60. #endif
  61. #ifdef DEBUG
  62. LOG("Pereodical Jobs Started");
  63. #endif
  64. m_bThreadStatus = TH_WORKING;
  65. if( !m_bEnabled ) {
  66. m_bThreadStatus = TH_ENDED;
  67. #ifdef DEBUG
  68. LOG("Module is Disabled!");
  69. #endif
  70. };
  71. while( m_bThreadStatus < TH_ENDED ) {
  72. m_llTicks ++;//= getMilliseconds();
  73. if( m_llTicks % 20 == 0 ) { // each 2 seconds
  74. #ifdef SIMULATOR
  75. sim_Az += tr_Az_step;
  76. for(int i=0; i < EVT_CLASS_COUNT; i++ ) {
  77. if( m_pEvtTemplates[i] != NULL ){
  78. for( int j=0; j< EVT_COUNT; j++ ) {
  79. if( m_pEvtTemplates[i][j] != NULL ){
  80. CSTR tpl;
  81. CSTR ts;
  82. CSTR uuid;
  83. char LON[10];
  84. char LAT[10];
  85. char AZ[10];
  86. char EL[10];
  87. char RN[10];
  88. float lat = 32.906153575029926;
  89. float lon = 35.09629379621815;
  90. sprintf( AZ, "%.3f", sim_Az+(0.6/RAND_MAX)*rand());
  91. sprintf( EL, "%.3f", sim_El+(0.6/RAND_MAX)*rand());
  92. sprintf( RN, "%.3f", sim_Ran+(0.6/RAND_MAX)*rand());
  93. sprintf( LAT, "%.10f", lat+(0.00006/RAND_MAX)*rand() );
  94. sprintf( LON, "%.10f", lon+(0.00006/RAND_MAX)*rand() );
  95. uuid = getUniqID_UUID();
  96. ts = getTimeStamp();
  97. src = i;
  98. tpl = buildTemplate( m_pEvtTemplates[i][j]->tpl, src, ts, uuid, AZ, RN, EL, LAT, LON );
  99. if( tpl ) {
  100. m_pProtocol->issuePost( m_pEvtTemplates[i][j]->topic, tpl, strlen( tpl ) );
  101. free( tpl );
  102. };
  103. if( uuid ) free( uuid );
  104. if( ts ) free( ts );
  105. };
  106. };
  107. };
  108. };
  109. #endif
  110. };
  111. if( m_bConsumerEnabled ) {
  112. for( uint8_t idx = 0; idx < EVT_COUNT; idx++ ) {
  113. if( m_pEvtTemplates[EVT_CLASS_EXTERNAL_CMD][idx] != NULL ){
  114. char BUF[1024];
  115. int len;
  116. if( m_pProtocol->receivePost( m_pEvtTemplates[EVT_CLASS_EXTERNAL_CMD][idx]->topic, partition, start_offset, BUF, len ) ){
  117. if( len > 0 ) {
  118. start_offset++;
  119. #ifdef DEBUG
  120. char ttt[64];
  121. LOG(" HAVE MESSAGE ");
  122. sprintf( ttt, " MSG Len: %d", len );
  123. LOG( ttt );
  124. #endif
  125. if( len > 0 ){
  126. LOG ( BUF );
  127. try{
  128. Json::Value root;
  129. #ifdef OLD_WAY_JSON
  130. Json::Reader reader;
  131. if( !reader.parse( BUF, root ) ){
  132. LOG_ERROR( "Error parsing the string" );
  133. } else {
  134. #else
  135. JSONCPP_STRING err;
  136. Json::CharReaderBuilder builder;
  137. const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
  138. if (!reader->parse(BUF, BUF + len, &root, &err)) {
  139. LOG_ERROR( "Error parsing the string" );
  140. LOG_ERROR( err.c_str() );
  141. } else {
  142. #endif
  143. if (root.size()) {
  144. FH_CTRL_DATA * tData;
  145. basicEventTypeID evtID = EVT_NOINIT;
  146. Json::Value tVal;
  147. tData =(FH_CTRL_DATA *)malloc( sizeof(FH_CTRL_DATA));
  148. if( tData ) {
  149. memset( tData, 0, sizeof(FH_CTRL_DATA));
  150. tVal = root["id"];
  151. if (!tVal.isNull()) {
  152. #ifdef DEBUG
  153. LOG(tVal.asString().c_str());
  154. #endif
  155. sprintf( tData->cmdUUID, "%s", tVal.asString().c_str() );
  156. };
  157. tVal = root["action"];
  158. if (!tVal.isNull()) {
  159. #ifdef DEBUG
  160. LOG( tVal.asString().c_str() );
  161. #endif
  162. if ( strcasecmp(tVal.asString().c_str(), "intercept_target") == 0) {
  163. evtID = EVT_FH_MISSION_BEGIN;
  164. };
  165. if ( strcasecmp(tVal.asString().c_str(), "jam_target") == 0) {
  166. tData->param = atoi( tVal.asString().c_str()+10 );
  167. if( tData->param == 0 ) {
  168. tData->param = FH_DEFAULT_JAMING_TIME;
  169. };
  170. evtID = EVT_FH_JAMING_BEGIN;
  171. };
  172. if ( strcasecmp(tVal.asString().c_str(), "cancel") == 0) {
  173. evtID = EVT_FH_CANCEL_ACTION;
  174. };
  175. };
  176. tVal = root["event_id"];
  177. if (!tVal.isNull()) {
  178. #ifdef DEBUG
  179. LOG( tVal.asString().c_str() );
  180. #endif
  181. sprintf( tData->evtUUID, "%s", tVal.asString().c_str() );
  182. };
  183. tVal = root["target_id"];
  184. if (!tVal.isNull()) {
  185. #ifdef DEBUG
  186. LOG( tVal.asString().c_str() );
  187. #endif
  188. sprintf( tData->targetUUID, "%s", tVal.asString().c_str() );
  189. };
  190. if( evtID ) {
  191. EVENT_CONTAINER * tEVT;
  192. tEVT = createBasicEvent( 0, EVT_CLASS_EXTERNAL_CMD, evtID, NULL, NULL, tData );
  193. if( tEVT ) {
  194. m_pEventHeap->broadcast( tEVT );
  195. } else {
  196. free( tData );
  197. };
  198. } else {
  199. free( tData );
  200. };
  201. };
  202. };
  203. };
  204. } catch( exception e ){
  205. LOG_ERROR("EXCEPTION");
  206. };
  207. };
  208. };
  209. };
  210. };
  211. };
  212. };
  213. SLEEP(100);
  214. };
  215. };
  216. bool argusModuleKafka::startService(){
  217. if(!m_bEnabled) return false;
  218. if( reloadSettings() ) {
  219. if( m_pSettings!= NULL ){
  220. m_pProtocol = new protocolKAFKA( m_pSettings );
  221. if( m_pProtocol->init() ){
  222. return init();
  223. };
  224. };
  225. };
  226. return false;
  227. };
  228. bool argusModuleKafka::stopService( bool force ){
  229. waitThreadEnd( true );
  230. return true;
  231. };
  232. bool argusModuleKafka::reloadSettings(){
  233. if( m_pDBC==NULL ) return false;
  234. if(! m_bEnabled) return false;
  235. m_pSettings = m_pDBC->getSettingsKAFKA( m_nDBAUXID );
  236. if( m_pSettings != NULL ) {
  237. if( m_pEvtTemplates != NULL ) {
  238. // Clear Templates
  239. for(int i = 0; i < EVT_CLASS_COUNT; i++ ){
  240. for(int j = 0; j < EVT_COUNT; j++ ){
  241. if( m_pEvtTemplates[i] != NULL ){
  242. if( m_pEvtTemplates[i][j] != NULL ){
  243. if( m_pEvtTemplates[i][j]->tpl ) free( m_pEvtTemplates[i][j]->tpl );
  244. free( m_pEvtTemplates[i][j] );
  245. m_pEvtTemplates[i][j] = NULL;
  246. };
  247. };
  248. };
  249. free( m_pEvtTemplates[i] );
  250. m_pEvtTemplates[i] = NULL;
  251. };
  252. for(int i = 0; i < EVT_CLASS_COUNT; i++ ){
  253. for(int j = 0; j < EVT_COUNT; j++ ){
  254. KAFKA_TEMPLATE * tT;
  255. tT = m_pDBC->getKafkaTemplateForEvent( m_nDBAUXID, i, j );
  256. if( tT != NULL ) {
  257. if( m_pEvtTemplates[i]== NULL ) {
  258. m_pEvtTemplates[i]=(KAFKA_TEMPLATE**)calloc(EVT_COUNT,sizeof(KAFKA_TEMPLATE*));
  259. memset( m_pEvtTemplates[i], 0, EVT_COUNT * sizeof(KAFKA_TEMPLATE*));
  260. if( i == EVT_CLASS_EXTERNAL_CMD ) {
  261. m_bConsumerEnabled = true;
  262. };
  263. };
  264. m_pEvtTemplates[i][j] = tT;
  265. };
  266. };
  267. };
  268. };
  269. return true;
  270. };
  271. return false;
  272. };
  273. CSTR argusModuleKafka::buildTemplate(
  274. CSTR tpl,
  275. uint32_t source_id,
  276. CSTR ts,
  277. CSTR uuid,
  278. CSTR az,
  279. CSTR range,
  280. CSTR width,
  281. CSTR el,
  282. CSTR lat,
  283. CSTR lon,
  284. CSTR alt,
  285. CSTR extID
  286. ){
  287. CSTR ct1, ct2;
  288. char sid[64];
  289. if( tpl ) {
  290. sprintf( sid, "%d", source_id );
  291. ct1 = strReplace( tpl, KAFKA_TPL_SUBST_ID, sid );
  292. if( ct1 ) {
  293. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_TS, ts );
  294. if( ct2 ) {
  295. free( ct1 );
  296. ct1 = ct2;
  297. };
  298. if( uuid ){
  299. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_UUID, uuid );
  300. if( ct2 ) {
  301. free( ct1 );
  302. ct1 = ct2;
  303. };
  304. };
  305. if( az ){
  306. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_AZ, az );
  307. if( ct2 ) {
  308. free( ct1 );
  309. ct1 = ct2;
  310. };
  311. };
  312. if( range ){
  313. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_RANGE, range );
  314. if( ct2 ) {
  315. free( ct1 );
  316. ct1 = ct2;
  317. };
  318. };
  319. if( width ){
  320. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_WIDTH, width );
  321. if( ct2 ) {
  322. free( ct1 );
  323. ct1 = ct2;
  324. };
  325. };
  326. if( el ){
  327. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_EL, el );
  328. if( ct2 ) {
  329. free( ct1 );
  330. ct1 = ct2;
  331. };
  332. };
  333. if( lat ){
  334. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_LAT, lat );
  335. if( ct2 ) {
  336. free( ct1 );
  337. ct1 = ct2;
  338. };
  339. };
  340. if( lon ){
  341. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_LON, lon );
  342. if( ct2 ) {
  343. free( ct1 );
  344. ct1 = ct2;
  345. };
  346. };
  347. if( alt ){
  348. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_ALT, alt );
  349. if( ct2 ) {
  350. free( ct1 );
  351. ct1 = ct2;
  352. };
  353. };
  354. if( extID ){
  355. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_EXEVTD, extID );
  356. if( ct2 ) {
  357. free( ct1 );
  358. ct1 = ct2;
  359. };
  360. };
  361. return ct1;
  362. };
  363. }
  364. return NULL;
  365. };
  366. void argusModuleKafka::dispatchEvent( EVENT_CONTAINER *Evt ){
  367. if( Evt == NULL ) return;
  368. if( !m_bEnabled ) return;
  369. #ifdef DEBUG
  370. char tBuffer[256];
  371. sprintf( tBuffer, "Event Class %d Type %d ", Evt->m_eClass , Evt->m_eType );
  372. LOG( tBuffer );
  373. #endif
  374. if( m_pEvtTemplates[ Evt->m_eClass ]!= NULL ) {
  375. // We have defined Channel for that class Events
  376. // Skip Event Handling by flags
  377. if( !m_bEvtFlags[ Evt->m_eType ] ){
  378. #ifdef DEBUG
  379. //LOG("Disabled By FLAG!");
  380. #endif
  381. //return;
  382. }
  383. // Hadle Event
  384. CSTR tDATA = NULL;
  385. if( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ] != NULL ) {
  386. switch( Evt->m_eType ) {
  387. case EVT_TARGET_2D_BLACK:
  388. case EVT_TARGET_2D_WHITE:
  389. case EVT_TARGET_3D_BLACK:
  390. case EVT_TARGET_3D_WHITE:{
  391. TARGET_DATA * tTARGET = ( TARGET_DATA *) Evt->m_pEvtData;
  392. if(tTARGET!=NULL){
  393. char AZ[10];
  394. char EL[10];
  395. char RA[10];
  396. char WID[10];
  397. char LAT[10];
  398. char LON[10];
  399. char ALT[10];
  400. char TS[64];
  401. sprintf( AZ,"%0.3f", tTARGET->local_target.fi );
  402. sprintf( EL,"%0.3f", tTARGET->local_target.tetta );
  403. sprintf( RA,"%0.3f", tTARGET->local_target.r );
  404. sprintf( WID,"%0.3f", tTARGET->area );
  405. sprintf( LAT,"%0.6f", tTARGET->global_target.lat );
  406. sprintf( LON,"%0.6f", tTARGET->global_target.lon );
  407. sprintf( ALT,"%0.6f", tTARGET->global_target.alt );
  408. sprintf( TS, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
  409. ( Evt->sTS.tm_year+1900),
  410. (Evt->sTS.tm_mon+1),
  411. Evt->sTS.tm_mday,
  412. Evt->sTS.tm_hour,
  413. Evt->sTS.tm_min,
  414. Evt->sTS.tm_sec,
  415. (Evt->m_UMSTS%1000)
  416. );
  417. tDATA = buildTemplate( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ]->tpl,
  418. Evt->m_nSourceUID,
  419. TS,
  420. Evt->m_UUID,
  421. AZ,
  422. RA,
  423. WID,
  424. EL,
  425. LAT,
  426. LON,
  427. ALT
  428. );
  429. };
  430. break;
  431. };
  432. case EVT_FH_JAMER_ON:
  433. case EVT_FH_JAMER_OFF:
  434. case EVT_FH_FLY_TO:
  435. case EVT_FH_WAYPOINT:
  436. case EVT_FH_INTERCEPT_OK:
  437. case EVT_FH_INTERCEPT_NOK:
  438. case EVT_FH_BUSY:
  439. case EVT_FH_LANDED:{
  440. FH_MISSION_DATA * tPOINT=(FH_MISSION_DATA *) Evt->m_pEvtData;
  441. if( tPOINT ) {
  442. char TS[64];
  443. char LAT[10];
  444. char LON[10];
  445. char ALT[10];
  446. sprintf( LAT,"%0.6f", tPOINT->location.lat );
  447. sprintf( LON,"%0.6f", tPOINT->location.lon );
  448. sprintf( ALT,"%0.6f", tPOINT->location.alt );
  449. sprintf( TS, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
  450. ( Evt->sTS.tm_year+1900),
  451. (Evt->sTS.tm_mon+1),
  452. Evt->sTS.tm_mday,
  453. Evt->sTS.tm_hour,
  454. Evt->sTS.tm_min,
  455. Evt->sTS.tm_sec,
  456. (Evt->m_UMSTS%1000)
  457. );
  458. tDATA = buildTemplate( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ]->tpl,
  459. Evt->m_nSourceUID,
  460. TS,
  461. Evt->m_UUID,
  462. NULL,
  463. NULL,
  464. NULL,
  465. NULL,
  466. LAT,
  467. LON,
  468. ALT,
  469. tPOINT->missionID
  470. );
  471. };
  472. break;
  473. };
  474. default:{
  475. break;
  476. };
  477. };
  478. if( tDATA!= NULL ) {
  479. m_pProtocol->issuePost( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ]->topic, tDATA, strlen( tDATA ) );
  480. } else {
  481. LOG_ERROR("Cant Build Template!");
  482. };
  483. };
  484. };
  485. };
  486. #undef LOG
  487. #undef LOG_ERROR