#include "stdafx.h" #include "DiosSMachineV3.h" #include "ConditionEvent.h" //----------------------------- DiosStMEvt::DiosStMEvt() { m_pEvt = new ResDataObject(); m_pInfo = new ResDataObject(); m_bTriggered = false; m_bActived = true; m_pConditions = nullptr; m_hTriggered = CreateEvent(NULL, TRUE, FALSE, NULL); //一旦触发,需要手动Reset } DiosStMEvt::DiosStMEvt(const DiosStMEvt& tValue) { m_pEvt = new ResDataObject(); m_pInfo = new ResDataObject(); (*m_pEvt) = (*tValue.m_pEvt); (*m_pInfo) = (*tValue.m_pInfo); m_bTriggered = (tValue.m_bTriggered); m_bActived = true; m_hTriggered = CreateEvent(NULL, TRUE, FALSE, NULL); //一旦触发,需要手动Reset } DiosStMEvt::~DiosStMEvt() { delete m_pEvt; m_pEvt = NULL; delete m_pInfo; m_pInfo = NULL; CloseHandle(m_hTriggered); } bool DiosStMEvt::SetEvt(const char* pKey, INT Val, const char* pInfo) { m_pEvt->clear(); m_pInfo->clear(); if (pKey) { m_pEvt->add(pKey, Val); m_pInfo->add(pKey, Val); } else { //m_pEvt->add("", Val); //m_pInfo->add("", Val); return true; } if (pInfo) { //有Info的,要进行计算和逻辑判定 m_pInfo->add(DiosStmEvtInfo, pInfo); m_bTriggered = false; } else { //没有Info的,设置即触发 m_pInfo->add(DiosStmEvtInfo, ""); m_bTriggered = true; } return true; } bool DiosStMEvt::IsEmpty() { return (m_pEvt->size() == 0); } bool DiosStMEvt::SetEvt(const char* pKey, const char* pVal, const char* pInfo) { bool ret = true; m_pEvt->clear(); m_pInfo->clear(); if (pKey == NULL) { return true; } if (pVal) { ret = m_pEvt->add(pKey, pVal); m_pInfo->add(pKey, pVal); } else { ret = m_pEvt->add(pKey, ""); m_pInfo->add(pKey, ""); } if (pInfo) { m_bTriggered = false; m_pInfo->add(DiosStmEvtInfo, pInfo); } else { m_bTriggered = true; m_pInfo->add(DiosStmEvtInfo, ""); } return ret; } ResDataObject& DiosStMEvt::GetEvtContext() { return (*m_pEvt); } DiosStMEvt& DiosStMEvt::operator = (const DiosStMEvt& tValue) { if (this != &tValue) { (*m_pEvt) = (*tValue.m_pEvt); (*m_pInfo) = (*tValue.m_pInfo); m_bTriggered = tValue.m_bTriggered; } return (*this); } bool DiosStMEvt::operator == (const DiosStMEvt& Obj) { return (*m_pEvt == (*(Obj.m_pEvt))); } bool DiosStMEvt::operator == (const char* pszEventName) { return string(m_pEvt->GetKey(0)) == string(pszEventName); } const char* DiosStMEvt::encode_s() { return m_pInfo->encode(); } bool DiosStMEvt::decode(const char* pdata) { ResDataObject Obj; bool ret = m_pInfo->decode(pdata); if (ret) { size_t size = m_pInfo->size(); if ((size > 0) && (size <= 2)) { ret = false; string strStmEvtInfo = DiosStmEvtInfo; for (size_t i = 0; i < size; i++) { const char* pKey = (*m_pInfo).GetKey(i); if (strStmEvtInfo == pKey) { continue; } m_pEvt->clear(); m_pEvt->add(pKey, (*m_pInfo)[i]); ret = true; } } else { ret = false; } } if (ret == false) { m_pEvt->clear(); m_pInfo->clear(); } return ret; } //设置事件上下文 bool DiosStMEvt::parse(ResDataObject *evtContext) { if (m_pConditions != nullptr) { delete m_pConditions; } m_pConditions = new ConditionEvent(evtContext); m_pEvt->update(m_pConditions->GetName(), evtContext); return true; } //过滤激活事件,包括Action/Notfiy/External bool DiosStMEvt::Active(const char* pszType, ResDataObject* resContext, const char* pszDevice) { //被禁用,不处理,直接false if (!m_bActived) return false; string strType = pszType; if (m_pConditions != nullptr) { if (m_pConditions->Check(pszType, resContext, pszDevice)) { m_bTriggered = true; SetEvent(m_hTriggered); return true; } } return false; } void DiosStMEvt::Reset() { ResetEvent(m_hTriggered); m_bTriggered = false; } int DiosStMEvt::GetTimeout() { return m_pConditions == nullptr ? 0 : m_pConditions->GetTimeout(); } //----------------------------- DiosStMRouteLine::DiosStMRouteLine() { m_ActiveState = false; m_InRoute = true; m_pEvt = new DiosStMEvt(); m_pSrcRoutePos = new string(); m_pGuard = new string(); m_pDesRoutePos = new string(); m_Timeout = TIMEOUT_TEMP; } DiosStMRouteLine::DiosStMRouteLine(const DiosStMRouteLine& tValue) { m_ActiveState = false; m_InRoute = true; m_pEvt = new DiosStMEvt(); m_pSrcRoutePos = new string(); m_pGuard = new string(); m_pDesRoutePos = new string(); m_Timeout = TIMEOUT_TEMP; if (tValue.m_InRoute) { SetRoute(*(tValue.m_pEvt), tValue.m_pSrcRoutePos->c_str(), tValue.m_pGuard->c_str(), tValue.m_pDesRoutePos->c_str()); } else { SetRoute(*(tValue.m_pEvt), tValue.m_pGuard->c_str(), tValue.m_pDesRoutePos->c_str()); } } DiosStMRouteLine::~DiosStMRouteLine() { delete m_pEvt; delete m_pSrcRoutePos; delete m_pDesRoutePos; delete m_pGuard; } void DiosStMRouteLine::SetRoute(DiosStMEvt& evt, const char* pGuard, const char* pDes) { m_InRoute = true; (*m_pSrcRoutePos) = ""; (*m_pEvt) = evt; if (pGuard) { (*m_pGuard) = pGuard; } else { (*m_pGuard) = ""; } if (pDes) { (*m_pDesRoutePos) = pDes; } else { (*m_pDesRoutePos) = ""; } } void DiosStMRouteLine::SetRoute(DiosStMEvt& evt, const char* pSrc, const char* pGuard, const char* pDes) { m_InRoute = false; (*m_pEvt) = evt; if (pSrc) { (*m_pSrcRoutePos) = pSrc; } else { (*m_pSrcRoutePos) = ""; } if (pGuard) { (*m_pGuard) = pGuard; } else { (*m_pGuard) = ""; } if (pDes) { (*m_pDesRoutePos) = pDes; } else { (*m_pDesRoutePos) = ""; } } const char* DiosStMRouteLine::GetDesName() { return m_pDesRoutePos->c_str(); } const char* DiosStMRouteLine::GetSrcName() { if (m_InRoute == false) { return m_pSrcRoutePos->c_str(); } return NULL; } const char* DiosStMRouteLine::GetGuardName() { return m_pGuard->c_str(); } //true:in,false:out bool DiosStMRouteLine::GetRouteType() { return m_InRoute; } DiosStMRouteLine::operator DiosStMEvt* () { return m_pEvt; } DiosStMRouteLine& DiosStMRouteLine::operator = (const DiosStMRouteLine& tValue) { if (&tValue != this) { if (tValue.m_InRoute) { SetRoute(*(tValue.m_pEvt), tValue.m_pSrcRoutePos->c_str(), tValue.m_pGuard->c_str(), tValue.m_pDesRoutePos->c_str()); } else { SetRoute(*(tValue.m_pEvt), tValue.m_pGuard->c_str(), tValue.m_pDesRoutePos->c_str()); } } return (*this); } void DiosStMRouteLine::Active(bool state, DWORD timeout) { m_ActiveState = state; if (m_ActiveState == false) { m_Timeout = timeout; } else { //get maximum timeperiod if (timeout > m_Timeout) { m_Timeout = timeout; } } } bool DiosStMRouteLine::GetActiveState() { return m_ActiveState; } DWORD DiosStMRouteLine::GetTimeout() { return m_Timeout; }