| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533 |
- #include "stdafx.h"
- #include "SpBase.h"
- #include "evtengine.h"
- #include "../include/EventCode.h"
- #include "fileutil.h"
- #include "modVer.h"
- 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)
- {
- LOG_FUNCTION();
- }
- //MessageBoxA(0,0,0,0);
- m_eDeviceType = eStand2sType; //add by chh
- m_nIslog = 1;
- ErrorCodeEnum Error;
- CSimpleStringA str;
- CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
- CSmartPointer<IConfigInfo> spConfig;
- Error = spEntityFunction->OpenConfig(Config_Software, spConfig);
- if (Error != Error_Succeed) {
- LOG_TRACE("open config failed!");
- return Error;
- }
- //is Pad Version
- CSystemStaticInfo stStaticinfo;
- CSmartPointer<IEntityFunction> spFunction = GetFunction();
- spFunction->GetSystemStaticInfo(stStaticinfo);
- LOG_TRACE("stStaticinfo.strMachineType = %s",stStaticinfo.strMachineType.GetData());
- if (stricmp(stStaticinfo.strMachineType,"RVC.PAD")==0)
- {
- if (stricmp(stStaticinfo.strSite,"CMB.FLB")==0)
- {
- LOG_TRACE("the type is mobile pad");
- m_eDeviceType = eMobilePadType;
- }
- else
- {
- LOG_TRACE("the type is pad");
- m_eDeviceType = ePadtype;
- }
- }
- else if (stricmp(stStaticinfo.strMachineType,"RVC.Desk2S")==0)
- {
- LOG_TRACE("the type is Desk2S");
- m_eDeviceType = eDesk2SType;
- }
- else if (stricmp(stStaticinfo.strMachineType,"RPM.Stand1S")==0)
- {
- LOG_TRACE("the type is RPM.Stand1S");
- m_eDeviceType = eRpm1sType;
- }
- else if (stricmp(stStaticinfo.strMachineType,"RVC.CardStore")==0)
- {
- LOG_TRACE("the type is RVC.CardStore");
- m_eDeviceType = eCardStore;
- }
- else
- {
- LOG_TRACE("the type is standard");
- m_eDeviceType = eStand2sType;
- }
- int num = 0;
- CAutoArray<CSimpleStringA> ConfigFiles;
- if ((ePadtype == m_eDeviceType)||(eMobilePadType == m_eDeviceType))
- {
- Error = spConfig->ReadConfigValue("padLoad", "Number", str);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("load config failed, read load.number failed!");
- return Error;
- }
- num = atoi((LPCSTR)str);
- ConfigFiles.Init(num);
- CSimpleStringA strConfigDir;
- Error = spEntityFunction->GetPath("CFG", strConfigDir);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("get etc directory failed!");
- return Error;
- }
- for (int i = 0; i < num; ++i) {
- char t[32];
- _itoa(i+1, &t[0], 10);
- Error = spConfig->ReadConfigValue("padLoad", &t[0], str);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("load config failed! read num %d failed!", i+1);
- return Error;
- }
- ConfigFiles[i] = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (LPCSTR)strConfigDir, (LPCSTR)str);
- }
- }
- else if (eRpm1sType == m_eDeviceType) // add by ly
- {
- Error = spConfig->ReadConfigValue("Rpm1sLoad", "Number", str);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("load config failed, read rpm1sLoad.number failed!");
- return Error;
- }
- num = atoi((LPCSTR)str);
- ConfigFiles.Init(num);
- CSimpleStringA strConfigDir;
- Error = spEntityFunction->GetPath("CFG", strConfigDir);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("get etc directory failed!");
- return Error;
- }
- for (int i = 0; i < num; ++i) {
- char t[32];
- _itoa(i+1, &t[0], 10);
- Error = spConfig->ReadConfigValue("Rpm1sLoad", &t[0], str);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("load config failed! read num %d failed!", i+1);
- return Error;
- }
- ConfigFiles[i] = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (LPCSTR)strConfigDir, (LPCSTR)str);
- }
- }
- else if (eDesk2SType == m_eDeviceType)
- {
- Error = spConfig->ReadConfigValue("Desk2SLoad", "Number", str);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("load config failed, read Desk2Sload.number failed!");
- return Error;
- }
- else
- {
- LOG_TRACE("read Desk2Sload.number success!");
- }
- num = atoi((LPCSTR)str);
- ConfigFiles.Init(num);
- CSimpleStringA strConfigDir;
- Error = spEntityFunction->GetPath("CFG", strConfigDir);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("get etc directory failed!");
- return Error;
- }
- for (int i = 0; i < num; ++i)
- {
- char t[32];
- _itoa(i+1, &t[0], 10);
- Error = spConfig->ReadConfigValue("Desk2SLoad", &t[0], str);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("load config failed! read num %d failed!", i+1);
- return Error;
- }
- ConfigFiles[i] = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (LPCSTR)strConfigDir, (LPCSTR)str);
- }
- }
- else if (eCardStore == m_eDeviceType)
- {
- Error = spConfig->ReadConfigValue("CardStoreLoad", "Number", str);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("load config failed, read CardStoreLoad.number failed!");
- return Error;
- }
- else
- {
- LOG_TRACE("read CardStoreLoad.number success!");
- }
- num = atoi((LPCSTR)str);
- ConfigFiles.Init(num);
- CSimpleStringA strConfigDir;
- Error = spEntityFunction->GetPath("CFG", strConfigDir);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("get etc directory failed!");
- return Error;
- }
- for (int i = 0; i < num; ++i)
- {
- char t[32];
- _itoa(i+1, &t[0], 10);
- Error = spConfig->ReadConfigValue("CardStoreLoad", &t[0], str);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("load config failed! read num %d failed!", i+1);
- return Error;
- }
- ConfigFiles[i] = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (LPCSTR)strConfigDir, (LPCSTR)str);
- }
- }
- else
- {
- Error = spConfig->ReadConfigValue("Load", "Number", str);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("load config failed, read load.number failed!");
- return Error;
- }
- num = atoi((LPCSTR)str);
- ConfigFiles.Init(num);
- CSimpleStringA strConfigDir;
- Error = spEntityFunction->GetPath("CFG", strConfigDir);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("get etc directory failed!");
- return Error;
- }
- for (int i = 0; i < num; ++i) {
- char t[32];
- _itoa(i+1, &t[0], 10);
- Error = spConfig->ReadConfigValue("Load", &t[0], str);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("load config failed! read num %d failed!", i+1);
- return Error;
- }
- ConfigFiles[i] = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (LPCSTR)strConfigDir, (LPCSTR)str);
- }
- }
- //is log
- Error = spConfig->ReadConfigValue("Log", "IsLog", str);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("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)
- {
- LOG_TRACE("create engine failed!");
- return (ErrorCodeEnum)rc;
- }
- if (m_nIslog)
- {
- LOG_TRACE("create engine ok!");
- }
- if (num > 0) {
- const char **files = new const char*[num];
- for (int i = 0; i < num; ++i)
- files[i] = (const char*)ConfigFiles[i];
- rc = evt_engine_load(engine, num, files);
- delete files;
- if (rc != 0) {
- LOG_TRACE("load config failed!");
- return (ErrorCodeEnum)rc;
- }
- }
- rc = evt_engine_start(engine);
- if (rc != 0) {
- LOG_TRACE("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);
- }
- ErrorCodeEnum __OnClose(ErrorCodeEnum preOperationError)
- {
- int rc;
- rc = evt_engine_stop(m_pEngine);
- if (rc != 0) {
- LOG_TRACE("stop engine failed!");
- return (ErrorCodeEnum)rc;
- }
- rc = evt_engine_unload(m_pEngine);
- if (rc != 0) {
- LOG_TRACE("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_nIslog)
- {
- Dbg("process sysvar key=%s oldvalue=%s, value=%s", pszKey, pszOldValue, pszValue);
- }
- if (m_pEngine)
- {
- int rc = evt_engine_process_sysvar(m_pEngine, pszKey, pszValue, pszOldValue, pszEntityName);
- if (rc != 0)
- {
- Dbg("process sysvar failed! Error; %d, key=%s oldvalue=%s, value=%s", rc,pszKey, pszOldValue, pszValue);
- }
- else
- {
- if (m_nIslog)
- {
- Dbg("process sysvar success");
- }
- }
- }
- }
- 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)
- {
- if (m_nIslog)
- {
- Dbg("process eLogTpye = %d, dwUserCode = 0x%08x, %s", eLogType, dwUserCode, pszMessage);
- }
- if (m_pEngine) {
- int rc = evt_engine_process_log(m_pEngine, SubIDs, nLogID, eLogType, eLevel, dwSysError, dwUserCode, wEntityDevelID, pszEntityName, pszModuleName, pszMessage);
- if (rc != 0) {
- Dbg("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) {
- LOG_TRACE("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) {
- LOG_TRACE("subscribe log failed! Error = %d", Error);
- } else {
- *sub_id = (unsigned long long)uuid;
- if (m_nIslog)
- {
- LOG_TRACE("subscribe log ok!");
- }
- }
- return (int)Error;
- }
- int unsubscribe_log(unsigned long long sub_id)
- {
- ErrorCodeEnum Error = GetFunction()->UnsubscribeLog(sub_id);
- if (Error == Error_Succeed) {
- if (m_nIslog)
- {
- LOG_TRACE("unsubscribe log ok!");
- }
- } else {
- LOG_TRACE("unsubscribe log failed! Error = %d", Error);
- }
- return (int)Error;
- }
- int subscribe_sysevent()
- {
- if (m_nIslog)
- {
- LOG_TRACE("subscribe sysevent!");
- }
- CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
- return (int)spEntityFunction->RegistSysVarEvent("*", this);
- }
- int unsubscribe_sysevent()
- {
- if (m_nIslog)
- {
- LOG_TRACE("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; // add by chh
- };
- SP_BEGIN_ENTITY_MAP()
- SP_ENTITY(CEventConverterEntity)
- SP_END_ENTITY_MAP()
|