StateMachineDevicePool.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. #include "stdafx.h"
  2. #include "DiosSMachineV3.h"
  3. #include "PacketAnalizer.h"
  4. #include <functional>
  5. #define STATEMACHINE_ACQ_EVENTWAITEX_TIMEOUT (60000)
  6. string formatstdStringA(const char* fmt, ...)
  7. {
  8. std::string strResult = "";
  9. {
  10. if (NULL != fmt)
  11. {
  12. va_list marker = NULL;
  13. va_start(marker, fmt);
  14. size_t nLength = _vscprintf(fmt, marker) + 1;
  15. std::vector<char> vBuffer(nLength, '\0');
  16. int nWritten = vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength, fmt, marker);
  17. if (nWritten > 0)
  18. {
  19. strResult = &vBuffer[0];
  20. }
  21. va_end(marker);
  22. }
  23. }
  24. return strResult;
  25. }
  26. //int FindEventResult(DiosStMEvt& evt, DiosStMRouteLine* pEvts[], DWORD size)
  27. //{
  28. // int result = -1;
  29. // for (DWORD i = 0; i < size; i++)
  30. // {
  31. // if ((DiosStMEvt*)(*(pEvts[i])) != NULL && evt == (*(DiosStMEvt*)(*(pEvts[i]))))
  32. // {
  33. // result = i;
  34. // break;
  35. // }
  36. // }
  37. // return result;
  38. //}
  39. //int FindEventIndex(DiosStMEvt& Evt, DiosStMRouteLine* pLocalEvts[], DWORD CountOfLocal,
  40. // //DiosStMRouteLine* pExternalEvts[], DWORD CountOfExternal,
  41. // DiosStMRouteLine* pOutpathEvts[], DWORD CountOfOutpath)
  42. //{
  43. // int result = FindEventResult(Evt, pLocalEvts, CountOfLocal);
  44. // if (result != -1)
  45. // {
  46. // return result;
  47. // }
  48. // //result = FindEventResult(Evt, pExternalEvts, CountOfExternal);
  49. // //if (result != -1)
  50. // //{
  51. // // result += CountOfLocal;
  52. // // return result;
  53. // //}
  54. // result = FindEventResult(Evt, pOutpathEvts, CountOfOutpath);
  55. // if (result != -1)
  56. // {
  57. // result += CountOfLocal /*+ CountOfExternal*/;
  58. // return result;
  59. // }
  60. // return result;
  61. //}
  62. StateMachineDevicePool::StateMachineDevicePool()
  63. {
  64. m_pErrorInfoQue = new MsgQueue<ResDataObject>();
  65. m_pDevpath = new map<DWORD, string>();
  66. m_pDevlist = new map<DWORD, LogicClient*>();
  67. m_pActionDevList = new map<DWORD, LogicClient*>();
  68. //m_pActionMap = new map<string, map<string, map<DWORD, map<string, ResDataObject>>>>();
  69. //m_pEventMap = new map<string, map<SMEVTTYPE, map<DWORD, map<string, string>>>>();
  70. //m_pHardwareEventMap = new map<DWORD, map<string, map<string, string>>>();
  71. }
  72. StateMachineDevicePool::~StateMachineDevicePool()
  73. {
  74. ClearDevices();
  75. delete m_pDevpath;
  76. delete m_pDevlist;
  77. //delete m_pActionMap;
  78. //delete m_pEventMap;
  79. //delete m_pHardwareEventMap;
  80. delete m_pErrorInfoQue;
  81. }
  82. //提取错误信息
  83. bool StateMachineDevicePool::PopErrInfo(ResDataObject& info)
  84. {
  85. if (m_pErrorInfoQue->size() > 0)
  86. {
  87. return m_pErrorInfoQue->DeQueue(info);
  88. }
  89. return false;
  90. }
  91. //推入错误信息
  92. void StateMachineDevicePool::PushErrInfo(const char* pszContext, DSMERRORTYPE ErrType)
  93. {
  94. ResDataObject Context;
  95. Context.add("code", ErrType);
  96. Context.add("desc", pszContext);
  97. m_pErrorInfoQue->InQueue(Context);
  98. }
  99. void StateMachineDevicePool::SetThreadExitHandle(HANDLE tHand)
  100. {
  101. m_ThreadExitHandle = tHand;
  102. }
  103. //通过设备路径 获取设备的下标索引
  104. DWORD StateMachineDevicePool::GetDeviceIdx(const char* pszDevpath)
  105. {
  106. map<DWORD, string>::iterator iter = m_pDevpath->begin();
  107. while (iter != m_pDevpath->end())
  108. {
  109. if (iter->second == pszDevpath)
  110. {
  111. return iter->first;
  112. }
  113. ++iter;
  114. }
  115. return INVALID_DEVIDX;
  116. }
  117. //往设备池子中添加设备
  118. bool StateMachineDevicePool::AddDevices(ResDataObject& Devices)
  119. {
  120. bool ret = true;
  121. //DWORD LockStatus = Thread_Lock(1000);
  122. //if (LockStatus == WAIT_TIMEOUT)
  123. //{
  124. // mLog::FERROR("DevicePool is Locked");
  125. // return false;
  126. //}
  127. ClearDevices();
  128. for (INT i = 0; i < Devices.size(); i++)
  129. {
  130. const char* pDevice = Devices.GetKey(i);
  131. if (strlen(pDevice) == 0)
  132. {
  133. mLog::FERROR("The idx[%d]s Devicepath is empty", i);
  134. ClearDevices();
  135. ret = false;
  136. break;
  137. }
  138. (*m_pDevpath)[i] = pDevice;
  139. }
  140. //Thread_UnLock();
  141. if (ret)
  142. {
  143. ret = OpenDevices();
  144. }
  145. return ret;
  146. }
  147. LogicClient* StateMachineDevicePool::FindClient(const char* pszDevShortPath)
  148. {
  149. auto iter = m_pDevpath->begin();
  150. while (iter != m_pDevpath->end())
  151. {
  152. if (iter->second.find_first_of(pszDevShortPath, 0) > 0)
  153. return (*m_pActionDevList)[iter->first];
  154. iter++;
  155. }
  156. return nullptr;
  157. }
  158. //清空事件列表
  159. void StateMachineDevicePool::ClearStateEventMaps()
  160. {
  161. //DWORD LockStatus = Thread_Lock(1000);
  162. //if (LockStatus == WAIT_TIMEOUT)
  163. //{
  164. // mLog::FERROR("DevicePool is Locked");
  165. //}
  166. //m_pEventMap->clear();
  167. //m_pHardwareEventMap->clear();
  168. //Thread_UnLock();
  169. }
  170. //清空设备Action列表
  171. void StateMachineDevicePool::ClearStateActionMaps()
  172. {
  173. //DWORD LockStatus = Thread_Lock(1000);
  174. //if (LockStatus == WAIT_TIMEOUT)
  175. //{
  176. // mLog::FERROR("DevicePool is Locked");
  177. //}
  178. //m_pActionMap->clear();
  179. //Thread_UnLock();
  180. }
  181. //添加Action列表,采用Map存储
  182. //bool StateMachineDevicePool::AddStateActions(ResDataObject& ActionMaps)
  183. //{
  184. // //DWORD LockStatus = Thread_Lock(1000);
  185. // //if (LockStatus == WAIT_TIMEOUT)
  186. // //{
  187. // // mLog::FERROR("DevicePool is Locked");
  188. // // return false;
  189. // //}
  190. //
  191. // //"ParentStateName":"StateName":"Devpath":ActionName:Params
  192. // for (INT ParentNameIdx = 0; ParentNameIdx < ActionMaps.size(); ParentNameIdx++)
  193. // {
  194. // for (INT NameIdx = 0; NameIdx < ActionMaps[ParentNameIdx].size(); NameIdx++)
  195. // {
  196. // for (INT DevIdx = 0; DevIdx < ActionMaps[ParentNameIdx][NameIdx].size(); DevIdx++)
  197. // {
  198. // for (INT ActionIdx = 0; ActionIdx < ActionMaps[ParentNameIdx][NameIdx][DevIdx].size(); ActionIdx++)
  199. // {
  200. // const char* pParentName = ActionMaps.GetKey(ParentNameIdx);
  201. // const char* pStateName = ActionMaps[ParentNameIdx].GetKey(NameIdx);
  202. // const char* pDevpath = ActionMaps[ParentNameIdx][NameIdx].GetKey(DevIdx);
  203. // const char* pAction = ActionMaps[ParentNameIdx][NameIdx][DevIdx].GetKey(ActionIdx);
  204. // ResDataObject& Params = ActionMaps[ParentNameIdx][NameIdx][DevIdx][ActionIdx];
  205. //
  206. // if (strlen(pParentName) == 0 || strlen(pStateName) == 0 || strlen(pDevpath) == 0 || strlen(pAction) == 0)
  207. // {
  208. // mLog::FERROR("ParentName:%s\nStateName:%s\nDevPath:%s\n,Action:%s\n", pParentName, pStateName, pDevpath, pAction);
  209. // Thread_UnLock();
  210. // return false;
  211. // }
  212. //
  213. // DWORD ClientIdx = GetDeviceIdx(pDevpath);
  214. // if (ClientIdx == INVALID_DEVIDX)
  215. // {
  216. // mLog::FERROR("Device not exist:%s", pDevpath);
  217. //
  218. // Thread_UnLock();
  219. // return false;
  220. // }
  221. //
  222. // mLog::FDEBUG("ParentName:%s\nStateName:%s\nDevPath:%s\n,Action:%s,Param:%s\n", pParentName, pStateName, pDevpath, pAction, Params.encode());
  223. //
  224. // if (ActionMaps[ParentNameIdx][NameIdx][DevIdx].size() > 1)
  225. // {
  226. // mLog::FERROR("Device[%s] has more than 1 action at a state[%s][%s].action count:%d", pDevpath, pParentName, pStateName, ActionMaps[ParentNameIdx][NameIdx][DevIdx].size());
  227. //
  228. // Thread_UnLock();
  229. // return false;
  230. // }
  231. //
  232. //
  233. // //ok
  234. // (*m_pActionMap)[pParentName][pStateName][ClientIdx][pAction] = Params;
  235. //
  236. // }
  237. // }
  238. // }
  239. // }
  240. //
  241. // //Thread_UnLock();
  242. // return true;
  243. //}
  244. //添加事件列表
  245. //bool StateMachineDevicePool::AddStateEvents(ResDataObject& EventMaps)
  246. //{
  247. // DWORD LockStatus = Thread_Lock(1000);
  248. // if (LockStatus == WAIT_TIMEOUT)
  249. // {
  250. // mLog::FERROR("DevicePool is Locked");
  251. // return false;
  252. // }
  253. //
  254. // //EventName:{ Need : 1 or 0,{Devpath : Key:Value}}
  255. // for (INT EventIdx = 0; EventIdx < EventMaps.size(); EventIdx++)
  256. // {
  257. // //get eventname
  258. // const char* pEventName = EventMaps.GetKey(EventIdx);
  259. // if (strlen(pEventName) == 0)
  260. // {
  261. // mLog::FERROR("EventName is Empty");
  262. // Thread_UnLock();
  263. // return false;
  264. // }
  265. //
  266. // //get need flag
  267. // INT NeedIdx = EventMaps[EventIdx].GetFirstOf("Need");
  268. // if (NeedIdx < 0)
  269. // {
  270. // mLog::FERROR("Event:%s Missing Need Flag", pEventName);
  271. // Thread_UnLock();
  272. // return false;
  273. // }
  274. //
  275. // bool NeedFlag = EventMaps[EventIdx][NeedIdx];
  276. //
  277. // if (NeedFlag == 0)
  278. // {
  279. // //no need devices
  280. // (*m_pEventMap)[pEventName][SMEVT_PASS][INVALID_DEVIDX][""] = "";
  281. // mLog::FINFO("Event:%s Set PASS", pEventName);
  282. // continue;
  283. // }
  284. //
  285. // //get readattr
  286. // INT ReadAttrIdx = EventMaps[EventIdx].GetFirstOf("ReadFromAttribute");
  287. // if (ReadAttrIdx < 0)
  288. // {
  289. // mLog::FERROR("Event:%s Missing ReadFromAttribute Flag", pEventName);
  290. // Thread_UnLock();
  291. // return false;
  292. // }
  293. //
  294. // bool ReadFlag = EventMaps[EventIdx][ReadAttrIdx];
  295. //
  296. // bool EventMapExist = false;
  297. //
  298. // for (INT DevIdx = 0; DevIdx < EventMaps[EventIdx].size(); DevIdx++)
  299. // {
  300. // if (DevIdx == NeedIdx || DevIdx == ReadAttrIdx)
  301. // {
  302. // continue;
  303. // }
  304. //
  305. // //
  306. // const char* pDevpath = EventMaps[EventIdx].GetKey(DevIdx);
  307. // if (strlen(pDevpath) == 0)
  308. // {
  309. // mLog::FERROR("Event:%s Missing Devicepath", pEventName);
  310. // Thread_UnLock();
  311. // return false;
  312. // }
  313. //
  314. // for (INT KeyIdx = 0; KeyIdx < EventMaps[EventIdx][DevIdx].size(); KeyIdx++)
  315. // {
  316. // const char* pKey = EventMaps[EventIdx][DevIdx].GetKey(KeyIdx);
  317. // if (strlen(pKey) == 0)
  318. // {
  319. // mLog::FERROR("Event:%s\nDevpath:%s Missing KeyTitle", pEventName, pDevpath);
  320. // Thread_UnLock();
  321. // return false;
  322. // }
  323. //
  324. // const char* pVal = EventMaps[EventIdx][DevIdx][KeyIdx];
  325. //
  326. // DWORD ClientIdx = GetDeviceIdx(pDevpath);
  327. // if (ClientIdx == INVALID_DEVIDX)
  328. // {
  329. // mLog::FERROR("Device not exist:%s", pDevpath);
  330. //
  331. // Thread_UnLock();
  332. // return false;
  333. // }
  334. // if (ReadFlag)
  335. // {
  336. // (*m_pEventMap)[pEventName][SMEVT_READATTR][ClientIdx][pKey] = pVal;
  337. // }
  338. // else
  339. // {
  340. // (*m_pEventMap)[pEventName][SMEVT_NOTIFY][ClientIdx][pKey] = pVal;
  341. // }
  342. // mLog::FINFO("EventName:%s ReadFlag:%d,ClientIdx:%d,Key[%s]:val[%s]", pEventName, ReadFlag, ClientIdx, pKey, pVal);
  343. //
  344. // (*m_pHardwareEventMap)[ClientIdx][pKey][pVal] = pEventName;
  345. //
  346. // EventMapExist = true;
  347. //
  348. //
  349. // }
  350. //
  351. // }
  352. //
  353. // if (EventMapExist == false)
  354. // {
  355. // //no need devices
  356. // mLog::FINFO("Event[%s] no map Exist.set it as BLOCK", pEventName);
  357. //
  358. // (*m_pEventMap)[pEventName][SMEVT_BLOCK][INVALID_DEVIDX][""] = "";
  359. //
  360. // }
  361. //
  362. //
  363. // }
  364. //
  365. // Thread_UnLock();
  366. // return true;
  367. //
  368. //}
  369. //device init,清除所有设备 信息
  370. bool StateMachineDevicePool::ClearDevices()
  371. {
  372. //DWORD LockStatus = Thread_Lock(1000);
  373. //if (LockStatus == WAIT_TIMEOUT)
  374. //{
  375. // mLog::FERROR("DevicePool is Locked");
  376. // return false;
  377. //}
  378. CloseDevices();
  379. m_pErrorInfoQue->Clear();
  380. m_pDevpath->clear();
  381. m_pDevlist->clear();
  382. //m_pActionMap->clear();
  383. //m_pEventMap->clear();
  384. //m_pHardwareEventMap->clear();
  385. //Thread_UnLock();
  386. return true;
  387. }
  388. //打开所有设备
  389. bool StateMachineDevicePool::OpenDevices()
  390. {
  391. bool ret = true;
  392. //DWORD LockStatus = Thread_Lock(1000);
  393. //if (LockStatus == WAIT_TIMEOUT)
  394. //{
  395. // mLog::FERROR("DevicePool is Locked");
  396. // return false;
  397. //}
  398. if (m_pDevlist->size() > 0)
  399. {
  400. CloseDevices();
  401. }
  402. //go open
  403. char szClientName[256];
  404. map<DWORD, string>::iterator iter = m_pDevpath->begin();
  405. while (iter != m_pDevpath->end())
  406. {
  407. //用于接收通知的连接
  408. sprintf(szClientName, "StateMachine_%s", iter->second.c_str());
  409. LogicClient* pClient = new LogicClient((const char*)szClientName);
  410. if (pClient->Open(iter->second.c_str(), ALL_ACCESS, STATEMACHINE_ACTION_TIMEOUT) >= RET_SUCCEED)
  411. {
  412. std::cout << "\n\n StateMachine try open Device " << iter->second << "Succced..." << endl;
  413. (*m_pDevlist)[iter->first] = pClient;
  414. }
  415. else
  416. {
  417. std::cout << "\n\n StateMachine try open Device " << iter->second << "Failed..." << endl;
  418. mLog::FERROR("Can't Open:%s", iter->second.c_str());
  419. CloseDevices();
  420. ret = false;
  421. }
  422. //用于执行action的连接
  423. sprintf(szClientName, "StateMachine_Action_%s", iter->second.c_str());
  424. LogicClient* pActionClient = new LogicClient((const char*)szClientName, "SMA", false);
  425. if (pActionClient->Open(iter->second.c_str(), ALL_ACCESS, STATEMACHINE_ACTION_TIMEOUT) >= RET_SUCCEED)
  426. {
  427. std::cout << "\n\n StateMachine try open Device for action " << iter->second << "Succced..." << endl;
  428. (*m_pActionDevList)[iter->first] = pActionClient;
  429. }
  430. else
  431. {
  432. std::cout << "\n\n StateMachine try open Device " << iter->second << "Failed..." << endl;
  433. mLog::FERROR("Can't Open:%s", iter->second.c_str());
  434. CloseDevices();
  435. ret = false;
  436. }
  437. ++iter;
  438. }
  439. //Thread_UnLock();
  440. return ret;
  441. }
  442. //关闭设备
  443. bool StateMachineDevicePool::CloseDevices()
  444. {
  445. //DWORD LockStatus = Thread_Lock(1000);
  446. //if (LockStatus == WAIT_TIMEOUT)
  447. //{
  448. // mLog::FERROR("DevicePool is Locked");
  449. // return false;
  450. //}
  451. map<DWORD, LogicClient*>::iterator iter = m_pDevlist->begin();
  452. while (iter != m_pDevlist->end())
  453. {
  454. iter->second->Close();
  455. delete iter->second;
  456. ++iter;
  457. }
  458. m_pDevlist->clear();
  459. iter = m_pActionDevList->begin();
  460. while (iter != m_pActionDevList->end())
  461. {
  462. iter->second->Close();
  463. delete iter->second;
  464. ++iter;
  465. }
  466. m_pActionDevList->clear();
  467. //Thread_UnLock();
  468. return true;
  469. }
  470. //获取设备属性Action列表
  471. //INT StateMachineDevicePool::GetDeviceActions(const char* pParentStatePos, const char* pStatePos, map<DWORD, map<string, ResDataObject>>* pDeviceActions)
  472. //{
  473. // INT ret = -1;
  474. //
  475. // if (m_pActionMap->size() == 0)
  476. // {
  477. // //no action match
  478. // if (pParentStatePos && pStatePos)
  479. // {
  480. // mLog::FERROR("state actionmap empty.request state[%s]:substate[%s]", pParentStatePos, pStatePos);
  481. //
  482. // }
  483. // return 0;
  484. // }
  485. //
  486. // //root state machine
  487. // map<string, map<string, map<DWORD, map<string, ResDataObject>>>>::iterator iter = m_pActionMap->find(pParentStatePos);
  488. // if (iter == m_pActionMap->end())
  489. // {
  490. // if (pParentStatePos && pStatePos)
  491. // {
  492. // mLog::FERROR("parentstate:%s no Action map", pParentStatePos);
  493. // }
  494. //
  495. // return 0;
  496. // }
  497. //
  498. // //this state machine
  499. // map<string, map<DWORD, map<string, ResDataObject>>>::iterator iter1 = iter->second.find(pStatePos);
  500. // if (iter1 == iter->second.end())
  501. // {
  502. // if (pParentStatePos && pStatePos)
  503. // {
  504. // mLog::FERROR("state[%s]:substate[%s] no Action map", pParentStatePos, pStatePos);
  505. // }
  506. //
  507. // return 0;
  508. // }
  509. //
  510. // ret = 0;
  511. //
  512. // map<DWORD, map<string, ResDataObject>>::iterator iterDev = iter1->second.begin();
  513. // while (iterDev != iter1->second.end())
  514. // {
  515. // map<string, ResDataObject>::iterator iterAction = iterDev->second.begin();
  516. // while (iterAction != iterDev->second.end())
  517. // {
  518. // ret = 1;
  519. //
  520. // (*pDeviceActions)[iterDev->first][iterAction->first] = iterAction->second;
  521. //
  522. // ++iterAction;
  523. // }
  524. //
  525. // ++iterDev;
  526. // }
  527. //
  528. // return ret;
  529. //
  530. //
  531. //}
  532. /// <summary>
  533. /// 新版执行设备组的Action
  534. /// 状态机Action处理,调用设备的action
  535. /// </summary>
  536. /// <param name="resActions">要执行的设备Action资源</param>
  537. /// <param name="resResult">执行的结果保存 返回值:DeviceName.ActionName : resResp</param>
  538. /// <param name="pVariable">输入用户变量</param>
  539. /// <param name="Timeout">超时时间</param>
  540. /// <returns>DIOSSMRET_OK 执行成功</returns>
  541. DIOSSTMRET StateMachineDevicePool::StateMachineAction(ResDataObject* resActions, ResDataObject& resResult, ResDataObject* pVariable, DWORD Timeout)
  542. {
  543. DIOSSTMRET ret = DIOSSMRET_NG;
  544. //按顺序投递消息,并接收应答
  545. int nActionCount = 0;
  546. DWORD dwStart = GetTickCount();
  547. for (int devIdx = 0; devIdx < resActions->size(); devIdx++)
  548. {
  549. const char* pszDevName = resActions->GetKey(devIdx);
  550. LogicClient* pClient = FindClient(pszDevName);
  551. if (pClient == nullptr)
  552. {
  553. std::cout << "Cannot find Action Clinet.... ERROR... " << pszDevName << endl;
  554. }
  555. ResDataObject actions = resActions[devIdx];
  556. ResDataObject resParam;
  557. for (int aIdx = 0; aIdx < actions.size(); aIdx++)
  558. {
  559. resParam = actions[aIdx];
  560. string param = (const char*)resParam;
  561. int nParamStart = param.find('$');
  562. if(nParamStart < 0)
  563. {
  564. pClient->Action_Req(actions.GetKey(aIdx), resParam);
  565. nActionCount++;
  566. }
  567. else
  568. {
  569. //需要处理用户变量
  570. //必须以$$开头和结束
  571. string paramName = param.substr(1, param.length() - 2);
  572. int nParamPos = paramName.find('.');
  573. if (nParamPos > 0)
  574. {
  575. string devName = param.substr(0, nParamPos);
  576. string pName = param.substr(nParamPos + 1);
  577. //是2层参数
  578. try {
  579. resParam = (*pVariable)[devName.c_str()][pName.c_str()];
  580. pClient->Action_Req(actions.GetKey(aIdx), resParam);
  581. nActionCount++;
  582. }
  583. catch (...)
  584. {
  585. //变量取不到
  586. }
  587. }
  588. else
  589. {
  590. //一层参数
  591. try
  592. {
  593. resParam = (*pVariable)[paramName.c_str()];
  594. pClient->Action_Req(actions.GetKey(aIdx), resParam);
  595. nActionCount++;
  596. }
  597. catch (...)
  598. {
  599. }
  600. }
  601. }
  602. }
  603. }
  604. //并将结果存入系统变量
  605. while(nActionCount > 0)
  606. {
  607. for (int devIdx = 0; devIdx < resActions->size(); devIdx++)
  608. {
  609. const char* pszDevName = resActions->GetKey(devIdx);
  610. LogicClient* pClient = FindClient(pszDevName);
  611. ResDataObject actions = resActions[devIdx];
  612. ResDataObject resResp;
  613. for (int aIdx = 0; aIdx < actions.size(); aIdx++)
  614. {
  615. if (pClient->IsDataArrived())
  616. {
  617. pClient->ReadCmd(resResp);
  618. //返回值:DeviceName.ActionName : resResp
  619. resResult.add((string(pszDevName) + "." + (string(resResp["KEY"]))).c_str(), resResp);
  620. nActionCount--;
  621. }
  622. }
  623. }
  624. //超时了,还没取完返回值
  625. if (GetTickCount() - dwStart > Timeout)
  626. return DIOSSMRET_TIMEOUT;
  627. }
  628. return DIOSSMRET_OK;
  629. }
  630. //通用的Action和Event处理
  631. //DIOSSTMRET StateMachineDevicePool::StateMachineAction(const char* pParentStatePos, const char* pStatePos, DWORD timeout)
  632. //{
  633. // Thread_Lock();
  634. // //收集符合状态点的设备.
  635. // //收集对应设备的Actions.
  636. // //devidx:action:params
  637. // map<DWORD, map<string, ResDataObject>> DeviceActions;
  638. // INT ret = GetDeviceActions(pParentStatePos, pStatePos, &DeviceActions);
  639. // if (ret < 0)
  640. // {
  641. // Thread_UnLock();
  642. // return DIOSSMRET_NG;
  643. // }
  644. //
  645. // if (ret == 0)
  646. // {
  647. // Thread_UnLock();
  648. // return DIOSSMRET_OK;
  649. // }
  650. //
  651. // if (DeviceActions.size() > MAXIMUM_WAIT_OBJECTS - 1)
  652. // {
  653. // string errinfo = formatstdStringA("Device Count Out of Windows Wait Count:%d,Limit:%d", DeviceActions.size(), MAXIMUM_WAIT_OBJECTS - 1);
  654. // mLog::FERROR("%s", errinfo.c_str());
  655. // PushErrInfo(errinfo.c_str());
  656. //
  657. // Thread_UnLock();
  658. // return DIOSSMRET_NG;
  659. // }
  660. //
  661. // DWORD DevIndex = 0;
  662. // DWORD DevCount = (DWORD)DeviceActions.size();
  663. //
  664. // vector<DWORD> ClientIdx;
  665. // vector<string> ActionIdx;
  666. // vector<HANDLE> ResHandle;
  667. //
  668. // vector<LogicClient*> ClientsObj;
  669. //
  670. // //线程退出事件
  671. // ResHandle.push_back(m_ThreadExitHandle);
  672. //
  673. // //异步发起ActionReq
  674. // map<DWORD, map<string, ResDataObject>>::iterator iter = DeviceActions.begin();
  675. // while (iter != DeviceActions.end())
  676. // {
  677. // LogicClient* pClient = (*m_pActionDevList)[iter->first];
  678. // if (pClient == 0)
  679. // {
  680. // string errinfo = formatstdStringA("state:[%s][%s],device:%s no client exist", pParentStatePos, pStatePos, (*m_pDevpath)[iter->first].c_str());
  681. // mLog::FERROR("%s", errinfo.c_str());
  682. // PushErrInfo(errinfo.c_str());
  683. //
  684. // Thread_UnLock();
  685. // return DIOSSMRET_NG;
  686. // }
  687. //
  688. // map<string, ResDataObject>::iterator iterAction = (iter)->second.begin();// iter->second.begin();
  689. // /*if (iter->second.size() > 1)
  690. // {
  691. // string errinfo = formatstdStringA("state:[%s][%s],device:%s has more than 1 action", pParentStatePos, pStatePos, (*m_pDevpath)[iter->first].c_str());
  692. // mLog::FERROR("%s", errinfo.c_str());
  693. // PushErrInfo(errinfo.c_str());
  694. //
  695. // Thread_UnLock();
  696. // return DIOSSMRET_NG;
  697. // }*/
  698. //
  699. // while (iterAction != (iter)->second.end())
  700. // {
  701. // RET_STATUS ActionReq = (RET_STATUS)pClient->Action_Req(iterAction->first.c_str(), iterAction->second, timeout);
  702. // if (ActionReq <= RET_FAILED)
  703. // {
  704. // string errinfo = formatstdStringA("state:[%s][%s] Action[%s] Req Failed.ret:%d,Device:%s", pParentStatePos, pStatePos, iterAction->first.c_str(), ActionReq, (*m_pDevpath)[iter->first].c_str());
  705. // mLog::FERROR("%s", errinfo.c_str());
  706. // PushErrInfo(errinfo.c_str(), DSM_ERROR_ACTION_FAILED);
  707. //
  708. // Thread_UnLock();
  709. // return DIOSSMRET_NG;
  710. // }
  711. //
  712. // //ActionIdx.push_back(iterAction->first.c_str());
  713. // //ClientIdx.push_back(iter->first);
  714. // //ResHandle.push_back(pClient->GetResponseHandle());
  715. // ClientsObj.push_back(pClient);
  716. //
  717. //
  718. // ++iterAction;
  719. // }
  720. //
  721. // ++iter;
  722. // }
  723. //
  724. // //轮询接收ActionRes
  725. // bool bHaveError = false;
  726. // int idx = 0;
  727. // DWORD EntryTime = GetTickCount();
  728. // while (ClientsObj.size() > 0)
  729. // {
  730. // //get client
  731. // LogicClient* pClient = ClientsObj[idx];
  732. //
  733. // //try get res
  734. // if (pClient->IsDataArrived())
  735. // {
  736. // ResDataObject res;
  737. // //收到应答了
  738. // PACKET_CMD cmd = pClient->ReadCmd(res);
  739. // RET_STATUS ActionRet = RET_FAILED;
  740. // PacketAnalizer::GetPacketRetCode(&res, ActionRet);
  741. //
  742. // if ((ActionRet < RET_SUCCEED) && (ActionRet != RET_TIMEOUT))
  743. // {
  744. // bHaveError = true;
  745. // ResDataObject path;
  746. // pClient->GetFilePath(path);
  747. // string errinfo = formatstdStringA("state:[%s][%s] Wait Action[%s] Res failed.ret:%d.device:%s", pParentStatePos, pStatePos,
  748. // PacketAnalizer::GetPacketKey(&res), ActionRet, (const char*)path);
  749. // mLog::FERROR("%s", errinfo.c_str());
  750. // PushErrInfo(errinfo.c_str(), DSM_ERROR_ACTION_FAILED);
  751. //
  752. // //return DIOSSMRET_NG;
  753. // }
  754. //
  755. // ClientsObj.erase(ClientsObj.begin() + idx);
  756. // if (ActionRet >= RET_SUCCEED)
  757. // {
  758. // //Succeed get res.do the kick
  759. // if (ClientsObj.size() == 0)
  760. // {
  761. // Thread_UnLock();
  762. // if (!bHaveError)
  763. // {
  764. // mLog::FDEBUG("state:[%s][%s] Action Succeed", pParentStatePos, pStatePos);
  765. // return DIOSSMRET_OK;
  766. // }
  767. // }
  768. //
  769. // }
  770. // }
  771. // else
  772. // {
  773. // idx++;
  774. // }
  775. //
  776. // //timeout here
  777. //
  778. // DWORD PassedTimePeriod = GetTickCount() - EntryTime;
  779. // if (timeout < PassedTimePeriod)
  780. // {
  781. // break;
  782. // }
  783. // timeout -= PassedTimePeriod;
  784. //
  785. // }
  786. //
  787. // mLog::FERROR("StateMachineAction Timeout");
  788. // PushErrInfo("StateMachineAction Timeout", DSM_ERROR_ACTION_TIMEOUT);
  789. //
  790. // Thread_UnLock();
  791. // return DIOSSMRET_TIMEOUT;
  792. //
  793. //}
  794. //wait event
  795. //从 Target状态机 全局In事件和当前状态的Out事件中等待 某个事件的发生
  796. //如果状态出路由有直接跳转的,就直接过去了
  797. //否则就等着事件发生
  798. //int StateMachineDevicePool::StateMachineWaitForEvents(
  799. // DiosSMachineIF* pTarget,
  800. // DiosStMRouteLine* pLocalEvts[], DWORD CountOfLocal,
  801. // //DiosStMRouteLine* pExternalEvts[], DWORD CountOfExternal,
  802. // DiosStMRouteLine* pOutpathEvts[], DWORD CountOfOutpath,
  803. // DWORD timeout
  804. //)
  805. //{
  806. // Thread_Lock();
  807. //
  808. // //check outpath event status
  809. // for (DWORD OutIdx = 0; OutIdx < CountOfOutpath; OutIdx++)
  810. // {
  811. // //(empty,pass)->direct jump
  812. // DiosStMEvt* pEvt = (*(pOutpathEvts[OutIdx]));
  813. // if (pEvt->IsEmpty() == true)
  814. // {
  815. // //Hit empty
  816. // Thread_UnLock();
  817. //
  818. // return (CountOfLocal /*+ CountOfExternal*/ + OutIdx);
  819. // }
  820. // else
  821. // {
  822. // const char* pszEvtName = pEvt->GetEvtContext().GetKey(0);
  823. // map<string, map<SMEVTTYPE, map<DWORD, map<string, string>>>>::iterator iter = m_pEventMap->find(pszEvtName);
  824. // if (iter != m_pEventMap->end())
  825. // {
  826. // map<SMEVTTYPE, map<DWORD, map<string, string>>>::iterator iterEvtType = iter->second.begin();
  827. // while (iterEvtType != iter->second.end())
  828. // {
  829. // //Hit pass
  830. // if (iterEvtType->first == SMEVT_PASS)
  831. // {
  832. // Thread_UnLock();
  833. // //return (CountOfLocal + CountOfExternal + OutIdx);
  834. // if (1 == iter->second.size())
  835. // {
  836. // //如果当前状态点下所有触发事件都是自动触发的,则自动跳转到下一个状态点
  837. // return (CountOfLocal +/* CountOfExternal +*/ OutIdx);
  838. // }
  839. // else
  840. // {
  841. // //已经确定状态点下还有手动触发的事件,没有必要继续判断,直接跳出当前循环
  842. // break;
  843. // }
  844. // }
  845. //
  846. // ++iterEvtType;
  847. //
  848. // }
  849. //
  850. // }
  851. // else
  852. // {
  853. // //没有映射的话,认为BLOCK
  854. // mLog::Warn("no map for Event:%s", pszEvtName);
  855. // }
  856. // }
  857. //
  858. // }
  859. //
  860. // //优先处理
  861. // int ReadEvetRet = ReadForDeviceEvents(pTarget, pLocalEvts, CountOfLocal, /*pExternalEvts, CountOfExternal,*/ pOutpathEvts, CountOfOutpath, timeout);
  862. // if (ReadEvetRet != -1)
  863. // {
  864. // Thread_UnLock();
  865. // return ReadEvetRet;
  866. // }
  867. //
  868. // Thread_UnLock();
  869. //
  870. // ReadEvetRet = WaitForDeviceEvents(pTarget, pLocalEvts, CountOfLocal, /*pExternalEvts, CountOfExternal,*/ pOutpathEvts, CountOfOutpath, timeout);
  871. //
  872. // return ReadEvetRet;
  873. //}
  874. //const char* StateMachineDevicePool::GetStateMachineEventName(DWORD DevIdx, const char* pKey, const char* pVal)
  875. //{
  876. // map<DWORD, map<string, map<string, string>>>::iterator iter = m_pHardwareEventMap->find(DevIdx);
  877. // if (iter != m_pHardwareEventMap->end())
  878. // {
  879. // map<string, map<string, string>>::iterator iterkey = iter->second.find(pKey);
  880. // if (iterkey != iter->second.end())
  881. // {
  882. // map<string, string>::iterator iterval = iterkey->second.find(pVal);
  883. // if (iterval != iterkey->second.end())
  884. // {
  885. // return iterval->second.c_str();
  886. // }
  887. // }
  888. // }
  889. //
  890. // return NULL;
  891. //}
  892. int StateMachineDevicePool::ReadForDeviceEvents(ResDataObject& resNotfiy, DWORD timeout, int nMaxNotifyNum)
  893. {
  894. vector<DWORD> devList;
  895. vector<HANDLE> waitList;
  896. //wait [threadexit]+[StateMachinesEvent]+[all device event]
  897. waitList.push_back(m_ThreadExitHandle);
  898. //waitList.push_back(pTarget->GetEvtNotifyHandle());
  899. map<DWORD, LogicClient*>::iterator iter = m_pDevlist->begin();
  900. while (iter != m_pDevlist->end())
  901. {
  902. devList.push_back(iter->first);
  903. waitList.push_back(iter->second->GetNotifyHandle());
  904. ++iter;
  905. }
  906. int nNotifyCount = 0;
  907. DWORD FirstTick = GetTickCount();
  908. DWORD waitUseTime = 0;
  909. long long waitTime = timeout;
  910. while(true)
  911. {
  912. DWORD wait_ret = WaitForMultipleObjects((DWORD)waitList.size(), waitList.data(), FALSE, waitTime);
  913. waitUseTime = GetTickCount() - FirstTick;
  914. waitTime = waitTime - waitUseTime;
  915. if (wait_ret == WAIT_TIMEOUT)
  916. {
  917. if (waitTime <= 0 || nNotifyCount >= nMaxNotifyNum)
  918. {
  919. //时间到了
  920. if (nNotifyCount > 0)
  921. return nNotifyCount;
  922. return -1;//timeout error
  923. }
  924. }
  925. else if (wait_ret == WAIT_OBJECT_0)
  926. {
  927. return -3;//exit thread
  928. }
  929. //stepB1:got hardware event
  930. else if (wait_ret > WAIT_OBJECT_0)
  931. {
  932. DWORD HitIdx = wait_ret - WAIT_OBJECT_0 - 1;
  933. DWORD DevIdx = devList[HitIdx];
  934. LogicClient* pClient = (*m_pDevlist)[DevIdx];
  935. while (pClient->IsDataArrived())
  936. {
  937. nNotifyCount++;
  938. ResDataObject ReadNotify;
  939. PACKET_CMD notifycmd = pClient->ReadNotify(ReadNotify);
  940. string strKey = PacketAnalizer::GetPacketKey(&ReadNotify);
  941. if (notifycmd == PACKET_CMD_UPDATE)
  942. {
  943. //将通知事件上报即可
  944. ResDataObject resDevicePath;
  945. pClient->GetFilePath(resDevicePath);
  946. resNotfiy.add(((string)resDevicePath + "." + strKey).c_str(), ReadNotify);
  947. }
  948. if (nNotifyCount >= nMaxNotifyNum)
  949. return nNotifyCount;
  950. }
  951. }
  952. }
  953. return nNotifyCount;
  954. }
  955. //int StateMachineDevicePool::ReadForDeviceEventsEx(
  956. // DiosStMRouteLine* pEvts[], DWORD Count,
  957. // DWORD timeout
  958. //)
  959. //{
  960. // //获取SMEVT_READATTR属性的状态机事件FROM [Outpath,External,Local].
  961. // for (DWORD OutIdx = 0; OutIdx < Count; OutIdx++)
  962. // {
  963. // DiosStMEvt* pEvt = (*(pEvts[OutIdx]));
  964. // const char* pszEvtName = pEvt->GetEvtContext().GetKey(0);
  965. // map<string, map<SMEVTTYPE, map<DWORD, map<string, string>>>>::iterator iter = m_pEventMap->find(pszEvtName);
  966. // if (iter != m_pEventMap->end())
  967. // {
  968. // //获取SMEVT_READATTR属性的状态机事件
  969. // map<SMEVTTYPE, map<DWORD, map<string, string>>>::iterator iterEvtType = iter->second.find(SMEVT_READATTR);
  970. // if (iterEvtType != iter->second.end())
  971. // {
  972. // //获取匹配的设备列表&属性列表.
  973. // map<DWORD, map<string, string>>::iterator iterDevice = iterEvtType->second.begin();
  974. // while (iterDevice != iterEvtType->second.end())
  975. // {
  976. // //check exit flag
  977. // if (WaitForSingleObject(m_ThreadExitHandle, 0) == WAIT_OBJECT_0)
  978. // {
  979. // mLog::FDEBUG("Exit Thread");
  980. // return -3;//exit thread
  981. // }
  982. //
  983. // //更新资源
  984. // ResDataObject DevRes;
  985. // LogicClient* pClient = (*m_pDevlist)[iterDevice->first];
  986. // if (pClient->UpdateDeviceResource(timeout) >= RET_SUCCEED && pClient->GetDeviceResource(&DevRes) >= RET_SUCCEED)
  987. // {
  988. // //匹配
  989. // try {
  990. // map<string, string>::iterator iterkeyval = iterDevice->second.begin();
  991. // while (iterkeyval != iterDevice->second.end())
  992. // {
  993. // const char* pVal = DevRes["Attribute"][iterkeyval->first.c_str()];
  994. // if (iterkeyval->second == pVal)
  995. // {
  996. // //got match
  997. // mLog::FINFO("Event[%s] hit match of read key[%s]:val[%s] on device:%s",
  998. // pszEvtName,
  999. // iterkeyval->first.c_str(),
  1000. // iterkeyval->second.c_str(),
  1001. // (*m_pDevpath)[iterDevice->first].c_str()
  1002. // );
  1003. //
  1004. // return OutIdx;
  1005. // }
  1006. //
  1007. // ++iterkeyval;
  1008. // }
  1009. //
  1010. //
  1011. //
  1012. // }
  1013. // catch (ResDataObjectExption& exp)
  1014. // {
  1015. // //exp.what()
  1016. // string errinfo = formatstdStringA("%s", exp.what());
  1017. // mLog::FERROR("%s", errinfo.c_str());
  1018. // PushErrInfo(errinfo.c_str());
  1019. // return -2;//device error
  1020. // }
  1021. // catch (...)
  1022. // {
  1023. // string errinfo = formatstdStringA("Unknown Exp Happened\n");
  1024. // mLog::FERROR("%s", errinfo.c_str());
  1025. // PushErrInfo(errinfo.c_str());
  1026. // return -2;//device error
  1027. // }
  1028. //
  1029. // }
  1030. // else
  1031. // {
  1032. // string errinfo = formatstdStringA("UpdateDeviceResource or GetDeviceResource failed on Device:%s", (*m_pDevpath)[iterDevice->first].c_str());
  1033. // mLog::FERROR("%s", errinfo.c_str());
  1034. // PushErrInfo(errinfo.c_str());
  1035. // return -2;//device error
  1036. // }
  1037. //
  1038. //
  1039. // ++iterDevice;
  1040. // }
  1041. //
  1042. //
  1043. // }
  1044. // }
  1045. //
  1046. // }
  1047. //
  1048. // return -1;//timeout or not match
  1049. //}
  1050. //尝试检查 设备Notify事件,如果有皆大欢喜,如果没有,霸王硬上弓,更新设备属性获取新值
  1051. //这里先检查Out事件
  1052. //后检查in事件
  1053. //int StateMachineDevicePool::ReadForDeviceEvents(
  1054. // DiosSMachineIF* pTarget,
  1055. // DiosStMRouteLine* pLocalEvts[], DWORD CountOfLocal,
  1056. // //DiosStMRouteLine* pExternalEvts[], DWORD CountOfExternal,
  1057. // DiosStMRouteLine* pOutpathEvts[], DWORD CountOfOutpath,
  1058. // DWORD timeout
  1059. //)
  1060. //{
  1061. // //尝试读取通知.
  1062. // int WaitRet = WaitForDeviceEvents(pTarget, pLocalEvts, CountOfLocal, /*pExternalEvts, CountOfExternal,*/ pOutpathEvts, CountOfOutpath, 0);
  1063. // if (WaitRet != -1)
  1064. // {
  1065. // return WaitRet;
  1066. // }
  1067. //
  1068. // //got timeout
  1069. // WaitRet = ReadForDeviceEventsEx(pOutpathEvts, CountOfOutpath, timeout);
  1070. // if (WaitRet != -1)
  1071. // {
  1072. // if (WaitRet >= 0)
  1073. // {
  1074. // return (CountOfLocal + /*CountOfExternal +*/ WaitRet);
  1075. // }
  1076. //
  1077. // return WaitRet;
  1078. // }
  1079. //
  1080. // //WaitRet = ReadForDeviceEventsEx(pExternalEvts, CountOfExternal, timeout);
  1081. // //if (WaitRet != -1)
  1082. // //{
  1083. // // if (WaitRet >= 0)
  1084. // // {
  1085. // // return (CountOfLocal + WaitRet);
  1086. // // }
  1087. // // return WaitRet;
  1088. // //}
  1089. //
  1090. // WaitRet = ReadForDeviceEventsEx(pLocalEvts, CountOfLocal, timeout);
  1091. // if (WaitRet != -1)
  1092. // {
  1093. // return WaitRet;
  1094. // }
  1095. //
  1096. // return WaitRet;
  1097. //}
  1098. //这里是等待 pTarget 状态机的事件发生
  1099. //如果是状态机的自己的状态发生了变化,也返回
  1100. // 或者是设备的Notify来了
  1101. //int StateMachineDevicePool::WaitForDeviceEvents(
  1102. // DiosSMachineIF* pTarget,
  1103. // DiosStMRouteLine* pLocalEvts[], DWORD CountOfLocal,
  1104. // //DiosStMRouteLine* pExternalEvts[], DWORD CountOfExternal,
  1105. // DiosStMRouteLine* pOutpathEvts[], DWORD CountOfOutpath,
  1106. // DWORD timeout
  1107. //)
  1108. //{
  1109. //
  1110. // vector<DWORD> devList;
  1111. // vector<HANDLE> waitList;
  1112. // //wait [threadexit]+[StateMachinesEvent]+[all device event]
  1113. // waitList.push_back(m_ThreadExitHandle);
  1114. // waitList.push_back(pTarget->GetEvtNotifyHandle());
  1115. // map<DWORD, LogicClient*>::iterator iter = m_pDevlist->begin();
  1116. // while (iter != m_pDevlist->end())
  1117. // {
  1118. // devList.push_back(iter->first);
  1119. // waitList.push_back(iter->second->GetNotifyHandle());
  1120. // ++iter;
  1121. // }
  1122. //
  1123. // do
  1124. // {
  1125. // DWORD FirstTick = GetTickCount();
  1126. //
  1127. // DWORD wait_ret = WaitForMultipleObjects((DWORD)waitList.size(), waitList.data(), FALSE, timeout);
  1128. //
  1129. // Thread_Lock();//
  1130. //
  1131. // if (wait_ret == WAIT_TIMEOUT)
  1132. // {
  1133. //
  1134. // if (timeout != 0)
  1135. // {
  1136. // string errinfo = formatstdStringA("Wait Event Timeout(%d)", timeout);
  1137. // mLog::FERROR("%s", errinfo.c_str());
  1138. //
  1139. // PushErrInfo(errinfo.c_str(), DSM_ERROR_WAITEVENT_TIMEOUT);
  1140. // }
  1141. // else
  1142. // {
  1143. // //normal
  1144. // mLog::FINFO("%s", "no Event in Que");
  1145. // }
  1146. //
  1147. // Thread_UnLock();
  1148. // return -1;//timeout error
  1149. // }
  1150. // else if (wait_ret == WAIT_OBJECT_0)
  1151. // {
  1152. // Thread_UnLock();
  1153. // return -3;//exit thread
  1154. // }
  1155. // else if (wait_ret == WAIT_OBJECT_0 + 1)
  1156. // {
  1157. // //stepA2:pop event,check the statemachine event exist in local&external&outpath,if so then return index.
  1158. // DiosStMEvt Evt;
  1159. // while (pTarget->PopEvent(Evt))
  1160. // {
  1161. // ResDataObject& EvtContext = Evt.GetEvtContext();
  1162. // printf("PopEvt Key[%s]:Val[%s]", EvtContext.GetKey(0), (const char*)EvtContext[0]);
  1163. // mLog::FDEBUG("PopEvt Key[%s]:Val[%s]", EvtContext.GetKey(0), (const char*)EvtContext[0]);
  1164. //
  1165. // int result = FindEventIndex(Evt, pLocalEvts, CountOfLocal, /*pExternalEvts, CountOfExternal,*/ pOutpathEvts, CountOfOutpath);
  1166. // if (result != -1)
  1167. // {
  1168. // Thread_UnLock();
  1169. // return result;
  1170. // }
  1171. // }
  1172. // }
  1173. // //stepB1:got hardware event
  1174. // else if (wait_ret > WAIT_OBJECT_0 + 1)
  1175. // {
  1176. // DWORD HitIdx = wait_ret - WAIT_OBJECT_0 - 2;
  1177. // DWORD DevIdx = devList[HitIdx];
  1178. // LogicClient* pClient = (*m_pDevlist)[DevIdx];
  1179. // while (pClient->IsDataArrived())
  1180. // {
  1181. // ResDataObject ReadNotify;
  1182. // PACKET_CMD notifycmd = pClient->ReadCmd(ReadNotify);
  1183. //
  1184. // string strKey = PacketAnalizer::GetPacketKey(&ReadNotify);
  1185. //
  1186. // //if (notifycmd == PACKET_CMD_ADD)
  1187. // //{
  1188. // // //stepB2:check Error Event,if so then return error index.(-1)
  1189. // // if (strKey == "ErrorList")
  1190. // // {
  1191. // // Thread_UnLock();
  1192. // // return -2;//device error
  1193. // // }
  1194. // //}
  1195. // if (notifycmd == PACKET_CMD_UPDATE)
  1196. // {
  1197. // ResDataObject ResNotify;
  1198. // PacketAnalizer::GetPacketContext(&ReadNotify, ResNotify);
  1199. // //stepB3:translate hardware event -> statemachine event.
  1200. // //这里将设备通知通过 当前状态的 事件进行遍历,检查哪个事件符合 触发条件,则返回目标事件
  1201. // const char* pStateEventName = GetStateMachineEventName(DevIdx, strKey.c_str(), ResNotify);
  1202. // if (pStateEventName)
  1203. // {
  1204. // DiosStMEvt Evt;
  1205. // Evt.SetEvt(pStateEventName, NULL);
  1206. //
  1207. // mLog::FINFO("Trigger Event:%s\n", pStateEventName);
  1208. // //stepB4:check the statemachine event exist in local&external&outpath,if so then return index.
  1209. // int result = FindEventIndex(Evt, pLocalEvts, CountOfLocal, /*pExternalEvts, CountOfExternal,*/ pOutpathEvts, CountOfOutpath);
  1210. // if (result != -1)
  1211. // {
  1212. // Thread_UnLock();
  1213. // mLog::FINFO("Event:%s maps[%d]\n", pStateEventName, result);
  1214. // return result;
  1215. // }
  1216. //
  1217. //
  1218. // }
  1219. //
  1220. // }
  1221. //
  1222. //
  1223. //
  1224. // }
  1225. //
  1226. // }
  1227. //
  1228. // Thread_UnLock();
  1229. //
  1230. //
  1231. //
  1232. // DWORD PassedTimePeriod = GetTickCount() - FirstTick;
  1233. //
  1234. // if (PassedTimePeriod < timeout)
  1235. // {
  1236. // timeout -= PassedTimePeriod;
  1237. // }
  1238. // else
  1239. // {
  1240. // break;
  1241. // }
  1242. //
  1243. //
  1244. // } while (timeout);
  1245. //
  1246. // if (timeout != 0)
  1247. // {
  1248. // string errinfo = formatstdStringA("Wait Event Timeout(%d)", timeout);
  1249. // mLog::FERROR("%s", errinfo.c_str());
  1250. //
  1251. // PushErrInfo(errinfo.c_str(), DSM_ERROR_WAITEVENT_TIMEOUT);
  1252. // }
  1253. // else
  1254. // {
  1255. // //normal
  1256. // mLog::FINFO("%s", "no Event in Que");
  1257. // }
  1258. //
  1259. // return -1;//timeout error
  1260. //
  1261. //}