DiosSMachineBase.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #include "stdafx.h"
  2. #include "DiosSMachineV3.h"
  3. #include "ConditionEvent.h"
  4. //-----------------------------
  5. DiosStMEvt::DiosStMEvt()
  6. {
  7. m_pEvt = new ResDataObject();
  8. m_pInfo = new ResDataObject();
  9. m_bTriggered = false;
  10. m_bActived = true;
  11. m_pConditions = nullptr;
  12. m_hTriggered = CreateEvent(NULL, TRUE, FALSE, NULL); //一旦触发,需要手动Reset
  13. }
  14. DiosStMEvt::DiosStMEvt(const DiosStMEvt& tValue)
  15. {
  16. m_pEvt = new ResDataObject();
  17. m_pInfo = new ResDataObject();
  18. (*m_pEvt) = (*tValue.m_pEvt);
  19. (*m_pInfo) = (*tValue.m_pInfo);
  20. m_bTriggered = (tValue.m_bTriggered);
  21. m_bActived = true;
  22. m_hTriggered = CreateEvent(NULL, TRUE, FALSE, NULL); //一旦触发,需要手动Reset
  23. }
  24. DiosStMEvt::~DiosStMEvt()
  25. {
  26. delete m_pEvt;
  27. m_pEvt = NULL;
  28. delete m_pInfo;
  29. m_pInfo = NULL;
  30. CloseHandle(m_hTriggered);
  31. }
  32. bool DiosStMEvt::SetEvt(const char* pKey, INT Val, const char* pInfo)
  33. {
  34. m_pEvt->clear();
  35. m_pInfo->clear();
  36. if (pKey)
  37. {
  38. m_pEvt->add(pKey, Val);
  39. m_pInfo->add(pKey, Val);
  40. }
  41. else
  42. {
  43. //m_pEvt->add("", Val);
  44. //m_pInfo->add("", Val);
  45. return true;
  46. }
  47. if (pInfo)
  48. {
  49. //有Info的,要进行计算和逻辑判定
  50. m_pInfo->add(DiosStmEvtInfo, pInfo);
  51. m_bTriggered = false;
  52. }
  53. else
  54. {
  55. //没有Info的,设置即触发
  56. m_pInfo->add(DiosStmEvtInfo, "");
  57. m_bTriggered = true;
  58. }
  59. return true;
  60. }
  61. bool DiosStMEvt::IsEmpty()
  62. {
  63. return (m_pEvt->size() == 0);
  64. }
  65. bool DiosStMEvt::SetEvt(const char* pKey, const char* pVal, const char* pInfo)
  66. {
  67. bool ret = true;
  68. m_pEvt->clear();
  69. m_pInfo->clear();
  70. if (pKey == NULL)
  71. {
  72. return true;
  73. }
  74. if (pVal)
  75. {
  76. ret = m_pEvt->add(pKey, pVal);
  77. m_pInfo->add(pKey, pVal);
  78. }
  79. else
  80. {
  81. ret = m_pEvt->add(pKey, "");
  82. m_pInfo->add(pKey, "");
  83. }
  84. if (pInfo)
  85. {
  86. m_bTriggered = false;
  87. m_pInfo->add(DiosStmEvtInfo, pInfo);
  88. }
  89. else
  90. {
  91. m_bTriggered = true;
  92. m_pInfo->add(DiosStmEvtInfo, "");
  93. }
  94. return ret;
  95. }
  96. ResDataObject& DiosStMEvt::GetEvtContext()
  97. {
  98. return (*m_pEvt);
  99. }
  100. DiosStMEvt& DiosStMEvt::operator = (const DiosStMEvt& tValue)
  101. {
  102. if (this != &tValue)
  103. {
  104. (*m_pEvt) = (*tValue.m_pEvt);
  105. (*m_pInfo) = (*tValue.m_pInfo);
  106. m_bTriggered = tValue.m_bTriggered;
  107. }
  108. return (*this);
  109. }
  110. bool DiosStMEvt::operator == (const DiosStMEvt& Obj)
  111. {
  112. return (*m_pEvt == (*(Obj.m_pEvt)));
  113. }
  114. bool DiosStMEvt::operator == (const char* pszEventName)
  115. {
  116. return string(m_pEvt->GetKey(0)) == string(pszEventName);
  117. }
  118. const char* DiosStMEvt::encode_s()
  119. {
  120. return m_pInfo->encode();
  121. }
  122. bool DiosStMEvt::decode(const char* pdata)
  123. {
  124. ResDataObject Obj;
  125. bool ret = m_pInfo->decode(pdata);
  126. if (ret)
  127. {
  128. size_t size = m_pInfo->size();
  129. if ((size > 0) && (size <= 2))
  130. {
  131. ret = false;
  132. string strStmEvtInfo = DiosStmEvtInfo;
  133. for (size_t i = 0; i < size; i++)
  134. {
  135. const char* pKey = (*m_pInfo).GetKey(i);
  136. if (strStmEvtInfo == pKey)
  137. {
  138. continue;
  139. }
  140. m_pEvt->clear();
  141. m_pEvt->add(pKey, (*m_pInfo)[i]);
  142. ret = true;
  143. }
  144. }
  145. else
  146. {
  147. ret = false;
  148. }
  149. }
  150. if (ret == false)
  151. {
  152. m_pEvt->clear();
  153. m_pInfo->clear();
  154. }
  155. return ret;
  156. }
  157. //设置事件上下文
  158. bool DiosStMEvt::parse(ResDataObject *evtContext)
  159. {
  160. if (m_pConditions != nullptr)
  161. {
  162. delete m_pConditions;
  163. }
  164. m_pConditions = new ConditionEvent(evtContext);
  165. m_pEvt->update(m_pConditions->GetName(), evtContext);
  166. return true;
  167. }
  168. //过滤激活事件,包括Action/Notfiy/External
  169. bool DiosStMEvt::Active(const char* pszType, ResDataObject* resContext, const char* pszDevice)
  170. {
  171. //被禁用,不处理,直接false
  172. if (!m_bActived)
  173. return false;
  174. string strType = pszType;
  175. if (m_pConditions != nullptr)
  176. {
  177. if (m_pConditions->Check(pszType, resContext, pszDevice))
  178. {
  179. m_bTriggered = true;
  180. SetEvent(m_hTriggered);
  181. return true;
  182. }
  183. }
  184. return false;
  185. }
  186. void DiosStMEvt::Reset()
  187. {
  188. ResetEvent(m_hTriggered);
  189. m_bTriggered = false;
  190. }
  191. int DiosStMEvt::GetTimeout()
  192. {
  193. return m_pConditions == nullptr ? 0 : m_pConditions->GetTimeout();
  194. }
  195. //-----------------------------
  196. DiosStMRouteLine::DiosStMRouteLine()
  197. {
  198. m_ActiveState = false;
  199. m_InRoute = true;
  200. m_pEvt = new DiosStMEvt();
  201. m_pSrcRoutePos = new string();
  202. m_pGuard = new string();
  203. m_pDesRoutePos = new string();
  204. m_Timeout = TIMEOUT_TEMP;
  205. }
  206. DiosStMRouteLine::DiosStMRouteLine(const DiosStMRouteLine& tValue)
  207. {
  208. m_ActiveState = false;
  209. m_InRoute = true;
  210. m_pEvt = new DiosStMEvt();
  211. m_pSrcRoutePos = new string();
  212. m_pGuard = new string();
  213. m_pDesRoutePos = new string();
  214. m_Timeout = TIMEOUT_TEMP;
  215. if (tValue.m_InRoute)
  216. {
  217. SetRoute(*(tValue.m_pEvt), tValue.m_pSrcRoutePos->c_str(), tValue.m_pGuard->c_str(), tValue.m_pDesRoutePos->c_str());
  218. }
  219. else
  220. {
  221. SetRoute(*(tValue.m_pEvt), tValue.m_pGuard->c_str(), tValue.m_pDesRoutePos->c_str());
  222. }
  223. }
  224. DiosStMRouteLine::~DiosStMRouteLine()
  225. {
  226. delete m_pEvt;
  227. delete m_pSrcRoutePos;
  228. delete m_pDesRoutePos;
  229. delete m_pGuard;
  230. }
  231. void DiosStMRouteLine::SetRoute(DiosStMEvt& evt, const char* pGuard, const char* pDes)
  232. {
  233. m_InRoute = true;
  234. (*m_pSrcRoutePos) = "";
  235. (*m_pEvt) = evt;
  236. if (pGuard)
  237. {
  238. (*m_pGuard) = pGuard;
  239. }
  240. else
  241. {
  242. (*m_pGuard) = "";
  243. }
  244. if (pDes)
  245. {
  246. (*m_pDesRoutePos) = pDes;
  247. }
  248. else
  249. {
  250. (*m_pDesRoutePos) = "";
  251. }
  252. }
  253. void DiosStMRouteLine::SetRoute(DiosStMEvt& evt, const char* pSrc, const char* pGuard, const char* pDes)
  254. {
  255. m_InRoute = false;
  256. (*m_pEvt) = evt;
  257. if (pSrc)
  258. {
  259. (*m_pSrcRoutePos) = pSrc;
  260. }
  261. else
  262. {
  263. (*m_pSrcRoutePos) = "";
  264. }
  265. if (pGuard)
  266. {
  267. (*m_pGuard) = pGuard;
  268. }
  269. else
  270. {
  271. (*m_pGuard) = "";
  272. }
  273. if (pDes)
  274. {
  275. (*m_pDesRoutePos) = pDes;
  276. }
  277. else
  278. {
  279. (*m_pDesRoutePos) = "";
  280. }
  281. }
  282. const char* DiosStMRouteLine::GetDesName()
  283. {
  284. return m_pDesRoutePos->c_str();
  285. }
  286. const char* DiosStMRouteLine::GetSrcName()
  287. {
  288. if (m_InRoute == false)
  289. {
  290. return m_pSrcRoutePos->c_str();
  291. }
  292. return NULL;
  293. }
  294. const char* DiosStMRouteLine::GetGuardName()
  295. {
  296. return m_pGuard->c_str();
  297. }
  298. //true:in,false:out
  299. bool DiosStMRouteLine::GetRouteType()
  300. {
  301. return m_InRoute;
  302. }
  303. DiosStMRouteLine::operator DiosStMEvt* ()
  304. {
  305. return m_pEvt;
  306. }
  307. DiosStMRouteLine& DiosStMRouteLine::operator = (const DiosStMRouteLine& tValue)
  308. {
  309. if (&tValue != this)
  310. {
  311. if (tValue.m_InRoute)
  312. {
  313. SetRoute(*(tValue.m_pEvt), tValue.m_pSrcRoutePos->c_str(), tValue.m_pGuard->c_str(), tValue.m_pDesRoutePos->c_str());
  314. }
  315. else
  316. {
  317. SetRoute(*(tValue.m_pEvt), tValue.m_pGuard->c_str(), tValue.m_pDesRoutePos->c_str());
  318. }
  319. }
  320. return (*this);
  321. }
  322. void DiosStMRouteLine::Active(bool state, DWORD timeout)
  323. {
  324. m_ActiveState = state;
  325. if (m_ActiveState == false)
  326. {
  327. m_Timeout = timeout;
  328. }
  329. else
  330. {
  331. //get maximum timeperiod
  332. if (timeout > m_Timeout)
  333. {
  334. m_Timeout = timeout;
  335. }
  336. }
  337. }
  338. bool DiosStMRouteLine::GetActiveState()
  339. {
  340. return m_ActiveState;
  341. }
  342. DWORD DiosStMRouteLine::GetTimeout()
  343. {
  344. return m_Timeout;
  345. }