mod_evtconverter.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "evtengine.h"
  4. #include "EventCode.h"
  5. #include "fileutil.h"
  6. #include "modVer.h"
  7. #include "SipphoneEvent.hpp"
  8. #include "CustomerApproachEvent.hpp"
  9. #include "CustomerApproachEventCardStore.hpp"
  10. static slot_trigger_elem_t slot_trigger_arr[] = {
  11. {"SipphoneEvent", SipphoneSlotList, sizeof(SipphoneSlotList)/sizeof(rvc_slot_t), SipphoneTriggerList, sizeof(SipphoneTriggerList)/sizeof(rvc_trigger_t)},
  12. {"CustomerApproachEvent", CustomerApproachSlotList,sizeof(CustomerApproachSlotList)/sizeof(rvc_slot_t), CustomerApproachTriggerList, sizeof(CustomerApproachTriggerList)/sizeof(rvc_trigger_t)},
  13. {"CustomerApproachCardStoreEvent", CustomerApproachCardStoreSlotList,sizeof(CustomerApproachCardStoreSlotList) / sizeof(rvc_slot_t), CustomerApproachCardStoreTriggerList, sizeof(CustomerApproachCardStoreTriggerList) / sizeof(rvc_trigger_t)}
  14. };
  15. class CEventConverterEntity : public CEntityBase, public ISysVarListener, public ILogListener
  16. {
  17. public:
  18. CEventConverterEntity() : m_pEngine(NULL), m_lIdSeq(0) {}
  19. virtual ~CEventConverterEntity() {}
  20. virtual const char *GetEntityName() const { return "EventConverter"; }
  21. const char* GetEntityVersion() const { return MODULE_VERSION_FULL; }
  22. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  23. {
  24. ErrorCodeEnum Error = __OnStart(Error_Succeed);
  25. pTransactionContext->SendAnswer(Error);
  26. }
  27. ErrorCodeEnum __OnStart(ErrorCodeEnum preOperationError)
  28. {
  29. if (m_nIslog){
  30. }
  31. m_eDeviceType = eStand2sType;
  32. m_nIslog = 1;
  33. ErrorCodeEnum Error;
  34. CSimpleStringA str;
  35. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  36. CSimpleStringA strConfigDir;
  37. Error = spEntityFunction->GetPath("CFG", strConfigDir);
  38. if (Error != Error_Succeed) {
  39. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("get etc directory failed!");
  40. return Error;
  41. }
  42. m_eDeviceType = RvcGetDeviceType();
  43. const int num = 2;
  44. int indexarr[2] = { 0 };
  45. if (eCardStore == m_eDeviceType) {
  46. indexarr[1] = 2;
  47. }
  48. else {
  49. indexarr[1] = 1;
  50. }
  51. CSmartPointer<IConfigInfo> spConfigCenterSetting;
  52. Error = spEntityFunction->OpenConfig(Config_CenterSetting, spConfigCenterSetting);
  53. if (Error != Error_Succeed) {
  54. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("open config failed!");
  55. return Error;
  56. }
  57. Error = spConfigCenterSetting->ReadConfigValue(GetEntityName(), "IsLog", str);
  58. if (Error != Error_Succeed) {
  59. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("load config failed, read Log.IsLog failed!");
  60. return Error;
  61. }
  62. m_nIslog = atoi(str);
  63. SetLogType(m_nIslog);
  64. evt_engine_t *engine;
  65. evt_engine_callback_t cb;
  66. cb.get_entity_id = &__get_entity_id;
  67. cb.get_sysevent = &__get_sysevent;
  68. cb.kill_timer = &__kill_timer;
  69. cb.new_timer_id = &__new_timer_id;
  70. cb.set_sysevent = &__set_sysevent;
  71. cb.set_timer = &__set_timer;
  72. cb.subscribe_log = &__subscribe_log;
  73. cb.subscribe_sysevent = &__subscribe_sysevent;
  74. cb.unsubscribe_log = &__unsubscribe_log;
  75. cb.unsubscribe_sysevent = &__unsubscribe_sysevent;
  76. cb.user_data = this;
  77. int rc = evt_engine_create(&cb, &engine);
  78. if (rc != 0) {
  79. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create engine failed!");
  80. return (ErrorCodeEnum)rc;
  81. }
  82. if (m_nIslog) {
  83. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create engine ok!");
  84. }
  85. if (num > 0) {
  86. rc = evt_engine_load(engine, indexarr, num, slot_trigger_arr, sizeof(slot_trigger_arr)/sizeof(slot_trigger_elem_t));
  87. if (rc != 0) {
  88. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("load config failed!");
  89. return (ErrorCodeEnum)rc;
  90. }
  91. }
  92. rc = evt_engine_start(engine);
  93. if (rc != 0) {
  94. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start engine failed!");
  95. return (ErrorCodeEnum)rc;
  96. }
  97. m_pEngine = engine;
  98. return preOperationError;
  99. }
  100. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  101. {
  102. ErrorCodeEnum Error = __OnClose(Error_Succeed);
  103. pTransactionContext->SendAnswer(Error);
  104. }
  105. DeviceTypeEnum RvcGetDeviceType()
  106. {
  107. DeviceTypeEnum eType = eStand2sType;
  108. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  109. CSystemStaticInfo stStaticinfo;
  110. spFunction->GetSystemStaticInfo(stStaticinfo);
  111. if (_stricmp(stStaticinfo.strMachineType.GetData(), "RVC.Stand1SPlus") == 0) {
  112. eType = eStand1SPlusType;
  113. }
  114. else if (_stricmp(stStaticinfo.strMachineType.GetData(), "RVC.CardStore") == 0 || _stricmp(stStaticinfo.strMachineType.GetData(), "RVC.CardPrinter") == 0){
  115. eType = eCardStore;
  116. }
  117. else{
  118. eType = eStand2sType;
  119. }
  120. if (eType >= 0 && eType < sizeof(Device_Type_Table)/sizeof(char*)){
  121. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("device type is %s.", Device_Type_Table[eType]);
  122. }
  123. return eType;
  124. }
  125. ErrorCodeEnum __OnClose(ErrorCodeEnum preOperationError)
  126. {
  127. int rc;
  128. rc = evt_engine_stop(m_pEngine);
  129. if (rc != 0) {
  130. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("stop engine failed!");
  131. return (ErrorCodeEnum)rc;
  132. }
  133. rc = evt_engine_unload(m_pEngine);
  134. if (rc != 0) {
  135. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unload engine failed!");
  136. return (ErrorCodeEnum)rc;
  137. }
  138. evt_engine_destroy(m_pEngine);
  139. m_pEngine = NULL;
  140. return Error_Succeed;
  141. }
  142. virtual void OnSysVarEvent(const char *pszKey,const char *pszValue,const char *pszOldValue,const char *pszEntityName)
  143. {
  144. //if (m_nIslog)
  145. {
  146. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("process sysvar key=%s oldvalue=%s, value=%s", pszKey, pszOldValue, pszValue);
  147. }
  148. if (m_pEngine)
  149. {
  150. int rc = evt_engine_process_sysvar(m_pEngine, pszKey, pszValue, pszOldValue, pszEntityName);
  151. if (rc != 0)
  152. {
  153. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("process sysvar failed! Error; %d, key=%s oldvalue=%s, value=%s", rc,pszKey, pszOldValue, pszValue);
  154. }
  155. else
  156. {
  157. if (m_nIslog)
  158. {
  159. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("process sysvar success");
  160. }
  161. }
  162. }
  163. }
  164. virtual void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  165. const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  166. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage, const linkContext &pLinkInfo)
  167. {
  168. if (m_nIslog){
  169. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("process eLogTpye = %d, dwUserCode = 0x%08x, %s", eLogType, dwUserCode, pszMessage);
  170. }
  171. if (m_pEngine) {
  172. int rc = evt_engine_process_log(m_pEngine, SubIDs, nLogID, eLogType, eLevel, dwSysError, dwUserCode, wEntityDevelID, pszEntityName, pszModuleName, pszMessage);
  173. if (rc != 0) {
  174. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("process log failed! Error: %d", rc);
  175. }
  176. }
  177. }
  178. private:
  179. struct CTimerWrap : public ITimerListener, public IReleasable
  180. {
  181. virtual ~CTimerWrap() {}
  182. virtual void OnTimeout(DWORD dwTimerID)
  183. {
  184. (*m_pTimerCallback)((int)dwTimerID, pUserData);
  185. }
  186. evt_engine_timer_func m_pTimerCallback;
  187. void *pUserData;
  188. };
  189. //.....
  190. int set_timer(int timer_id, int interval, evt_engine_timer_func timer_cb, void *timer_user_data)
  191. {
  192. CTimerWrap *pWarp = new CTimerWrap();
  193. pWarp->m_pTimerCallback = timer_cb;
  194. pWarp->pUserData = timer_user_data;
  195. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  196. ErrorCodeEnum Error = spEntityFunction->SetTimer((DWORD)timer_id, pWarp, (DWORD)interval);
  197. if (Error != Error_Succeed) {
  198. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("set timer %d failed!", timer_id);
  199. delete pWarp;
  200. }
  201. else {
  202. spEntityFunction->SetTimerData((DWORD)timer_id, pWarp);
  203. }
  204. return (int)Error;
  205. }
  206. int kill_timer(int timer_id)
  207. {
  208. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  209. CSmartPointer<IReleasable> pWarp;
  210. spEntityFunction->GetTimerData((DWORD)timer_id, pWarp);
  211. spEntityFunction->KillTimer((DWORD)timer_id);
  212. return 0;
  213. }
  214. int subscribe_log(unsigned long long *sub_id, LogTypeEnum eLogType, const char *szEntity, SeverityLevelEnum eSeverityLevel, ErrorCodeEnum eSysError, DWORD dwUserCode, bool bIgnoreMessage)
  215. {
  216. CUUID uuid;
  217. ErrorCodeEnum Error = GetFunction()->SubscribeLog(uuid, this, eLogType, eSeverityLevel, eSysError, dwUserCode, szEntity, bIgnoreMessage);
  218. if (Error != Error_Succeed) {
  219. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("subscribe log failed! Error = %d", Error);
  220. } else {
  221. *sub_id = (unsigned long long)uuid;
  222. if (m_nIslog)
  223. {
  224. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("subscribe log ok!");
  225. }
  226. }
  227. return (int)Error;
  228. }
  229. int unsubscribe_log(unsigned long long sub_id)
  230. {
  231. ErrorCodeEnum Error = GetFunction()->UnsubscribeLog(sub_id);
  232. if (Error == Error_Succeed) {
  233. if (m_nIslog){
  234. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unsubscribe log ok!");
  235. }
  236. }
  237. else {
  238. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unsubscribe log failed! Error = %d", Error);
  239. }
  240. return (int)Error;
  241. }
  242. int subscribe_sysevent()
  243. {
  244. if (m_nIslog){
  245. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("subscribe sysevent!");
  246. }
  247. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  248. return (int)spEntityFunction->RegistSysVarEvent("*", this);
  249. }
  250. int unsubscribe_sysevent()
  251. {
  252. if (m_nIslog){
  253. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unsubscribe sysevent!");
  254. }
  255. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  256. return (int)spEntityFunction->UnregistSysVarEvent("*");
  257. }
  258. int get_sysevent(const char *key, CSimpleStringA &strValue)
  259. {
  260. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  261. return (int)spEntityFunction->GetSysVar(key, strValue);
  262. }
  263. int set_sysevent(const char *key,const char *val)
  264. {
  265. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  266. return (int)spEntityFunction->SetSysVar(key, val);
  267. }
  268. int get_entity_id(const char *entity)
  269. {
  270. CEntityStaticInfo Info;
  271. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  272. ErrorCodeEnum Error = spEntityFunction->GetEntityStaticInfo(entity, Info);
  273. if (Error == Error_Succeed) {
  274. return (int)Info.wEntityDevelopID;
  275. }
  276. else {
  277. return -1; // failed
  278. }
  279. }
  280. int new_timer_id()
  281. {
  282. return ++m_lIdSeq;
  283. }
  284. 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)
  285. {
  286. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  287. return pThis->set_timer(timer_id, interval, timer_cb, timer_user_data);
  288. }
  289. static int __kill_timer(evt_engine_t *engine, int timer_id, void *user_data)
  290. {
  291. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  292. return pThis->kill_timer(timer_id);
  293. }
  294. 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)
  295. {
  296. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  297. return pThis->subscribe_log(sub_id, eLogType, szEntity, eSeverityLevel, eSysError, dwUserCode, bIgnoreMessage);
  298. }
  299. static int __unsubscribe_log(evt_engine_t *engine, unsigned long long sub_id, void *user_data)
  300. {
  301. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  302. return pThis->unsubscribe_log(sub_id);
  303. }
  304. static int __subscribe_sysevent(evt_engine_t *engine, void *user_data)
  305. {
  306. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  307. return pThis->subscribe_sysevent();
  308. }
  309. static int __unsubscribe_sysevent(evt_engine_t *engine, void *user_data)
  310. {
  311. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  312. return pThis->unsubscribe_sysevent();
  313. }
  314. static int __get_sysevent(evt_engine_t *engine, const char *key, CSimpleStringA &strValue, void *user_data)
  315. {
  316. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  317. return pThis->get_sysevent(key, strValue);
  318. }
  319. static int __set_sysevent(evt_engine_t *engine, const char *key,const char *val, void *user_data)
  320. {
  321. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  322. return pThis->set_sysevent(key, val);
  323. }
  324. static int __get_entity_id(evt_engine_t *engine, const char *entity, void *user_data)
  325. {
  326. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  327. return pThis->get_entity_id(entity);
  328. }
  329. static int __new_timer_id(evt_engine_t *engine, void *user_data)
  330. {
  331. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  332. return pThis->new_timer_id();
  333. }
  334. private:
  335. LONG m_lIdSeq;
  336. evt_engine_t *m_pEngine;
  337. int m_nIslog;
  338. DeviceTypeEnum m_eDeviceType;
  339. };
  340. SP_BEGIN_ENTITY_MAP()
  341. SP_ENTITY(CEventConverterEntity)
  342. SP_END_ENTITY_MAP()