|
|
#include "kernelModuleKafka.h"
//#include "mojoKernel.h"
#include "../hardware/soc/general/3DLRF.h"
#include "../data/json/json_tool.h"
#include <strings.h>
#include <json/value.h>
#include <json/writer.h>
#include <json/reader.h>
#define LOG( X ) kernelModule::m_pLog->Log( "MOD_KAFKA", this, X )
#ifdef DEBUG
#define LOG_ERROR( X ) {\
char _tBfrErrMSG2w34[256];\ sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\ kernelModule::m_pLog->Log( "MOD_KAFKA", this, _LOG_ERROR, _tBfrErrMSG2w34 );\ }#else
#define LOG_ERROR( X ) kernelModule::m_pLog->Log( "MOD_KAFKA", this, _LOG_ERROR, X );
#endif
kernelModuleKafka::kernelModuleKafka( uint32_t ModuleID ): kernelModule( ModuleID, KERNEL_MODULE_KAFKA ){ m_pProtocol = NULL; m_bConsumerEnabled = false; m_pEventHeap = basicEventHeap::getInstance(); partition = 0; //RdKafka::Topic::PARTITION_UA;
//start_offset = RdKafka::Topic::OFFSET_END;// OFFSET_BEGINNING;
start_offset = 0;//RdKafka::Topic::OFFSET_BEGINNING;
//start_offset = RdKafka::Topic::OFFSET_STORED;
memset( m_sServerIP, 0 ,48 ); CSTR TVAL = NULL;//m_pKernel->getServerIp();
if ( TVAL ) { strncpy( m_sServerIP, TVAL, 48 ); free( TVAL ); } else { LOG_ERROR("Error Get Argus Server IP!"); strncpy( m_sServerIP, "192.168.0.127", 48 ); }; memset( m_sServerName, 0, 48 ); TVAL = NULL;//m_pKernel->getServerName();
if ( TVAL ) { strncpy( m_sServerName, TVAL, 48 ); free( TVAL ); } else { LOG_ERROR("Error Get Argus Server Name!"); strncpy( m_sServerName, "Argus Server", 48 ); };
m_pEvtTemplates = (KAFKA_TEMPLATE***) calloc(EVT_CLASS_COUNT, sizeof(KAFKA_TEMPLATE**)); if( m_pEvtTemplates!= NULL ) { memset( m_pEvtTemplates, 0, EVT_CLASS_COUNT * sizeof(KAFKA_TEMPLATE**)); } else LOG_ERROR("EVT Templates Memory Allocation Error!"); /*
Do it in Kernel!! if ( m_pKernel!= NULL ) { argusHardwareManager *tHWM = m_pKernel->getHardwareManager(); if( tHWM != NULL ) { DBC_INT_LIST *rIDS = tHWM->requestResourcesByType( ARGUS_HARDWARE_PROVIDER_NETWORK ); if( rIDS != NULL ) { for( DBC_INT_LIST::iterator idx = rIDS->begin(); idx < rIDS->end(); idx++ ){ argusHardwareResourceNetwork *tRES = (argusHardwareResourceNetwork *) tHWM->requestResource( ARGUS_HARDWARE_PROVIDER_NETWORK, *idx ); if( tRES != NULL ) { m_pServerIP = tRES->getAddress(); break; } else { LOG_ERROR("Cannot Get Resource!"); }; }; delete( rIDS ); } else { LOG("No Networks Detected!"); m_bEnabled = false; }; } else { LOG_ERROR("No HW Manager"); }; } else { LOG_ERROR("No Kernel"); }; */ };
kernelModuleKafka::~kernelModuleKafka(){
m_bEnabled = false;
waitThreadEnd( true );
if( m_pProtocol!= NULL ) { delete( m_pProtocol ); };
if( m_pEvtTemplates!= NULL ) { for(int i=0; i < EVT_CLASS_COUNT; i++ ) { if( m_pEvtTemplates[i] != NULL ){ for( int j=0; j< EVT_COUNT; j++ ) { if( m_pEvtTemplates[i][j] != NULL ){ if( m_pEvtTemplates[i][j]->tpl ) free( m_pEvtTemplates[i][j]->tpl ); free( m_pEvtTemplates[i][j] ); }; }; free( m_pEvtTemplates[i] ); }; }; free( m_pEvtTemplates ); };};/* Periodical Job Handler */void kernelModuleKafka::m_fThreadProc( void ){#ifdef SIMULATOR
float sim_Az = 0.f; float sim_El = 20.f; float sim_Ran = 80.35f; float tr_Az_step = 0.3f; uint32_t src = 128;#endif
#ifdef DEBUG
LOG("Pereodical Jobs Started");#endif
m_bThreadStatus = TH_WORKING; if( !m_bEnabled ) { m_bThreadStatus = TH_ENDED;#ifdef DEBUG
LOG("Module is Disabled!");#endif
}; while( m_bThreadStatus < TH_ENDED ) { m_llTicks ++;//= getMilliseconds();
if( m_llTicks % 20 == 0 ) { // each 2 seconds
#ifdef SIMULATOR
sim_Az += tr_Az_step; for(int i=0; i < EVT_CLASS_COUNT; i++ ) { if( m_pEvtTemplates[i] != NULL ){ for( int j=0; j< EVT_COUNT; j++ ) { if( m_pEvtTemplates[i][j] != NULL ){ CSTR tpl; CSTR ts; CSTR uuid; char LON[10]; char LAT[10]; char AZ[10]; char EL[10]; char RN[10]; float lat = 32.906153575029926; float lon = 35.09629379621815; sprintf( AZ, "%.3f", sim_Az+(0.6/RAND_MAX)*rand()); sprintf( EL, "%.3f", sim_El+(0.6/RAND_MAX)*rand()); sprintf( RN, "%.3f", sim_Ran+(0.6/RAND_MAX)*rand()); sprintf( LAT, "%.10f", lat+(0.00006/RAND_MAX)*rand() ); sprintf( LON, "%.10f", lon+(0.00006/RAND_MAX)*rand() ); uuid = getUniqID_UUID(); ts = getTimeStamp(); src = i; tpl = buildTemplate( m_pEvtTemplates[i][j]->tpl, src, ts, uuid, AZ, RN, EL, LAT, LON ); if( tpl ) { m_pProtocol->issuePost( m_pEvtTemplates[i][j]->topic, tpl, strlen( tpl ) ); free( tpl ); }; if( uuid ) free( uuid ); if( ts ) free( ts ); }; }; }; };#endif
}; if( m_bConsumerEnabled ) { for( uint8_t idx = 0; idx < EVT_COUNT; idx++ ) { if( m_pEvtTemplates[EVT_CLASS_EXTERNAL_CMD][idx] != NULL ){ char BUF[1024]; int len; if( m_pProtocol->receivePost( m_pEvtTemplates[EVT_CLASS_EXTERNAL_CMD][idx]->topic, partition, start_offset, BUF, len ) ){ if( len > 0 ) { start_offset++;#ifdef DEBUG
char ttt[64]; LOG(" HAVE MESSAGE "); sprintf( ttt, " MSG Len: %d", len ); LOG( ttt );#endif
if( len > 0 ){ LOG ( BUF ); try{ Json::Value root;#ifdef OLD_WAY_JSON
Json::Reader reader; if( !reader.parse( BUF, root ) ){ LOG_ERROR( "Error parsing the string" ); } else {#else
JSONCPP_STRING err; Json::CharReaderBuilder builder; const std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); if (!reader->parse(BUF, BUF + len, &root, &err)) { LOG_ERROR( "Error parsing the string" ); LOG_ERROR( err.c_str() ); } else {#endif
if (root.size()) { FH_CTRL_DATA * tData; basicEventTypeID evtID = EVT_NOINIT; Json::Value tVal; tData =(FH_CTRL_DATA *)malloc( sizeof(FH_CTRL_DATA)); if( tData ) { memset( tData, 0, sizeof(FH_CTRL_DATA)); tVal = root["id"]; if (!tVal.isNull()) { #ifdef DEBUG
LOG(tVal.asString().c_str()); #endif
sprintf( tData->cmdUUID, "%s", tVal.asString().c_str() ); }; tVal = root["action"]; if (!tVal.isNull()) { #ifdef DEBUG
LOG( tVal.asString().c_str() ); #endif
if ( strcasecmp(tVal.asString().c_str(), "intercept_target") == 0) { evtID = EVT_FH_MISSION_BEGIN; }; if ( strcasecmp(tVal.asString().c_str(), "jam_target") == 0) { tData->param = atoi( tVal.asString().c_str()+10 ); if( tData->param == 0 ) { tData->param = FH_DEFAULT_JAMING_TIME; }; evtID = EVT_FH_JAMING_BEGIN; }; if ( strcasecmp(tVal.asString().c_str(), "cancel") == 0) { evtID = EVT_FH_CANCEL_ACTION; }; }; tVal = root["event_id"]; if (!tVal.isNull()) { #ifdef DEBUG
LOG( tVal.asString().c_str() ); #endif
sprintf( tData->evtUUID, "%s", tVal.asString().c_str() ); }; tVal = root["target_id"]; if (!tVal.isNull()) { #ifdef DEBUG
LOG( tVal.asString().c_str() ); #endif
sprintf( tData->targetUUID, "%s", tVal.asString().c_str() ); }; if( evtID ) { EVENT_CONTAINER * tEVT; tEVT = createBasicEvent( 0, EVT_CLASS_EXTERNAL_CMD, evtID, NULL, NULL, tData ); if( tEVT ) { m_pEventHeap->broadcast( tEVT ); } else { free( tData ); }; } else { free( tData ); }; }; }; }; } catch( exception e ){ LOG_ERROR("EXCEPTION"); }; }; }; }; }; }; }; SLEEP(100); };};
bool kernelModuleKafka::startService(){ if(!m_bEnabled) return false; if( reloadSettings() ) { if( m_pSettings!= NULL ){ m_pProtocol = new protocolKAFKA( m_pSettings ); if( m_pProtocol->init() ){ return init(); }; }; }; return false;};
bool kernelModuleKafka::stopService( bool force ){ waitThreadEnd( true ); return true;};
bool kernelModuleKafka::reloadSettings(){ if( m_pDBC==NULL ) return false; if(! m_bEnabled) return false; m_pSettings = m_pDBC->getSettingsKAFKA( m_nDBAUXID ); if( m_pSettings != NULL ) { if( m_pEvtTemplates != NULL ) { // Clear Templates
for(int i = 0; i < EVT_CLASS_COUNT; i++ ){ for(int j = 0; j < EVT_COUNT; j++ ){ if( m_pEvtTemplates[i] != NULL ){ if( m_pEvtTemplates[i][j] != NULL ){ if( m_pEvtTemplates[i][j]->tpl ) free( m_pEvtTemplates[i][j]->tpl ); free( m_pEvtTemplates[i][j] ); m_pEvtTemplates[i][j] = NULL; }; }; }; free( m_pEvtTemplates[i] ); m_pEvtTemplates[i] = NULL; }; for(int i = 0; i < EVT_CLASS_COUNT; i++ ){ for(int j = 0; j < EVT_COUNT; j++ ){ KAFKA_TEMPLATE * tT; tT = m_pDBC->getKafkaTemplateForEvent( m_nDBAUXID, i, j ); if( tT != NULL ) { if( m_pEvtTemplates[i]== NULL ) { m_pEvtTemplates[i]=(KAFKA_TEMPLATE**)calloc(EVT_COUNT,sizeof(KAFKA_TEMPLATE*)); memset( m_pEvtTemplates[i], 0, EVT_COUNT * sizeof(KAFKA_TEMPLATE*)); if( i == EVT_CLASS_EXTERNAL_CMD ) { m_bConsumerEnabled = true; }; }; m_pEvtTemplates[i][j] = tT; }; }; }; }; return true; }; return false;};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, CSTR src_rot_x, CSTR src_rot_y, CSTR src_rot_z){ CSTR ct1, ct2; char sid[64]; if( tpl ) { sprintf( sid, "%d", source_id ); ct1 = strReplace( tpl, KAFKA_TPL_SUBST_ID, sid ); if( ct1 ) { ct2 = strReplace( ct1, KAFKA_TPL_SUBST_TS, ts ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; if( uuid ){ ct2 = strReplace( ct1, KAFKA_TPL_SUBST_UUID, uuid ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; }; if( az ){ ct2 = strReplace( ct1, KAFKA_TPL_SUBST_AZ, az ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; }; if( range ){ ct2 = strReplace( ct1, KAFKA_TPL_SUBST_RANGE, range ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; }; if( width ){ ct2 = strReplace( ct1, KAFKA_TPL_SUBST_WIDTH, width ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; }; if( el ){ ct2 = strReplace( ct1, KAFKA_TPL_SUBST_EL, el ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; }; if( lat ){ ct2 = strReplace( ct1, KAFKA_TPL_SUBST_LAT, lat ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; }; if( lon ){ ct2 = strReplace( ct1, KAFKA_TPL_SUBST_LON, lon ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; }; if( alt ){ ct2 = strReplace( ct1, KAFKA_TPL_SUBST_ALT, alt ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; }; if( extID ){ ct2 = strReplace( ct1, KAFKA_TPL_SUBST_EXEVTD, extID ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; }; if( src_Name ){ ct2 = strReplace( ct1, KAFKA_TPL_SUBST_SRC_NAME, src_Name ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; }; if( src_IP ){ ct2 = strReplace( ct1, KAFKA_TPL_SUBST_SRC_IP, src_IP ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; }; if( src_lat ){ ct2 = strReplace( ct1, KAFKA_TPL_SUBST_SRC_LAT, src_lat ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; }; if( src_lon ){ ct2 = strReplace( ct1, KAFKA_TPL_SUBST_SRC_LON, src_lon ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; }; if( src_alt ){ ct2 = strReplace( ct1, KAFKA_TPL_SUBST_SRC_ALT, src_alt ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; }; if( src_rot_x ){ ct2 = strReplace( ct1, KAFKA_TPL_SUBST_SRC_ROT_X, src_rot_x ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; }; if( src_rot_y ){ ct2 = strReplace( ct1, KAFKA_TPL_SUBST_SRC_ROT_Y, src_rot_y ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; }; if( src_rot_z ){ ct2 = strReplace( ct1, KAFKA_TPL_SUBST_SRC_ROT_Z, src_rot_z ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; }; //Finalaze Message by System Wide Data
// UUID
CSTR TVAL = getUniqID_UUID(); if ( TVAL != NULL ) { ct2 = strReplace(ct1, KAFKA_TPL_SUBST_MSG_UUID, TVAL ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; free( TVAL ); } else { LOG_ERROR("Error Generate Message UUID!"); }; // Server IP
ct2 = strReplace(ct1, KAFKA_TPL_SUBST_SRV_IP, m_sServerIP ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; // Server Name
ct2 = strReplace(ct1, KAFKA_TPL_SUBST_SRV_NAME, m_sServerName ); if( ct2 ) { free( ct1 ); ct1 = ct2; }; return ct1; }; } return NULL;};
bool kernelModuleKafka::dispatchEvent( EVENT_CONTAINER *Evt ){ if( Evt == NULL ) return false; if( !m_bEnabled ) return true;#ifdef DEBUG
char tBuffer[256]; sprintf( tBuffer, "Event Class %d Type %d ", Evt->m_eClass , Evt->m_eType ); LOG( tBuffer );#endif
if( m_pEvtTemplates[ Evt->m_eClass ]!= NULL ) { // We have defined Channel for that class Events
// Skip Event Handling by flags
if( !m_bEvtFlags[ Evt->m_eType ] ){#ifdef DEBUG
//LOG("Disabled By FLAG!");
#endif
//return;
} // Hadle Event
CSTR tDATA = NULL; if( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ] != NULL ) { switch( Evt->m_eType ) { case EVT_TARGET_2D_BLACK: case EVT_TARGET_2D_WHITE:{ TARGET_DATA * tTARGET = ( TARGET_DATA *) Evt->m_pEvtData; if(tTARGET!=NULL){ char AZ[10]; char RA[10]; char WID[10];
char LAT[10]; char LON[10]; char ALT[10];
char SLAT[10]; char SLON[10]; char SALT[10]; char SROT_X[10]; char SROT_Y[10]; char SROT_Z[10]; char SNAME[64]; char TS[64];
sprintf( AZ,"%0.3f", tTARGET->local_target.fi ); sprintf( RA,"%0.3f", tTARGET->local_target.r ); sprintf( WID,"%0.3f", tTARGET->area ); sprintf( LAT,"%0.6f", tTARGET->global_target.lat ); sprintf( LON,"%0.6f", tTARGET->global_target.lon ); sprintf( ALT,"%0.6f", tTARGET->global_target.alt ); 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) ); sprintf( SLAT,"%0.6f", tTARGET->source_position.lat ); sprintf( SLON,"%0.6f", tTARGET->source_position.lon ); sprintf( SALT,"%0.6f", tTARGET->source_position.alt );
sprintf( SROT_X,"%0.6f", tTARGET->source_orient.rot_x ); sprintf( SROT_Y,"%0.6f", tTARGET->source_orient.rot_y ); sprintf( SROT_Z,"%0.6f", tTARGET->source_orient.rot_z ); CSTR tN = m_pDBC->getNodeName( Evt->m_nSourceNodeID ); if ( tN ) { strncpy( SNAME, tN, 64 ); free( tN ); } else { strncpy(SNAME, "Noname-Laser-Detector", 64); }; 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 ); }; break; }; case EVT_TARGET_3D_BLACK: case EVT_TARGET_3D_WHITE:{ TARGET_DATA * tTARGET = ( TARGET_DATA *) Evt->m_pEvtData; if(tTARGET!=NULL){ char AZ[10]; char EL[10]; char RA[10]; char WID[10]; char LAT[10]; char LON[10]; char ALT[10];
char SLAT[10]; char SLON[10]; char SALT[10]; char SROT_X[10]; char SROT_Y[10]; char SROT_Z[10];
char SIP[16]; char SNAME[64];
char TS[64]; sprintf( AZ,"%0.3f", tTARGET->local_target.fi ); sprintf( EL,"%0.3f", tTARGET->local_target.tetta ); sprintf( RA,"%0.3f", tTARGET->local_target.r ); sprintf( WID,"%0.3f", tTARGET->area ); sprintf( LAT,"%0.6f", tTARGET->global_target.lat ); sprintf( LON,"%0.6f", tTARGET->global_target.lon ); sprintf( ALT,"%0.6f", tTARGET->global_target.alt ); 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) ); sprintf( SLAT,"%0.6f", tTARGET->source_position.lat ); sprintf( SLON,"%0.6f", tTARGET->source_position.lon ); sprintf( SALT,"%0.6f", tTARGET->source_position.alt );
sprintf( SROT_X,"%0.6f", tTARGET->source_orient.rot_x ); sprintf( SROT_Y,"%0.6f", tTARGET->source_orient.rot_y ); sprintf( SROT_Z,"%0.6f", tTARGET->source_orient.rot_z ); CSTR tN = m_pDBC->getNodeName( Evt->m_nSourceNodeID ); if ( tN ) { strncpy( SNAME, tN, 64 ); free( tN ); } else { strncpy(SNAME, "Noname-Laser-Detector", 64); };
if ( 0 ) { strncpy( SIP, tN, 16 ); free( tN ); } else { strncpy(SIP, "192.168.1.24", 16); };
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 ); }; break; }; case EVT_FH_JAMER_ON: case EVT_FH_JAMER_OFF: case EVT_FH_FLY_TO: case EVT_FH_WAYPOINT: case EVT_FH_INTERCEPT_OK: case EVT_FH_INTERCEPT_NOK: case EVT_FH_BUSY: case EVT_FH_LANDED:{ FH_MISSION_DATA * tPOINT=(FH_MISSION_DATA *) Evt->m_pEvtData; if( tPOINT ) { char TS[64]; char LAT[10]; char LON[10]; char ALT[10]; sprintf( LAT,"%0.6f", tPOINT->location.lat ); sprintf( LON,"%0.6f", tPOINT->location.lon ); sprintf( ALT,"%0.6f", tPOINT->location.alt ); 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) ); 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 ); }; break; }; default:{ break; }; }; if( tDATA!= NULL ) {#ifdef KAFKA_DUMMY_SEND
LOG( tDATA );#else
m_pProtocol->issuePost( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ]->topic, tDATA, strlen( tDATA ) );#endif
} else { LOG_ERROR("Cant Build Template!"); }; }; }; return true;};
#undef LOG
#undef LOG_ERROR
|