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.
 
 
 
 
 
 

326 lines
9.1 KiB

/*******************************************************************
*
* DESCRIPTION: ARGUS Kernel source
*
* AUTHOR: gagua
*
* HISTORY:
*
* DATE:5/29/2019
*
*******************************************************************/
#include "RGoose_Kernel.h"
#define LOG( X ) m_pLog->Log( "Kernel", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "Kernel", this, _LOG_ERROR, X )
ARGUS_Kernel::ARGUS_Kernel( config_setting_t *Settings ):basicThread(){
m_bExitCause = 0;
m_pDBC = NULL;
m_tTimeTSeconds = time( NULL );
m_pLog = basicLogCollection::getInstance();
m_pEventHeap = basicEventHeap::getInstance();
m_pOnvifMediaObserver = onvifMediaObserver::getInstance();
m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "KERNEL" );
if( basicSettingsProvider::getInstance()->isCompromised() ) {
basicSettingsProvider::getInstance()->validate();
};
};
ARGUS_Kernel::~ARGUS_Kernel(){
#ifdef DEBUG
LOG("Destructor");
#endif
WaitFinish(true);
#ifdef DEBUG
LOG("Deleting Components");
#endif
#ifdef DEBUG
LOG("Destructor Finished");
#endif
};
bool ARGUS_Kernel::create( void ){
if( m_pSettings != NULL ) {
config_setting_t * pSetting;
int tVal;
char tBuffer[256];
//Connect( CSTR DBName, CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port = 3300 );
LOG("Getting Settings ...");
/*
#ifdef __EU_PROJECT__
if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_SAFE_SHORE, &tVal ) ){
tVal = m_bSafeShoreSupport = true;
pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_SAFE_SHORE, CONFIG_TYPE_BOOL );
config_setting_set_bool( pSetting, tVal );
m_pSettingsProvider->compromise();
LOG("Safe-Shore support not defined");
} else {
m_bSafeShoreSupport = (bool)tVal;
sprintf( tBuffer, "Safe-Shore support Read From Settings: %d", tVal );
LOG( tBuffer );
};
#endif
if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_DEFENDER, &tVal ) ){
tVal = m_bDefenderSupport = true;
pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DEFENDER, CONFIG_TYPE_BOOL );
config_setting_set_bool( pSetting, tVal );
m_pSettingsProvider->compromise();
LOG("Defender support not defined");
} else {
m_bDefenderSupport = (bool)tVal;
sprintf( tBuffer, "Defender support Read From Settings: %d", tVal );
LOG( tBuffer );
};
if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_DETECTOR_WIDTH, &tVal )){
tVal = m_nWidth = 1000;
pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DETECTOR_WIDTH, CONFIG_TYPE_INT);
config_setting_set_int(pSetting, tVal );
m_pSettingsProvider->compromise();
LOG("No Detector Width");
} else {
m_nWidth = tVal;
sprintf( tBuffer, "Detector Width Read From Settings: %d", tVal );
LOG( tBuffer );
};
if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_FILTER_KERNELS, &tVal )){
tVal = m_nKernelFilter = 0x000000ff;
pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_FILTER_KERNELS, CONFIG_TYPE_INT);
config_setting_set_int(pSetting, tVal );
m_pSettingsProvider->compromise();
LOG("No Fltered Kernels Defined - All LRF Will Work");
} else {
m_nKernelFilter = tVal;
sprintf( tBuffer, "Filter Kernels Read From Settings: %d", tVal );
LOG( tBuffer );
};
if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_DETECTOR_SST, &tVal )){
tVal = m_nSST = 0;
pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DETECTOR_SST, CONFIG_TYPE_INT);
config_setting_set_int(pSetting, tVal );
m_pSettingsProvider->compromise();
LOG("No Detector SST");
} else {
m_nSST = tVal;
sprintf( tBuffer, "Detector SST Read From Settings: %d", tVal );
LOG( tBuffer );
};
if( m_pSettingsProvider->isCompromised() ) {
LOG("Save Settings");
m_pSettingsProvider->validate();
};
*/
} else {
LOG_ERROR("Failed Read Config Settings!");
return false;
};
if( m_pDBC == NULL ) {
LOG("Explicitly Create DBC Object");
m_pDBC = new ARGUS_DBC(m_pSettings);
};
if( m_pDBC == NULL ){
LOG_ERROR("DBC Object Creation Failure!");
return false;
};
if(!m_pDBC->connect()){
LOG_ERROR("DBC Connection Failure!");
SLEEP(5000);
//ToDO: Set Restart Kernel!
return false;
};
#ifdef DEBUG
LOG( "Settings Read Done" );
#endif
m_pDBC->getIPCamRecords( m_vCamRecords );
#ifdef DEBUG
LOG("Init IP Cameras");
#endif
if( m_vCamRecords.size() ) {
ARGUS_Camera *tPtr = NULL;
for( int i=0; i < m_vCamRecords.size(); i++ ) {
tPtr = new ARGUS_Camera();
if( tPtr != NULL ) {
if( tPtr->create( m_vCamRecords.at(i)->ip, m_vCamRecords.at(i)->username, m_vCamRecords.at(i)->password ) ){
#ifdef DEBUG
LOG("Camera Created!");
#endif
m_vCameras.push_back( tPtr );
tPtr = NULL;
} else {
LOG_ERROR("Camera Creation Failure!");
delete( tPtr );
tPtr = NULL;
};
};
};
};
#ifdef DEBUG
LOG("Start OnVIF Discovery");
#endif
if( m_pOnvifMediaObserver!= NULL ) {
m_pOnvifMediaObserver->startDiscovery();
m_bDiscoveryStarted = true;
};
#ifdef DEBUG
LOG( "Kernel Trhead is about to be Created" );
#endif
if( init() == false ){
LOG_ERROR("Error in starting Kernel Thread!");
return false;
};
#ifdef DEBUG
LOG( "Kernel Thread Created" );
#endif
return true;
};
void ARGUS_Kernel::WaitFinish( bool Force ){
if( Force ){
#ifdef DEBUG
LOG( "Thread End Forced" );
#endif
} else {
// 3 seconds sleep to settle down processes
SLEEP(3000);
};
waitThreadEnd( Force );
#ifdef DEBUG
LOG( "Thread is Detached" );
#endif
};
/* Main Kernel Thread Procedure */
void ARGUS_Kernel::m_fThreadProc( void ){
char Buffer[CTEXT];
time_t tTimeTSeconds;
int lastTSS = 0;
int lastTMS = 0;
EventRecordDBC * tEvtPtr;
m_bThreadStatus = TH_WORKING;
while( m_bThreadStatus < TH_ENDED ){
m_tTimeTSeconds = time( NULL );
// Network IP OnVif Discovery
if( m_pOnvifMediaObserver!= NULL ){
if( !m_bDiscoveryStarted ) {
if((m_tTimeTSeconds%ONVIF_DISCOVERY_INTERVAL)==0){
#ifdef DEBUG
LOG("Start OnVIF Discovery");
#endif
m_pOnvifMediaObserver->startDiscovery();
m_bDiscoveryStarted = true;
};
} else {
if((m_tTimeTSeconds%ONVIF_DISCOVERY_INTERVAL)==ONVIF_DISCOVERY_RESULT_TIME){
#ifdef DEBUG
LOG("Validate IP Cameras");
#endif
validateCameras();
m_bDiscoveryStarted = false;
};
};
};
// Event Processing Loop
if( !m_pDBC->getLastEvents( m_vEvtRecords, lastTSS, lastTMS ) ){
// Error In DB Access
m_bThreadStatus = TH_ENDED;
m_bExitCause = 1;
continue;
};
if( m_vEvtRecords.size() > 0 ){
validateEvents( lastTSS, lastTMS );
};
if( m_tTimeTSeconds != tTimeTSeconds ){ // Each Second Process
tTimeTSeconds = m_tTimeTSeconds;
};
SLEEP( SLEEP_KERNEL_INTERVAL );
};
};
int ARGUS_Kernel::isCameraListed( CSTR uid ){
if( m_vCamRecords.size() )
for( int index = 0; index < m_vCamRecords.size(); index++ )
if(strstr(m_vCamRecords.at(index)->uuid,uid)!=NULL){
m_vCamRecords.at(index)->online = true;
return index;
};
return -1;
};
void ARGUS_Kernel::validateCameras( void ){
vectorWoWoResults *DiscoverResults = m_pOnvifMediaObserver->getResults();
if( DiscoverResults->size() > 0 ) {
for (int index = 0; index < DiscoverResults->size(); index++) {
char tBuffer[256];
sprintf( tBuffer,"Found Camwera [%s] %s", DiscoverResults->at(index)->XAddrs, DiscoverResults->at(index)->RefID );
LOG( tBuffer );
int cid = isCameraListed(DiscoverResults->at(index)->RefID);
if( cid != -1 ) {
sprintf( tBuffer, "Camera is Listed as %d", cid );
LOG( tBuffer );
if( strstr(DiscoverResults->at(index)->XAddrs,m_vCamRecords.at(cid)->ip)==NULL ) {
m_pDBC->updateIPCam( DiscoverResults->at(index)->RefID, DiscoverResults->at(index)->XAddrs );
};
} else {
// Uncknown camera - no checking
LOG("Camera is Not Listed");
m_pDBC->updateIPCam( DiscoverResults->at(index)->RefID, DiscoverResults->at(index)->XAddrs );
};
};
} else {
LOG("No Online IP Cameras in Network");
};
};
void ARGUS_Kernel::validateEvents( int &lastTSS, int &lastTMS ){
EventRecordDBC *tPtr = NULL;
basicEvent *tEvt = NULL;
if(m_vEvtRecords.size()) {
lastTSS = m_vEvtRecords.at(m_vEvtRecords.size()-1)->ts_s;
lastTMS = m_vEvtRecords.at(m_vEvtRecords.size()-1)->ts_ms;
#ifndef ONE_EVENT_PER_LOOP
while(m_vEvtRecords.size()) {
#endif
tPtr = *m_vEvtRecords.begin();
if( tPtr != NULL ) {
if( tEvt != NULL ) {
m_pEventHeap->broadcast( tEvt );
//free( tEvt );
};
free( tPtr );
};
m_vEvtRecords.erase(m_vEvtRecords.begin());
#ifndef ONE_EVENT_PER_LOOP
};
#endif
};
};
bool ARGUS_Kernel::setStatus( int A ){
m_bThreadStatus = (thredStatus)A;
};
bool ARGUS_Kernel::signalEvent( void ){
if( m_pDBC->getLastEvents( m_vEvtRecords, 0,0 ) ){
};
return true;
};
#undef LOG
#undef LOG_ERROR