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.
 
 
 
 
 
 

393 lines
12 KiB

#include "MEKOROT_Kernel.h"
#define LOG( X ) m_pLog->Log( "KRNL", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "KRNL", this, _LOG_ERROR, X )
bool getconchar(KEY_EVENT_RECORD& krec) {
DWORD cc, NOF;
INPUT_RECORD irec;
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
if (h == NULL) {
return false; // console not found
};
if (!GetNumberOfConsoleInputEvents(h, &NOF)) return false;
if( NOF ) for(int i=0;i<NOF; i++)
{
ReadConsoleInput(h, &irec, 1, &cc);
if (irec.EventType == KEY_EVENT
&& ((KEY_EVENT_RECORD&)irec.Event).bKeyDown
)//&& ! ((KEY_EVENT_RECORD&)irec.Event).wRepeatCount )
{
krec = (KEY_EVENT_RECORD&)irec.Event;
return true;
}
}
return false; //future ????
};
mekorotKernel::mekorotKernel(config_setting_t* ptr) : basicEngine(), basicThread(), basicMediaDestination(), protocolLGS_CLIENT() {
m_pLog = basicLogCollection::getInstance();
m_pSettings = basicSettingsProvider::getInstance()->getSubNode(ptr, (CSTR)"KERNEL");
m_bStage = MEKOROT_KRNL_STG_INIT;
m_pSerial = NULL;
m_pSerialDevice[0] = (char)0;
m_nSerialSpeed = 19200;
m_bDeviceID[0] = m_bDeviceID[1] = m_bDeviceID[2] = -1;
m_bDeviceFlags[0] = m_bDeviceFlags[1] = m_bDeviceFlags[2] = false;
m_bDeviceTO[0] = m_bDeviceTO[1] = m_bDeviceTO[2] = 0;
m_bDeviceAlarms[0] = m_bDeviceAlarms[1] = m_bDeviceAlarms[2] = false;
m_pVideoOutput = NULL;
m_pStorageBase = NULL;
m_nVideoInputs = 0;
m_nWidth = 320;
m_nHeight = 240;
m_bHorizontal = true;
m_nCLoopMask = 0xFFFFFFFF;
m_nCLoopCheck = 0;
};
mekorotKernel::~mekorotKernel() {
if (m_pSerial != NULL) delete( m_pSerial );
};
bool mekorotKernel::init(void){
config_setting_t *pSetting;
pSetting = config_setting_get_member(m_pSettings, MEKOROT_KRNL_SERIAL_LINE_TAG);
if (pSetting != NULL) {
sprintf(m_pSerialDevice, "%s", config_setting_get_string(pSetting));
} else {
sprintf(m_pSerialDevice, "\\\\.\\COM2");
pSetting = config_setting_add(m_pSettings, MEKOROT_KRNL_SERIAL_LINE_TAG, CONFIG_TYPE_STRING);
basicSettingsProvider::getInstance()->compromise();
if (pSetting == NULL) {
LOG_ERROR("Failure while adding setting!");
return false;
};
config_setting_set_string(pSetting, m_pSerialDevice);
};
pSetting = config_setting_get_member(m_pSettings, MEKOROT_KRNL_SERIAL_SPEED_TAG);
if (pSetting != NULL) {
m_nSerialSpeed = config_setting_get_int(pSetting);
} else {
pSetting = config_setting_add(m_pSettings, MEKOROT_KRNL_SERIAL_SPEED_TAG, CONFIG_TYPE_INT);
basicSettingsProvider::getInstance()->compromise();
if (pSetting == NULL) {
LOG_ERROR("Failure while adding setting!");
return false;
};
config_setting_set_int(pSetting, m_nSerialSpeed );
};
pSetting = config_setting_get_member(m_pSettings, MEKOROT_KRNL_DEVICE_0_ID_TAG);
if (pSetting != NULL) {
m_bDeviceID[0] = config_setting_get_int(pSetting);
} else {
pSetting = config_setting_add(m_pSettings, MEKOROT_KRNL_DEVICE_0_ID_TAG, CONFIG_TYPE_INT);
basicSettingsProvider::getInstance()->compromise();
if (pSetting == NULL) {
LOG_ERROR("Failure while adding setting!");
return false;
};
config_setting_set_int(pSetting, m_bDeviceID[0]);
};
pSetting = config_setting_get_member(m_pSettings, MEKOROT_KRNL_DEVICE_1_ID_TAG);
if (pSetting != NULL) {
m_bDeviceID[1] = config_setting_get_int(pSetting);
} else {
pSetting = config_setting_add(m_pSettings, MEKOROT_KRNL_DEVICE_1_ID_TAG, CONFIG_TYPE_INT);
basicSettingsProvider::getInstance()->compromise();
if (pSetting == NULL) {
LOG_ERROR("Failure while adding setting!");
return false;
};
config_setting_set_int(pSetting, m_bDeviceID[1]);
};
pSetting = config_setting_get_member(m_pSettings, MEKOROT_KRNL_DEVICE_2_ID_TAG);
if (pSetting != NULL) {
m_bDeviceID[2] = config_setting_get_int(pSetting);
} else {
pSetting = config_setting_add(m_pSettings, MEKOROT_KRNL_DEVICE_2_ID_TAG, CONFIG_TYPE_INT);
basicSettingsProvider::getInstance()->compromise();
if (pSetting == NULL) {
LOG_ERROR("Failure while adding setting!");
return false;
};
config_setting_set_int(pSetting, m_bDeviceID[2]);
};
basicSettingsProvider::getInstance()->validate();
m_nInitialFSID = (uint32_t)time(NULL);
/* Storage Init */
m_pStorageProvider = storageProvider::getInstance( m_pStorageBase );
m_pVideoOutput = new lapsedVideoRecorder();
if (m_pVideoOutput == NULL) {
LOG_ERROR("TimeLapse Recorder on create Failure!");
// m_bAssembleMP4 = false;
return false;
} else {
m_pVideoOutput->setFrameLapse( MAX_RECORD_FRAMES );
};
pSetting = basicSettingsProvider::getInstance()->getSubNode(m_pSettings, KNL_SETTINGS_VIDEO_SECTION);
int mCount = config_setting_length(pSetting);
if (mCount) {
char tBuffer[256];
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++) {
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.");
};
};
};
};
m_bStage = MEKOROT_KRNL_STG_CONNECT;
return basicThread::init();
};
void mekorotKernel::m_fThreadProc(void){
m_bThreadStatus = TH_WORKING;
while (m_bThreadStatus < TH_ENDED) {
KEY_EVENT_RECORD krec;
if( getconchar(krec) ){
//Parse KeyBoard Input
switch (krec.wVirtualKeyCode) {
case 13:
case 32:
case 82:{
// 'r' Or 'spc' or 'etr' = Start/Stop video record
break;
};
case 81:
case 75: {
// 'q' Or 'k' - Exit Application
LOG("Exit by Operator Request!");
m_bThreadStatus = TH_ENDED;
continue;
};
default: {
char tBuffer[128];
sprintf(tBuffer, "Key Stroke Code: %d", krec.wVirtualKeyCode);
LOG(tBuffer);
break;
};
};
};
switch( m_bStage ) {
case MEKOROT_KRNL_STG_CONNECT:{
if (m_pSerial == NULL) {
m_pSerial = new serialPort(m_pSerialDevice, m_nSerialSpeed);
if (m_pSerial != NULL) {
setTransport( m_pSerial );
m_bStage = MEKOROT_KRNL_STG_PING_DEVICES;
};
};
break;
};
case MEKOROT_KRNL_STG_PING_DEVICES: {
bool LB = true;
for (uint8_t index = 0; index < 3; index++) {
if (m_bDeviceID[index] != -1 && !m_bDeviceFlags[index]){
LB = false;
if (setDeviceID(m_bDeviceID[index])) {
if (!getState()) {
LOG_ERROR("Get STATE FAILURE!");
};
} else {
LOG_ERROR("Set DevID FAILURE!");
};
m_bDeviceTO[index] = getMilliseconds();
};
};
if (LB) {
m_bStage = MEKOROT_KRNL_STG_WORKING;
} else {
SLEEP(KERNEL_SLOW_SLEEP_TIME); // SLOW wait responce
};
break;
};
case MEKOROT_KRNL_STG_WORKING: {
unsigned long long cTS;
cTS = getMilliseconds();
bool AllOffLine = true;
for (uint8_t index = 0; index < 3; index++) {
if (!m_bDeviceFlags[index]) continue;
AllOffLine = false;
if ((cTS - m_bDeviceTO[index]) > 3000) {
LOG_ERROR("Device TO Detected!");
m_bDeviceFlags[index] = false;
m_bStage = MEKOROT_KRNL_STG_PING_DEVICES;
continue;
};
if (setDeviceID(m_bDeviceID[index])) {
if (!getState2()) {
LOG_ERROR("Get STATE2 FAILURE!");
m_bDeviceFlags[index] = false;
m_bStage = MEKOROT_KRNL_STG_PING_DEVICES;
};
} else {
LOG_ERROR("Set DevID FAILURE!");
m_bDeviceFlags[index] = false;
m_bStage = MEKOROT_KRNL_STG_PING_DEVICES;
continue;
};
};
if (AllOffLine) {
m_bStage = MEKOROT_KRNL_STG_PING_DEVICES;
continue;
} else {
SLEEP(KERNEL_SLOW_SLEEP_TIME);
};
break;
};
};
};
};
bool mekorotKernel::packetHandler(protocolPackage* Package) {
uint8_t cDeviceID;
uint8_t cDeviceIDX = 255;
if( Package == NULL ) return true;
if( Package->m_nSize < 4 ) return true;
cDeviceID = Package->m_pData[2];
for (uint8_t idx = 0; idx < 3; idx++) {
if (cDeviceID == m_bDeviceID[idx]) cDeviceIDX = idx;
};
if (cDeviceIDX == 255) {
LOG_ERROR("Package from not registered device!");
return true;
};
m_bDeviceTO[cDeviceIDX] = getMilliseconds();
if (!m_bDeviceFlags[cDeviceIDX]) {
char tBuff[64];
sprintf(tBuff, "Device %d Became OnLine", cDeviceIDX);
LOG(tBuff);
m_bDeviceFlags[cDeviceIDX] = true;
};
switch (cDeviceIDX) {
case 0: {
LOG("Left Vertical");
break;
};
case 1: {
LOG("Center Horisontal");
break;
};
case 2: {
LOG("Right Vertical");
break;
};
};
return true;// to erase data
};
void mekorotKernel::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);
if (m_bRecordIsActive) {
rectangle((*m_pFinalFrame), Point(X + 260, 3), Point(X + 275, 12), Scalar(0, 0, 255), cv::FILLED);
}
else {
rectangle((*m_pFinalFrame), Point(X + 260, 3), Point(X + 275, 12), Scalar(0, 255, 0), cv::FILLED);
};
//if( m_bAssembleMP4 ) {
m_pVideoOutput->pushFrame(m_pFinalFrame);
//};
m_pMatProtector->unlock();
// Assemble Final Frame;
m_nCLoopMask = 0xFFFFFFFF;
};
};
};
bool mekorotKernel::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;
};
#undef LOG
#undef LOG_ERROR