mod_evtconverter.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "evtengine.h"
  4. #include "../include/EventCode.h"
  5. #include "fileutil.h"
  6. #include "modVer.h"
  7. class CEventConverterEntity : public CEntityBase, public ISysVarListener, public ILogListener
  8. {
  9. public:
  10. CEventConverterEntity() : m_pEngine(NULL), m_lIdSeq(0) {}
  11. virtual ~CEventConverterEntity() {}
  12. virtual const char *GetEntityName() const { return "EventConverter"; }
  13. const char* GetEntityVersion() const { return MODULE_VERSION_FULL; }
  14. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  15. {
  16. ErrorCodeEnum Error = __OnStart(Error_Succeed);
  17. pTransactionContext->SendAnswer(Error);
  18. }
  19. ErrorCodeEnum __OnStart(ErrorCodeEnum preOperationError)
  20. {
  21. if (m_nIslog)
  22. {
  23. LOG_FUNCTION();
  24. }
  25. //MessageBoxA(0,0,0,0);
  26. m_eDeviceType = eStand2sType; //add by chh
  27. m_nIslog = 1;
  28. ErrorCodeEnum Error;
  29. CSimpleStringA str;
  30. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  31. CSmartPointer<IConfigInfo> spConfig;
  32. Error = spEntityFunction->OpenConfig(Config_Software, spConfig);
  33. if (Error != Error_Succeed) {
  34. LOG_TRACE("open config failed!");
  35. return Error;
  36. }
  37. //is Pad Version
  38. CSystemStaticInfo stStaticinfo;
  39. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  40. spFunction->GetSystemStaticInfo(stStaticinfo);
  41. LOG_TRACE("stStaticinfo.strMachineType = %s",stStaticinfo.strMachineType.GetData());
  42. if (stricmp(stStaticinfo.strMachineType,"RVC.PAD")==0)
  43. {
  44. if (stricmp(stStaticinfo.strSite,"CMB.FLB")==0)
  45. {
  46. LOG_TRACE("the type is mobile pad");
  47. m_eDeviceType = eMobilePadType;
  48. }
  49. else
  50. {
  51. LOG_TRACE("the type is pad");
  52. m_eDeviceType = ePadtype;
  53. }
  54. }
  55. else if (stricmp(stStaticinfo.strMachineType,"RVC.Desk2S")==0)
  56. {
  57. LOG_TRACE("the type is Desk2S");
  58. m_eDeviceType = eDesk2SType;
  59. }
  60. else if (stricmp(stStaticinfo.strMachineType,"RPM.Stand1S")==0)
  61. {
  62. LOG_TRACE("the type is RPM.Stand1S");
  63. m_eDeviceType = eRpm1sType;
  64. }
  65. else if (stricmp(stStaticinfo.strMachineType,"RVC.CardStore")==0)
  66. {
  67. LOG_TRACE("the type is RVC.CardStore");
  68. m_eDeviceType = eCardStore;
  69. }
  70. else
  71. {
  72. LOG_TRACE("the type is standard");
  73. m_eDeviceType = eStand2sType;
  74. }
  75. int num = 0;
  76. CAutoArray<CSimpleStringA> ConfigFiles;
  77. if ((ePadtype == m_eDeviceType)||(eMobilePadType == m_eDeviceType))
  78. {
  79. Error = spConfig->ReadConfigValue("padLoad", "Number", str);
  80. if (Error != Error_Succeed)
  81. {
  82. LOG_TRACE("load config failed, read load.number failed!");
  83. return Error;
  84. }
  85. num = atoi((LPCSTR)str);
  86. ConfigFiles.Init(num);
  87. CSimpleStringA strConfigDir;
  88. Error = spEntityFunction->GetPath("CFG", strConfigDir);
  89. if (Error != Error_Succeed)
  90. {
  91. LOG_TRACE("get etc directory failed!");
  92. return Error;
  93. }
  94. for (int i = 0; i < num; ++i) {
  95. char t[32];
  96. _itoa(i+1, &t[0], 10);
  97. Error = spConfig->ReadConfigValue("padLoad", &t[0], str);
  98. if (Error != Error_Succeed)
  99. {
  100. LOG_TRACE("load config failed! read num %d failed!", i+1);
  101. return Error;
  102. }
  103. ConfigFiles[i] = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (LPCSTR)strConfigDir, (LPCSTR)str);
  104. }
  105. }
  106. else if (eRpm1sType == m_eDeviceType) // add by ly
  107. {
  108. Error = spConfig->ReadConfigValue("Rpm1sLoad", "Number", str);
  109. if (Error != Error_Succeed)
  110. {
  111. LOG_TRACE("load config failed, read rpm1sLoad.number failed!");
  112. return Error;
  113. }
  114. num = atoi((LPCSTR)str);
  115. ConfigFiles.Init(num);
  116. CSimpleStringA strConfigDir;
  117. Error = spEntityFunction->GetPath("CFG", strConfigDir);
  118. if (Error != Error_Succeed)
  119. {
  120. LOG_TRACE("get etc directory failed!");
  121. return Error;
  122. }
  123. for (int i = 0; i < num; ++i) {
  124. char t[32];
  125. _itoa(i+1, &t[0], 10);
  126. Error = spConfig->ReadConfigValue("Rpm1sLoad", &t[0], str);
  127. if (Error != Error_Succeed)
  128. {
  129. LOG_TRACE("load config failed! read num %d failed!", i+1);
  130. return Error;
  131. }
  132. ConfigFiles[i] = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (LPCSTR)strConfigDir, (LPCSTR)str);
  133. }
  134. }
  135. else if (eDesk2SType == m_eDeviceType)
  136. {
  137. Error = spConfig->ReadConfigValue("Desk2SLoad", "Number", str);
  138. if (Error != Error_Succeed)
  139. {
  140. LOG_TRACE("load config failed, read Desk2Sload.number failed!");
  141. return Error;
  142. }
  143. else
  144. {
  145. LOG_TRACE("read Desk2Sload.number success!");
  146. }
  147. num = atoi((LPCSTR)str);
  148. ConfigFiles.Init(num);
  149. CSimpleStringA strConfigDir;
  150. Error = spEntityFunction->GetPath("CFG", strConfigDir);
  151. if (Error != Error_Succeed)
  152. {
  153. LOG_TRACE("get etc directory failed!");
  154. return Error;
  155. }
  156. for (int i = 0; i < num; ++i)
  157. {
  158. char t[32];
  159. _itoa(i+1, &t[0], 10);
  160. Error = spConfig->ReadConfigValue("Desk2SLoad", &t[0], str);
  161. if (Error != Error_Succeed)
  162. {
  163. LOG_TRACE("load config failed! read num %d failed!", i+1);
  164. return Error;
  165. }
  166. ConfigFiles[i] = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (LPCSTR)strConfigDir, (LPCSTR)str);
  167. }
  168. }
  169. else if (eCardStore == m_eDeviceType)
  170. {
  171. Error = spConfig->ReadConfigValue("CardStoreLoad", "Number", str);
  172. if (Error != Error_Succeed)
  173. {
  174. LOG_TRACE("load config failed, read CardStoreLoad.number failed!");
  175. return Error;
  176. }
  177. else
  178. {
  179. LOG_TRACE("read CardStoreLoad.number success!");
  180. }
  181. num = atoi((LPCSTR)str);
  182. ConfigFiles.Init(num);
  183. CSimpleStringA strConfigDir;
  184. Error = spEntityFunction->GetPath("CFG", strConfigDir);
  185. if (Error != Error_Succeed)
  186. {
  187. LOG_TRACE("get etc directory failed!");
  188. return Error;
  189. }
  190. for (int i = 0; i < num; ++i)
  191. {
  192. char t[32];
  193. _itoa(i+1, &t[0], 10);
  194. Error = spConfig->ReadConfigValue("CardStoreLoad", &t[0], str);
  195. if (Error != Error_Succeed)
  196. {
  197. LOG_TRACE("load config failed! read num %d failed!", i+1);
  198. return Error;
  199. }
  200. ConfigFiles[i] = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (LPCSTR)strConfigDir, (LPCSTR)str);
  201. }
  202. }
  203. else
  204. {
  205. Error = spConfig->ReadConfigValue("Load", "Number", str);
  206. if (Error != Error_Succeed)
  207. {
  208. LOG_TRACE("load config failed, read load.number failed!");
  209. return Error;
  210. }
  211. num = atoi((LPCSTR)str);
  212. ConfigFiles.Init(num);
  213. CSimpleStringA strConfigDir;
  214. Error = spEntityFunction->GetPath("CFG", strConfigDir);
  215. if (Error != Error_Succeed)
  216. {
  217. LOG_TRACE("get etc directory failed!");
  218. return Error;
  219. }
  220. for (int i = 0; i < num; ++i) {
  221. char t[32];
  222. _itoa(i+1, &t[0], 10);
  223. Error = spConfig->ReadConfigValue("Load", &t[0], str);
  224. if (Error != Error_Succeed)
  225. {
  226. LOG_TRACE("load config failed! read num %d failed!", i+1);
  227. return Error;
  228. }
  229. ConfigFiles[i] = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (LPCSTR)strConfigDir, (LPCSTR)str);
  230. }
  231. }
  232. //is log
  233. Error = spConfig->ReadConfigValue("Log", "IsLog", str);
  234. if (Error != Error_Succeed)
  235. {
  236. LOG_TRACE("load config failed, read Log.IsLog failed!");
  237. return Error;
  238. }
  239. m_nIslog = atoi(str);
  240. SetLogType(m_nIslog);
  241. evt_engine_t *engine;
  242. evt_engine_callback_t cb;
  243. cb.get_entity_id = &__get_entity_id;
  244. cb.get_sysevent = &__get_sysevent;
  245. cb.kill_timer = &__kill_timer;
  246. cb.new_timer_id = &__new_timer_id;
  247. cb.set_sysevent = &__set_sysevent;
  248. cb.set_timer = &__set_timer;
  249. cb.subscribe_log = &__subscribe_log;
  250. cb.subscribe_sysevent = &__subscribe_sysevent;
  251. cb.unsubscribe_log = &__unsubscribe_log;
  252. cb.unsubscribe_sysevent = &__unsubscribe_sysevent;
  253. cb.user_data = this;
  254. int rc = evt_engine_create(&cb, &engine);
  255. if (rc != 0)
  256. {
  257. LOG_TRACE("create engine failed!");
  258. return (ErrorCodeEnum)rc;
  259. }
  260. if (m_nIslog)
  261. {
  262. LOG_TRACE("create engine ok!");
  263. }
  264. if (num > 0) {
  265. const char **files = new const char*[num];
  266. for (int i = 0; i < num; ++i)
  267. files[i] = (const char*)ConfigFiles[i];
  268. rc = evt_engine_load(engine, num, files);
  269. delete files;
  270. if (rc != 0) {
  271. LOG_TRACE("load config failed!");
  272. return (ErrorCodeEnum)rc;
  273. }
  274. }
  275. rc = evt_engine_start(engine);
  276. if (rc != 0) {
  277. LOG_TRACE("start engine failed!");
  278. return (ErrorCodeEnum)rc;
  279. }
  280. m_pEngine = engine;
  281. return preOperationError;
  282. }
  283. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  284. {
  285. ErrorCodeEnum Error = __OnClose(Error_Succeed);
  286. pTransactionContext->SendAnswer(Error);
  287. }
  288. ErrorCodeEnum __OnClose(ErrorCodeEnum preOperationError)
  289. {
  290. int rc;
  291. rc = evt_engine_stop(m_pEngine);
  292. if (rc != 0) {
  293. LOG_TRACE("stop engine failed!");
  294. return (ErrorCodeEnum)rc;
  295. }
  296. rc = evt_engine_unload(m_pEngine);
  297. if (rc != 0) {
  298. LOG_TRACE("unload engine failed!");
  299. return (ErrorCodeEnum)rc;
  300. }
  301. evt_engine_destroy(m_pEngine);
  302. m_pEngine = NULL;
  303. return Error_Succeed;
  304. }
  305. virtual void OnSysVarEvent(const char *pszKey,const char *pszValue,const char *pszOldValue,const char *pszEntityName)
  306. {
  307. if (m_nIslog)
  308. {
  309. Dbg("process sysvar key=%s oldvalue=%s, value=%s", pszKey, pszOldValue, pszValue);
  310. }
  311. if (m_pEngine)
  312. {
  313. int rc = evt_engine_process_sysvar(m_pEngine, pszKey, pszValue, pszOldValue, pszEntityName);
  314. if (rc != 0)
  315. {
  316. Dbg("process sysvar failed! Error; %d, key=%s oldvalue=%s, value=%s", rc,pszKey, pszOldValue, pszValue);
  317. }
  318. else
  319. {
  320. if (m_nIslog)
  321. {
  322. Dbg("process sysvar success");
  323. }
  324. }
  325. }
  326. }
  327. virtual void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  328. const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  329. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage)
  330. {
  331. if (m_nIslog)
  332. {
  333. Dbg("process eLogTpye = %d, dwUserCode = 0x%08x, %s", eLogType, dwUserCode, pszMessage);
  334. }
  335. if (m_pEngine) {
  336. int rc = evt_engine_process_log(m_pEngine, SubIDs, nLogID, eLogType, eLevel, dwSysError, dwUserCode, wEntityDevelID, pszEntityName, pszModuleName, pszMessage);
  337. if (rc != 0) {
  338. Dbg("process log failed! Error: %d", rc);
  339. }
  340. }
  341. }
  342. private:
  343. struct CTimerWrap : public ITimerListener, public IReleasable
  344. {
  345. virtual ~CTimerWrap() {}
  346. virtual void OnTimeout(DWORD dwTimerID)
  347. {
  348. (*m_pTimerCallback)((int)dwTimerID, pUserData);
  349. }
  350. evt_engine_timer_func m_pTimerCallback;
  351. void *pUserData;
  352. };
  353. //.....
  354. int set_timer(int timer_id, int interval, evt_engine_timer_func timer_cb, void *timer_user_data)
  355. {
  356. CTimerWrap *pWarp = new CTimerWrap();
  357. pWarp->m_pTimerCallback = timer_cb;
  358. pWarp->pUserData = timer_user_data;
  359. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  360. ErrorCodeEnum Error = spEntityFunction->SetTimer((DWORD)timer_id, pWarp, (DWORD)interval);
  361. if (Error != Error_Succeed) {
  362. LOG_TRACE("set timer %d failed!", timer_id);
  363. delete pWarp;
  364. } else {
  365. spEntityFunction->SetTimerData((DWORD)timer_id, pWarp);
  366. }
  367. return (int)Error;
  368. }
  369. int kill_timer(int timer_id)
  370. {
  371. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  372. CSmartPointer<IReleasable> pWarp;
  373. spEntityFunction->GetTimerData((DWORD)timer_id, pWarp);
  374. spEntityFunction->KillTimer((DWORD)timer_id);
  375. return 0;
  376. }
  377. int subscribe_log(unsigned long long *sub_id, LogTypeEnum eLogType, const char *szEntity, SeverityLevelEnum eSeverityLevel, ErrorCodeEnum eSysError, DWORD dwUserCode, bool bIgnoreMessage)
  378. {
  379. CUUID uuid;
  380. ErrorCodeEnum Error = GetFunction()->SubscribeLog(uuid, this, eLogType, eSeverityLevel, eSysError, dwUserCode, szEntity, bIgnoreMessage);
  381. if (Error != Error_Succeed) {
  382. LOG_TRACE("subscribe log failed! Error = %d", Error);
  383. } else {
  384. *sub_id = (unsigned long long)uuid;
  385. if (m_nIslog)
  386. {
  387. LOG_TRACE("subscribe log ok!");
  388. }
  389. }
  390. return (int)Error;
  391. }
  392. int unsubscribe_log(unsigned long long sub_id)
  393. {
  394. ErrorCodeEnum Error = GetFunction()->UnsubscribeLog(sub_id);
  395. if (Error == Error_Succeed) {
  396. if (m_nIslog)
  397. {
  398. LOG_TRACE("unsubscribe log ok!");
  399. }
  400. } else {
  401. LOG_TRACE("unsubscribe log failed! Error = %d", Error);
  402. }
  403. return (int)Error;
  404. }
  405. int subscribe_sysevent()
  406. {
  407. if (m_nIslog)
  408. {
  409. LOG_TRACE("subscribe sysevent!");
  410. }
  411. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  412. return (int)spEntityFunction->RegistSysVarEvent("*", this);
  413. }
  414. int unsubscribe_sysevent()
  415. {
  416. if (m_nIslog)
  417. {
  418. LOG_TRACE("unsubscribe sysevent!");
  419. }
  420. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  421. return (int)spEntityFunction->UnregistSysVarEvent("*");
  422. }
  423. int get_sysevent(const char *key, CSimpleStringA &strValue)
  424. {
  425. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  426. return (int)spEntityFunction->GetSysVar(key, strValue);
  427. }
  428. int set_sysevent(const char *key,const char *val)
  429. {
  430. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  431. return (int)spEntityFunction->SetSysVar(key, val);
  432. }
  433. int get_entity_id(const char *entity)
  434. {
  435. CEntityStaticInfo Info;
  436. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  437. ErrorCodeEnum Error = spEntityFunction->GetEntityStaticInfo(entity, Info);
  438. if (Error == Error_Succeed) {
  439. return (int)Info.wEntityDevelopID;
  440. } else {
  441. return -1; // failed
  442. }
  443. }
  444. int new_timer_id()
  445. {
  446. return ++m_lIdSeq;
  447. }
  448. static int __set_timer(evt_engine_t *engine, int timer_id, int interval, evt_engine_timer_func timer_cb, void *timer_user_data, void *user_data)
  449. {
  450. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  451. return pThis->set_timer(timer_id, interval, timer_cb, timer_user_data);
  452. }
  453. static int __kill_timer(evt_engine_t *engine, int timer_id, void *user_data)
  454. {
  455. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  456. return pThis->kill_timer(timer_id);
  457. }
  458. static int __subscribe_log(evt_engine_t *engine, unsigned long long *sub_id, LogTypeEnum eLogType, const char *szEntity, SeverityLevelEnum eSeverityLevel, ErrorCodeEnum eSysError, DWORD dwUserCode, bool bIgnoreMessage, void *user_data)
  459. {
  460. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  461. return pThis->subscribe_log(sub_id, eLogType, szEntity, eSeverityLevel, eSysError, dwUserCode, bIgnoreMessage);
  462. }
  463. static int __unsubscribe_log(evt_engine_t *engine, unsigned long long sub_id, void *user_data)
  464. {
  465. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  466. return pThis->unsubscribe_log(sub_id);
  467. }
  468. static int __subscribe_sysevent(evt_engine_t *engine, void *user_data)
  469. {
  470. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  471. return pThis->subscribe_sysevent();
  472. }
  473. static int __unsubscribe_sysevent(evt_engine_t *engine, void *user_data)
  474. {
  475. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  476. return pThis->unsubscribe_sysevent();
  477. }
  478. static int __get_sysevent(evt_engine_t *engine, const char *key, CSimpleStringA &strValue, void *user_data)
  479. {
  480. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  481. return pThis->get_sysevent(key, strValue);
  482. }
  483. static int __set_sysevent(evt_engine_t *engine, const char *key,const char *val, void *user_data)
  484. {
  485. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  486. return pThis->set_sysevent(key, val);
  487. }
  488. static int __get_entity_id(evt_engine_t *engine, const char *entity, void *user_data)
  489. {
  490. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  491. return pThis->get_entity_id(entity);
  492. }
  493. static int __new_timer_id(evt_engine_t *engine, void *user_data)
  494. {
  495. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  496. return pThis->new_timer_id();
  497. }
  498. private:
  499. LONG m_lIdSeq;
  500. evt_engine_t *m_pEngine;
  501. int m_nIslog;
  502. DeviceTypeEnum m_eDeviceType; // add by chh
  503. };
  504. SP_BEGIN_ENTITY_MAP()
  505. SP_ENTITY(CEventConverterEntity)
  506. SP_END_ENTITY_MAP()