diff --git a/Projects/MEKOROT/MEKOROT_Kernel.cpp b/Projects/MEKOROT/MEKOROT_Kernel.cpp index 0fdba262..e58826c5 100644 --- a/Projects/MEKOROT/MEKOROT_Kernel.cpp +++ b/Projects/MEKOROT/MEKOROT_Kernel.cpp @@ -31,12 +31,14 @@ mekorotKernel::mekorotKernel(config_setting_t* ptr) : basicEngine(), basicThread m_pLog = basicLogCollection::getInstance(); m_pSettings = basicSettingsProvider::getInstance()->getSubNode(ptr, (CSTR)"KERNEL"); m_bStage = MEKOROT_KRNL_STG_INIT; + m_nOperation = MEKOROT_KRNL_OP_PING_DEV0; 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_bDeviceTO[0] = m_bDeviceTO[1] = m_bDeviceTO[2] = 0; + m_bOperationTO = 0; m_bDeviceAlarms[0] = m_bDeviceAlarms[1] = m_bDeviceAlarms[2] = false; m_pVideoOutput = NULL; m_pStorageBase = NULL; @@ -433,83 +435,155 @@ void mekorotKernel::m_fThreadProc(void){ 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; + 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 ){ - if (!m_bDeviceFlags[index]) { - LB = false; - if (setDeviceID(m_bDeviceID[index])) { - if (!getVoltage()) { - LOG_ERROR("Get Voltage FAILURE!"); + if (m_nOperation & MEKOROT_KRNL_OP_OP_MATRIX) { + switch (m_nOperation) { + case MEKOROT_KRNL_OP_PING_DEV0: { + if (m_bDeviceID[0] != -1) { + if (setDeviceID(m_bDeviceID[0])) { + if (!getVoltage()) { + LOG_ERROR("Get Voltage FAILURE!"); + } else { + m_bOperationTO = getMilliseconds(); + m_nOperation = MEKOROT_KRNL_OP_PONG_DEV0; +#ifdef DEBUG + LOG("Ping Device 0"); +#endif + }; } else { - SLEEP(50); + LOG_ERROR("Set DevID FAILURE!"); + }; + }; + break; + }; + case MEKOROT_KRNL_OP_PING_DEV1: { + if (m_bDeviceID[1] != -1) { + if (setDeviceID(m_bDeviceID[1])) { + if (!getVoltage()) { + LOG_ERROR("Get Voltage FAILURE!"); + } else { + m_bOperationTO = getMilliseconds(); + m_nOperation = MEKOROT_KRNL_OP_PONG_DEV1; #ifdef DEBUG - char tBuf[128]; - sprintf(tBuf, "Ping Device %d", index); - LOG(tBuf); + LOG("Ping Device 1"); #endif + }; + } else { + LOG_ERROR("Set DevID FAILURE!"); }; - } else { - LOG_ERROR("Set DevID FAILURE!"); }; + break; + }; + case MEKOROT_KRNL_OP_PING_DEV2: { + if (m_bDeviceID[2] != -1) { + if (setDeviceID(m_bDeviceID[2])) { + if (!getVoltage()) { + LOG_ERROR("Get Voltage FAILURE!"); + } else { + m_bOperationTO = getMilliseconds(); + m_nOperation = MEKOROT_KRNL_OP_PONG_DEV2; +#ifdef DEBUG + LOG("Ping Device 2"); +#endif + }; + } else { + LOG_ERROR("Set DevID FAILURE!"); + }; + }; + break; }; - m_bDeviceTO[index] = getMilliseconds(); }; - }; - if (LB) { - m_bStage = MEKOROT_KRNL_STG_WORKING; } else { - SLEEP(KERNEL_SLOW_SLEEP_TIME); // SLOW wait responce + unsigned long long cTS; + cTS = getMilliseconds(); + if ((cTS - m_bOperationTO) > 1000) { + LOG_ERROR("FAILED TO COMMUNICATE WITH SENSOR"); + }; }; 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]) > 5000) { - char tBuf[128]; - sprintf(tBuf, "Device %d Timeout!", index ); - LOG_ERROR(tBuf); - m_bDeviceFlags[index] = false; - m_bStage = MEKOROT_KRNL_STG_PING_DEVICES; - LOG_ERROR("Going to Ping Mode!"); - continue; - }; - if (setDeviceID(m_bDeviceID[index])) { - if (!getState2()) { - LOG_ERROR("Get STATE2 FAILURE!"); - m_bDeviceFlags[index] = false; + if (m_nOperation & MEKOROT_KRNL_OP_OP_MATRIX) { + switch (m_nOperation) { + case MEKOROT_KRNL_OP_QSTG_DEV0: { + if (m_bDeviceFlags[0]) { + if (setDeviceID(m_bDeviceID[0])) { + if (!getState2()) { + LOG_ERROR("Get STATE2 FAILURE!"); + m_bDeviceFlags[0] = false; + m_bStage = MEKOROT_KRNL_STG_PING_DEVICES; + LOG_ERROR("Going to Ping Mode!"); + }; + } else { + LOG_ERROR("Set DevID FAILURE!"); + m_bDeviceFlags[0] = false; + m_bStage = MEKOROT_KRNL_STG_PING_DEVICES; + LOG_ERROR("Going to Ping Mode!"); + continue; + }; + break; + }; + // In Case Device is Offline - Skip to next Device + }; + case MEKOROT_KRNL_OP_QSTG_DEV1: { + if (m_bDeviceFlags[1]) { + if (setDeviceID(m_bDeviceID[1])) { + if (!getState2()) { + LOG_ERROR("Get STATE2 FAILURE!"); + m_bDeviceFlags[1] = false; + m_bStage = MEKOROT_KRNL_STG_PING_DEVICES; + LOG_ERROR("Going to Ping Mode!"); + }; + } else { + LOG_ERROR("Set DevID FAILURE!"); + m_bDeviceFlags[1] = false; + m_bStage = MEKOROT_KRNL_STG_PING_DEVICES; + LOG_ERROR("Going to Ping Mode!"); + continue; + }; + break; + }; + // In Case Device is Offline - Skip to next Device + }; + case MEKOROT_KRNL_OP_QSTG_DEV2: { + if (m_bDeviceFlags[2]) { + if (setDeviceID(m_bDeviceID[2])) { + if (!getState2()) { + LOG_ERROR("Get STATE2 FAILURE!"); + m_bDeviceFlags[2] = false; + m_bStage = MEKOROT_KRNL_STG_PING_DEVICES; + LOG_ERROR("Going to Ping Mode!"); + }; + } else { + LOG_ERROR("Set DevID FAILURE!"); + m_bDeviceFlags[2] = false; + m_bStage = MEKOROT_KRNL_STG_PING_DEVICES; + LOG_ERROR("Going to Ping Mode!"); + continue; + }; + break; + }; + // Loop Query + m_nOperation = MEKOROT_KRNL_OP_QSTG_DEV0; + }; + default: { m_bStage = MEKOROT_KRNL_STG_PING_DEVICES; - LOG_ERROR("Going to Ping Mode!"); - continue; - } else { - SLEEP(250); + m_nOperation = MEKOROT_KRNL_OP_PING_DEV0; + LOG_ERROR("Conveyor Error! Falling down to Pinging sensors ..."); + break; }; - } else { - LOG_ERROR("Set DevID FAILURE!"); - m_bDeviceFlags[index] = false; - m_bStage = MEKOROT_KRNL_STG_PING_DEVICES; - LOG_ERROR("Going to Ping Mode!"); - continue; }; - }; - if (AllOffLine) { - m_bStage = MEKOROT_KRNL_STG_PING_DEVICES; - continue; } else { - SLEEP(KERNEL_SLOW_SLEEP_TIME); + unsigned long long cTS; + cTS = getMilliseconds(); }; + SLEEP(KERNEL_SLOW_SLEEP_TIME); break; }; }; @@ -529,7 +603,7 @@ bool mekorotKernel::packetHandler(protocolPackage* Package) { LOG_ERROR("Package from not registered device!"); return true; }; - m_bDeviceTO[cDeviceIDX] = getMilliseconds(); + //m_bDeviceTO[cDeviceIDX] = getMilliseconds(); if (!m_bDeviceFlags[cDeviceIDX]) { #ifdef DEBUG char tBuff[64]; diff --git a/Projects/MEKOROT/MEKOROT_Kernel.h b/Projects/MEKOROT/MEKOROT_Kernel.h index 029c9f21..c16ac1df 100644 --- a/Projects/MEKOROT/MEKOROT_Kernel.h +++ b/Projects/MEKOROT/MEKOROT_Kernel.h @@ -44,6 +44,22 @@ #define MEKOROT_KRNL_STG_PING_DEVICES 2 #define MEKOROT_KRNL_STG_WORKING 3 +#define MEKOROT_KRNL_OP_PING_DEV0 1 +#define MEKOROT_KRNL_OP_PONG_DEV0 2 +#define MEKOROT_KRNL_OP_PING_DEV1 4 +#define MEKOROT_KRNL_OP_PONG_DEV1 8 +#define MEKOROT_KRNL_OP_PING_DEV2 16 +#define MEKOROT_KRNL_OP_PONG_DEV2 32 +#define MEKOROT_KRNL_OP_QSTG_DEV0 64 +#define MEKOROT_KRNL_OP_GSTG_DEV0 128 +#define MEKOROT_KRNL_OP_QSTG_DEV1 256 +#define MEKOROT_KRNL_OP_GSTG_DEV1 512 +#define MEKOROT_KRNL_OP_QSTG_DEV2 1024 +#define MEKOROT_KRNL_OP_GSTG_DEV2 2048 + +#define MEKOROT_KRNL_OP_TO_MATRIX 0xAAA +#define MEKOROT_KRNL_OP_OP_MATRIX 0x555 + #define MEKOROT_DEV_LEFT 0 #define MEKOROT_DEV_CENTER 1 #define MEKOROT_DEV_RIGHT 2 @@ -60,10 +76,12 @@ class mekorotKernel: public basicEngine, public basicThread, public basicMediaDe config_setting_t *m_pSettings; serialPort *m_pSerial; uint8_t m_bStage; + uint16_t m_nOperation; char m_pSerialDevice[128]; uint32_t m_nSerialSpeed; int8_t m_bDeviceID[3]; - unsigned long long m_bDeviceTO[3]; + //unsigned long long m_bDeviceTO[3]; + unsigned long long m_bOperationTO; bool m_bDeviceFlags[3]; bool m_bDeviceAlarms[3];