From c148b6adf2dd4dfe740fae13b2185073e3407c23 Mon Sep 17 00:00:00 2001 From: Evgeny Gagua Date: Wed, 5 Aug 2020 08:21:41 +0300 Subject: [PATCH] Kafka protocol started! --- Projects/LRF_Server/LRF_Server.vpj | 2 + lib/data/protocol/protocolInfoStress.cpp | 795 +++++++++++++++++++++++ lib/data/protocol/protocolInfoStress.h | 151 +++++ lib/rgoose/rgooseNodeFactory.h | 1 + 4 files changed, 949 insertions(+) create mode 100644 lib/data/protocol/protocolInfoStress.cpp create mode 100644 lib/data/protocol/protocolInfoStress.h diff --git a/Projects/LRF_Server/LRF_Server.vpj b/Projects/LRF_Server/LRF_Server.vpj index 27b82ad8..619419b6 100644 --- a/Projects/LRF_Server/LRF_Server.vpj +++ b/Projects/LRF_Server/LRF_Server.vpj @@ -254,6 +254,7 @@ + @@ -306,6 +307,7 @@ + diff --git a/lib/data/protocol/protocolInfoStress.cpp b/lib/data/protocol/protocolInfoStress.cpp new file mode 100644 index 00000000..c261708b --- /dev/null +++ b/lib/data/protocol/protocolInfoStress.cpp @@ -0,0 +1,795 @@ +#include "protocolInfoStress.h" +#include "../../alg/generic/md5.h" +#include "../json/json_tool.h" +#include +#include +#include +#include + +#define LOG( X ) m_pLog->Log( "InfoStress", this, X ) +#define LOGS( X ) basicLogCollection::getInstance()->Log( "InfoStress", NULL, X ) +#define LOG_ERROR( X ) m_pLog->Log( "InfoStress", this, _LOG_ERROR, X ) + +CSTR defenderStatusMode[] = { + "OPERATIONAL", + "DEGRADED" +}; + +CSTR defenderFaultCode[] = { + "Critical", + "Warning", + "Info" +}; + +CSTR defenderCategories[] = { + DFSL_CATEGORY_ERROR, + DFSL_CATEGORY_STATUS, + DFSL_CATEGORY_ALARM_2D, + DFSL_CATEGORY_ALARM_3D +}; + +CSTR defenderTypeDescription[] = { + "Fault Event", + "Status Message", + "Target Detection Event", + "Target Detection Event" +}; + +CSTR defenderTypeSeverity[] = { + "very-low", + "low", + "medium", + "high", + "very-high" +}; + +/* Global Time Diff Value to correct Timestamps */ +static int g_iTimeDiff = 0; + +/** + * number of seconds from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC + */ +//#define EPOCH_DIFF 11644473600LL + +#define UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT 116444736000000000 + +#ifdef __linux__ +unsigned long long getSHDTimeValue( void ){ + unsigned long long tMilliseconds; + struct timeval tData; + struct timezone tTZ; + tMilliseconds = 0; + tTZ.tz_minuteswest = 0; + tTZ.tz_dsttime = 0; + if( gettimeofday( &tData, &tTZ ) == -1 ){ + } else { + tMilliseconds = tData.tv_sec; + tMilliseconds = tMilliseconds *1000; + tMilliseconds += tData.tv_usec / 1000; + }; +#else +/* +void TimetToFileTime( time_t t, LPFILETIME pft ){ + LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000; + pft->dwLowDateTime = (DWORD) ll; + pft->dwHighDateTime = ll >>32; +}*/ +unsigned __int64 getSHDTimeValue( void ){ + unsigned __int64 tMilliseconds; + SYSTEMTIME Ztime; + GetSystemTime(&Ztime); + SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds ); + tMilliseconds -= UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT; + tMilliseconds = tMilliseconds / 10000; +#endif + tMilliseconds += g_iTimeDiff; + return tMilliseconds; +}; + +CSTR getSHDTimeStamp( void ){ + CSTR RetVal = NULL; + time_t rawtime; + uint32_t msec; + struct tm * ptm = NULL; + + time ( &rawtime ); + ptm = gmtime ( &rawtime ); + msec = getSHTimeValue() % 1000; + //2019-03-24T12:32:59.670Z + if( ptm != NULL ) { + RetVal = (CSTR) malloc( 128 ); + if( RetVal!=NULL ){ + sprintf( RetVal, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", + (ptm->tm_year+1900), + (ptm->tm_mon+1), + ptm->tm_mday, + ptm->tm_hour, + ptm->tm_min, + ptm->tm_sec, + msec + ); + }; + }; + return RetVal; +}; + +bool correctSHDTime( int diff ){ +#ifdef __linux__ + struct timeval tData; + struct timezone tTZ; + tTZ.tz_minuteswest = 0; + tTZ.tz_dsttime = 0; + if( gettimeofday( &tData, &tTZ ) == -1 ){ + return false; + } else { + tData.tv_sec += diff / 1000; + tData.tv_usec += ( diff * 1000 ) % 1000000; + }; + if( settimeofday( &tData, &tTZ ) == -1 ){ + return false; + }; +#else + unsigned __int64 tMilliseconds; + SYSTEMTIME Ztime; + GetSystemTime(&Ztime); + SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds ); + tMilliseconds += diff * 10000; + FileTimeToSystemTime( (FILETIME*) &tMilliseconds, &Ztime ); + if( 0 == SetSystemTime(&Ztime) ) return false; +#endif + return true; +}; + + +CSTR protocolInfoStress::buildURL( CSTR templ, int dim, CSTR devID ){ + CSTR RetVal; + RetVal = (CSTR) malloc( 256 ); + if (RetVal != NULL) { + if (m_bUseSSL) { + sprintf(RetVal, "https://%s:%d%s", m_pServerIP, m_nServerPort, m_pAccessURL); + } else { + sprintf(RetVal, "http://%s:%d%s", m_pServerIP, m_nServerPort, m_pAccessURL); + }; + }; + return RetVal; +}; + +CSTR protocolInfoStress::buildUniqID( void ){ + CSTR RetVal; + RetVal = (CSTR) malloc(64); + if(RetVal) sprintf( RetVal, "%s", MD5String( getSHDTimeStamp() ) ); + return RetVal; +}; + +/* Default Constructor */ +protocolInfoStress::protocolInfoStress( config_setting_t *Settings ){ + + m_pLog = basicLogCollection::getInstance(); + m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "INOSTRESS_CONNECTION"); + m_pKafka = NULL; + m_pKafkaConf = NULL; + m_pKafkaBrokers = (CSTR)malloc(CLOG); + if( m_pKafkaBrokers == NULL ) return; + memset( m_pKafkaBrokers, 0 , CLOG); + m_pScannerName = (CSTR)malloc(OS_SCANNER_NAME_SIZE); + if( m_pScannerName == NULL ) return; + memset( m_pScannerName, 0 , OS_SCANNER_NAME_SIZE); + m_pServerIP = (CSTR)malloc(OS_CEP_ADDRESS_SIZE); + if( m_pServerIP == NULL ) return; + memset( m_pServerIP, 0 , OS_CEP_ADDRESS_SIZE); + m_pAccessURL = (CSTR)malloc(CTEXT); + if (m_pAccessURL == NULL) return; + memset(m_pAccessURL, 0, CTEXT); + + m_bCurrentStatus = false; + m_bMitigationStatus = false; + + if( m_pSettings != NULL ){ + int intVar; + double floatVar; + config_setting_t *pSetting; + if (Settings != NULL) { + m_bEnabled = true; + } else { + if (!config_setting_lookup_bool(m_pSettings, "ENABLE", &intVar)) { + intVar = m_bEnabled = false; + LOG("PROTOCOL DISABLED[default]"); + pSetting = config_setting_add(m_pSettings, "ENABLE", CONFIG_TYPE_BOOL); + config_setting_set_bool(pSetting, intVar); + basicSettingsProvider::getInstance()->compromise(); + } else { + m_bEnabled = (bool)intVar; + }; + }; + if ( m_bEnabled ) { + if (!config_setting_lookup_float(m_pSettings, "LOCATION_LON", &floatVar)) { + m_fLon = floatVar = 35.0938962; + pSetting = config_setting_add(m_pSettings, "LOCATION_LON", CONFIG_TYPE_FLOAT); + config_setting_set_float(pSetting, floatVar); + basicSettingsProvider::getInstance()->compromise(); + } else { + m_fLon = floatVar; + }; + if (!config_setting_lookup_float(m_pSettings, "LOCATION_LAT", &floatVar)) { + m_fLat = floatVar = 32.904208; + pSetting = config_setting_add(m_pSettings, "LOCATION_LAT", CONFIG_TYPE_FLOAT); + config_setting_set_float(pSetting, floatVar); + basicSettingsProvider::getInstance()->compromise(); + } else { + m_fLat = floatVar; + }; + if (!config_setting_lookup_bool(m_pSettings, "USE_SSL", &intVar)) { + intVar = m_bUseSSL = false; + LOG("PROTOCOL DISABLED[default]"); + pSetting = config_setting_add(m_pSettings, "USE_SSL", CONFIG_TYPE_BOOL); + config_setting_set_bool(pSetting, intVar); + basicSettingsProvider::getInstance()->compromise(); + } else { + m_bUseSSL = (bool)intVar; + }; + if (!config_setting_lookup_string(m_pSettings, "SCANNER_NAME", (const char**)&m_pScannerName)) { + strncpy(m_pScannerName, "LRF_2D_001", 64); + LOG(m_pScannerName); + pSetting = config_setting_add(m_pSettings, "SCANNER_NAME", CONFIG_TYPE_STRING); + config_setting_set_string(pSetting, m_pScannerName); + basicSettingsProvider::getInstance()->compromise(); + }; + if (!config_setting_lookup_int(m_pSettings, "SCANNER_DIMENTIONS", &m_nScannerDimentions)) { + m_nScannerDimentions = 2; + pSetting = config_setting_add(m_pSettings, "SCANNER_DIMENTIONS", CONFIG_TYPE_INT); + config_setting_set_int(pSetting, m_nScannerDimentions); + basicSettingsProvider::getInstance()->compromise(); + }; + if (!config_setting_lookup_string(m_pSettings, "CEP_IP_ADDRESS", (const char**)&m_pServerIP)) { + strncpy(m_pServerIP, "192.168.200.204", 16); + LOG(m_pServerIP); + pSetting = config_setting_add(m_pSettings, "CEP_IP_ADDRESS", CONFIG_TYPE_STRING); + config_setting_set_string(pSetting, m_pServerIP); + basicSettingsProvider::getInstance()->compromise(); + }; + LOG(m_pServerIP); + if (!config_setting_lookup_int(m_pSettings, "CEP_IP_PORT", &m_nServerPort)) { + m_nServerPort = 80; + pSetting = config_setting_add(m_pSettings, "CEP_IP_PORT", CONFIG_TYPE_INT); + config_setting_set_int(pSetting, m_nServerPort); + basicSettingsProvider::getInstance()->compromise(); + }; + if (!config_setting_lookup_string(m_pSettings, "ACCESS_URL", (const char**)&m_pAccessURL)) { + strncpy( m_pAccessURL, DFSL_DEFENDER_DEFAULT_URL, CTEXT ); + pSetting = config_setting_add(m_pSettings, "ACCESS_URL", CONFIG_TYPE_STRING); + config_setting_set_string(pSetting, m_pAccessURL); + basicSettingsProvider::getInstance()->compromise(); + }; + LOG(m_pAccessURL); + }; + basicSettingsProvider::getInstance()->validate(); + + }; +}; + +/** + * @brief Message delivery report callback. + * + * This callback is called exactly once per message, indicating if + * the message was succesfully delivered + * (rkmessage->err == RD_KAFKA_RESP_ERR_NO_ERROR) or permanently + * failed delivery (rkmessage->err != RD_KAFKA_RESP_ERR_NO_ERROR). + * + * The callback is triggered from rd_kafka_poll() and executes on + * the application's thread. + */ +static void dr_msg_cb( rd_kafka_t *rk, const rd_kafka_message_t *rkmessage, void *opaque ){ + if (rkmessage->err) + fprintf(stderr, "%% Message delivery failed: %s\n", + rd_kafka_err2str(rkmessage->err)); + else + fprintf(stderr, + "%% Message delivered (%zd bytes, " + "partition %"PRId32")\n", + rkmessage->len, rkmessage->partition); + + /* The rkmessage is destroyed automatically by librdkafka */ +}; + +protocolInfoStress::~protocolInfoStress(){ + if(m_pKafka!=NULL){ + /* Wait for final messages to be delivered or fail. + * rd_kafka_flush() is an abstraction over rd_kafka_poll() which + * waits for all messages to be delivered. */ + LOG("Flushing final messages.."); + rd_kafka_flush( m_pKafka, 3000 ); + /* If the output queue is still not empty there is an issue + * with producing messages to the clusters. */ + if( rd_kafka_outq_len( m_pKafka ) > 0 ) LOG_ERROR("Some messages are not delivered!"); + rd_kafka_destroy(m_pKafka); + }; + if(m_pKafkaConf!=NULL)rd_kafka_destroy(m_pKafkaConf); + if(m_pScannerName!=NULL)free(m_pScannerName); + if(m_pServerIP!=NULL)free(m_pServerIP); + if( m_pAccessURL != NULL)free(m_pAccessURL); +}; +//https://github.com/edenhill/librdkafka/blob/master/examples/producer.c +//https://github.com/edenhill/librdkafka/blob/master/examples/producer.cpp + +bool protocolInfoStress::init(){ + char tBuffer[CLOG]; + if( strlen( m_pKafkaBrokers ) == 0 ) { + LOG_ERROR("No Kafka Brokers are defined!"); + return false; + }; + m_pKafkaConf = rd_kafka_conf_new(); + if( m_pKafkaConf == NULL ) { + LOG_ERROR("Eror Creating Kafka Conf!"); + return false; + }; + if( rd_kafka_conf_set( m_pKafkaConf, "bootstrap.servers", m_pKafkaBrokers, tBuffer, CLOG ) != RD_KAFKA_CONF_OK ){ + LOG_ERROR( tBuffer ); + return false; + }; + /* Set the delivery report callback. + * This callback will be called once per message to inform + * the application if delivery succeeded or failed. + * See dr_msg_cb() above. + * The callback is only triggered from rd_kafka_poll() and + * rd_kafka_flush(). */ + rd_kafka_conf_set_dr_msg_cb( m_pKafkaConf, dr_msg_cb ); + /* + * Create producer instance. + * + * NOTE: rd_kafka_new() takes ownership of the conf object + * and the application must not reference it again after + * this call. + */ + m_pKafka = rd_kafka_new( RD_KAFKA_PRODUCER, m_pKafkaConf, tBuffer, CLOG ); + if( m_pKafka == NULL ){ + LOG_ERROR( tBuffer ); + return false; + }; + return true; +}; + +void protocolInfoStress::setScannerName( CSTR Name ){ + sprintf( m_pScannerName, "%s", Name ); +}; +void protocolInfoStress::setProtocolType( bool DeviceIs3D ){ + if( DeviceIs3D ) { + m_nScannerDimentions = 3; + } else { + m_nScannerDimentions = 2; + }; +}; +CSTR protocolInfoStress::assembleStatusData( bool StatusOK ){ + CSTR RetVal = NULL; + CSTR TIME_STAMP; + CSTR tSRC; + CSTR tTARGET; + CSTR tID; + RetVal = (CSTR) malloc(CLOG); + if ( RetVal != NULL ) { + TIME_STAMP = getSHDTimeStamp(); + if( TIME_STAMP != NULL ) { + tTARGET = (CSTR) malloc(128); + if( tTARGET!= NULL) { + tSRC = (CSTR) malloc(128); + if( tSRC ) { + tID = (CSTR) malloc(64); + if( tID != NULL ) { + sprintf( tID, "%s", MD5String( TIME_STAMP ) ); + sprintf( tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName ); + sprintf( tTARGET, DFSL_STATUS_DTO, TIME_STAMP , defenderStatusMode[((StatusOK)?1:0)] ); + sprintf( RetVal, + PACKAGE_SE_STATUS_DATA_ELEMENT, + defenderTypeDescription[DC_STATUS], + TIME_STAMP, + tSRC, + "DFSL_LIDAR_SENSOR_STATUS", + DFSL_DATA_TYPE_STATUS, + tID, + tTARGET + ); + free( tID ); + free( tSRC ); + free( tTARGET ); + free( TIME_STAMP ); + } else { + free( tSRC ); + free( tTARGET ); + free( TIME_STAMP ); + free( RetVal ); + } + } else { + free( tTARGET ); + free( TIME_STAMP ); + free( RetVal ); + }; + } else { + free( TIME_STAMP ); + free( RetVal ); + }; + } else { + free( RetVal ); + RetVal = NULL; + }; + }; + return RetVal; +}; +CSTR protocolInfoStress::assembleFaultData( uint8_t Code, CSTR Desc ){ + CSTR RetVal; + CSTR TIME_STAMP; + CSTR tSRC; + CSTR tTARGET; + CSTR tID; + RetVal = (CSTR) malloc(CLOG); + if ( RetVal != NULL ) { + TIME_STAMP = getSHDTimeStamp(); + if( TIME_STAMP != NULL ) { + tTARGET = (CSTR) malloc(128); + if( tTARGET!= NULL) { + tSRC = (CSTR) malloc(128); + if( tSRC ) { + tID = (CSTR) malloc(64); + if( tID != NULL ) { + sprintf( tID, "%s", MD5String( TIME_STAMP ) ); + sprintf( tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName ); + sprintf( tTARGET, DFSL_ERROR_DTO, TIME_STAMP, Code, Desc ); + sprintf( RetVal, + PACKAGE_SE_STATUS_DATA_ELEMENT, + Desc, + TIME_STAMP, + tSRC, + "DFSL_LIDARE_SENSOR_FAULT_DATA", + DFSL_DATA_TYPE_ERROR, + tID, + tTARGET + ); + free( tID ); + free( tSRC ); + free( tTARGET ); + free( TIME_STAMP ); + } else { + free( tSRC ); + free( tTARGET ); + free( TIME_STAMP ); + free( RetVal ); + } + } else { + free( tTARGET ); + free( TIME_STAMP ); + free( RetVal ); + }; + } else { + free( TIME_STAMP ); + free( RetVal ); + }; + } else { + free( RetVal ); + RetVal = NULL; + }; + }; + return RetVal; +}; +CSTR protocolInfoStress::assembleTargetData( int targetID, int Range, int Width, int Azimuth, int Elevation ){ + CSTR RetVal; + CSTR TIME_STAMP; + CSTR tSRC; + CSTR tTARGET; + CSTR tID; + RetVal = (CSTR) malloc(CLOG); + if ( RetVal != NULL ) { + TIME_STAMP = getSHDTimeStamp(); + if( TIME_STAMP != NULL ) { + tTARGET = (CSTR) malloc(128); + if( tTARGET!= NULL) { + tSRC = (CSTR) malloc(128); + if( tSRC ) { + tID = (CSTR) malloc(64); + if( tID != NULL ) { + sprintf( tID, "%s", MD5String( TIME_STAMP ) ); + sprintf( tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName ); + if( m_nScannerDimentions > 2 ) { + sprintf( tTARGET, DFSL_3D_TARGET_DTO, Azimuth, Elevation, Range ); + } else { + sprintf( tTARGET, DFSL_2D_TARGET_DTO, Azimuth, Range, Width ); + }; + sprintf( RetVal, + PACKAGE_SE_TARGET_DATA_ELEMENT, + defenderTypeDescription[DC_ALARM], + TIME_STAMP, + tSRC, + "DFSL_TARGET_DATA", + DFSL_DATA_TYPE_TARGET, + tID, + tTARGET + ); + free( tID ); + free( tSRC ); + free( tTARGET ); + free( TIME_STAMP ); + } else { + free( tSRC ); + free( tTARGET ); + free( TIME_STAMP ); + free( RetVal ); + } + } else { + free( tTARGET ); + free( TIME_STAMP ); + free( RetVal ); + }; + } else { + free( TIME_STAMP ); + free( RetVal ); + }; + } else { + free( RetVal ); + RetVal = NULL; + }; + }; + return RetVal; +}; +static uint32_t DEFENDER_MESSAGE_ID = 0; +size_t protocolInfoStress::assembleDefenderJSON( CSTR Buffer, defenderCategoryTypes Type, uint8_t Severity, CSTR Location, CSTR Data ){ + char tBuffer[CLOG]; + size_t RetVal; + CSTR tBuf; + RetVal = 0; + tBuf = getSHDTimeStamp(); + DEFENDER_MESSAGE_ID++; + //memset( tBuffer, 0, CLOG ); + if( tBuf != NULL ){ + //char ID[64]; + switch ( Type ) { + case DC_STATUS: + Severity = 0; + break; + case DC_ERROR: + Severity = 2; + break; + case DC_ALARM: + Severity = 4; + break; + default: + Severity = 2; + }; + //sprintf( ID, "%s", MD5String( tBuf ) ); + if( (m_nScannerDimentions > 2) && ( Type == DC_ALARM ) ) Type = DC_ALARM_3D; + sprintf( tBuffer, + DFSL_SE_ENVELOPE, + //DEFENDER_MESSAGE_ID, + defenderCategories[Type], + defenderTypeSeverity[Severity], + tBuf, tBuf, + Location, + Data + ); + RetVal = strlen(tBuffer); + memcpy( Buffer, tBuffer, RetVal ); + Buffer[RetVal] = (char) 0; + free( tBuf ); + }; + return RetVal; +}; + +bool protocolInfoStress::buildStatusPackage( unsigned char *Buffer, int &BufferSize, bool Status ){ + if( Buffer!= NULL ){ + CSTR tBuf; + tBuf = assembleStatusData( Status ); +#ifdef DEBUG + //LOG(tBuf); +#endif + if( tBuf != NULL ) { + char DFSL_LOCATION[256]; + sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat ); + BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_STATUS, ((Status)?0:2), DFSL_LOCATION, tBuf ); +#ifdef DEBUG + LOG((CSTR)Buffer); +#endif + free( tBuf ); + return true; + } + }; + return false; +}; + + +bool protocolInfoStress::buildFaultPackage( unsigned char *Buffer, int &BufferSize, unsigned char Type, CSTR Description ){ + if( Buffer!= NULL ){ + CSTR tBuf; + tBuf = assembleFaultData( Type, Description ); + if( tBuf != NULL ){ + char DFSL_LOCATION[256]; + sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat ); + BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_ERROR, 3, DFSL_LOCATION, tBuf ); + free( tBuf ); + return true; + }; + }; + return false; +}; + +bool protocolInfoStress::buildEvent2DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, float Angle, float Range, float Width){ + if( Buffer!= NULL ){ + CSTR tBuf; + tBuf = assembleTargetData( targetID, Range, Width, Angle, 0 ); + if( tBuf != NULL ){ + char DFSL_LOCATION[256]; + sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat ); + BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_ALARM, 3, DFSL_LOCATION, tBuf ); + free( tBuf ); + return true; + }; + }; + return false; +}; + +bool protocolInfoStress::buildEvent3DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, float Angle1, float Angle2, float Range ){ + if( Buffer!= NULL ){ + CSTR tBuf; + tBuf = assembleTargetData( targetID, Range, 0, Angle1, Angle2 ); +#ifdef DEBUG + //LOG(tBuf); +#endif + if( tBuf != NULL ){ + char DFSL_LOCATION[256]; + sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat ); + BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_ALARM, 3, DFSL_LOCATION, tBuf ); +#ifdef DEBUG + LOG((CSTR)Buffer); +#endif + free( tBuf ); + return true; + }; + }; + return false; +}; + +bool protocolInfoStress::buildClockSyncPackage( unsigned char *Buffer, int &BufferSize ){ + /*if( Buffer!= NULL ){ + CSTR tBuf; + tBuf = getSHTimeStamp(); + if( tBuf != NULL ){ + sprintf( + ((char *)(Buffer+BufferSize)), + PACKAGE_ENVELOPE_TEMPLATE_CLOCKSYNC, + tBuf + ); + BufferSize = strlen( (char *)Buffer ); + free( tBuf ); + return true; + }; + };*/ + return false; +}; + +bool protocolInfoStress::buildCallBackPackage( unsigned char *Buffer, int &BufferSize, CSTR CallBackPath ){ + /*if( Buffer!= NULL ){ + sprintf( + ((char *)(Buffer+BufferSize)), + PACKAGE_ENVELOPE_TEMPLATE_REG_CALL, + CallBackPath + ); + BufferSize = strlen( (char *)Buffer ); + return true; + };*/ + return false; +}; + + +bool protocolInfoStress::Alarm2D( float Angle, float Range, float Width, int ID ){ + bool RetVal= false; + char Buffer[1024]; + int BufferSize = 0; + CSTR URL; + CSTR UID; + if(!m_bEnabled) return false; + UID = buildUniqID(); + if( UID!= NULL ){ + URL = buildURL( SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName ); +#ifdef DEBUG + LOG(URL); +#endif + RetVal = buildEvent2DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle, Range, Width ); + if( URL != NULL ){ + RetVal = issuePost( URL, Buffer, BufferSize ); + free( URL ); + }; + free( UID ); + }; + return RetVal; +}; + +bool protocolInfoStress::Alarm3D( float Angle1, float Angle2, float Range, int ID ){ + bool RetVal= false; + char Buffer[1024]; + int BufferSize = 0; + CSTR URL; + CSTR UID; + if(!m_bEnabled) return false; + UID = buildUniqID(); + if( UID!= NULL ){ + URL = buildURL( SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName ); +#ifdef DEBUG + LOG(URL); +#endif + RetVal = buildEvent3DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle1, Angle2, Range ); + if( URL != NULL ){ + RetVal = issuePost( URL, Buffer, BufferSize ); + free( URL ); + }; + free( UID ); + }; + return RetVal; +}; + +bool protocolInfoStress::VisibilityChanged( bool Worsened ){ + bool RetVal= false; + char Buffer[1024]; + int BufferSize = 0; + CSTR URL; + if(!m_bEnabled) return false; + URL = buildURL( SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName ); +#ifdef DEBUG + LOG(URL); +#endif + RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, !Worsened ); + if( URL != NULL ){ + RetVal = issuePost( URL, Buffer, BufferSize ); + free( URL ); + }; + return RetVal; +}; + +bool protocolInfoStress::StatusUpdate( void ){ + bool RetVal= false; + char Buffer[1024]; + int BufferSize = 0; + CSTR URL; + if(!m_bEnabled) return false; + URL = buildURL( SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName ); +#ifdef DEBUG + LOG(URL); +#endif + RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, m_bCurrentStatus ); + if( URL != NULL ){ + RetVal = issuePost( URL, Buffer, BufferSize ); + free( URL ); + }; + return RetVal; +}; + +bool protocolInfoStress::ErrorReporting( int lvl, CSTR Descr ){ + bool RetVal= false; + char Buffer[1024]; + int BufferSize = 0; + CSTR URL; + if(!m_bEnabled) return false; + URL = buildURL( SERVICE_URL_TEMPLATE_FAULT, m_nScannerDimentions, m_pScannerName ); +#ifdef DEBUG + LOG(URL); +#endif + RetVal = buildFaultPackage( ( unsigned char *) Buffer, BufferSize, lvl, Descr ); + if( URL != NULL ){ + RetVal = issuePost( URL, Buffer, BufferSize ); + free( URL ); + }; + return RetVal; +}; + +bool protocolInfoStress::StartTimeSynch( void ){ + bool RetVal= false; + //LOG("TimeSYnc"); + /*char Buffer[1024]; + int BufferSize = 0; + CSTR URL = NULL; + URL = buildURL( SERVICE_URL_TEMPLATE_CLOCKSYNC, m_nScannerDimentions, m_pScannerName ); + RetVal = buildClockSyncPackage(( unsigned char *) Buffer, BufferSize ); + if( URL != NULL ){ + RetVal = issuePost( URL, Buffer, BufferSize ); + free( URL ); + };*/ + return RetVal; +}; + + +#undef LOG +#undef LOG_ERROR diff --git a/lib/data/protocol/protocolInfoStress.h b/lib/data/protocol/protocolInfoStress.h new file mode 100644 index 00000000..494e141c --- /dev/null +++ b/lib/data/protocol/protocolInfoStress.h @@ -0,0 +1,151 @@ +#ifndef __PROTOCOL_INFOSTRESS_HEADER__ +#define __PROTOCOL_INFOSTRESS_HEADER__ + +#ifdef __linux__ + #include +#endif + +#include +#include +#include +#include +#include +#include +#include + + +#include "../../components/base/basicFunctionality.h" +#include "../../components/base/basicLogCollection.h" +#include "../../components/base/basicSettingsProvider.h" + + +#define PROTOCOL_TYPE_3D true +#define PROTOCOL_TYPE_2D false +#define SERVICE_URL_TEMPLATE_NOTIFY "/simple-event/notify" +#define SERVICE_URL_TEMPLATE_STATUS "/sensors/lidar%dd/%s/status" +#define SERVICE_URL_TEMPLATE_FAULT "/sensors/lidar%dd/%s/fault" +#define SERVICE_URL_TEMPLATE_EVENT "/sensors/lidar%dd/%s/event" +#define SERVICE_URL_TEMPLATE_CALLBACK "/sensors/lidar%dd/%s/httpcallback" +#define SERVICE_URL_TEMPLATE_CLOCKSYNC "/sensors/lidar%dd/%s/clocksync" + +#define DFSL_HARDWARE_TYPE "SensorNode" + +#ifdef PRU_SLAVE + #define DFSL_SOURCE_NAME_ "SLAVE_SENSOR" +#else + #define DFSL_SOURCE_NAME_ "MASTER_SENSOR" +#endif + +#define DFSL_CATEGORY_ALARM_3D "dfsl#intruder-drone-detected" +#define DFSL_CATEGORY_ALARM_2D "dfsl#intruder-human-detected" +#define DFSL_CATEGORY_STATUS "dfsl#sensor-status-message" +#define DFSL_CATEGORY_ERROR "dfsl#sensor-error-message" +#define DFRL_PROVIDER_ID "DFSL" +#define DFSL_DATA_TYPE_TARGET "EventLidarTarget" +#define DFSL_DATA_TYPE_STATUS "EventLidarStatus" +#define DFSL_DATA_TYPE_ERROR "EventLidarFault" +//defender-eps.eng.it +#define DFSL_DEFENDER_DEFAULT_URL "/defender-eps/rest/simple-event/notify" + +typedef enum E_DEFENDER_CATEGORY{ + DC_ERROR = 0, + DC_STATUS = 1, + DC_ALARM = 2, + DC_ALARM_3D = 3 +} defenderCategoryTypes; + +#define DFSL_DEVICE_ "DFSL Lidar Sensor" +#define DFSL_LOCATION_DTO "{\"point\": [ %2.5f, %2.5f ] }" +#define DFSL_SENSOR_SOURCE_DTO "{ \"type\": \"%s\", \"providerId\": \"%s\", \"sensorNodeId\" : \"%s\" }" + +#define DFSL_2D_TARGET_DTO "{ \"AZ\": %d, \"Range\": %d, \"Width\": %d }" +#define DFSL_3D_TARGET_DTO "{ \"AZ\": %d, \"EL\": %d, \"Range\": %d }" +#define DFSL_STATUS_DTO "{ \"created\": \"%s\", \"name\": \"status\", \"value\": \"%s\" }" +#define DFSL_ERROR_DTO "{ \"created\": \"%s\", \"name\": \"code\", \"value\": \"%d\", \"description\": \"%s\" }" + +#define DFSL_SE_ENVELOPE "{\"category\": \"%s\",\"severity\": \"%s\"," \ + "\"ceiId\": \"ENGIE_CEI\"," \ + "\"startTime\": \"%s\",\"endTime\": \"%s\", \"location\": %s, " \ + "\"radius\": 0, \"data\": [ %s ]}" + +#define PACKAGE_SE_TARGET_DATA_ELEMENT "{\"description\": \"%s\", \"created\": \"%s\", \"source\": %s," \ + " \"name\": \"%s\", \"type\": \"%s\", \"lidarSensorEventId\": \"%s\", \"targets\": [%s] }" + +#define PACKAGE_SE_STATUS_DATA_ELEMENT "{\"description\": \"%s\", \"created\": \"%s\", \"source\": %s, " \ + " \"name\": \"%s\", \"type\": \"%s\", \"lidarSensorEventId\": \"%s\", \"measurements\": [%s] }" + +#define OS_SCANNER_NAME_SIZE 128 +#define OS_CEP_ADDRESS_SIZE 128 + +/** +* TODO: Add class description +* +* @author gagua +*/ +class protocolInfoStress { +private: + basicLogCollection * m_pLog; + config_setting_t * m_pSettings; + rd_kafka_t * m_pKafka; /* Producer instance handle */ + rd_kafka_conf_t * m_pKafkaConf; /* Temporary configuration object */ + CSTR m_pKafkaBrokers; + //uint8_t m_bKafkaBrokersCount; + + int m_nScannerDimentions; + CSTR m_pScannerName; + CSTR m_pServerIP; + CSTR m_pAccessURL; + bool m_bEnabled; + bool m_bUseSSL; + bool m_bMitigationStatus; + float m_fLon; + float m_fLat; + int m_nServerPort; + bool m_bCurrentStatus; + CSTR buildURL( CSTR templ, int dim, CSTR devID ); + CSTR buildUniqID( void ); + bool buildClockSyncPackage( unsigned char *Buffer, int &BufferSize ); + bool buildCallBackPackage( unsigned char *Buffer, int &BufferSize, CSTR CallBackPath ); + bool buildStatusPackage( unsigned char *Buffer, int &BufferSize, bool Status ); + bool buildFaultPackage( unsigned char *Buffer, int &BufferSize, unsigned char Type, CSTR Description ); + bool buildEvent2DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, float Angle, float Range, float Width ); + bool buildEvent3DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, float Angle1, float Angle2, float Range ); + + CSTR assembleStatusData( bool StatusOK ); + CSTR assembleFaultData( uint8_t Code, CSTR Desc ); + CSTR assembleTargetData( int targetID, int Range, int Width, int Azimuth, int Elevation = 0 ); + size_t assembleDefenderJSON( CSTR Buffer, defenderCategoryTypes Type, uint8_t Severity, CSTR Location, CSTR Data ); +public: + // Constructor + explicit protocolInfoStress( config_setting_t *Settings = NULL ); + //ToDo: change Serial to BaseClass of some kind of socket + //protocolSafeShore( clientPort *SocketTransport, config_setting_t *Settings = NULL ); + // Destructor + virtual ~protocolInfoStress(); + bool init(); + bool Alarm2D( float Angle, float Range, float Width, int ID=0 ); + bool Alarm3D( float Angle1, float Angle2, float Range, int ID=0 ); + bool VisibilityChanged( bool Worsened ); + bool StatusUpdate( void ); + void SetStatus( bool A ){ m_bCurrentStatus = A; }; + bool ErrorReporting( int lvl, CSTR Descr ); + bool StartTimeSynch( void ); +#ifdef __linux__ + //bool FinishTimeSynch( unsigned long long *tVals ); +#else + //bool FinishTimeSynch( unsigned __int64 *tVals ); +#endif + bool SetupCallBack( CSTR CallBackPath ); + void setProtocolType( bool DeviceIs3D ); + void setScannerName( CSTR Name ); + void setScannerPosition( float Lat, float Lon ){ m_fLon = Lon; m_fLat = Lat; }; + bool getMitigationStatus(void) { return m_bMitigationStatus; }; +}; + +#ifdef __linux__ + unsigned long long getSHTimeValue( void ); +#else + unsigned __int64 getSHTimeValue( void ); +#endif + +#endif \ No newline at end of file diff --git a/lib/rgoose/rgooseNodeFactory.h b/lib/rgoose/rgooseNodeFactory.h index a0d38071..ef461eee 100644 --- a/lib/rgoose/rgooseNodeFactory.h +++ b/lib/rgoose/rgooseNodeFactory.h @@ -21,6 +21,7 @@ class argusNodeFactory{ bool loadNodes( uint32_t SiteID = 0 ); void setDBC(ARGUS_DBC * tPtr); void validate( void ); + uint32_t getNodeCount( type ); protected: argusNodeFactory(); private: