123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- #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<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> 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<IEntityFunction> 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<IConfigInfo> 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<ITransactionContext> pTransactionContext)
- {
- ErrorCodeEnum Error = __OnClose(Error_Succeed);
- pTransactionContext->SendAnswer(Error);
- }
- DeviceTypeEnum RvcGetDeviceType()
- {
- DeviceTypeEnum eType = eStand2sType;
- CSmartPointer<IEntityFunction> 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<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
- const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
- const CAutoArray<DWORD> &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<IEntityFunction> 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<IEntityFunction> spEntityFunction = GetFunction();
- CSmartPointer<IReleasable> 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<IEntityFunction> spEntityFunction = GetFunction();
- return (int)spEntityFunction->RegistSysVarEvent("*", this);
- }
- int unsubscribe_sysevent()
- {
- CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
- return (int)spEntityFunction->UnregistSysVarEvent("*");
- }
- int get_sysevent(const char *key, CSimpleStringA &strValue)
- {
- CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
- return (int)spEntityFunction->GetSysVar(key, strValue);
- }
- int set_sysevent(const char *key,const char *val)
- {
- CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
- return (int)spEntityFunction->SetSysVar(key, val);
- }
- int get_entity_id(const char *entity)
- {
- CEntityStaticInfo Info;
- CSmartPointer<IEntityFunction> 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<CEventConverterEntity *>(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<CEventConverterEntity *>(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<CEventConverterEntity *>(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<CEventConverterEntity *>(user_data);
- return pThis->unsubscribe_log(sub_id);
- }
- static int __subscribe_sysevent(evt_engine_t *engine, void *user_data)
- {
- CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(user_data);
- return pThis->subscribe_sysevent();
- }
- static int __unsubscribe_sysevent(evt_engine_t *engine, void *user_data)
- {
- CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(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<CEventConverterEntity *>(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<CEventConverterEntity *>(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<CEventConverterEntity *>(user_data);
- return pThis->get_entity_id(entity);
- }
- static int __new_timer_id(evt_engine_t *engine, void *user_data)
- {
- CEventConverterEntity *pThis = static_cast<CEventConverterEntity *>(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()
|