#include "stdafx.h" #include "SpBase.h" #include "evtengine.h" #include "EventCode.h" #include "fileutil.h" #include "modVer.h" #include "SipphoneEvent.hpp" #include "CustomerApproachEvent.hpp" #include "CustomerApproachEventCardStore.hpp" static slot_trigger_elem_t slot_trigger_arr[] = { {"SipphoneEvent", SipphoneSlotList, sizeof(SipphoneSlotList)/sizeof(rvc_slot_t), SipphoneTriggerList, sizeof(SipphoneTriggerList)/sizeof(rvc_trigger_t)}, {"CustomerApproachEvent", CustomerApproachSlotList,sizeof(CustomerApproachSlotList)/sizeof(rvc_slot_t), CustomerApproachTriggerList, sizeof(CustomerApproachTriggerList)/sizeof(rvc_trigger_t)}, {"CustomerApproachCardStoreEvent", CustomerApproachCardStoreSlotList,sizeof(CustomerApproachCardStoreSlotList) / sizeof(rvc_slot_t), CustomerApproachCardStoreTriggerList, sizeof(CustomerApproachCardStoreTriggerList) / sizeof(rvc_trigger_t)} }; class CEventConverterEntity : public CEntityBase, public ISysVarListener, public ILogListener { public: CEventConverterEntity() : m_pEngine(NULL), m_lIdSeq(0) {} virtual ~CEventConverterEntity() {} virtual const char *GetEntityName() const { return "EventConverter"; } const char* GetEntityVersion() const { return MODULE_VERSION_FULL; } virtual void OnPreStart(CAutoArray strArgs,CSmartPointer pTransactionContext) { ErrorCodeEnum Error = __OnStart(Error_Succeed); pTransactionContext->SendAnswer(Error); } ErrorCodeEnum __OnStart(ErrorCodeEnum preOperationError) { if (m_nIslog){ } m_eDeviceType = eStand2sType; m_nIslog = 1; ErrorCodeEnum Error; CSimpleStringA str; CSmartPointer spEntityFunction = GetFunction(); CSimpleStringA strConfigDir; Error = spEntityFunction->GetPath("CFG", strConfigDir); if (Error != Error_Succeed) { DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("get etc directory failed!"); return Error; } m_eDeviceType = RvcGetDeviceType(); const int num = 2; int indexarr[2] = { 0 }; if (eCardStore == m_eDeviceType) { indexarr[1] = 2; } else { indexarr[1] = 1; } CSmartPointer spConfigCenterSetting; Error = spEntityFunction->OpenConfig(Config_CenterSetting, spConfigCenterSetting); if (Error != Error_Succeed) { DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("open config failed!"); return Error; } Error = spConfigCenterSetting->ReadConfigValue(GetEntityName(), "IsLog", str); if (Error != Error_Succeed) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("load config failed, read Log.IsLog failed!"); return Error; } m_nIslog = atoi(str); SetLogType(m_nIslog); evt_engine_t *engine; evt_engine_callback_t cb; cb.get_entity_id = &__get_entity_id; cb.get_sysevent = &__get_sysevent; cb.kill_timer = &__kill_timer; cb.new_timer_id = &__new_timer_id; cb.set_sysevent = &__set_sysevent; cb.set_timer = &__set_timer; cb.subscribe_log = &__subscribe_log; cb.subscribe_sysevent = &__subscribe_sysevent; cb.unsubscribe_log = &__unsubscribe_log; cb.unsubscribe_sysevent = &__unsubscribe_sysevent; cb.user_data = this; int rc = evt_engine_create(&cb, &engine); if (rc != 0) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create engine failed!"); return (ErrorCodeEnum)rc; } if (num > 0) { rc = evt_engine_load(engine, indexarr, num, slot_trigger_arr, sizeof(slot_trigger_arr)/sizeof(slot_trigger_elem_t)); if (rc != 0) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("load config failed!"); return (ErrorCodeEnum)rc; } } rc = evt_engine_start(engine); if (rc != 0) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start engine failed!"); return (ErrorCodeEnum)rc; } m_pEngine = engine; return preOperationError; } virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer pTransactionContext) { ErrorCodeEnum Error = __OnClose(Error_Succeed); pTransactionContext->SendAnswer(Error); } DeviceTypeEnum RvcGetDeviceType() { DeviceTypeEnum eType = eStand2sType; CSmartPointer spFunction = GetFunction(); CSystemStaticInfo stStaticinfo; spFunction->GetSystemStaticInfo(stStaticinfo); if (_stricmp(stStaticinfo.strMachineType.GetData(), "RVC.Stand1SPlus") == 0) { eType = eStand1SPlusType; } else if (_stricmp(stStaticinfo.strMachineType.GetData(), "RVC.CardStore") == 0 || _stricmp(stStaticinfo.strMachineType.GetData(), "RVC.CardPrinter") == 0){ eType = eCardStore; } else{ eType = eStand2sType; } if (eType >= 0 && eType < sizeof(Device_Type_Table)/sizeof(char*)){ DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("device type is %s.", Device_Type_Table[eType]); } return eType; } ErrorCodeEnum __OnClose(ErrorCodeEnum preOperationError) { int rc; rc = evt_engine_stop(m_pEngine); if (rc != 0) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("stop engine failed!"); return (ErrorCodeEnum)rc; } rc = evt_engine_unload(m_pEngine); if (rc != 0) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unload engine failed!"); return (ErrorCodeEnum)rc; } evt_engine_destroy(m_pEngine); m_pEngine = NULL; return Error_Succeed; } virtual void OnSysVarEvent(const char *pszKey,const char *pszValue,const char *pszOldValue,const char *pszEntityName) { if (m_pEngine) { int rc = evt_engine_process_sysvar(m_pEngine, pszKey, pszValue, pszOldValue, pszEntityName); if (rc != 0) { DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("process sysvar failed! Error; %d, key=%s oldvalue=%s, value=%s", rc,pszKey, pszOldValue, pszValue); } } } virtual void OnLog(const CAutoArray &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel, const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID, const CAutoArray &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage, const linkContext &pLinkInfo) { if (m_pEngine) { int rc = evt_engine_process_log(m_pEngine, SubIDs, nLogID, eLogType, eLevel, dwSysError, dwUserCode, wEntityDevelID, pszEntityName, pszModuleName, pszMessage); if (rc != 0) { DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("process log failed! Error: %d", rc); } } } private: struct CTimerWrap : public ITimerListener, public IReleasable { virtual ~CTimerWrap() {} virtual void OnTimeout(DWORD dwTimerID) { (*m_pTimerCallback)((int)dwTimerID, pUserData); } evt_engine_timer_func m_pTimerCallback; void *pUserData; }; //..... int set_timer(int timer_id, int interval, evt_engine_timer_func timer_cb, void *timer_user_data) { CTimerWrap *pWarp = new CTimerWrap(); pWarp->m_pTimerCallback = timer_cb; pWarp->pUserData = timer_user_data; CSmartPointer spEntityFunction = GetFunction(); ErrorCodeEnum Error = spEntityFunction->SetTimer((DWORD)timer_id, pWarp, (DWORD)interval); if (Error != Error_Succeed) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("set timer %d failed!", timer_id); delete pWarp; } else { spEntityFunction->SetTimerData((DWORD)timer_id, pWarp); } return (int)Error; } int kill_timer(int timer_id) { CSmartPointer spEntityFunction = GetFunction(); CSmartPointer pWarp; spEntityFunction->GetTimerData((DWORD)timer_id, pWarp); spEntityFunction->KillTimer((DWORD)timer_id); return 0; } int subscribe_log(unsigned long long *sub_id, LogTypeEnum eLogType, const char *szEntity, SeverityLevelEnum eSeverityLevel, ErrorCodeEnum eSysError, DWORD dwUserCode, bool bIgnoreMessage) { CUUID uuid; ErrorCodeEnum Error = GetFunction()->SubscribeLog(uuid, this, eLogType, eSeverityLevel, eSysError, dwUserCode, szEntity, bIgnoreMessage); if (Error != Error_Succeed) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("subscribe log failed! Error = %d", Error); } else { *sub_id = (unsigned long long)uuid; } return (int)Error; } int unsubscribe_log(unsigned long long sub_id) { ErrorCodeEnum Error = GetFunction()->UnsubscribeLog(sub_id); if (Error == Error_Succeed) { } else { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unsubscribe log failed! Error = %d", Error); } return (int)Error; } int subscribe_sysevent() { CSmartPointer spEntityFunction = GetFunction(); return (int)spEntityFunction->RegistSysVarEvent("*", this); } int unsubscribe_sysevent() { CSmartPointer spEntityFunction = GetFunction(); return (int)spEntityFunction->UnregistSysVarEvent("*"); } int get_sysevent(const char *key, CSimpleStringA &strValue) { CSmartPointer spEntityFunction = GetFunction(); return (int)spEntityFunction->GetSysVar(key, strValue); } int set_sysevent(const char *key,const char *val) { CSmartPointer spEntityFunction = GetFunction(); return (int)spEntityFunction->SetSysVar(key, val); } int get_entity_id(const char *entity) { CEntityStaticInfo Info; CSmartPointer spEntityFunction = GetFunction(); ErrorCodeEnum Error = spEntityFunction->GetEntityStaticInfo(entity, Info); if (Error == Error_Succeed) { return (int)Info.wEntityDevelopID; } else { return -1; // failed } } int new_timer_id() { return ++m_lIdSeq; } 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) { CEventConverterEntity *pThis = static_cast(user_data); return pThis->set_timer(timer_id, interval, timer_cb, timer_user_data); } static int __kill_timer(evt_engine_t *engine, int timer_id, void *user_data) { CEventConverterEntity *pThis = static_cast(user_data); return pThis->kill_timer(timer_id); } 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) { CEventConverterEntity *pThis = static_cast(user_data); return pThis->subscribe_log(sub_id, eLogType, szEntity, eSeverityLevel, eSysError, dwUserCode, bIgnoreMessage); } static int __unsubscribe_log(evt_engine_t *engine, unsigned long long sub_id, void *user_data) { CEventConverterEntity *pThis = static_cast(user_data); return pThis->unsubscribe_log(sub_id); } static int __subscribe_sysevent(evt_engine_t *engine, void *user_data) { CEventConverterEntity *pThis = static_cast(user_data); return pThis->subscribe_sysevent(); } static int __unsubscribe_sysevent(evt_engine_t *engine, void *user_data) { CEventConverterEntity *pThis = static_cast(user_data); return pThis->unsubscribe_sysevent(); } static int __get_sysevent(evt_engine_t *engine, const char *key, CSimpleStringA &strValue, void *user_data) { CEventConverterEntity *pThis = static_cast(user_data); return pThis->get_sysevent(key, strValue); } static int __set_sysevent(evt_engine_t *engine, const char *key,const char *val, void *user_data) { CEventConverterEntity *pThis = static_cast(user_data); return pThis->set_sysevent(key, val); } static int __get_entity_id(evt_engine_t *engine, const char *entity, void *user_data) { CEventConverterEntity *pThis = static_cast(user_data); return pThis->get_entity_id(entity); } static int __new_timer_id(evt_engine_t *engine, void *user_data) { CEventConverterEntity *pThis = static_cast(user_data); return pThis->new_timer_id(); } private: LONG m_lIdSeq; evt_engine_t *m_pEngine; int m_nIslog; DeviceTypeEnum m_eDeviceType; }; SP_BEGIN_ENTITY_MAP() SP_ENTITY(CEventConverterEntity) SP_END_ENTITY_MAP()