mod_evtconverter.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 (num > 0) {
  83. rc = evt_engine_load(engine, indexarr, num, slot_trigger_arr, sizeof(slot_trigger_arr)/sizeof(slot_trigger_elem_t));
  84. if (rc != 0) {
  85. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("load config failed!");
  86. return (ErrorCodeEnum)rc;
  87. }
  88. }
  89. rc = evt_engine_start(engine);
  90. if (rc != 0) {
  91. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start engine failed!");
  92. return (ErrorCodeEnum)rc;
  93. }
  94. m_pEngine = engine;
  95. return preOperationError;
  96. }
  97. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  98. {
  99. ErrorCodeEnum Error = __OnClose(Error_Succeed);
  100. pTransactionContext->SendAnswer(Error);
  101. }
  102. DeviceTypeEnum RvcGetDeviceType()
  103. {
  104. DeviceTypeEnum eType = eStand2sType;
  105. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  106. CSystemStaticInfo stStaticinfo;
  107. spFunction->GetSystemStaticInfo(stStaticinfo);
  108. if (_stricmp(stStaticinfo.strMachineType.GetData(), "RVC.Stand1SPlus") == 0) {
  109. eType = eStand1SPlusType;
  110. }
  111. else if (_stricmp(stStaticinfo.strMachineType.GetData(), "RVC.CardStore") == 0 || _stricmp(stStaticinfo.strMachineType.GetData(), "RVC.CardPrinter") == 0){
  112. eType = eCardStore;
  113. }
  114. else{
  115. eType = eStand2sType;
  116. }
  117. if (eType >= 0 && eType < sizeof(Device_Type_Table)/sizeof(char*)){
  118. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("device type is %s.", Device_Type_Table[eType]);
  119. }
  120. return eType;
  121. }
  122. ErrorCodeEnum __OnClose(ErrorCodeEnum preOperationError)
  123. {
  124. int rc;
  125. rc = evt_engine_stop(m_pEngine);
  126. if (rc != 0) {
  127. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("stop engine failed!");
  128. return (ErrorCodeEnum)rc;
  129. }
  130. rc = evt_engine_unload(m_pEngine);
  131. if (rc != 0) {
  132. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unload engine failed!");
  133. return (ErrorCodeEnum)rc;
  134. }
  135. evt_engine_destroy(m_pEngine);
  136. m_pEngine = NULL;
  137. return Error_Succeed;
  138. }
  139. virtual void OnSysVarEvent(const char *pszKey,const char *pszValue,const char *pszOldValue,const char *pszEntityName)
  140. {
  141. if (m_pEngine) {
  142. int rc = evt_engine_process_sysvar(m_pEngine, pszKey, pszValue, pszOldValue, pszEntityName);
  143. if (rc != 0) {
  144. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("process sysvar failed! Error; %d, key=%s oldvalue=%s, value=%s", rc,pszKey, pszOldValue, pszValue);
  145. }
  146. }
  147. }
  148. virtual void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  149. const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  150. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage, const linkContext &pLinkInfo)
  151. {
  152. if (m_pEngine) {
  153. int rc = evt_engine_process_log(m_pEngine, SubIDs, nLogID, eLogType, eLevel, dwSysError, dwUserCode, wEntityDevelID, pszEntityName, pszModuleName, pszMessage);
  154. if (rc != 0) {
  155. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("process log failed! Error: %d", rc);
  156. }
  157. }
  158. }
  159. private:
  160. struct CTimerWrap : public ITimerListener, public IReleasable
  161. {
  162. virtual ~CTimerWrap() {}
  163. virtual void OnTimeout(DWORD dwTimerID)
  164. {
  165. (*m_pTimerCallback)((int)dwTimerID, pUserData);
  166. }
  167. evt_engine_timer_func m_pTimerCallback;
  168. void *pUserData;
  169. };
  170. //.....
  171. int set_timer(int timer_id, int interval, evt_engine_timer_func timer_cb, void *timer_user_data)
  172. {
  173. CTimerWrap *pWarp = new CTimerWrap();
  174. pWarp->m_pTimerCallback = timer_cb;
  175. pWarp->pUserData = timer_user_data;
  176. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  177. ErrorCodeEnum Error = spEntityFunction->SetTimer((DWORD)timer_id, pWarp, (DWORD)interval);
  178. if (Error != Error_Succeed) {
  179. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("set timer %d failed!", timer_id);
  180. delete pWarp;
  181. }
  182. else {
  183. spEntityFunction->SetTimerData((DWORD)timer_id, pWarp);
  184. }
  185. return (int)Error;
  186. }
  187. int kill_timer(int timer_id)
  188. {
  189. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  190. CSmartPointer<IReleasable> pWarp;
  191. spEntityFunction->GetTimerData((DWORD)timer_id, pWarp);
  192. spEntityFunction->KillTimer((DWORD)timer_id);
  193. return 0;
  194. }
  195. int subscribe_log(unsigned long long *sub_id, LogTypeEnum eLogType, const char *szEntity, SeverityLevelEnum eSeverityLevel, ErrorCodeEnum eSysError, DWORD dwUserCode, bool bIgnoreMessage)
  196. {
  197. CUUID uuid;
  198. ErrorCodeEnum Error = GetFunction()->SubscribeLog(uuid, this, eLogType, eSeverityLevel, eSysError, dwUserCode, szEntity, bIgnoreMessage);
  199. if (Error != Error_Succeed) {
  200. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("subscribe log failed! Error = %d", Error);
  201. }
  202. else {
  203. *sub_id = (unsigned long long)uuid;
  204. }
  205. return (int)Error;
  206. }
  207. int unsubscribe_log(unsigned long long sub_id)
  208. {
  209. ErrorCodeEnum Error = GetFunction()->UnsubscribeLog(sub_id);
  210. if (Error == Error_Succeed) {
  211. }
  212. else {
  213. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unsubscribe log failed! Error = %d", Error);
  214. }
  215. return (int)Error;
  216. }
  217. int subscribe_sysevent()
  218. {
  219. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  220. return (int)spEntityFunction->RegistSysVarEvent("*", this);
  221. }
  222. int unsubscribe_sysevent()
  223. {
  224. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  225. return (int)spEntityFunction->UnregistSysVarEvent("*");
  226. }
  227. int get_sysevent(const char *key, CSimpleStringA &strValue)
  228. {
  229. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  230. return (int)spEntityFunction->GetSysVar(key, strValue);
  231. }
  232. int set_sysevent(const char *key,const char *val)
  233. {
  234. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  235. return (int)spEntityFunction->SetSysVar(key, val);
  236. }
  237. int get_entity_id(const char *entity)
  238. {
  239. CEntityStaticInfo Info;
  240. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  241. ErrorCodeEnum Error = spEntityFunction->GetEntityStaticInfo(entity, Info);
  242. if (Error == Error_Succeed) {
  243. return (int)Info.wEntityDevelopID;
  244. }
  245. else {
  246. return -1; // failed
  247. }
  248. }
  249. int new_timer_id()
  250. {
  251. return ++m_lIdSeq;
  252. }
  253. 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)
  254. {
  255. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  256. return pThis->set_timer(timer_id, interval, timer_cb, timer_user_data);
  257. }
  258. static int __kill_timer(evt_engine_t *engine, int timer_id, void *user_data)
  259. {
  260. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  261. return pThis->kill_timer(timer_id);
  262. }
  263. 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)
  264. {
  265. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  266. return pThis->subscribe_log(sub_id, eLogType, szEntity, eSeverityLevel, eSysError, dwUserCode, bIgnoreMessage);
  267. }
  268. static int __unsubscribe_log(evt_engine_t *engine, unsigned long long sub_id, void *user_data)
  269. {
  270. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  271. return pThis->unsubscribe_log(sub_id);
  272. }
  273. static int __subscribe_sysevent(evt_engine_t *engine, void *user_data)
  274. {
  275. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  276. return pThis->subscribe_sysevent();
  277. }
  278. static int __unsubscribe_sysevent(evt_engine_t *engine, void *user_data)
  279. {
  280. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  281. return pThis->unsubscribe_sysevent();
  282. }
  283. static int __get_sysevent(evt_engine_t *engine, const char *key, CSimpleStringA &strValue, void *user_data)
  284. {
  285. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  286. return pThis->get_sysevent(key, strValue);
  287. }
  288. static int __set_sysevent(evt_engine_t *engine, const char *key,const char *val, void *user_data)
  289. {
  290. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  291. return pThis->set_sysevent(key, val);
  292. }
  293. static int __get_entity_id(evt_engine_t *engine, const char *entity, void *user_data)
  294. {
  295. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  296. return pThis->get_entity_id(entity);
  297. }
  298. static int __new_timer_id(evt_engine_t *engine, void *user_data)
  299. {
  300. CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
  301. return pThis->new_timer_id();
  302. }
  303. private:
  304. LONG m_lIdSeq;
  305. evt_engine_t *m_pEngine;
  306. int m_nIslog;
  307. DeviceTypeEnum m_eDeviceType;
  308. };
  309. SP_BEGIN_ENTITY_MAP()
  310. SP_ENTITY(CEventConverterEntity)
  311. SP_END_ENTITY_MAP()