|
|
//----------------------------------------------------------------------------
// Dr.Frucht Systems Ltd.
// Copyright � 2000-2018. All Rights Reserved.
// Version 1.0 12/12/2018
// Last modification: 12/12/2018
// PROJECT:
// SUBSYSTEM:
// FILE: Kernel.cpp
// AUTHOR: gagua
// OVERVIEW
// ~~~~~~~~
// Kernel.cpp
//----------------------------------------------------------------------------
//
#include "Kernel.h"
//#define _GNU_SOURCE
//#include <stdio.h>
//#include <stdlib.h>
#define LOG( X ) m_pLog->Log( "Kernel", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "Kernel", this, _LOG_ERROR, X )
// Constructor implementation
Kernel::Kernel( CSTR *HM ): basicThread(){ m_bExitCause = 0; m_pLog = basicLogCollection::getInstance(); m_pEventHeap = basicEventHeap::getInstance(); m_pSettingsProvider = basicSettingsProvider::getInstance(); m_pDataBase = NULL; m_pNetClientsServer = NULL; m_pVideoInputs = NULL; m_pVideoNames = NULL; m_pMatProtector = NULL; m_pFinalFrame = NULL; m_bEngineIsAvlib = false; m_nPictureRot = 0; m_nConvertType = 0; m_bPictureRotated = false; m_pDBHost = m_pDBUser = m_pDBPass = m_pDBName = m_pStorageBase = NULL; for( uint8_t index = 0; index < MAX_LRF_SENSORS; index++ )m_nLRF_Background[index] = 0x03FF; m_nFrameCounter = 0; m_nAlarmCnt = 0; m_bRecordIsActive = false; m_bRecordIsSaving = false; m_lStartTS = 0ll; m_nCOnlineMask = 0; m_nCOKMask = 0; m_nDetectorSST = 0; m_fDetectorMinSpeed = -1.f;
#ifdef ASSEMBLE_MP4
m_bAssembleMP4 = true;#else
m_bAssembleMP4 = false;#endif
#ifdef AMIR_MODE
m_nCurrentRange = 0;#endif
#ifdef USE_TIME_LAPSE_RECORDER
m_pVideoOutput = NULL;#else
for( int index = 0; index < MAX_RECORD_FRAMES; index++ ) { m_pVidRecFrame[index] = NULL; };#endif
m_nVideoInputs = 0; m_nWidth = 320; m_nHeight = 240; m_bHorizontal = true; m_nCLoopMask = 0xFFFFFFFF; m_nCLoopCheck = 0; m_nScheduledTaskCounter = 0; LOG("K-C1"); if( m_pSettingsProvider ) { m_pSettings = m_pSettingsProvider->getSubNode( NULL, KNL_SETTINGS_MAIN_TAG ); if( m_pSettingsProvider->isCompromised() ) { m_pSettingsProvider->validate(); }; } else { m_pSettings = NULL; }; LOG("K-CE");};
// Destructor implementation
Kernel::~Kernel(){#ifdef DEBUG
LOG("Destructor");#endif
WaitFinish(true);#ifdef DEBUG
LOG("Deleting Components");#endif
if( m_pVideoInputs != NULL ) { for( int index = 0; index < m_nVideoInputs; index++ ) { if( m_pVideoInputs[index] != NULL ) delete( m_pVideoInputs[index] ); if( m_pVideoFrame[index] != NULL ) delete( m_pVideoFrame[index] ); }; free(m_pVideoInputs); free(m_pVideoFrame); }; if( m_pFinalFrame!= NULL ) { delete(m_pFinalFrame); m_pFinalFrame == NULL; }; if(m_pMatProtector != NULL ) { m_pMatProtector->unlock(); delete(m_pMatProtector); };
#ifndef USE_TIME_LAPSE_RECORDER
for( int index = 0; index < MAX_RECORD_FRAMES; index++ ) { if(m_pVidRecFrame[index]!= NULL )delete(m_pVidRecFrame[index]); };#endif
#ifdef DEBUG
LOG("Destructor Finished");#endif
};
bool Kernel::attachNetServer( networkServer *srvPort ){ if( srvPort != NULL ) { m_pNetClientsServer = srvPort; }; if( m_pNetClientsServer!= NULL ) { LOG("Binding Network Manager to Kernel module"); return srvPort->attachEngine( (basicEngine *)this ); }; return false;};
bool Kernel::getDevicePath(CSTR Path, CSTR BusID ){ bool tFlag = false; if(Path == NULL ) return false; if( BusID == NULL ) return false; Path[0] = (char)0; for( int i = 0; i< m_InputInfo.size(); i++ ) { if( strcmp( m_InputInfo.at(i)->FriendlyName, BusID ) == 0 ){ sprintf( Path, "%s", m_InputInfo.at(i)->DevicePath ); tFlag = true; break; }; }; return tFlag;};//https://www.youtube.com/watch?time_continue=84&v=a4lY6uX3q1M
//http://flv-player.net/players/mini/
bool Kernel::create( CSTR *ptr, networkServer *srvPort ){ char tBuffer[256]; if( m_pSettings != NULL ) { config_setting_t *pSetting; config_setting_t *pVideoInputs; int tVal, mCount; double tFVal;
LOG("Attach Network ..."); attachNetServer( srvPort );
LOG("Getting Local Video Sources ..."); if( !enumerateDevices( &m_InputInfo ) ){ LOG_ERROR("Error Geting local Sources!"); } else { sprintf( tBuffer, "Detected %d Video Sources", m_InputInfo.size() ); LOG( tBuffer ); };
LOG("Getting Settings ..."); if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_CONVERT_TYPE, &tVal)){ tVal = 0; pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_CONVERT_TYPE, CONFIG_TYPE_INT); config_setting_set_int(pSetting, tVal ); basicSettingsProvider::getInstance()->compromise(); }; m_nConvertType = tVal;
if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_VIDEO_ENGINE, &tVal)){ tVal = 1; pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_VIDEO_ENGINE, CONFIG_TYPE_INT); config_setting_set_int(pSetting, tVal ); basicSettingsProvider::getInstance()->compromise(); };
m_bEngineIsAvlib = (tVal == 0);
if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_DATABASE_ENA, &tVal ) ){ tVal = m_bDatabaseEnable = false; pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DATABASE_ENA, CONFIG_TYPE_BOOL ); config_setting_set_bool( pSetting, tVal ); m_pSettingsProvider->compromise(); LOG("Database support not defined -> Disabling"); } else { m_bDatabaseEnable = (bool)tVal; sprintf( tBuffer, "Database Support Read From Settings: %d", m_bDatabaseEnable ); LOG( tBuffer ); }; if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_VIDEO_OUTPUT, &tVal ) ){#ifdef ASSEMBLE_MP4
tVal = m_bAssembleMP4 = true;#else
tVal = m_bAssembleMP4 = false;#endif
pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_VIDEO_OUTPUT, CONFIG_TYPE_BOOL ); config_setting_set_bool( pSetting, tVal ); m_pSettingsProvider->compromise();#ifdef ASSEMBLE_MP4
LOG("MP4 support not defined -> Enabling");#else
LOG("MP4 support not defined -> Disabling");#endif
} else { m_bAssembleMP4 = (bool)tVal; sprintf( tBuffer, "MP4 Support Read From Settings: %d", m_bAssembleMP4 ); LOG( tBuffer ); }; if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_DETECT_ARMED, &tVal ) ){ tVal = m_bDetectorArmed = false; pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DETECT_ARMED, CONFIG_TYPE_BOOL ); config_setting_set_bool( pSetting, tVal ); m_pSettingsProvider->compromise(); LOG("Detector Armed not defined -> Disabling"); } else { m_bDetectorArmed = (bool)tVal; sprintf( tBuffer, "Detector Armed Read From Settings: %d", m_bDetectorArmed ); LOG( tBuffer ); }; if( !config_setting_lookup_float( m_pSettings, KNL_SETTINGS_DETECT_LAYER_DIST, &tFVal ) ){ tFVal = (double)(m_fDetectorLayerDistance = -1.f); pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DETECT_LAYER_DIST, CONFIG_TYPE_FLOAT ); config_setting_set_float( pSetting, tFVal ); m_pSettingsProvider->compromise(); LOG("Detector Layer Distance not defined -> Disabling"); } else { m_fDetectorLayerDistance = (float)tFVal; sprintf( tBuffer, "Detector Layer Distance Read From Settings: %0.2f", m_fDetectorLayerDistance ); LOG( tBuffer ); }; if( !config_setting_lookup_float( m_pSettings, KNL_SETTINGS_DETECT_MAX_RANGE, &tFVal ) ){ tFVal = (double)(m_fDetectorMaxDistance = 3.f); pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DETECT_MAX_RANGE, CONFIG_TYPE_FLOAT ); config_setting_set_float( pSetting, tFVal ); m_pSettingsProvider->compromise(); LOG("Detector Max Distance not defined set 3.0 meters"); } else { m_fDetectorMaxDistance = (float)tFVal; sprintf( tBuffer, "Detector Max Distance Read From Settings: %0.2f", m_fDetectorMaxDistance ); LOG( tBuffer ); }; if( !config_setting_lookup_float( m_pSettings, KNL_SETTINGS_DETECT_MIN_SPEED, &tFVal ) ){ tFVal = (double)(m_fDetectorMinSpeed = 3.f); pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DETECT_MIN_SPEED, CONFIG_TYPE_FLOAT ); config_setting_set_float( pSetting, tFVal ); m_pSettingsProvider->compromise(); LOG("Detector Min Speed not defined set to 3.0 m/s"); } else { m_fDetectorMinSpeed = (float)tFVal; sprintf( tBuffer, "Detector Min Speed Read From Settings: %0.2f m/s", m_fDetectorMaxDistance ); LOG( tBuffer ); }; if( !config_setting_lookup_int( m_pSettings, KNL_SETTINGS_DETECT_OFFSET, &tVal ) ){ tVal = m_nDetectorOffset = 20; pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DETECT_OFFSET, CONFIG_TYPE_INT ); config_setting_set_int( pSetting, tVal ); m_pSettingsProvider->compromise(); LOG("Detector Offset not defined set 20"); } else { m_nDetectorOffset = tVal; sprintf( tBuffer, "Detector Offset Read From Settings: %d", m_nDetectorOffset ); LOG( tBuffer ); }; if( !config_setting_lookup_int( m_pSettings, KNL_SETTINGS_DETECT_SST, &tVal ) ){ tVal = m_nDetectorSST = 0; pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DETECT_SST, CONFIG_TYPE_INT ); config_setting_set_int( pSetting, tVal ); m_pSettingsProvider->compromise(); LOG("Detector SST not defined set 0"); } else { m_nDetectorSST = tVal; sprintf( tBuffer, "Detector SST Read From Settings: %d", m_nDetectorOffset ); LOG( tBuffer ); };
if (!config_setting_lookup_string(m_pSettings, KNL_SETTINGS_STORAGE_BASE,(const char**)&m_pStorageBase)){ m_pStorageBase = "./storage/"; pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_STORAGE_BASE, CONFIG_TYPE_STRING); config_setting_set_string(pSetting, m_pStorageBase); basicSettingsProvider::getInstance()->compromise(); }; if( m_bDatabaseEnable ) { if (!config_setting_lookup_string(m_pSettings, KNL_SETTINGS_DATABASE_HOST,(const char**)&m_pDBHost)){ m_pDBHost = "localhost"; pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DATABASE_HOST, CONFIG_TYPE_STRING); config_setting_set_string(pSetting, m_pDBHost); basicSettingsProvider::getInstance()->compromise(); }; if (!config_setting_lookup_string(m_pSettings, KNL_SETTINGS_DATABASE_NAME,(const char**)&m_pDBName)){ m_pDBName = "mastbaum"; pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DATABASE_NAME, CONFIG_TYPE_STRING); config_setting_set_string(pSetting, m_pDBName); basicSettingsProvider::getInstance()->compromise(); }; if (!config_setting_lookup_string(m_pSettings, KNL_SETTINGS_DATABASE_USER,(const char**)&m_pDBUser)){ m_pDBUser = "dbroot"; pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DATABASE_USER, CONFIG_TYPE_STRING); config_setting_set_string(pSetting, m_pDBUser); basicSettingsProvider::getInstance()->compromise(); }; if (!config_setting_lookup_string(m_pSettings, KNL_SETTINGS_DATABASE_PASS,(const char**)&m_pDBPass)){ m_pDBPass = "123567"; pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DATABASE_PASS, CONFIG_TYPE_STRING); config_setting_set_string(pSetting, m_pDBPass); basicSettingsProvider::getInstance()->compromise(); }; } if( m_pSettingsProvider->isCompromised() ) { LOG("Save Settings"); m_pSettingsProvider->validate(); };
pVideoInputs = m_pSettingsProvider->getSubNode( m_pSettings, KNL_SETTINGS_VIDEO_SECTION ); mCount = config_setting_length( pVideoInputs ); if( mCount ){ sprintf( tBuffer, "Defined %d Video Input", mCount ); LOG(tBuffer); m_pVideoNames = (CSTR *) calloc( mCount, sizeof(CSTR*) ); //for( int i=0; i < mCount; i++ ){ m_pVideoNames[i]= (CSTR) malloc(256); };
m_pVideoInputs = (basicMediaSource**) calloc( mCount, sizeof(basicMediaSource*) ); m_pVideoFrame = (cv::Mat**) calloc( mCount, sizeof(cv::Mat*)); memset( m_pVideoInputs, 0, sizeof(m_pVideoInputs)); memset( m_pVideoFrame, 0, sizeof(m_pVideoFrame)); if( m_pVideoInputs != NULL ) { config_setting_t *tRec; config_setting_t *ttRec; int tIID, tWIDTH, tHEIGHT, tSCALEW, tSCALEH; bool tSCALED; double tFPS; m_nVideoInputs = mCount; for( int index = 0; index < m_nVideoInputs; index++ ){ tRec = config_setting_get_elem( pVideoInputs, index ); if( tRec != NULL ){ mCount = config_setting_length( tRec ); sprintf( tBuffer, "Records: %d", mCount ); LOG(tBuffer); tSCALED = false; //if( !config_setting_lookup_int( tRec, KNL_SETTINGS_VIDEO_INPUT_ID, &tIID)) continue;
if( !config_setting_lookup_string( tRec, KNL_SETTINGS_VIDEO_INPUT_ID, (const char**)&m_pVideoNames[index] )) continue; if( !config_setting_lookup_int( tRec, KNL_SETTINGS_VIDEO_INPUT_WIDTH, &tWIDTH)) continue; if( !config_setting_lookup_int( tRec, KNL_SETTINGS_VIDEO_INPUT_HEIGHT, &tHEIGHT)) continue; if( !config_setting_lookup_float( tRec, KNL_SETTINGS_VIDEO_INPUT_FPS, &tFPS)) continue; if( !config_setting_lookup_int( tRec, KNL_SETTINGS_VIDEO_SCALE_WIDTH, &tSCALEW)) tSCALEW = -1; if( !config_setting_lookup_int( tRec, KNL_SETTINGS_VIDEO_SCALE_HEIGHT, &tSCALEH)) tSCALEH = -1; if(( tSCALEW != -1 )&&( tSCALEH != -1 )) tSCALED = true; LOG( "Begin" ); LOG( m_pVideoNames[index] ); if( !getDevicePath( tBuffer, m_pVideoNames[index] ) ) continue; LOG( "Found:" ); LOG( tBuffer ); //sprintf( tBuffer, "/dev/video%d", tIID);
if( m_bEngineIsAvlib ){ m_pVideoInputs[index] = (basicMediaSource*)new videoAVLIBSource(); if( !((videoAVLIBSource*)m_pVideoInputs[index])->_open( tBuffer, false, (float)tFPS, -1, tWIDTH, tHEIGHT, tSCALED, tSCALEW, tSCALEH )){ delete((videoAVLIBSource*)m_pVideoInputs[index]); m_pVideoInputs[index] = NULL; LOG_ERROR("Source Open Failure."); } else { if(tSCALED) { if( m_nWidth < tSCALEW ) m_nWidth = tSCALEW; if( m_nHeight < tSCALEH ) m_nHeight = tSCALEH; }else{ if( m_nWidth < tWIDTH ) m_nWidth = tWIDTH; if( m_nHeight < tHEIGHT ) m_nHeight = tHEIGHT; }; m_pVideoFrame[index] = new cv::Mat( tHEIGHT, tWIDTH, CV_8UC3 ); m_pVideoInputs[index]->attachDestination( this ); LOG("Source Opened OK."); }; } else { m_pVideoInputs[index] = (basicMediaSource*)new videoV4L2Source(); if( !((videoV4L2Source*)m_pVideoInputs[index])->_open( tBuffer, false, (float)tFPS, -1, tWIDTH, tHEIGHT, tSCALED, tSCALEW, tSCALEH )){ delete((videoV4L2Source*)m_pVideoInputs[index]); m_pVideoInputs[index] = NULL; LOG_ERROR("Source Open Failure."); } else { if(tSCALED) { if( m_nWidth < tSCALEW ) m_nWidth = tSCALEW; if( m_nHeight < tSCALEH ) m_nHeight = tSCALEH; }else{ if( m_nWidth < tWIDTH ) m_nWidth = tWIDTH; if( m_nHeight < tHEIGHT ) m_nHeight = tHEIGHT; }; m_pVideoFrame[index] = new cv::Mat( tHEIGHT, tWIDTH, CV_8UC3 ); m_pVideoInputs[index]->attachDestination( this ); LOG("Source Opened OK."); }; }; } else { LOG_ERROR("Mem Allocation Error!"); }; m_nCLoopCheck |=(1<<index); }; } else { m_nVideoInputs = 0; LOG_ERROR("Mem Allocation Error!"); }; } else { LOG_ERROR("No Video Inputs Defined!"); return false; };
if( m_bHorizontal ) { m_nWidth = m_nWidth * m_nVideoInputs; } else { m_nHeight = m_nHeight * m_nVideoInputs; }; m_nHeight += 16; // Top Info Band
m_pMatProtector = new basicMutex(); m_pFinalFrame = new cv::Mat( m_nHeight, m_nWidth, CV_8UC3 );
#ifndef USE_TIME_LAPSE_RECORDER
for( int index = 0; index < MAX_RECORD_FRAMES; index++ ) { m_pVidRecFrame[index] = new cv::Mat( m_nHeight, m_nWidth, CV_8UC3 ); };#endif
m_nRecordCounter = MAX_RECORD_FRAMES;
sprintf( m_sWEBAPIName, "%s/%s", m_pStorageBase, "web_pipe" ); sprintf( m_sFSReportName, "%s/%s", m_pStorageBase, "report" ); sprintf( m_sWEBStatus, "%s/%s", m_pStorageBase, "status" );
LOG(m_sWEBAPIName); LOG(m_sFSReportName);
m_nInitialFSID = ( uint32_t ) time( NULL ); /* Storage Init */ m_pStorageProvider = storageProvider::getInstance( m_pStorageBase );#ifdef USE_TIME_LAPSE_RECORDER
if( m_bAssembleMP4 ) { m_pVideoOutput = new lapsedVideoRecorder(); if( m_pVideoOutput == NULL ) { LOG_ERROR("TimeLapse Recorder on create Failure!"); m_bAssembleMP4 = false; } else { m_pVideoOutput->setFrameLapse( MAX_RECORD_FRAMES ); }; };#endif
if( m_bDatabaseEnable ) { /* DataBase */ m_pDataBase = new MySQL_Connector(); if( m_pDataBase!= NULL ) { if( !m_pDataBase->Connect( m_pDBName, m_pDBHost, m_pDBUser, m_pDBPass ) ){ LOG_ERROR("Error Connecting to Database!"); //return false;
} else { }; }; }; } else { LOG_ERROR("Failed Read Config Settings!"); return false; };
if( m_pEventHeap == NULL ){ LOG_ERROR("No Event Heap attached!"); return false; };
#ifdef DEBUG
LOG( "Trhead about to be created" );#endif
if( init() == false ){ LOG_ERROR("Error in starting Kernel Thread!"); return false; };
m_pProtocol = new protocolMastBaum( m_pSettings );
if( m_pProtocol != NULL ) { m_pProtocol->attachKernel( this ); m_pProtocol->create(); m_nScheduledTaskCounter = 200; // 1 = 10 mlsec
m_nScheduledTaskID = ST_SEND_SETTINGS; } else { LOG_ERROR("Error in starting Kernel Protocol!"); return false; };
putEvent2FS( FSL_SYSTEM_STARTUP ); postStatus2FS(); m_bTargetOverflow = MAX_TARGET_ACCEPTED_AT_ONCE;#ifdef DEBUG
LOG( "Created" );#endif
return true;};//https://dist.plone.org/download/
void Kernel::putEvent2FS( uint8_t code, int x1, int y1, int x2, int y2, float speed, bool has_image, CSTR path){ FILE *fd; fd = fopen(m_sFSReportName, "a+" ); if( fd ){ m_nInitialFSID++; uint32_t ts = ( uint32_t ) time(NULL); switch( code ) { case FSL_OPERATOR_ALARM: case FSL_ALARM:{ float r1, r2; r1 = y1;//( m_fDetectorMaxDistance / 8.0f ) * (float) y1;
r2 = y2;//( m_fDetectorMaxDistance / 8.0f ) * (float) y2;
fprintf( fd, "%d,%d,%d,%d,%0.2f,%d,%0.2f,%0.3f,%d,%s\n", m_nInitialFSID, ts, code, x1, r1, x2, r2, speed, has_image, path ); break; }; default: fprintf( fd, "%d,%d,%d\n", m_nInitialFSID, ts, code ); break; }; fclose(fd); chmod( m_sFSReportName, STORAGE_FILE_MODE ); };};//https://www.ebay.com/itm/Nvidia-Tesla-M2090-6GB-GDDR5-PCIe-Accelerator-Video-Graphics-Card-775NK/123590917738
//http://www.zoomify.com/free.htm
void Kernel::postStatus2FS( bool isActive ){ FILE *fd; fd = fopen( m_sWEBStatus, "w+" ); if( fd ){ uint32_t ts = ( uint32_t ) time(NULL); fprintf( fd, "%d,%d,%d,%d,%d,%d,%d,%0.3f,%0.3f,%0.3f \n", ts, isActive, m_bDetectorArmed, m_nCOnlineMask, m_nCOKMask, m_nDetectorOffset, m_nDetectorSST, m_fDetectorMaxDistance, m_fDetectorLayerDistance, m_fDetectorMinSpeed ); for( uint8_t index=0; index < MAX_LRF_SENSORS; index++ ) fprintf( fd, "%d\n", m_nLRF_Background[index] ); fclose(fd); chmod( m_sWEBStatus, STORAGE_FILE_MODE ); };};
void Kernel::alarm( bool real ){#ifdef USE_TIME_LAPSE_RECORDER
#ifdef DEBUG
LOG("Get VO Status!");#endif
if( !m_pVideoOutput->getRecordStatus() ){ m_nAlarmCnt++; char Buffer[1024]; sprintf( m_sSeriesName, "%s", m_pStorageProvider->getNewFileName()); sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER );#ifdef DEBUG
LOG("Begin VO!");#endif
m_bRecordIsSaving = m_pVideoOutput->beginOutput( Buffer ); } else {#ifdef DEBUG
LOG("Cont VO!");#endif
m_bRecordIsSaving = m_pVideoOutput->continueOutput(); }; m_bRecordIsActive = m_bRecordIsSaving; m_bEventRegistered = false;#else //USE_TIME_LAPSE_RECORDER
if( !m_bRecordIsActive ){ m_nAlarmCnt++; //m_pStorageProvider->startNewEntry();
sprintf( m_sSeriesName, "%s", m_pStorageProvider->getNewFileName()); m_bRecordIsActive = true; m_bRecordIsSaving = true; m_bEventRegistered = false;#endif // USE_TIME_LAPSE_RECORDER
#ifdef NO_GPIO_HANDLER
if( !real ) {#else
if( real ) { // Request Alarm Info from HW
#ifdef DEBUG
LOG("HW ALARM is SET!");#endif
m_pProtocol->requestTargets(); } else {#endif
char Buffer[1024]; #ifdef USE_TIME_LAPSE_RECORDER
sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER ); #else
#ifdef ASSEMBLE_MP4
if( m_bAssembleMP4 ) { sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER ); } else { sprintf( Buffer, "%s_%02d_%%03d.jpg", m_sSeriesName, m_nAlarmCnt ); }; #else
sprintf( Buffer, "%s_%02d_%%03d.jpg", m_sSeriesName, m_nAlarmCnt ); #endif
#endif // USE_TIME_LAPSE_RECORDER
putEvent2FS( FSL_OPERATOR_ALARM, 20,20,20,20,0,true, Buffer );//https://www.deviantart.com/mushroombrain
m_bEventRegistered = true;#ifdef DEBUG
LOG("SYM ALARM is SET!");#endif
};#ifndef USE_TIME_LAPSE_RECORDER
};#endif
};
void 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
};
#ifndef USE_TIME_LAPSE_RECORDER
void *Kernel::t_fSavePictures_Proc(void *ptr){ Kernel *me; me = (Kernel*) ptr; //Save Last Record On disk
vector<int> compression_params; //compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
//compression_params.push_back(9);
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY); compression_params.push_back(75); vector<uchar> buf; char Buffer[MAX_PATH]; int DataSize; int z_index = 0; while( me->m_rPrerecord.count > 0 ) { cv:Mat *tMat; tMat = me->m_rPrerecord.pop(); if( tMat != NULL ) { FILE *FID; imencode( ".jpg", *tMat, buf, compression_params ); DataSize = buf.size(); sprintf( Buffer, "%s_%02d_%03d.jpg", me->m_sSeriesName, me->m_nAlarmCnt, z_index ); FID = fopen( Buffer, "wb+"); fwrite(buf.data(),DataSize, 1,FID ); fclose(FID); z_index++; }; delete( tMat ); }; for( int index = MAX_RECORD_FRAMES - 1; index > -1; index -- ) { FILE *FID; imencode( ".jpg", (*me->m_pVidRecFrame[index]), buf, compression_params ); DataSize = buf.size(); sprintf( Buffer, "%s_%02d_%03d.jpg", me->m_sSeriesName, me->m_nAlarmCnt, z_index ); FID = fopen( Buffer, "wb+"); fwrite(buf.data(),DataSize, 1,FID ); fclose(FID); chmod( Buffer, STORAGE_FILE_MODE ); z_index++; };#ifdef ASSEMBLE_MP4
if( me->m_bAssembleMP4 ) { // Create Video From Pictures
sprintf( Buffer, //"ffmpeg -loglevel panic -hide_banner -f image2 -r 15 -i %s_%02d_%%03d.jpg -vb 1200k -y -an -r 15 %s_%02d.flv",
"ffmpeg -loglevel panic -hide_banner -f image2 -r 15 -i %s_%02d_%%03d.jpg -vb 500k -pix_fmt yuv420p -y -an -r 15 %s_%02d.mp4", me->m_sSeriesName, me->m_nAlarmCnt, me->m_sSeriesName, me->m_nAlarmCnt ); basicLogCollection::getInstance()->Log( "Kernel-ST", NULL, Buffer ); FILE *fid; char temp; fid = popen( Buffer, "r" ); if( fid!= NULL ){ while( !feof( fid ) ) fread( &temp, 1, 1, fid ); pclose( fid ); sprintf( Buffer, "%s_%02d.mp4", me->m_sSeriesName, me->m_nAlarmCnt ); chmod( Buffer, STORAGE_FILE_MODE ); #ifndef KEEP_JPEG_IMAGES
for( int index = ( MAX_RECORD_FRAMES << 1 ) - 1; index > -1; index -- ) { sprintf( Buffer, "%s_%02d_%03d.jpg", me->m_sSeriesName, me->m_nAlarmCnt, index ); unlink( Buffer ); }; #endif
} else { basicLogCollection::getInstance()->Log( "Kernel-ST", NULL, "ERROR CREATE POPEN!" ); }; };#endif
me->m_bRecordIsSaving = false; me->m_nRecordCounter = MAX_RECORD_FRAMES; return NULL;};#endif //USE_TIME_LAPSE_RECORDER
/* Main Kernel Thread Procedure */void Kernel::m_fThreadProc( void ){ char Buffer[1024]; m_bThreadStatus = TH_WORKING; while( m_bThreadStatus < TH_ENDED ){ int cmd = WEB_CMD_NOP, param; float fparam1, fparam2, fparam3; /* check Video Subsystem */ for( int index = 0; index < m_nVideoInputs; index++ ){ if( m_pVideoInputs[index] != NULL ){ MediaStateStatus tMediaStatus; tMediaStatus = ((videoAVLIBSource*)m_pVideoInputs[index])->getStatus(); if( tMediaStatus != MS_PRESENT ) { if( m_bEngineIsAvlib ){ // Delete Loosed Video Input
LOG("Deleting AVLIB Video Source"); delete((videoAVLIBSource*)m_pVideoInputs[index]); rectangle( (*m_pVideoFrame[index]), Point( 0, 0 ), Point( m_pVideoFrame[index]->cols, m_pVideoFrame[index]->rows ), Scalar( 0, 0, 0 ), CV_FILLED ); putText( (*m_pVideoFrame[index]), "Video Source Failure!", Point( 20 ,40 ), FONT_HERSHEY_SIMPLEX, .4, Scalar( 0, 0, 255 ), 1 ); /* Polish Memory */ m_pVideoInputs[index] = NULL; m_nCLoopMask &= ~(1<<index); m_nCLoopCheck &= ~(1<<index); } else { LOG("Deleting V4L2 Video Source"); delete((videoV4L2Source*)m_pVideoInputs[index]); rectangle( (*m_pVideoFrame[index]), Point( 0, 0 ), Point( m_pVideoFrame[index]->cols, m_pVideoFrame[index]->rows ), Scalar( 0, 0, 0 ), CV_FILLED ); putText( (*m_pVideoFrame[index]), "Video Source Failure!", Point( 20 ,40 ), FONT_HERSHEY_SIMPLEX, .4, Scalar( 0, 0, 255 ), 1 ); m_pVideoInputs[index] = NULL; m_nCLoopMask &= ~(1<<index); m_nCLoopCheck &= ~(1<<index); }; }; } else { char tBuffer[256]; // Handle Unrecoverable Video Input Lose
if( !enumerateDevices( &m_InputInfo ) ) { LOG_ERROR("Source Enumeration Failure."); } else if( getDevicePath( tBuffer, m_pVideoNames[index] ) ){ if( m_bEngineIsAvlib ){ m_pVideoInputs[index] = (basicMediaSource*)new videoAVLIBSource(); if( !((videoAVLIBSource*)m_pVideoInputs[index])->_open( tBuffer, false )){ delete((videoAVLIBSource*)m_pVideoInputs[index]); m_pVideoInputs[index] = NULL; LOG_ERROR("Source ReOpen Failure."); } else { m_pVideoInputs[index]->attachDestination( this ); LOG("Source ReOpened OK."); }; } else { m_pVideoInputs[index] = (basicMediaSource*)new videoV4L2Source(); if( !((videoV4L2Source*)m_pVideoInputs[index])->_open( tBuffer, false )){ delete((videoV4L2Source*)m_pVideoInputs[index]); m_pVideoInputs[index] = NULL; LOG_ERROR("Source ReOpen Failure."); } else { m_pVideoInputs[index]->attachDestination( this ); LOG("Source ReOpened OK."); }; }; }; }; }; /* Get Commands Via File */ struct stat stFileInfo; int intStat; char *cmdBuffer; intStat = stat( m_sWEBAPIName ,&stFileInfo); cmdBuffer = NULL; if( intStat == 0 ) { // check file size
if( stFileInfo.st_size > 0 ){ FILE *tFD; int RetVal; size_t nSize; tFD = fopen( m_sWEBAPIName, "r" ); if( tFD != NULL ) { RetVal = getline( &cmdBuffer, &nSize, tFD ); fclose(tFD); if( RetVal ) { //sscanf( cmdBuffer, "%d %d %f %f %f", &cmd, ¶m, &fparam1, &fparam2, &fparam3 );
sscanf( cmdBuffer, "%d", &cmd ); }; }; unlink( m_sWEBAPIName ); }; }; /* Get COmmands Via Database - Dont use now */ if( m_pDataBase ) { AAssoc *Result; sprintf( Buffer, "SELECT * FROM web2engine LIMIT 1"); Result = m_pDataBase->Query( Buffer ); if( Result!= NULL ){ int c_id; c_id = atoi( Result->at(0)->at("id").c_str()); cmd = atoi( Result->at(0)->at("cmd").c_str()); param = atoi( Result->at(0)->at("param").c_str()); // Delete Command From Queue
sprintf( Buffer, "DELETE FROM web2engine WHERE id = %d", c_id ); m_pDataBase->SilentQuery( Buffer ); // Free Assoc
delete( Result ); }; }; switch ( cmd ) { case WEB_CMD_NOP: break; case WEB_CMD_RESET_DETECTION:{ LOG( "WEB_CMD_RESET_DETECTION"); if( m_pProtocol->sendSoftReset() ){ putEvent2FS( FSL_SYSTEM_RESTARTED ); m_nScheduledTaskCounter = 200; // 1 = 10 mlsec
m_nScheduledTaskID = ST_CHECK_STATUS; }; break; }; case WEB_CMD_CLEAR_LOG:{ m_pStorageProvider->startNewEntry(); putEvent2FS( FSL_LOG_CLEARED ); break; }; case WEB_CMD_SET_PARAMS:{ LOG("WEB_CMD_SET_PARAMS"); sscanf( cmdBuffer, "%d %d %d %f %f %f", &cmd, &m_nDetectorSST, &m_nDetectorOffset, &m_fDetectorMaxDistance, &m_fDetectorLayerDistance, &m_fDetectorMinSpeed ); //m_nDetectorOffset = param;
//m_fDetectorMaxDistance = fparam1;
//m_fDetectorLayerDistance = fparam2;
//m_fDetectorMinSpeed = fparam3;
if( m_pProtocol->sendSettings( m_nDetectorSST, m_nDetectorOffset, m_fDetectorLayerDistance, m_fDetectorMinSpeed ) ){ config_setting_t *pSetting;
putEvent2FS( FSL_DETECTION_CHANGED ); postStatus2FS(); pSetting = config_setting_lookup(m_pSettings, KNL_SETTINGS_DETECT_SST); if( pSetting != NULL ) { config_setting_set_int(pSetting, m_nDetectorSST); basicSettingsProvider::getInstance()->compromise(); }; pSetting = config_setting_lookup(m_pSettings, KNL_SETTINGS_DETECT_OFFSET); if( pSetting != NULL ) { config_setting_set_int(pSetting, m_nDetectorOffset); basicSettingsProvider::getInstance()->compromise(); }; pSetting = config_setting_lookup(m_pSettings, KNL_SETTINGS_DETECT_LAYER_DIST); if( pSetting != NULL ) { config_setting_set_float(pSetting, (double)m_fDetectorLayerDistance ); basicSettingsProvider::getInstance()->compromise(); }; pSetting = config_setting_lookup(m_pSettings, KNL_SETTINGS_DETECT_MAX_RANGE); if( pSetting != NULL ) { config_setting_set_float(pSetting, (double)m_fDetectorMaxDistance); basicSettingsProvider::getInstance()->compromise(); }; pSetting = config_setting_lookup(m_pSettings, KNL_SETTINGS_DETECT_MIN_SPEED); if( pSetting != NULL ) { config_setting_set_float(pSetting, (double)m_fDetectorMinSpeed ); basicSettingsProvider::getInstance()->compromise(); }; if (basicSettingsProvider::getInstance()->isCompromised()) basicSettingsProvider::getInstance()->validate(); m_nScheduledTaskCounter = 200; // 1 = 10 mlsec
m_nScheduledTaskID = ST_CHECK_STATUS; }; break; }; case WEB_CMD_DISABLE_DETECTION:{ LOG("WEB_CMD_DISABLE_DETECTION"); m_bDetectorArmed = false; if( m_pProtocol->sendMode( m_bDetectorArmed ) ) { putEvent2FS( FSL_OPERATOR_DISARMED ); postStatus2FS(); }; config_setting_t *pSetting; pSetting = config_setting_lookup(m_pSettings, KNL_SETTINGS_DETECT_ARMED); if( pSetting != NULL ) { config_setting_set_bool( pSetting, m_bDetectorArmed ); basicSettingsProvider::getInstance()->compromise(); }; if (basicSettingsProvider::getInstance()->isCompromised()) basicSettingsProvider::getInstance()->validate(); break; }; case WEB_CMD_ENABLE_DETECTION:{ LOG("WEB_CMD_ENABLE_DETECTION"); m_bDetectorArmed = true; if( m_pProtocol->sendMode( m_bDetectorArmed ) ) { putEvent2FS( FSL_OPERATOR_ARMED ); postStatus2FS(); }; config_setting_t *pSetting; pSetting = config_setting_lookup(m_pSettings, KNL_SETTINGS_DETECT_ARMED); if( pSetting != NULL ) { config_setting_set_bool( pSetting, m_bDetectorArmed ); basicSettingsProvider::getInstance()->compromise(); }; if (basicSettingsProvider::getInstance()->isCompromised()) basicSettingsProvider::getInstance()->validate(); break; }; case WEB_CMD_MAKE_TEST_ALARM:{ LOG("WEB_CMD_MAKE_TEST_ALARM"); if( m_pProtocol->sendLED( true ) ) { if(m_nScheduledTaskID == WEB_CMD_NOP ) { m_nScheduledTaskCounter = 200; // 1 = 10 mlsec
m_nScheduledTaskID = ST_TURN_OFF_LED; } else m_pProtocol->sendLED( false ); alarm( false ); }; break; }; case WEB_CMD_RESET_DEVICE:{ LOG("WEB_CMD_RESET_DEVICE"); if( m_pProtocol->sendFullReset() ) { putEvent2FS( FSL_SYSTEM_RESTARTED ); m_nScheduledTaskCounter = 200; // 1 = 10 mlsec
m_nScheduledTaskID = ST_CHECK_STATUS; }; break; }; default: LOG("Unknown Web CMD"); }; if( cmdBuffer != NULL ) { free( cmdBuffer ); };#ifdef USE_TIME_LAPSE_RECORDER
if( m_bRecordIsActive ) { m_bRecordIsActive = m_pVideoOutput->getRecordStatus(); if( !m_bRecordIsActive ) { if( !m_bTargetOverflow ){ if ( m_bDetectorArmed ){ if( m_pProtocol->sendMode( true ) ) { putEvent2FS( FSL_DETECTION_RESTORED ); }; }; }; m_bTargetOverflow = MAX_TARGET_ACCEPTED_AT_ONCE; }; };#else
if( !m_bRecordIsActive && m_bRecordIsSaving ) { if( m_pSaveThreadHandler == NULL ){ int RetVal = pthread_create( &m_pSaveThreadHandler, NULL, t_fSavePictures_Proc, ( void * ) this ); if( RetVal == -1 ) { LOG_ERROR("File Savier Thread Creation Faled."); m_pSaveThreadHandler = NULL; } else { if( !m_bEventRegistered ){ #ifdef USE_TIME_LAPSE_RECORDER
sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER ); #else
#ifdef ASSEMBLE_MP4
if( m_bAssembleMP4 ) { sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER ); } else { sprintf( Buffer, "%s_%02d_%%03d.jpg", m_sSeriesName, m_nAlarmCnt ); }; #else
sprintf( Buffer, "%s_%02d_%%03d.jpg", m_sSeriesName, m_nAlarmCnt ); #endif
#endif // USE_TIME_LAPSE_RECORDER
putEvent2FS( FSL_OPERATOR_ALARM, 0,0,0,0,0,true, Buffer ); m_bEventRegistered = true; }; }; }; }; if(!m_bRecordIsSaving && m_pSaveThreadHandler!= NULL ){ // Reap thread crete event
pthread_join( m_pSaveThreadHandler, NULL ); m_pSaveThreadHandler = NULL; };#endif // USE_TIME_LAPSE_RECORDER
if( m_nScheduledTaskCounter ){ m_nScheduledTaskCounter--; if(m_nScheduledTaskCounter == 0) { switch( m_nScheduledTaskID ){ case ST_CHECK_STATUS: LOG("SCHEDULED STATUS"); if( m_pProtocol->requestStatus() ) m_nScheduledTaskID = ST_NO_TASK; break; case ST_CHECK_SETTINGS: LOG("SCHEDULED SETTTNGS"); if( m_pProtocol->requestSettings() ) m_nScheduledTaskID = ST_NO_TASK; break; case ST_ISSUE_SOFT_RESET: LOG("SCHEDULED STUDY/SOFT RESET"); if( m_pProtocol->sendSoftReset() ) m_nScheduledTaskID = ST_NO_TASK; break; case ST_TURN_OFF_LED: LOG("SCHEDULED ST_TURN_OFF_LED"); if( m_pProtocol->sendLED( false ) ) m_nScheduledTaskID = ST_NO_TASK; break; case ST_SEND_SETTINGS: LOG("SCHEDULED ST_SEND_SETTINGS"); if( m_pProtocol->sendSettings( m_nDetectorSST, m_nDetectorOffset, m_fDetectorLayerDistance, m_fDetectorMinSpeed ) ) m_nScheduledTaskID = ST_NO_TASK; m_nScheduledTaskCounter = 200; // 1 = 10 mlsec
m_nScheduledTaskID = ST_SEND_WMODE; break; case ST_SEND_WMODE: if( m_pProtocol->sendMode( m_bDetectorArmed ) ) m_nScheduledTaskID = ST_NO_TASK; break; }; }; }; SLEEP( SLEEP_KERNEL_INTERVAL ); };};
void Kernel::pushFrame( cv::Mat *Frame, void *src ){ if( src == NULL) { return; }; if( m_nFrameCounter == 0 ) m_lStartTS = getMilliseconds(); m_nFrameCounter++; for( int index = 0; index < m_nVideoInputs; index++ ) { if( src == m_pVideoInputs[index] ) { Frame->copyTo((*m_pVideoFrame[index])); m_nCLoopMask &= ~(1<<index); break; }; }; //If we get all frames;
if( m_pFinalFrame!= NULL ) { if( ( m_nCLoopMask & m_nCLoopCheck ) == 0 ){ int cx,cy; cx=0; cy=15; m_pMatProtector->lock(); //rectangle( (*m_pFinalFrame), Point(0,0), Point(m_nWidth, m_nHeight), Scalar(0,0,0), CV_FILLED );
for( int index = 0; index < m_nVideoInputs; index++ ){ if(m_bHorizontal){ m_pVideoFrame[index]->copyTo((*m_pFinalFrame)(cv::Rect(cx,cy, m_pVideoFrame[index]->cols, m_pVideoFrame[index]->rows ))); cx+=m_pVideoFrame[index]->cols; }else{ m_pVideoFrame[index]->copyTo((*m_pFinalFrame)(cv::Rect(cx,cy, m_pVideoFrame[index]->cols, m_pVideoFrame[index]->rows ))); cy+=m_pVideoFrame[index]->rows; }; }; int X, W = 260; X = ( m_pFinalFrame->cols - W ) >> 1; //rectangle( (*m_pFinalFrame), Point( X, 10 ), Point( X+W, 25 ), Scalar( 0, 0, 0 ), CV_FILLED );
rectangle( (*m_pFinalFrame), Point( 0, 0 ), Point( m_pFinalFrame->cols, 15 ), Scalar( 0, 0, 0 ), CV_FILLED ); long long aMilliseconds = getMilliseconds(); long long tMilliseconds; float fps = 0.f;
tMilliseconds = aMilliseconds - m_lStartTS; if( tMilliseconds > 0 && m_nVideoInputs > 0 ){ fps = ((float)( m_nFrameCounter / m_nVideoInputs ) * 1000.f ) / (float)tMilliseconds; }; char DateStr[200]; sprintf( DateStr, "TS:%010lu", tMilliseconds ); putText( (*m_pFinalFrame), DateStr, Point( X, 12 ), FONT_HERSHEY_SIMPLEX, .4, Scalar( 0, 255, 255 ), 1 ); sprintf( DateStr, "FID:%06lu", m_nFrameCounter ); putText( (*m_pFinalFrame), DateStr, Point( X+110, 12 ), FONT_HERSHEY_SIMPLEX, .4, Scalar( 0, 255, 0 ), 1 ); sprintf( DateStr, "FPS:%03.2f", fps ); putText( (*m_pFinalFrame), DateStr, Point( X+190, 12 ), FONT_HERSHEY_SIMPLEX, .4, Scalar( 255, 0, 255 ), 1 );#ifdef AMIR_MODE
sprintf( DateStr, "Range:%d", m_nCurrentRange ); putText( (*m_pFinalFrame), DateStr, Point( 10, 12 ), FONT_HERSHEY_SIMPLEX, .4, Scalar( 255, 255, 0 ), 1 );#endif
if( m_bRecordIsActive ){ rectangle( (*m_pFinalFrame), Point( X+260, 3 ), Point( X+275, 12 ), Scalar( 0, 0, 255 ), CV_FILLED );#ifndef USE_TIME_LAPSE_RECORDER
if( m_nRecordCounter ) { m_pFinalFrame->copyTo((*m_pVidRecFrame[--m_nRecordCounter])); } else { m_bRecordIsActive = false; };#endif
} else { rectangle( (*m_pFinalFrame), Point( X+260, 3 ), Point( X+275, 12 ), Scalar( 0, 255, 0 ), CV_FILLED );#ifndef USE_TIME_LAPSE_RECORDER
if( m_bRecordIsSaving == false ) { cv::Mat * tMat; if( m_rPrerecord.count >= MAX_RECORD_FRAMES ){ tMat = m_rPrerecord.pop(); delete( tMat ); }; tMat = new cv::Mat(); *tMat = m_pFinalFrame->clone(); m_rPrerecord.add( tMat ); };#endif
};#ifdef USE_TIME_LAPSE_RECORDER
//if( m_bAssembleMP4 ) {
m_pVideoOutput->pushFrame( m_pFinalFrame ); //};
#endif
m_pMatProtector->unlock(); // Assemble Final Frame;
m_nCLoopMask = 0xFFFFFFFF; }; };};
bool Kernel::copyFrame( cv::Mat &Frame ){ if( m_pFinalFrame== NULL ) return false; m_pMatProtector->lock(); if( m_pFinalFrame== NULL ) return false; m_pFinalFrame->copyTo(Frame); m_pMatProtector->unlock(); return true;};
//https://www.pointsinfocus.com/tools/depth-of-field-and-equivalent-lens-calculator/#{%22c%22:[{%22f%22:9,%22av%22:%222%22,%22fl%22:11,%22d%22:50000,%22cm%22:%220%22}],%22m%22:1}
bool Kernel::reset( bool FullRestart ){ if( FullRestart ) { //Shutting down Motors and Hardware
//Starting up Motors and Hardware
shutdown( true ); } else { // Reset Components
//m_pHardwareManager->reset( true );
}; return true;};
bool Kernel::shutdown( bool Restart ){ if( Restart ) { putEvent2FS( FSL_SYSTEM_RESTARTED ); LOG("Restarting the Kernel by SHUTDOWN!"); m_bExitCause = 1; } else { putEvent2FS( FSL_SYSTEM_SHUTDOWN ); LOG("Shutting down the Kernel by SHUTDOWN!"); m_bExitCause = 0; } postStatus2FS( false ); setStatus( TH_ENDED ); return true;};
void Kernel::updateStatus( bool ArmMode, uint8_t nCount, uint16_t *bgArray ){ if( nCount > MAX_LRF_SENSORS ) return; if( bgArray == NULL ) return; m_bDetectorArmed = ArmMode; LOG("updateStatus"); for( uint8_t index = 0; index < nCount; index++ ) { m_nLRF_Background[index] = bgArray[index];#ifdef DEBUG
char tBuff[16]; sprintf( tBuff, "BG[%d]=%d", index, m_nLRF_Background[index] ); LOG( tBuff );#endif
}; postStatus2FS( true );};
void Kernel::updateSettings( uint16_t offset, uint16_t min_time, uint8_t sst, uint32_t ol_status, uint32_t ok_status ){ LOG("updateSettings"); m_nDetectorOffset = offset; m_fDetectorMinSpeed = m_fDetectorLayerDistance * min_time * 0.000001f; m_nDetectorSST = sst; m_nCOnlineMask = ol_status; m_nCOKMask = ok_status;#ifdef DEBUG
char tBuff[128]; sprintf( tBuff, "OS:%d MS:%.3f SST:%d OM: 0x%02H OK: 0x%02H", m_nDetectorOffset, m_fDetectorMinSpeed, m_nDetectorSST, m_nCOnlineMask, m_nCOKMask ); LOG( tBuff );#endif
postStatus2FS( true );};
void Kernel::updateTarget( uint8_t LRF, uint8_t Range ){ // In case handaling all targets at once - see handleTargets
#ifndef MULTI_TARGETS
char Buffer[1024]; LOG("updateTarget"); #ifdef NO_GPIO_HANDLER
alarm( true ); #endif
m_bEventRegistered = true; #ifdef USE_TIME_LAPSE_RECORDER
sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER ); #else
#ifdef ASSEMBLE_MP4
if( m_bAssembleMP4 ) { sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER ); } else { sprintf( Buffer, "%s_%02d_%%03d.jpg", m_sSeriesName, m_nAlarmCnt ); }; #else
sprintf( Buffer, "%s_%02d_%%03d.jpg", m_sSeriesName, m_nAlarmCnt ); #endif
#endif // USE_TIME_LAPSE_RECORDER
putEvent2FS( FSL_ALARM, LRF, Range, 0, 0, 0, true, Buffer ); LOG("Done");#endif // MULTI_TARGETS
};
void Kernel::handleTargets( uint32_t tMask, uint8_t cnt, uint8_t *tRanges ){ if( cnt == 0 ) return; if( tRanges == NULL ) return; if( m_bTargetOverflow ){ m_bTargetOverflow --; if( !m_bTargetOverflow ) { if( m_pProtocol->sendMode( false ) ) { putEvent2FS( FSL_TARGET_OVERFLOW ); }; }; } else return;#ifdef MULTI_TARGETS
char Buffer[1024]; #ifdef DEBUG
LOG("handleTargets"); #endif
#ifdef NO_GPIO_HANDLER
alarm( true ); #endif
#ifdef USE_TIME_LAPSE_RECORDER
sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER ); #else
#ifdef ASSEMBLE_MP4
if( m_bAssembleMP4 ) { sprintf( Buffer, "%s_%02d.%s", m_sSeriesName, m_nAlarmCnt, VIDEO_OUTPUT_CONTAINER ); } else { sprintf( Buffer, "%s_%02d_%%03d.jpg", m_sSeriesName, m_nAlarmCnt ); }; #else
sprintf( Buffer, "%s_%02d_%%03d.jpg", m_sSeriesName, m_nAlarmCnt ); #endif
#endif // USE_TIME_LAPSE_RECORDER
switch( m_nConvertType ) { case 0:{ uint16_t x1, x2, sum; float r1, r2; x1 = tMask & 0x1FF; // 9 bit mask
x2 = ( tMask >> 9 ) & 0x1FF; sum = 0; for( uint8_t index = 0; index < cnt; index++ ) { sum += tRanges[index]; }; if( cnt ) { r1 = ( m_fDetectorMaxDistance /8.f ) * ( 1.f + ((float)sum) / ((float)cnt)); } else r1 =0; r2 = 0; putEvent2FS( FSL_ALARM, x1, r1, x2, r2, 0, true, Buffer ); break; }; case 1:{ uint16_t x1, x2, sum; float r1, r2; x1 = tMask & 0x1FF; // 9 bit mask
x2 = ( tMask >> 9 ) & 0x1FF; sum = 0; for( uint8_t index = 0; index < cnt; index++ ) { sum += tRanges[index]; }; if( cnt ) { r1 = ( m_fDetectorMaxDistance * (float)sum) / (float)cnt; } else r1 = 0; r2 = 0; putEvent2FS( FSL_ALARM, x1, r1, x2, r2, 0, true, Buffer ); break; }; case 2:{ uint16_t x1, x2, sum; float r1, r2; x1 = tMask & 0x1FF; // 9 bit mask
x2 = ( tMask >> 9 ) & 0x1FF; sum = 0; int idx = 0; for( uint8_t index = 0; index < MAX_LRF_SENSORS; index++ ) { if( tMask & ( 0x00000001 << index ) ) { sum += ( m_fDetectorMaxDistance / m_nLRF_Background[index] ) * tRanges[idx]; idx++; }; }; if( cnt ) { r1 = ((float)sum) / ((float)cnt); } else r1 = 0; r2 = 0; putEvent2FS( FSL_ALARM, x1, r1, x2, r2, 0, true, Buffer ); break; }; case 3:{ uint16_t x1, x2, sum; float r1, r2; x1 = tMask & 0x1FF; // 9 bit mask
x2 = ( tMask >> 9 ) & 0x1FF; sum = 0; r1 = tRanges[0]; r2 = 0; putEvent2FS( FSL_ALARM, x1, r1, x2, r2, 0, true, Buffer ); break; }; }; m_bEventRegistered = true; #ifdef DEBUG
LOG("Done"); #endif
#endif // MULTI_TARGETS
};
#undef LOG
#undef LOG_ERROR
|