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.

493 lines
20 KiB

6 years ago
​
​
​
​
6 years ago
​
​
​
​
6 years ago
​
6 years ago
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "rgooseKafkaModule.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( "KM", 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( "KM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  14. }
  15. #else
  16. #define LOG_ERROR( X ) argusKernelModule::m_pLog->Log( "KM", this, _LOG_ERROR, X );
  17. #endif
  18. argusKafkaModule::argusKafkaModule( 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. argusKafkaModule::~argusKafkaModule(){
  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 argusKafkaModule::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. Json::Reader reader;
  130. if( !reader.parse( BUF, root ) ){
  131. LOG_ERROR( "Error parsing the string" );
  132. } else {
  133. if (root.size()) {
  134. FH_CTRL_DATA * tData;
  135. basicEventTypeID evtID = EVT_NOINIT;
  136. Json::Value tVal;
  137. tData =(FH_CTRL_DATA *)malloc( sizeof(FH_CTRL_DATA));
  138. if( tData ) {
  139. memset( tData, 0, sizeof(FH_CTRL_DATA));
  140. tVal = root["id"];
  141. if (tVal != NULL) {
  142. #ifdef DEBUG
  143. LOG(tVal.asString().c_str());
  144. #endif
  145. sprintf( tData->cmdUUID, "%s", tVal.asString().c_str() );
  146. };
  147. tVal = root["action"];
  148. if (tVal != NULL) {
  149. #ifdef DEBUG
  150. LOG( tVal.asString().c_str() );
  151. #endif
  152. if ( strcasecmp(tVal.asString().c_str(), "intercept_target") == 0) {
  153. evtID = EVT_FH_MISSION_BEGIN;
  154. };
  155. if ( strcasecmp(tVal.asString().c_str(), "jam_target") == 0) {
  156. tData->param = atoi( tVal.asString().c_str()+10 );
  157. if( tData->param == 0 ) {
  158. tData->param = FH_DEFAULT_JAMING_TIME;
  159. };
  160. evtID = EVT_FH_JAMING_BEGIN;
  161. };
  162. if ( strcasecmp(tVal.asString().c_str(), "cancel") == 0) {
  163. evtID = EVT_FH_CANCEL_ACTION;
  164. };
  165. };
  166. tVal = root["event_id"];
  167. if (tVal != NULL) {
  168. #ifdef DEBUG
  169. LOG( tVal.asString().c_str() );
  170. #endif
  171. sprintf( tData->evtUUID, "%s", tVal.asString().c_str() );
  172. };
  173. tVal = root["target_id"];
  174. if (tVal != NULL) {
  175. #ifdef DEBUG
  176. LOG( tVal.asString().c_str() );
  177. #endif
  178. sprintf( tData->targetUUID, "%s", tVal.asString().c_str() );
  179. };
  180. if( evtID ) {
  181. EVENT_CONTAINER * tEVT;
  182. tEVT = createBasicEvent( 0, EVT_CLASS_EXTERNAL_CMD, evtID, NULL, tData );
  183. if( tEVT ) {
  184. m_pEventHeap->broadcast( tEVT );
  185. } else {
  186. free( tData );
  187. };
  188. } else {
  189. free( tData );
  190. };
  191. };
  192. };
  193. };
  194. } catch( exception e ){
  195. LOG_ERROR("EXCEPTION");
  196. };
  197. };
  198. };
  199. };
  200. };
  201. };
  202. };
  203. SLEEP(100);
  204. };
  205. };
  206. bool argusKafkaModule::startService(){
  207. if(!m_bEnabled) return false;
  208. if( reloadSettings() ) {
  209. if( m_pSettings!= NULL ){
  210. m_pProtocol = new protocolKAFKA( m_pSettings );
  211. if( m_pProtocol->init() ){
  212. return init();
  213. };
  214. };
  215. };
  216. return false;
  217. };
  218. bool argusKafkaModule::stopService(){
  219. waitThreadEnd( true );
  220. return true;
  221. };
  222. bool argusKafkaModule::reloadSettings(){
  223. if( m_pDBC==NULL ) return false;
  224. if(! m_bEnabled) return false;
  225. m_pSettings = m_pDBC->getSettingsKAFKA( m_nDBAUXID );
  226. if( m_pSettings != NULL ) {
  227. if( m_pEvtTemplates != NULL ) {
  228. // Clear Templates
  229. for(int i = 0; i < EVT_CLASS_COUNT; i++ ){
  230. for(int j = 0; j < EVT_COUNT; j++ ){
  231. if( m_pEvtTemplates[i] != NULL ){
  232. if( m_pEvtTemplates[i][j] != NULL ){
  233. if( m_pEvtTemplates[i][j]->tpl ) free( m_pEvtTemplates[i][j]->tpl );
  234. free( m_pEvtTemplates[i][j] );
  235. m_pEvtTemplates[i][j] = NULL;
  236. };
  237. };
  238. };
  239. free( m_pEvtTemplates[i] );
  240. m_pEvtTemplates[i] = NULL;
  241. };
  242. for(int i = 0; i < EVT_CLASS_COUNT; i++ ){
  243. for(int j = 0; j < EVT_COUNT; j++ ){
  244. KAFKA_TEMPLATE * tT;
  245. tT = m_pDBC->getKafkaTemplateForEvent( m_nDBAUXID, i, j );
  246. if( tT != NULL ) {
  247. if( m_pEvtTemplates[i]== NULL ) {
  248. m_pEvtTemplates[i]=(KAFKA_TEMPLATE**)calloc(EVT_COUNT,sizeof(KAFKA_TEMPLATE*));
  249. memset( m_pEvtTemplates[i], 0, EVT_COUNT * sizeof(KAFKA_TEMPLATE*));
  250. if( i == EVT_CLASS_EXTERNAL_CMD ) {
  251. m_bConsumerEnabled = true;
  252. };
  253. };
  254. m_pEvtTemplates[i][j] = tT;
  255. };
  256. };
  257. };
  258. };
  259. return true;
  260. };
  261. return false;
  262. };
  263. CSTR argusKafkaModule::buildTemplate(
  264. CSTR tpl,
  265. uint32_t source_id,
  266. CSTR ts,
  267. CSTR uuid,
  268. CSTR az,
  269. CSTR range,
  270. CSTR width,
  271. CSTR el,
  272. CSTR lat,
  273. CSTR lon,
  274. CSTR alt,
  275. CSTR extID
  276. ){
  277. CSTR ct1, ct2;
  278. char sid[64];
  279. if( tpl ) {
  280. sprintf( sid, "%d", source_id );
  281. ct1 = strReplace( tpl, KAFKA_TPL_SUBST_ID, sid );
  282. if( ct1 ) {
  283. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_TS, ts );
  284. if( ct2 ) {
  285. free( ct1 );
  286. ct1 = ct2;
  287. };
  288. if( uuid ){
  289. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_UUID, uuid );
  290. if( ct2 ) {
  291. free( ct1 );
  292. ct1 = ct2;
  293. };
  294. };
  295. if( az ){
  296. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_AZ, az );
  297. if( ct2 ) {
  298. free( ct1 );
  299. ct1 = ct2;
  300. };
  301. };
  302. if( range ){
  303. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_RANGE, range );
  304. if( ct2 ) {
  305. free( ct1 );
  306. ct1 = ct2;
  307. };
  308. };
  309. if( width ){
  310. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_WIDTH, width );
  311. if( ct2 ) {
  312. free( ct1 );
  313. ct1 = ct2;
  314. };
  315. };
  316. if( el ){
  317. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_EL, el );
  318. if( ct2 ) {
  319. free( ct1 );
  320. ct1 = ct2;
  321. };
  322. };
  323. if( lat ){
  324. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_LAT, lat );
  325. if( ct2 ) {
  326. free( ct1 );
  327. ct1 = ct2;
  328. };
  329. };
  330. if( lon ){
  331. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_LON, lon );
  332. if( ct2 ) {
  333. free( ct1 );
  334. ct1 = ct2;
  335. };
  336. };
  337. if( alt ){
  338. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_ALT, alt );
  339. if( ct2 ) {
  340. free( ct1 );
  341. ct1 = ct2;
  342. };
  343. };
  344. if( extID ){
  345. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_EXEVTD, extID );
  346. if( ct2 ) {
  347. free( ct1 );
  348. ct1 = ct2;
  349. };
  350. };
  351. return ct1;
  352. };
  353. }
  354. return NULL;
  355. };
  356. void argusKafkaModule::dispatchEvent( EVENT_CONTAINER *Evt ){
  357. if( Evt == NULL ) return;
  358. if( !m_bEnabled ) return;
  359. #ifdef DEBUG
  360. char tBuffer[256];
  361. sprintf( tBuffer, "Event Class %d Type %d ", Evt->m_eClass , Evt->m_eType );
  362. LOG( tBuffer );
  363. #endif
  364. if( m_pEvtTemplates[ Evt->m_eClass ]!= NULL ) {
  365. // We have defined Channel for that class Events
  366. // Skip Event Handling by flags
  367. if( !m_bEvtFlags[ Evt->m_eType ] ){
  368. #ifdef DEBUG
  369. //LOG("Disabled By FLAG!");
  370. #endif
  371. //return;
  372. }
  373. // Hadle Event
  374. CSTR tDATA = NULL;
  375. if( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ] != NULL ) {
  376. switch( Evt->m_eType ) {
  377. case EVT_TARGET_2D_BLACK:
  378. case EVT_TARGET_2D_WHITE:
  379. case EVT_TARGET_3D_BLACK:
  380. case EVT_TARGET_3D_WHITE:{
  381. TARGET_DATA * tTARGET = ( TARGET_DATA *) Evt->m_pEvtData;
  382. if(tTARGET!=NULL){
  383. char AZ[10];
  384. char EL[10];
  385. char RA[10];
  386. char WID[10];
  387. char LAT[10];
  388. char LON[10];
  389. char ALT[10];
  390. char TS[64];
  391. sprintf( AZ,"%0.3f", tTARGET->local_target.fi );
  392. sprintf( EL,"%0.3f", tTARGET->local_target.tetta );
  393. sprintf( RA,"%0.3f", tTARGET->local_target.r );
  394. sprintf( WID,"%0.3f", tTARGET->area );
  395. sprintf( LAT,"%0.6f", tTARGET->global_target.lat );
  396. sprintf( LON,"%0.6f", tTARGET->global_target.lon );
  397. sprintf( ALT,"%0.6f", tTARGET->global_target.alt );
  398. sprintf( TS, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
  399. ( Evt->sTS.tm_year+1900),
  400. (Evt->sTS.tm_mon+1),
  401. Evt->sTS.tm_mday,
  402. Evt->sTS.tm_hour,
  403. Evt->sTS.tm_min,
  404. Evt->sTS.tm_sec,
  405. (Evt->m_UMSTS%1000)
  406. );
  407. tDATA = buildTemplate( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ]->tpl,
  408. Evt->m_nSourceUID,
  409. TS,
  410. Evt->m_UUID,
  411. AZ,
  412. RA,
  413. WID,
  414. EL,
  415. LAT,
  416. LON,
  417. ALT
  418. );
  419. };
  420. break;
  421. };
  422. case EVT_FH_JAMER_ON:
  423. case EVT_FH_JAMER_OFF:
  424. case EVT_FH_FLY_TO:
  425. case EVT_FH_WAYPOINT:
  426. case EVT_FH_INTERCEPT_OK:
  427. case EVT_FH_INTERCEPT_NOK:
  428. case EVT_FH_BUSY:
  429. case EVT_FH_LANDED:{
  430. FH_MISSION_DATA * tPOINT=(FH_MISSION_DATA *) Evt->m_pEvtData;
  431. if( tPOINT ) {
  432. char TS[64];
  433. char LAT[10];
  434. char LON[10];
  435. char ALT[10];
  436. sprintf( LAT,"%0.6f", tPOINT->location.lat );
  437. sprintf( LON,"%0.6f", tPOINT->location.lon );
  438. sprintf( ALT,"%0.6f", tPOINT->location.alt );
  439. sprintf( TS, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
  440. ( Evt->sTS.tm_year+1900),
  441. (Evt->sTS.tm_mon+1),
  442. Evt->sTS.tm_mday,
  443. Evt->sTS.tm_hour,
  444. Evt->sTS.tm_min,
  445. Evt->sTS.tm_sec,
  446. (Evt->m_UMSTS%1000)
  447. );
  448. tDATA = buildTemplate( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ]->tpl,
  449. Evt->m_nSourceUID,
  450. TS,
  451. Evt->m_UUID,
  452. NULL,
  453. NULL,
  454. NULL,
  455. NULL,
  456. LAT,
  457. LON,
  458. ALT,
  459. tPOINT->missionID
  460. );
  461. };
  462. break;
  463. };
  464. default:{
  465. break;
  466. };
  467. };
  468. if( tDATA!= NULL ) {
  469. m_pProtocol->issuePost( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ]->topic, tDATA, strlen( tDATA ) );
  470. } else {
  471. LOG_ERROR("Cant Build Template!");
  472. };
  473. };
  474. };
  475. };
  476. #undef LOG
  477. #undef LOG_ERROR