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.
 
 
 
 
 
 

349 lines
12 KiB

#include "rgooseKafkaModule.h"
#include "../hardware/soc/general/3DLRF.h"
#define LOG( X ) argusKernelModule::m_pLog->Log( "KM", 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 );\
argusKernelModule::m_pLog->Log( "KM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
}
#else
#define LOG_ERROR( X ) argusKernelModule::m_pLog->Log( "KM", this, _LOG_ERROR, X );
#endif
argusKafkaModule::argusKafkaModule( uint32_t ModuleID ): argusKernelModule( ModuleID, ARGUS_MODULE_KAFKA ){
m_pProtocol = NULL;
m_bConsumerEnabled = false;
partition = RdKafka::Topic::PARTITION_UA;
start_offset = RdKafka::Topic::OFFSET_BEGINNING;
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!");
};
argusKafkaModule::~argusKafkaModule(){
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 argusKafkaModule::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
sim_Az += tr_Az_step;
#ifdef SIMULATOR
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 ) ){
LOG(" HAVE MESSAGE ");
};
};
};
};
SLEEP(100);
};
};
bool argusKafkaModule::startService(){
if(!m_bEnabled) return false;
if( reloadSettings() ) {
if( m_pSettings!= NULL ){
m_pProtocol = new protocolKAFKA( m_pSettings );
if( m_pProtocol->init() ){
// No need pereodical jobs unless SIMULATOR
#ifdef SIMULATOR
LOG("Start PJ");
return init();
#else
return true;
#endif
};
};
};
return false;
};
bool argusKafkaModule::stopService(){
waitThreadEnd( true );
return true;
};
bool argusKafkaModule::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_nDBID, 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;
#ifdef DEBUG
LOG( "Consumer Enabled" );
#endif
}
};
m_pEvtTemplates[i][j] = tT;
};
};
};
};
return true;
};
return false;
};
CSTR argusKafkaModule::buildTemplate(
CSTR tpl,
uint32_t source_id,
CSTR ts,
CSTR uuid,
CSTR az,
CSTR range,
CSTR width,
CSTR el,
CSTR lat,
CSTR lon
){
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;
};
};
return ct1;
};
}
return NULL;
};
void argusKafkaModule::dispatchEvent( basicEvent *Evt ){
#ifdef DEBUG
LOG( "EVT DISPATCHER");
#endif
if( Evt == NULL ) {
LOG( "Evt NULL");
return;
};
if( !m_bEnabled )return;
if( m_pEvtTemplates[ Evt->m_eClass ]!= NULL ) {
// We have defined Channel for that class Events
#ifdef DEBUG
char tBuffer[256];
sprintf( tBuffer, "Event Class %d Type %d ", Evt->m_eClass , Evt->m_eType );
LOG( tBuffer );
#endif
// Skip Event Handling by flags
if( !m_bEvtFlags[ Evt->m_eType ] ) return;
// Hadle Event
CSTR tDATA;
if( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ] != NULL ) {
CLIENT_EVENT *tCEVT;
tCEVT = ( CLIENT_EVENT *) Evt->m_pEvtData;
tDATA = buildTemplate( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ]->tpl,
Evt->m_nSourceUID,
getUniqID_MD5()
);
if( tDATA!= NULL ) {
m_pProtocol->issuePost( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ]->topic, tDATA, strlen( tDATA ) );
};
};
/*
switch( Evt->m_eType ) {
case EVT_TARGET_1D:
case EVT_TARGET_2D_WHITE:
case EVT_TARGET_2D_BLACK:
case EVT_TARGET_3D_WHITE:
case EVT_TARGET_3D_BLACK:{
CLIENT_EVENT *tCEVT;
tCEVT = ( CLIENT_EVENT *) Evt->m_pEvtData;
if( tCEVT != NULL ){
float Az, El;
uint32_t cRange;
cRange = (uint32_t)(((float) tCEVT->range ) * 0.0732421875f ) ; // To meters
#ifdef PRU_SLAVE
make3Ddeg( tCEVT->th_id, Az, El, false );
#else
make3Ddeg( tCEVT->th_id, Az, El, true );
#endif
m_pProtocol->Alarm3D( (int)Az, (int)El, cRange , tCEVT->k_id );
};
};
break;
case EVT_ERROR:
m_pProtocol->SetStatus( false );
break;
case EVT_SCAN:
//StatusUpdate();
//break;
case EVT_FLIP:
m_pProtocol->SetStatus( true );
//StatusUpdate();
break;
case EVT_COMMAND:
break;
default:
LOG_ERROR("Unknown Event!");
};
*/
};
};
#undef LOG
#undef LOG_ERROR