123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- #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;
- }
|