58 changed files with 2736 additions and 653 deletions
-
1Projects/la_mojo/la_mojo.vpj
-
16Projects/la_mojo/makefile
-
11Projects/la_serva/la_serva.vpj
-
200Projects/la_serva/makefile
-
6lib/App/common/SQLite_dataBinder.cpp
-
1lib/App/common/dataBinder.h
-
14lib/App/common/dataBinder_structures.h
-
1lib/App/common/deviceNode.h
-
245lib/App/common/deviceNodeCamera.cpp
-
19lib/App/common/deviceNodeCamera.h
-
12lib/App/common/deviceNodeFactory.cpp
-
4lib/App/common/deviceNodeSensor.cpp
-
3lib/App/common/deviceNodeSensor.h
-
116lib/App/common/hardwareResourceSerialProvider.cpp
-
1lib/App/common/kernelEventFilter.h
-
10lib/App/common/kernelEventFilterTrain.cpp
-
1lib/App/common/kernelModule.h
-
101lib/App/common/kernelModuleEmbedVideo.cpp
-
7lib/App/common/kernelModuleEmbedVideo.h
-
6lib/App/common/kernelPrototype.cpp
-
11lib/App/common/kernelPrototype.h
-
132lib/App/common/serverDataBinder.h
-
428lib/App/common/serverMySQL_dataBinder.cpp
-
17lib/App/common/serverMySQL_dataBinder.h
-
1lib/App/mojo/mojoHardwareManager.h
-
4lib/App/mojo/mojoKernel.cpp
-
1lib/App/mojo/mojoKernel.h
-
346lib/App/mojo/mojoRemoteClient.cpp
-
14lib/App/mojo/mojoRemoteClient.h
-
1lib/App/mojo/rgooseModuleEmbedVideo.h
-
1lib/App/mojo/rgooseModuleFlyHunter.h
-
1lib/App/mojo/rgooseModuleKafka.h
-
1lib/App/mojo/rgooseModuleOnvifObserver.h
-
1lib/App/mojo/rgooseModuleSSDP.h
-
16lib/App/serva/lifeMonitoring.cpp
-
15lib/App/serva/lifeMonitoring.h
-
379lib/App/serva/servaKernel.cpp
-
5lib/App/serva/servaKernel.h
-
2lib/App/serva/servaKernelToolKit.cpp
-
201lib/App/serva/servaLifeMonitoring.cpp
-
41lib/App/serva/servaLifeMonitoring.h
-
17lib/App/serva/servaMediaEncoder.h
-
486lib/App/serva/servaModBusManager.cpp
-
59lib/App/serva/servaModBusManager.h
-
166lib/App/serva/servaRemoteClientHandler.cpp
-
6lib/App/serva/servaRemoteClientHandler.h
-
19lib/App/serva/servaSessionManager.cpp
-
1lib/App/serva/servaSessionManager.h
-
4lib/components/base/basicEvent.cpp
-
30lib/components/base/basicEvent.h
-
125lib/components/base/basicFunctionality.cpp
-
5lib/components/base/basicFunctionality.h
-
48lib/data/net/clientPort.cpp
-
2lib/data/net/clientPort.h
-
3lib/data/net/netServer.cpp
-
2lib/data/protocol/protocolARGUS_DATA.cpp
-
23lib/data/protocol/protocolARGUS_DATA.h
-
0lib/rgoose/rgooseTypes_.h
@ -1,16 +0,0 @@ |
|||
#include "lifeMonitoring.h"
|
|||
|
|||
servaLifeMonitoring *servaLifeMonitoring::_Instance = 0; |
|||
|
|||
servaLifeMonitoring *servaLifeMonitoring::getInstance(){ |
|||
if( _Instance == 0 ){ |
|||
_Instance = new servaLifeMonitoring(); |
|||
}; |
|||
return _Instance; |
|||
}; |
|||
|
|||
servaLifeMonitoring::servaLifeMonitoring(){ |
|||
}; |
|||
|
|||
servaLifeMonitoring::~servaLifeMonitoring(){ |
|||
}; |
|||
@ -1,15 +0,0 @@ |
|||
#ifndef __ARGUS_SERVER_LIFE_MONOTORING__ |
|||
#define __ARGUS_SERVER_LIFE_MONOTORING__ |
|||
|
|||
|
|||
class servaLifeMonitoring{ |
|||
private: |
|||
static servaLifeMonitoring * _Instance; |
|||
servaLifeMonitoring(); |
|||
public: |
|||
static servaLifeMonitoring * getInstance(); |
|||
~servaLifeMonitoring(); |
|||
}; |
|||
|
|||
|
|||
#endif |
|||
@ -0,0 +1,201 @@ |
|||
#include "servaLifeMonitoring.h"
|
|||
#include "../../components/base/basicEvent.h"
|
|||
#include "../../components/base/basicEventHeap.h"
|
|||
|
|||
#define LOG( X ) m_pLog->Log( "HELZ", 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 );\ |
|||
m_pLog->Log( "HELZ", this, _LOG_ERROR, _tBfrErrMSG2w34 );\ |
|||
} |
|||
#else
|
|||
#define LOG_ERROR( X ) m_pLog->Log( "HELZ", this, _LOG_ERROR, X );
|
|||
#endif
|
|||
|
|||
servaLifeMonitoring *servaLifeMonitoring::_Instance = 0; |
|||
|
|||
servaLifeMonitoring *servaLifeMonitoring::getInstance(){ |
|||
if( _Instance == 0 ){ |
|||
_Instance = new servaLifeMonitoring(); |
|||
}; |
|||
return _Instance; |
|||
}; |
|||
|
|||
servaLifeMonitoring::servaLifeMonitoring():basicValidatable(),basicThread(){ |
|||
m_pLog = basicLogCollection::getInstance(); |
|||
m_pDBC = servaMySQLDBC::getInstance(); |
|||
DBC_UINT64_LIST *tList = m_pDBC->listPlatforms('E'); |
|||
if ( tList ) { |
|||
if ( tList->size() ) { |
|||
for ( DBC_UINT64_LIST::iterator idx = tList->begin(); idx != tList->end(); idx++ ) { |
|||
DBC_UINT64_LIST *tML = m_pDBC->listPlatformModules( *idx ); |
|||
if ( tML ) { |
|||
if ( tML->size() ) { |
|||
HEALTH_MONITORING_ZONE *tHZ = (HEALTH_MONITORING_ZONE*) malloc( sizeof( HEALTH_MONITORING_ZONE)); |
|||
if (tHZ) { |
|||
memset( tHZ, 0, sizeof(HEALTH_MONITORING_ZONE)); |
|||
tHZ->platform_id = *idx; |
|||
uint8_t cid = 0; |
|||
tHZ->lvl = 255; // To clean prev State
|
|||
for ( DBC_UINT64_LIST::iterator jdx = tML->begin(); jdx != tML->end(); jdx++ ) { |
|||
tHZ->module_id[cid] = *jdx; |
|||
tHZ->module_monitored[cid] = true; |
|||
cid++; |
|||
}; |
|||
m_mMonitoringZonesProtector.lock(); |
|||
m_vMonitoringZones.push_back( tHZ ); |
|||
m_mMonitoringZonesProtector.unlock(); |
|||
}; |
|||
}; |
|||
delete(tML); |
|||
}; |
|||
}; |
|||
}; |
|||
delete( tList ); |
|||
}; |
|||
m_nStartupTS = time(NULL); |
|||
init(); |
|||
}; |
|||
|
|||
servaLifeMonitoring::~servaLifeMonitoring(){ |
|||
waitThreadEnd( true ); |
|||
m_mMonitoringZonesProtector.lock(); |
|||
if ( m_vMonitoringZones.size() ) { |
|||
for ( V_HEALTH_MONITORING_ZONES::iterator idx = m_vMonitoringZones.begin(); idx != m_vMonitoringZones.end(); idx++ ) { |
|||
free( *idx ); |
|||
}; |
|||
m_vMonitoringZones.clear(); |
|||
}; |
|||
m_mMonitoringZonesProtector.unlock(); |
|||
}; |
|||
|
|||
bool servaLifeMonitoring::validateZone( HEALTH_MONITORING_ZONE *ptr ){ |
|||
bool retVal = true; |
|||
if ( ptr ) { |
|||
uint8_t min_lvl = 0; |
|||
for ( uint8_t i = 0; i < 4; i++ ) { |
|||
if (ptr->module_monitored[i]) { |
|||
if( ptr->module_status[i] > min_lvl ) min_lvl = ptr->module_status[i]; |
|||
}; |
|||
}; |
|||
if( min_lvl != ptr->lvl ) { |
|||
retVal = false; |
|||
ptr->lvl = min_lvl; |
|||
}; |
|||
}; |
|||
return retVal; |
|||
}; |
|||
|
|||
uint8_t servaLifeMonitoring::validateModule( uint64_t module_id ){ |
|||
uint8_t retVal = 0; |
|||
if ( module_id ) { |
|||
uint32_t cts = time( NULL ); |
|||
uint32_t mts = m_pDBC->getCloudLTS(ARGUS_OBJECT_MODULE, module_id ); |
|||
uint8_t ms = m_pDBC->getCloudStatus( ARGUS_OBJECT_MODULE, module_id ); |
|||
if ( mts ) { |
|||
if ((cts - mts) > 30) { |
|||
retVal = ARGUS_LIFE_STATUS_LVL_4; |
|||
} else { |
|||
//Online - Check Status
|
|||
if( ms < ARGUS_CLOUD_STATUS_E_GENERIC ) { |
|||
retVal = ARGUS_LIFE_STATUS_CLEAN; |
|||
} else { |
|||
switch ( ms ) { |
|||
case ARGUS_CLOUD_STATUS_E_GENERIC: break; |
|||
case ARGUS_CLOUD_STATUS_E_CAM_GENERIC: |
|||
case ARGUS_CLOUD_STATUS_E_CAM_NC: |
|||
case ARGUS_CLOUD_STATUS_E_CAM_NPTZ: |
|||
case ARGUS_CLOUD_STATUS_E_CAM_NIM: retVal = ARGUS_LIFE_STATUS_LVL_1; break; |
|||
case ARGUS_CLOUD_STATUS_E_SEN_GENERIC: |
|||
case ARGUS_CLOUD_STATUS_E_US_LRF: |
|||
case ARGUS_CLOUD_STATUS_E_BS_LRF: retVal = ARGUS_LIFE_STATUS_LVL_2; break; |
|||
case ARGUS_CLOUD_STATUS_E_US_NC: |
|||
case ARGUS_CLOUD_STATUS_E_BS_NC: retVal = ARGUS_LIFE_STATUS_LVL_3; break; |
|||
default: retVal = ARGUS_LIFE_STATUS_LVL_3; |
|||
}; |
|||
}; |
|||
}; |
|||
} else { |
|||
retVal = ARGUS_LIFE_STATUS_LVL_4; |
|||
}; |
|||
if ( retVal == ARGUS_LIFE_STATUS_LVL_4 ) { |
|||
DBC_IS_MODULE_DATA *tData = m_pDBC->getModuleData( module_id ); |
|||
if ( tData ) { |
|||
if( !pingIP4( tData->ip4 ) ) { |
|||
#ifdef DEBUG_EXTRA
|
|||
LOG_ERROR("No Network Access to Platform!"); |
|||
#endif
|
|||
retVal = ARGUS_LIFE_STATUS_LVL_5; |
|||
}; |
|||
free( tData ); |
|||
}; |
|||
}; |
|||
}; |
|||
return retVal; |
|||
}; |
|||
|
|||
bool servaLifeMonitoring::validate(){ |
|||
uint32_t cts = time(NULL); |
|||
if ( ( cts - m_nStartupTS ) < LM_SKEEP_MONITORING_SECONDS ) return true; |
|||
m_mMonitoringZonesProtector.lock(); |
|||
for ( V_HEALTH_MONITORING_ZONES::iterator idx = m_vMonitoringZones.begin(); idx != m_vMonitoringZones.end(); idx++ ) { |
|||
if (!validateZone(*idx)) { |
|||
LOG("Zone Health is Changed"); |
|||
MODULE_HEALTH_STATUS *tData = (MODULE_HEALTH_STATUS*) malloc(sizeof(MODULE_HEALTH_STATUS)); |
|||
if ( tData ) { |
|||
tData->status = (*idx)->lvl; |
|||
tData->platform_id = (*idx)->platform_id; |
|||
char tT[128]; |
|||
sprintf( tT, "gen Event HEALTH %d", tData->status ); |
|||
LOG( tT ); |
|||
EVENT_CONTAINER *tEvt = createBasicEvent(0, EVT_CLASS_SYSTEM, EVT_SRV_MODULE_HEALTH, NULL, NULL, tData ); |
|||
if ( tEvt ) { |
|||
basicEventHeap::getInstance()->broadcast( tEvt ); |
|||
} else free( tData ); |
|||
} else LOG_ERROR("No Data Fetched from DB"); |
|||
#ifdef DEBUG_EXTRA
|
|||
} else { |
|||
char tT[128]; |
|||
sprintf( tT, "Zone Health is same %d",(*idx)->lvl ); |
|||
LOG( tT ); |
|||
#endif
|
|||
}; |
|||
}; |
|||
m_mMonitoringZonesProtector.unlock(); |
|||
return true; |
|||
}; |
|||
|
|||
void servaLifeMonitoring::m_fThreadProc( void ){ |
|||
m_bThreadStatus = TH_WORKING; |
|||
uint32_t ots = time(NULL); |
|||
while( m_bThreadStatus < TH_ENDED ) { |
|||
uint32_t cts = time(NULL); |
|||
if ( ( cts - m_nStartupTS ) > LM_SKEEP_MONITORING_SECONDS ){ |
|||
if ( ots != cts ) { |
|||
ots = cts; |
|||
// Taking to validate one record then return it to a queue
|
|||
HEALTH_MONITORING_ZONE * tCWZ = NULL; |
|||
m_mMonitoringZonesProtector.lock(); |
|||
if ( m_vMonitoringZones.size() ) { |
|||
tCWZ = m_vMonitoringZones.at(0); |
|||
m_vMonitoringZones.erase(m_vMonitoringZones.begin()); |
|||
}; |
|||
m_mMonitoringZonesProtector.unlock(); |
|||
if ( tCWZ ) { |
|||
for ( uint8_t i = 0; i < 4; i++ ) { |
|||
if (tCWZ->module_monitored[i]) tCWZ->module_status[i] = validateModule( tCWZ->module_id[i] ); |
|||
}; |
|||
m_mMonitoringZonesProtector.lock(); |
|||
m_vMonitoringZones.push_back( tCWZ ); |
|||
m_mMonitoringZonesProtector.unlock(); |
|||
}; |
|||
}; |
|||
}; |
|||
SLEEP( DEFAULT_THREAD_SLEEP_TIME ); |
|||
}; |
|||
}; |
|||
|
|||
#undef LOG
|
|||
#undef LOG_ERROR
|
|||
@ -0,0 +1,41 @@ |
|||
#ifndef __ARGUS_SERVER_LIFE_MONOTORING__ |
|||
#define __ARGUS_SERVER_LIFE_MONOTORING__ |
|||
|
|||
#include "../../components/base/basicValidatable.h" |
|||
#include "../../components/base/basicLogCollection.h" |
|||
#include "../../components/base/basicThread.h" |
|||
#include "../common/serverMySQL_dataBinder.h" |
|||
|
|||
#define LM_SKEEP_MONITORING_SECONDS 180 // 3 Minutes Seep time from beginning |
|||
|
|||
typedef struct S_HEALTH_MONITORING_ZONE{ |
|||
bool module_monitored[4]; |
|||
uint64_t module_id[4]; |
|||
uint8_t module_status[4]; |
|||
uint64_t platform_id; |
|||
uint8_t lvl; |
|||
}HEALTH_MONITORING_ZONE; |
|||
|
|||
typedef std::vector<HEALTH_MONITORING_ZONE *> V_HEALTH_MONITORING_ZONES; |
|||
|
|||
class servaLifeMonitoring : public basicValidatable, basicThread { |
|||
private: |
|||
static servaLifeMonitoring * _Instance; |
|||
basicLogCollection * m_pLog; |
|||
servaMySQLDBC * m_pDBC; |
|||
basicMutex m_mMonitoringZonesProtector; |
|||
V_HEALTH_MONITORING_ZONES m_vMonitoringZones; |
|||
uint32_t m_nStartupTS; // to skip initial startup time |
|||
bool validateZone( HEALTH_MONITORING_ZONE *ptr ); |
|||
uint8_t validateModule( uint64_t module_id ); |
|||
servaLifeMonitoring(); |
|||
protected: |
|||
virtual void m_fThreadProc( void ); |
|||
public: |
|||
static servaLifeMonitoring * getInstance(); |
|||
~servaLifeMonitoring(); |
|||
virtual bool validate(); |
|||
}; |
|||
|
|||
|
|||
#endif |
|||
@ -1,11 +1,493 @@ |
|||
#include "servaModBusManager.h"
|
|||
#include "servaKernel.h"
|
|||
|
|||
#define LOG( X ) m_pLog->Log( "MODBUS", this, X )
|
|||
|
|||
servaModBusManager::_instance = NULL; |
|||
#ifdef DEBUG
|
|||
#define LOG_ERROR( X ) {\
|
|||
char _tBfrErrMSG2w34[256];\ |
|||
sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\ |
|||
m_pLog->Log( "MODBUS", this, _LOG_ERROR, _tBfrErrMSG2w34 );\ |
|||
} |
|||
#else
|
|||
#define LOG_ERROR( X ) m_pLog->Log( "MODBUS", this, _LOG_ERROR, X );
|
|||
#endif
|
|||
|
|||
servaModBusManager *servaModBustManager::getInstance(){ |
|||
servaModBusManager *servaModBusManager::_instance = NULL; |
|||
|
|||
servaModBusManager *servaModBusManager::getInstance(){ |
|||
if ( _instance == NULL ) { |
|||
_instance = new servaModBusManager(); |
|||
}; |
|||
return _instance; |
|||
}; |
|||
|
|||
servaModBusManager::servaModBusManager():basicThread(),basicValidatable(){ |
|||
m_bStage = MBUS_STAGE_INIT; |
|||
m_pDBC = NULL; |
|||
m_pLog = basicLogCollection::getInstance(); |
|||
m_pMaster = servaKernel::getInstance(); |
|||
if ( m_pMaster == NULL ) return; |
|||
m_pDBC = m_pMaster->getDBC(); |
|||
memset( &m_sManagerData, 0, sizeof( MODBUS_MANAGER_DATA )); |
|||
}; |
|||
|
|||
servaModBusManager::~servaModBusManager(){ |
|||
waitThreadEnd( true ); |
|||
if (m_vRtuUnits.size()) { |
|||
for( V_MODBUS_RTU_INFO::iterator idx=m_vRtuUnits.begin(); idx != m_vRtuUnits.end(); idx++ ) { |
|||
free( *idx ); |
|||
}; |
|||
m_vRtuUnits.clear(); |
|||
}; |
|||
}; |
|||
|
|||
bool servaModBusManager::create(){ |
|||
if( m_pMaster == NULL ) return false; |
|||
if( m_pDBC == NULL ) return false; |
|||
MODBUS_MANAGER_DATA * tData = m_pDBC->getModBusManagerData(); |
|||
if ( tData ) { |
|||
memcpy( &m_sManagerData, tData, sizeof( MODBUS_MANAGER_DATA ) ); |
|||
free( tData ); |
|||
}; |
|||
m_bStage = MBUS_STAGE_INIT; |
|||
return init(); |
|||
}; |
|||
|
|||
int servaModBusManager::modbus_get_rtu( CSTR ip, uint16_t port, uint8_t addr, uint8_t &status ){ |
|||
uint8_t buf[16]; |
|||
modbus_t * ctx; // Modbus context pointer
|
|||
int rc; // Return code for error checking
|
|||
// Create a new Modbus TCP context for a slave device
|
|||
ctx = modbus_new_tcp( ip, port ); |
|||
if (ctx == NULL) { |
|||
#ifdef DEBUG_EXTRA
|
|||
LOG( "Unable to create the Modbus context" ); |
|||
#endif
|
|||
return -1; |
|||
}; |
|||
// Connect to the Modbus TCP slave device
|
|||
rc = modbus_connect(ctx); |
|||
if (rc == -1) { |
|||
#ifdef DEBUG_EXTRA
|
|||
char tB[256]; |
|||
sprintf(tB, "Connection failed: %s", modbus_strerror(errno)); |
|||
LOG( tB ); |
|||
#endif
|
|||
modbus_free(ctx); |
|||
return -1; |
|||
}; |
|||
rc = modbus_read_bits(ctx, addr, 8, buf ); //0x10
|
|||
if( rc == -1 ) { |
|||
#ifdef DEBUG_EXTRA
|
|||
char tB[256]; |
|||
sprintf(tB, "Write register failed: %s", modbus_strerror(errno)); |
|||
LOG( tB ); |
|||
#endif
|
|||
} else { |
|||
status = modbus_get_byte_from_bits( buf, 0, 8 ); |
|||
}; |
|||
// Close the Modbus TCP connection and free the context
|
|||
modbus_close(ctx); |
|||
modbus_free(ctx); |
|||
return rc; |
|||
}; |
|||
int servaModBusManager::modbus_set_rtu( CSTR ip, uint16_t port, uint8_t addr, uint8_t data ){ |
|||
uint8_t bits[8]; |
|||
modbus_t * ctx; // Modbus context pointer
|
|||
int rc; // Return code for error checking
|
|||
// Create a new Modbus TCP context for a slave device
|
|||
ctx = modbus_new_tcp( ip, port ); |
|||
if (ctx == NULL) { |
|||
#ifdef DEBUG_EXTRA
|
|||
LOG( "Unable to create the Modbus context"); |
|||
#endif
|
|||
return -1; |
|||
}; |
|||
// Connect to the Modbus TCP slave device
|
|||
rc = modbus_connect(ctx); |
|||
if (rc == -1) { |
|||
#ifdef DEBUG_EXTRA
|
|||
char tB[256]; |
|||
sprintf(tB, "Connection failed: %s", modbus_strerror(errno)); |
|||
LOG( tB ); |
|||
#endif
|
|||
modbus_free(ctx); |
|||
return -1; |
|||
}; |
|||
modbus_set_bits_from_byte( bits, 0, data ); |
|||
rc = modbus_write_bits(ctx, addr, 8, bits ); //0x10
|
|||
if (rc == -1) { |
|||
#ifdef DEBUG_EXTRA
|
|||
char tB[256]; |
|||
sprintf(tB, "Write register failed: %s", modbus_strerror(errno)); |
|||
LOG( tB ); |
|||
#endif
|
|||
}; |
|||
// Close the Modbus TCP connection and free the context
|
|||
modbus_close(ctx); |
|||
modbus_free(ctx); |
|||
return rc; |
|||
}; |
|||
int servaModBusManager::modbus_set_rtu_bit( CSTR ip, uint16_t port, uint8_t addr, uint8_t bit ){ |
|||
modbus_t * ctx; // Modbus context pointer
|
|||
int rc; // Return code for error checking
|
|||
// Create a new Modbus TCP context for a slave device
|
|||
ctx = modbus_new_tcp( ip, port ); |
|||
if (ctx == NULL) { |
|||
#ifdef DEBUG_EXTRA
|
|||
LOG( "Unable to create the Modbus context"); |
|||
#endif
|
|||
return -1; |
|||
}; |
|||
// Connect to the Modbus TCP slave device
|
|||
rc = modbus_connect(ctx); |
|||
if (rc == -1) { |
|||
#ifdef DEBUG_EXTRA
|
|||
char tB[256]; |
|||
sprintf(tB, "Connection failed: %s", modbus_strerror(errno)); |
|||
LOG( tB ); |
|||
#endif
|
|||
modbus_free(ctx); |
|||
return -1; |
|||
}; |
|||
rc = modbus_write_bit(ctx, addr, bit ); //0x10
|
|||
if (rc == -1) { |
|||
#ifdef DEBUG_EXTRA
|
|||
char tB[256]; |
|||
sprintf(tB, "Write register failed: %s", modbus_strerror(errno)); |
|||
LOG( tB ); |
|||
#endif
|
|||
}; |
|||
// Close the Modbus TCP connection and free the context
|
|||
modbus_close(ctx); |
|||
modbus_free(ctx); |
|||
return rc; |
|||
}; |
|||
|
|||
void servaModBusManager::m_fThreadProc( void ){ |
|||
m_bThreadStatus = TH_WORKING; |
|||
uint32_t oss = time(NULL); |
|||
while ( m_bThreadStatus < TH_ENDED ) { |
|||
uint32_t css = time(NULL); |
|||
switch (m_bStage) { |
|||
case MBUS_STAGE_INIT:{ |
|||
#ifdef DEBUG
|
|||
LOG("Load Enabled ModBus Modules"); |
|||
#endif
|
|||
DBC_UINT64_LIST *tList = m_pDBC->listModBusRTUs( 0, 'E' ); |
|||
if ( tList ) { |
|||
if ( tList->size() ) { |
|||
for ( DBC_UINT64_LIST::iterator idx = tList->begin(); idx != tList->end(); idx++ ) { |
|||
MODBUS_RTU_INFO *tData = m_pDBC->getModBusRTU( *idx ); |
|||
if ( tData ) { |
|||
m_vRtuUnits.push_back( tData ); |
|||
}; |
|||
}; |
|||
}; |
|||
delete(tList); |
|||
#ifdef DEBUG
|
|||
LOG("Load ModBus Modules Done"); |
|||
#endif
|
|||
} else LOG("No ModBus Modules Defined"); |
|||
m_bStage = MBUS_STAGE_SYNC; |
|||
break; |
|||
}; |
|||
case MBUS_STAGE_SYNC:{ |
|||
#ifdef DEBUG_EXTRA
|
|||
LOG("Sync ModBus Modules"); |
|||
#endif
|
|||
if( m_vRtuUnits.size() ){ |
|||
for( V_MODBUS_RTU_INFO::iterator idx = m_vRtuUnits.begin(); idx!= m_vRtuUnits.end(); idx++ ){ |
|||
int rv = modbus_get_rtu((*idx)->ip, (*idx)->port, (*idx)->output_base_addr, (*idx)->c_OutBits ); |
|||
if ( rv != -1 ) { |
|||
rv = modbus_get_rtu((*idx)->ip, (*idx)->port, (*idx)->input_base_addr, (*idx)->c_InBits ); |
|||
if ( rv != -1 ) { |
|||
(*idx)->status = MBUS_STATUS_ONLINE; |
|||
m_pDBC->updateModBusRTU( (*idx)->id, (*idx)->status, (*idx)->c_InBits, (*idx)->c_OutBits ); |
|||
continue; |
|||
} |
|||
#ifdef DEBUG_EXTRA
|
|||
else LOG_ERROR("Read DI Failure"); |
|||
#endif
|
|||
} |
|||
#ifdef DEBUG_EXTRA
|
|||
else LOG_ERROR("Read DO Failure"); |
|||
#endif
|
|||
(*idx)->status = MBUS_STATUS_OFFLINE; |
|||
m_pDBC->updateModBusRTU( (*idx)->id, (*idx)->status, (*idx)->c_InBits, (*idx)->c_OutBits ); |
|||
}; |
|||
}; |
|||
#ifdef DEBUG_EXTRA
|
|||
LOG("Sync ModBus Modules Done"); |
|||
#endif
|
|||
m_bStage = MBUS_STAGE_WORK; |
|||
break; |
|||
}; |
|||
case MBUS_STAGE_WORK:{ |
|||
uint64_t cts = getMilliseconds(); |
|||
m_mProtector.lock(); |
|||
if ( m_vActioItems.size() ) { |
|||
for( V_MODBUS_ACION_ITEMS::iterator idx = m_vActioItems.begin(); idx != m_vActioItems.end(); idx++ ){ |
|||
if( (*idx)->scheduled_on < cts ) { |
|||
MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( (*idx)->module_id ); |
|||
if ( tRTU ) { |
|||
if( ( tRTU->c_OutBits & (*idx)->value ) == 0 ) { |
|||
//Set
|
|||
tRTU->c_OutBits |= (*idx)->value; |
|||
int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits ); |
|||
if ( rv < 0 ) { |
|||
m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 ); |
|||
} else { |
|||
m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits ); |
|||
}; |
|||
}; |
|||
if( ( tRTU->c_OutBits & (*idx)->mask ) != 0 ) { |
|||
//Clear
|
|||
tRTU->c_OutBits &= ~(*idx)->mask; |
|||
int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits ); |
|||
if ( rv < 0 ) { |
|||
m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 ); |
|||
} else { |
|||
m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits ); |
|||
}; |
|||
}; |
|||
free(tRTU); |
|||
}; |
|||
free( *idx ); |
|||
m_vActioItems.erase( idx ); |
|||
//Make Just One SCADA per loop
|
|||
break; |
|||
}; |
|||
}; |
|||
}; |
|||
m_mProtector.unlock(); |
|||
break; |
|||
}; |
|||
case MBUS_STAGE_END:{ |
|||
m_bThreadStatus = TH_ENDED; |
|||
break; |
|||
}; |
|||
}; |
|||
if ( oss != css ) { |
|||
//Each Second
|
|||
oss = css; |
|||
m_bStage = MBUS_STAGE_SYNC; |
|||
}; |
|||
SLEEP(DEFAULT_THREAD_SLEEP_TIME); |
|||
}; |
|||
}; |
|||
|
|||
bool servaModBusManager::validate(){ |
|||
MODBUS_MANAGER_DATA * tData = m_pDBC->getModBusManagerData(); |
|||
if ( tData ) { |
|||
if( memcmp( &m_sManagerData, tData, sizeof( MODBUS_MANAGER_DATA ) ) != 0 ){ |
|||
#ifdef DEBUG
|
|||
LOG("Settings was changed ... reapplying"); |
|||
#endif
|
|||
memcpy( &m_sManagerData, tData, sizeof( MODBUS_MANAGER_DATA ) ); |
|||
}; |
|||
free( tData ); |
|||
}; |
|||
return true; |
|||
}; |
|||
|
|||
bool servaModBusManager::dispatchEvent( EVENT_CONTAINER *Evt ){ |
|||
if ( Evt == NULL ) return false; |
|||
switch ( Evt->m_eType ) { |
|||
case EVT_SRV_SESSION_STARTED:{ |
|||
if( m_sManagerData.automaticAlarmRisingTO ) { |
|||
//setup Schedule
|
|||
SCADA_TRACK_INFO *tData = m_pDBC->getTrackSCADA( Evt->m_nSourceNodeID ); |
|||
if ( tData ) { |
|||
MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id ); |
|||
if ( tRTU ) { |
|||
MODBUS_ACION_ITEM *tAI = (MODBUS_ACION_ITEM *)malloc(sizeof(MODBUS_ACION_ITEM)); |
|||
if ( tAI ) { |
|||
memset( tAI, 0, sizeof(MODBUS_ACION_ITEM) ); |
|||
tAI->scheduled_on = getMilliseconds() + ( m_sManagerData.automaticAlarmRisingTO * 1000 ); |
|||
tAI->module_id = tRTU->id; |
|||
tAI->value = ( 1 << tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)] ); |
|||
m_mProtector.lock(); |
|||
m_vActioItems.push_back( tAI ); |
|||
m_mProtector.unlock(); |
|||
}; |
|||
free( tRTU ); |
|||
}; |
|||
free( tData ); |
|||
}; |
|||
}; |
|||
break; |
|||
}; |
|||
case EVT_SRV_SESSION_ENDED:{ |
|||
SCADA_TRACK_INFO *tData = m_pDBC->getTrackSCADA( Evt->m_nSourceNodeID ); |
|||
if ( tData ) { |
|||
MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id ); |
|||
if ( tRTU ) { |
|||
uint8_t bON = ( 1 << tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)] ); |
|||
uint8_t bOFF = ~( 1 << tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)] ); |
|||
//Remove Scheduled Signal change
|
|||
m_mProtector.lock(); |
|||
for ( V_MODBUS_ACION_ITEMS::iterator idx=m_vActioItems.begin(); idx!=m_vActioItems.end(); idx++ ){ |
|||
if( (*idx)->module_id == tRTU->id ) { |
|||
//check if shceduled signal is current
|
|||
if( (((*idx)->value & bON ) == bON ) || (((*idx)->mask & bOFF ) == bOFF ) ){ |
|||
free((*idx)); |
|||
m_vActioItems.erase( idx ); |
|||
}; |
|||
break; |
|||
}; |
|||
}; |
|||
m_mProtector.unlock(); |
|||
|
|||
//Mitigate Signal if sent
|
|||
if ( (tRTU->c_OutBits & bON ) == bON ) { |
|||
tRTU->c_OutBits &= bOFF; |
|||
int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits ); |
|||
if ( rv < 0 ) { |
|||
m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 ); |
|||
} else { |
|||
m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits ); |
|||
}; |
|||
}; |
|||
|
|||
free( tRTU ); |
|||
}; |
|||
free( tData ); |
|||
}; |
|||
break; |
|||
}; |
|||
case EVT_SRV_SESSION_RATED:{ |
|||
if ( Evt->m_pEvtData == NULL ) return false; |
|||
if ( m_sManagerData.hazardSignalLevel ) { |
|||
SESSION_RATE_DATA *tSRD = (SESSION_RATE_DATA *)Evt->m_pEvtData; |
|||
SCADA_TRACK_INFO *tData = m_pDBC->getTrackSCADA( tSRD->track_id ); |
|||
if ( tData ) { |
|||
MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id ); |
|||
if ( tRTU ) { |
|||
uint8_t bON = ( 1 << tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)] ); |
|||
uint8_t bOFF = ~( 1 << tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)] ); |
|||
//Remove Scheduled Signal change
|
|||
m_mProtector.lock(); |
|||
for ( V_MODBUS_ACION_ITEMS::iterator idx=m_vActioItems.begin(); idx!=m_vActioItems.end(); idx++ ){ |
|||
if( (*idx)->module_id == tRTU->id ) { |
|||
//check if shceduled signal is current
|
|||
if( (((*idx)->value & bON ) == bON ) || (((*idx)->mask & bOFF ) == bOFF ) ){ |
|||
free((*idx)); |
|||
m_vActioItems.erase( idx ); |
|||
}; |
|||
break; |
|||
}; |
|||
}; |
|||
m_mProtector.unlock(); |
|||
|
|||
if ( tSRD->level < m_sManagerData.hazardSignalLevel ) { |
|||
//Mitigate Signal if sent
|
|||
if ( (tRTU->c_OutBits & bON ) == bON ) { |
|||
tRTU->c_OutBits &= bOFF; |
|||
int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits ); |
|||
if ( rv < 0 ) { |
|||
m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 ); |
|||
} else { |
|||
m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits ); |
|||
}; |
|||
}; |
|||
} else { |
|||
//Set Signal if was Cleared
|
|||
if ( (tRTU->c_OutBits & bON ) == 0 ) { |
|||
tRTU->c_OutBits |= bON; |
|||
int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits ); |
|||
if ( rv < 0 ) { |
|||
m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 ); |
|||
} else { |
|||
m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits ); |
|||
}; |
|||
}; |
|||
}; |
|||
free( tRTU ); |
|||
}; |
|||
free( tData ); |
|||
}; |
|||
}; |
|||
break; |
|||
}; |
|||
case EVT_SRV_TEST_MODBUS:{ |
|||
MODBUS_TEST_DATA *tData = (MODBUS_TEST_DATA *)Evt->m_pEvtData; |
|||
if ( tData ) { |
|||
MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id ); |
|||
if ( tRTU ) { |
|||
#ifdef DEBUG_EXTRA
|
|||
char tB[200]; |
|||
sprintf( tB, "%s:%d 0x%02x 0x%02x", tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits ); |
|||
LOG( tB ); |
|||
#endif
|
|||
int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits ); |
|||
if ( rv < 0 ) { |
|||
m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 ); |
|||
} else { |
|||
m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tData->c_OutBits ); |
|||
}; |
|||
free( tRTU ); |
|||
}; |
|||
}; |
|||
return false; // No need Propogate this Evt
|
|||
}; |
|||
case EVT_SRV_MODULE_HEALTH:{ |
|||
if ( Evt->m_pEvtData == NULL ) return false; |
|||
if ( m_sManagerData.helthSignalLevel ) { |
|||
MODULE_HEALTH_STATUS *tData = (MODULE_HEALTH_STATUS *)Evt->m_pEvtData; |
|||
if ( tData->status >= m_sManagerData.helthSignalLevel ) { |
|||
MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id ); |
|||
if ( tRTU ) { |
|||
uint8_t bON = ( 1 << tRTU->crossbar[MBUS_CROSSBAR_HEALTH] ); |
|||
uint8_t bOFF = ~( 1 << tRTU->crossbar[MBUS_CROSSBAR_HEALTH] ); |
|||
#ifdef DEBUG_EXTRA
|
|||
char tB[200]; |
|||
sprintf( tB, "%s:%d 0x%02x 0x%02x", tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits ); |
|||
LOG( tB ); |
|||
#endif
|
|||
//Set Signal if was Cleared
|
|||
if ( (tRTU->c_OutBits & bON ) == 0 ) { |
|||
tRTU->c_OutBits |= bON; |
|||
int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits ); |
|||
if ( rv < 0 ) { |
|||
m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 ); |
|||
} else { |
|||
m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits ); |
|||
}; |
|||
}; |
|||
free( tRTU ); |
|||
}; |
|||
} else { //Below Level
|
|||
MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id ); |
|||
if ( tRTU ) { |
|||
uint8_t bON = ( 1 << tRTU->crossbar[MBUS_CROSSBAR_HEALTH] ); |
|||
uint8_t bOFF = ~( 1 << tRTU->crossbar[MBUS_CROSSBAR_HEALTH] ); |
|||
#ifdef DEBUG_EXTRA
|
|||
char tB[200]; |
|||
sprintf( tB, "%s:%d 0x%02x 0x%02x", tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits ); |
|||
LOG( tB ); |
|||
#endif
|
|||
//Mitigate Signal if sent
|
|||
if ( (tRTU->c_OutBits & bON ) == bON ) { |
|||
tRTU->c_OutBits &= bOFF; |
|||
int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits ); |
|||
if ( rv < 0 ) { |
|||
m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 ); |
|||
} else { |
|||
m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits ); |
|||
}; |
|||
}; |
|||
free( tRTU ); |
|||
}; |
|||
}; |
|||
}; |
|||
break; |
|||
}; |
|||
}; |
|||
return true; |
|||
}; |
|||
|
|||
#undef LOG
|
|||
#undef LOG_ERROR
|
|||
|
|||
@ -1,11 +1,68 @@ |
|||
#ifndef __SERVA_MODBUS_MANAGER_HEADER__ |
|||
#define __SERVA_MODBUS_MANAGER_HEADER__ |
|||
|
|||
class servaModBusManager{ |
|||
#include "../../components/base/basicTypes.h" |
|||
#include "../../components/base/basicValidatable.h" |
|||
#include "../../components/base/basicMutex.h" |
|||
#include "../../components/base/basicThread.h" |
|||
#include "../../components/base/basicEventHandler.h" |
|||
#include "../../components/base/basicLogCollection.h" |
|||
#include "../common/serverDataBinder.h" |
|||
|
|||
#include <modbus/modbus.h> // Include the libmodbus header |
|||
|
|||
#define MBUS_STAGE_INIT 0 |
|||
#define MBUS_STAGE_SYNC 1 |
|||
#define MBUS_STAGE_WORK 2 |
|||
#define MBUS_STAGE_END 3 |
|||
|
|||
#define MBUS_STATUS_OFFLINE 0 |
|||
#define MBUS_STATUS_ONLINE 1 |
|||
|
|||
#define MBUS_CROSSBAR_UPPER_TRACK 0 |
|||
#define MBUS_CROSSBAR_BOTTOM_TRACK 1 |
|||
#define MBUS_CROSSBAR_HEALTH 2 |
|||
#define MBUS_CROSSBAR_AUX 3 |
|||
|
|||
class servaKernel; |
|||
|
|||
#define IMMIDIATE_ACTION 0 |
|||
|
|||
typedef struct S_MODBUS_ACION_ITEM{ |
|||
uint64_t module_id; |
|||
uint64_t scheduled_on; |
|||
uint8_t value; |
|||
uint8_t mask; |
|||
}MODBUS_ACION_ITEM; |
|||
|
|||
typedef std::vector<MODBUS_ACION_ITEM*> V_MODBUS_ACION_ITEMS; |
|||
|
|||
class servaModBusManager: public basicEventHandler, basicThread, basicValidatable{ |
|||
private: |
|||
static servaModBusManager * _instance; |
|||
basicLogCollection * m_pLog; |
|||
servaKernel * m_pMaster; |
|||
serverDataBinder * m_pDBC; |
|||
MODBUS_MANAGER_DATA m_sManagerData; |
|||
V_MODBUS_ACION_ITEMS m_vActioItems; |
|||
uint8_t m_bStage; |
|||
basicMutex m_mProtector; |
|||
V_MODBUS_RTU_INFO m_vRtuUnits; |
|||
servaModBusManager(); |
|||
virtual void m_fThreadProc( void ); |
|||
int modbus_get_rtu( CSTR ip, uint16_t port, uint8_t addr, uint8_t &status );//port=502 |
|||
int modbus_set_rtu( CSTR ip, uint16_t port, uint8_t addr, uint8_t data ); |
|||
int modbus_set_rtu_bit( CSTR ip, uint16_t port, uint8_t addr, uint8_t bit ); |
|||
public: |
|||
virtual ~servaModBusManager(); |
|||
bool create(); |
|||
static servaModBusManager * getInstance(); |
|||
virtual bool dispatchEvent( EVENT_CONTAINER *Evt ); |
|||
virtual bool validate(); |
|||
}; |
|||
|
|||
|
|||
|
|||
|
|||
#endif |
|||
|
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue