#include "stdafx2.h" #include "mod_GuiConsole.h" #include "guitask.h" #include "EventCode.h" #include "MaterialMgrCnn.h" #include "CardIssuer_client_g.h" using namespace CardIssuer; #include "MaintainWatcher_client_g.h" using namespace MaintainWatcher; void CGUIConsoleEntity::OnPreStart(CAutoArray strArgs,CSmartPointer pTransactionContext) { m_FSM.Init(this); auto rc = SubscribeEntitysEvents(); if (rc != Error_Succeed) Dbg("subscribe MaintainWatcher event fail, %s", SpStrError(rc)); else Dbg("subscribe MaintainWatcher event succeed"); pTransactionContext->SendAnswer(Error_Succeed) ; } void CGUIConsoleEntity::OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer pTransactionContext) { m_FSM.PostExitEvent(); UnsubscribeEntitysEvents(); pTransactionContext->SendAnswer(Error_Succeed); } CServerSessionBase *CGUIConsoleEntity::OnNewSession(const char* /*pszRemoteEntityName*/, const char * /*pszParam*/) { return new CGUIConsoleSession(this); } void CGUIConsoleEntity::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) { Dbg("OnLog(entity = %s, syscode = 0x%X, usercode = 0x%X", pszEntityName, dwSysError, dwUserCode); if (dwUserCode == EVENT_MACHINE_COVER_OPEN) // 机盖打开 { Dbg("the machine cover open!!"); m_FSM.PostEventFIFO(new FSMEvent(CGUIConsoleFSM::Event_CoverOpen)); } else if (dwUserCode == EVENT_MACHINE_COVER_CLOSE) // 机盖关闭 { Dbg("the machine cover close."); m_FSM.PostEventFIFO(new FSMEvent(CGUIConsoleFSM::Event_CoverClose)); } else if (dwUserCode == EVENT_UKEY_INSERTED) // 证书插入 { Dbg("the key inserted"); m_FSM.PostEventFIFO(new FSMEvent(CGUIConsoleFSM::Event_UKeyInserted)); } else if ((dwUserCode == EVENT_UKEY_LOW_LEVEL) // 具有维护权 || (dwUserCode == EVENT_UKEY_HIGH_LEVEL)) // 具有高级权限) { bool bLowLevel = dwUserCode == EVENT_UKEY_LOW_LEVEL; if (bLowLevel) Dbg("lower priviledge"); else Dbg("higher priviledge"); // 调用UKey接口取维护人员信息 m_strUserID = ""; m_strUserName = ""; m_strAuthorizer = ""; m_strAuthTime = ""; MaintainCertificate_ClientBase *pClient = new MaintainCertificate_ClientBase(this); auto rc = pClient->Connect(); if (rc != Error_Succeed) { Dbg("connect to MaintainWatcher entity fail: %d", rc); pClient->SafeDelete(); } else { MaintainCertificate_GetUserInfo_Req req = {}; MaintainCertificate_GetUserInfo_Ans ans = {}; rc = pClient->GetUserInfo(req, ans, 10000); if (rc != Error_Succeed) { Dbg("get userinfo fail from MaintainWatcher: %s", SpStrError(rc)); } else { // UserID=SP00000004;UserName=RVC0001;AuthorierID=SP00000001;AuthorizeTime=2014/8/13 18:46:29;RecommenderSAP=80274390;RecommenderOfficeID=274390; CSimpleStringA &str = ans.UserInfo; Dbg("get maintainer info: %s", (const char*)str); auto arr = str.Split(';'); for (int i = 0; i < arr.GetCount(); i++) { auto arr2 = arr[i].Split('='); if (arr2.GetCount() != 2) continue; if (arr2[0] == "UserID") m_strUserID = arr2[1]; else if (arr2[0] == "UserName") m_strUserName = arr2[1]; else if (arr2[0] == "AuthorierID") m_strAuthorizer = arr2[1]; else if (arr2[0] == "AuthorizeTime") m_strAuthTime = arr2[1]; } } pClient->GetFunction()->CloseSession(); } pClient = NULL; m_strCurRight = bLowLevel ? "Low" : "High"; m_dwBeginTime = CSmallDateTime::GetNow(); auto pEvent = new FSMEvent(CGUIConsoleFSM::Event_CertVerified); pEvent->param1 = bLowLevel ? 1 : 2; m_FSM.PostEventFIFO(pEvent); } else if (dwUserCode == EVENT_UKEY_PULLOUT) // 证书拔出 { m_strUserID = ""; m_strUserName = ""; m_strAuthorizer = ""; m_strAuthTime = ""; m_strCurRight = ""; m_dwBeginTime = 0; Dbg("certificate key pull out"); m_FSM.PostEventFIFO(new FSMEvent(CGUIConsoleFSM::Event_UKeyPulled)); } else if (dwUserCode == LOG_EVT_HEALTH_FIRST_ENTER_MAINPADE) { // IE首页打开, 关闭运行输出 Dbg("IE main page has been loaded successfully, close console terminal!"); GetFunction()->GetPrivilegeFunction()->CloseOuputConsole(); } } ErrorCodeEnum CGUIConsoleEntity::SubscribeEntitysEvents() { auto pFunc = GetFunction(); auto rc = pFunc->SubscribeLog(m_SubLogID1, this, Log_Event, Severity_None, Error_IgnoreAll, -1, "MaintainWatcher"); rc = pFunc->SubscribeLog(m_SubLogID2, this, Log_Event, Severity_None, Error_IgnoreAll, -1, "Gpio"); // 业务首页加载事件 rc = pFunc->SubscribeLog(m_SubLogID3, this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_HEALTH_FIRST_ENTER_MAINPADE, "HealthManager"); return rc; } ErrorCodeEnum CGUIConsoleEntity::UnsubscribeEntitysEvents() { auto pFunc = GetFunction(); if ((__int64)m_SubLogID1 != 0) pFunc->UnsubscribeLog(m_SubLogID1); if ((__int64)m_SubLogID2 != 0) pFunc->UnsubscribeLog(m_SubLogID2); if ((__int64)m_SubLogID3 != 0) pFunc->UnsubscribeLog(m_SubLogID3); return Error_Succeed; } ErrorCodeEnum CGUIConsoleEntity::GetCurrentMaintainer(CSimpleStringA &strUserID, CSimpleStringA &strUserName, CSimpleStringA &strCurRight, CSimpleStringA &strAuthorizer, unsigned int &dwBeginTime) { strUserID = m_strUserID; strUserName = m_strUserName; strCurRight = m_strCurRight; strAuthorizer = m_strAuthorizer; dwBeginTime = m_dwBeginTime; return Error_Succeed; } ErrorCodeEnum CGUIConsoleEntity::Empower(const char *pszUserID) { if (m_strUserID.IsNullOrEmpty() || m_strUserID.Compare(pszUserID) != 0) return Error_NoPrivilege; m_strCurRight = "High"; m_FSM.PostEventFIFO(new FSMEvent(CGUIConsoleFSM::Event_HighLevel)); return Error_Succeed; } ErrorCodeEnum CGUIConsoleEntity::Takeover(const char *pszUserID) { if (m_strUserID.IsNullOrEmpty() || m_strUserID.Compare(pszUserID) != 0) return Error_NoPrivilege; m_strCurRight = "Low"; m_FSM.PostEventFIFO(new FSMEvent(CGUIConsoleFSM::Event_Takeover)); return Error_Succeed; } ErrorCodeEnum CGUIConsoleEntity::ForceQuit() { m_strUserID = ""; m_strUserName = ""; m_strAuthorizer = ""; m_strAuthTime = ""; m_strCurRight = ""; m_dwBeginTime = 0; m_FSM.PostEventFIFO(new FSMEvent(CGUIConsoleFSM::Event_ForceQuit)); return Error_Succeed; } ErrorCodeEnum CGUIConsoleEntity::PostUserLoginEvent(const char *pszUserID) { m_strUserID = pszUserID; m_strUserName = pszUserID; m_strAuthorizer = ""; m_strAuthTime = ""; m_strCurRight = "Low"; m_dwBeginTime = CSmallDateTime::GetNow(); m_FSM.PostEventFIFO(new FSMEvent(CGUIConsoleFSM::Event_CoverOpen)); auto pEvent = new FSMEvent(CGUIConsoleFSM::Event_CertVerified); pEvent->param1 = 1; m_FSM.PostEventFIFO(pEvent); return Error_Succeed; } ErrorCodeEnum CGUIConsoleEntity::AddTradeManage(const char *pszTerminalNo, const char *pszAgentID, const char *pszTime) { Dbg("AddTradeManage: %s, %s, %s", pszTerminalNo, pszAgentID, pszTime); ErrorCodeEnum rc = Error_Succeed; CMaterialMgrCnn *pCnn = new CMaterialMgrCnn(this); if (pCnn->ConnectFromCentralSetting() && pCnn->IsConnectionOK()) { rc = pCnn->AddTradeManage(pszTerminalNo, pszAgentID, pszTime); pCnn->Close(); } else { Dbg("连接物料服务失败"); rc = Error_NetBroken; } pCnn->DecRefCount(); pCnn = NULL; if (Error_Succeed != rc) Dbg("mod AddTradeManage return : %s", SpStrError(rc)); return rc; } ErrorCodeEnum CGUIConsoleEntity::AddMaterialCounter(const char *pszMaterialCode) { // 递增后台计数 Dbg("AddMaterialCounter: %s", pszMaterialCode); CSystemStaticInfo sysInfo; auto rc = GetFunction()->GetSystemStaticInfo(sysInfo); assert(rc == Error_Succeed); CMaterialMgrCnn *pCnn = new CMaterialMgrCnn(this); if (pCnn->ConnectFromCentralSetting() && pCnn->IsConnectionOK()) { rc = pCnn->AddMaterialCounter(sysInfo.strTerminalID, pszMaterialCode); pCnn->Close(); } else { Dbg("Connect to MaterialMgrEntity failed!"); rc = Error_NetBroken; } pCnn->DecRefCount(); pCnn = NULL; if (rc == Error_Succeed) { Dbg("Add material counter for %s succ.", pszMaterialCode); } else { LogError(Severity_Middle, rc, 0, CSimpleStringA::Format("add material counter failed: %s", SpStrError(rc))); } return rc; } //查询物料信息 ErrorCodeEnum CGUIConsoleEntity::QueryMaterialInfo(const char *pszDeviceNo, unsigned int &count, CAutoArray &arrDeviceNo, CAutoArray &arrMateriel, CAutoArray &CardGroove, CAutoArray &CardBoxNo, CAutoArray &PsbCode, CAutoArray &PsbName, CAutoArray &CardInit, CAutoArray &CardRemains, CAutoArray &CardIssued, CAutoArray &CardMixed, CAutoArray &CardPercent, CAutoArray &TerminalNo, CAutoArray &Maintainer, CAutoArray &MaintainTime, CAutoArray &UpdateTime) { Dbg("QueryMaterialInfo DeviceNo = %s", pszDeviceNo); ErrorCodeEnum rc = Error_Succeed; CMaterialMgrCnn *pCnn = new CMaterialMgrCnn(this); if (pCnn->ConnectFromCentralSetting() && pCnn->IsConnectionOK()) { CSystemStaticInfo sysInfo; auto rc = GetFunction()->GetSystemStaticInfo(sysInfo); assert(rc == Error_Succeed); rc = pCnn->QueryMaterialInfo(pszDeviceNo, count, arrDeviceNo, arrMateriel, CardGroove, CardBoxNo, PsbCode, PsbName, CardInit, CardRemains, CardIssued, CardMixed, CardPercent, TerminalNo, Maintainer, MaintainTime, UpdateTime); pCnn->Close(); } else { Dbg("Connect to MaterialMgr failed!"); rc = Error_NetBroken; } pCnn->DecRefCount(); pCnn = NULL; return rc; } ErrorCodeEnum CGUIConsoleEntity::GetMaterialCounter(const char *pszMaterial, unsigned int &dwLastCapacity, unsigned int &dwUsedCounter) { ErrorCodeEnum rc = Error_Succeed; CMaterialMgrCnn *pCnn = new CMaterialMgrCnn(this); if (pCnn->ConnectFromCentralSetting() && pCnn->IsConnectionOK()) { CSystemStaticInfo sysInfo; auto rc = GetFunction()->GetSystemStaticInfo(sysInfo); assert(rc == Error_Succeed); GetMaterialCounterRet ret = {}; rc = pCnn->GetMaterialCounter(pszMaterial, (const char*)sysInfo.strTerminalID, (const char*)GetCurMaintainer(), dwLastCapacity, dwUsedCounter); pCnn->Close(); } else { Dbg("Connect to MaterialMgrEntity failed!"); rc = Error_NetBroken; } pCnn->DecRefCount(); pCnn = NULL; return rc; } ErrorCodeEnum CGUIConsoleEntity::ResetMaterialCounter(const char *pszMaterial, unsigned int dwResetCapacity, unsigned int dwUsedCounter, unsigned int dwRemainCounter, const char *pszComment) { Dbg("ResetMaterialCounter: %s, reset: %d", pszMaterial, dwResetCapacity); // 重置硬件计数 ErrorCodeEnum rc = Error_Succeed; CSimpleStringA strMaterialCode = pszMaterial; if (strMaterialCode == "DebitCard" || strMaterialCode == "RetainCard") { CardIssuerService_ClientBase *pClient = new CardIssuerService_ClientBase(this); rc = pClient->Connect(); if (rc == Error_Succeed) { CardIssuerService_SetMaterialCount_Req req = {}; if (strMaterialCode == "DebitCard") { req.bRemains = true; req.remains = dwResetCapacity; req.bIssued = true; req.issued = 0; req.bMixed = true; req.mixed = 0; req.bCaptured = false; } else if (strMaterialCode == "RetainCard") { req.bCaptured = true; req.captured = 0; req.bRemains = false; req.bIssued = false; req.bMixed = false; } CardIssuerService_SetMaterialCount_Ans ret = {}; rc = pClient->SetMaterialCount(req, ret, 10000); pClient->GetFunction()->CloseSession(); } else { Dbg("连接CardIssuer失败"); pClient->SafeDelete(); } } else { rc = Error_NoTarget; Dbg("Unknow material code: {%s}", (const char*)strMaterialCode); } if (rc != Error_Succeed) { LogError(Severity_Middle, rc, 0, CSimpleStringA::Format("Reset teminal material counter failed: %s", SpStrError(rc))); return rc; } // 重置后台计数 CMaterialMgrCnn *pCnn = new CMaterialMgrCnn(this); if (pCnn->ConnectFromCentralSetting() && pCnn->IsConnectionOK()) { CSystemStaticInfo sysInfo; rc = GetFunction()->GetSystemStaticInfo(sysInfo); assert(rc == Error_Succeed); rc = pCnn->ResetMaterialCounter(strMaterialCode, sysInfo.strTerminalID, GetCurMaintainer(), dwResetCapacity, dwUsedCounter, dwRemainCounter, pszComment); pCnn->Close(); } else { rc = Error_NetBroken; } pCnn->DecRefCount(); pCnn = NULL; if (rc == Error_Succeed) { Dbg("Reset teminal material counter succ: %s", (const char*)strMaterialCode); } else { LogError(Severity_Middle, rc, 0, CSimpleStringA::Format("Reset teminal material counter from server failed:%s", SpStrError(rc))); } return rc; } ErrorCodeEnum CGUIConsoleEntity::RegistSwallowedCard(const char *pszCardNo, const char *pszReasonCode, const char *pszDate, const char *pszTime, const char *pszDeviceSciNo) { Dbg("RegistSwallowedCard card = %s, reson = %s", pszCardNo, pszReasonCode); // 20170210: 空卡或无卡号吞卡也要登记 //if (pszCardNo == NULL || strlen(pszCardNo) == 0) // return Error_Param; // invoke material branch service ErrorCodeEnum rc = Error_Break; CMaterialMgrCnn *pCnn = new CMaterialMgrCnn(this); if (pCnn->ConnectFromCentralSetting() && pCnn->IsConnectionOK()) { CSystemStaticInfo sysInfo; rc = GetFunction()->GetSystemStaticInfo(sysInfo); assert(rc == Error_Succeed); rc = pCnn->RegistSwallowedCard(sysInfo.strMachineType, sysInfo.strTerminalID, pszCardNo, pszReasonCode, pszDate, pszTime, pszDeviceSciNo); pCnn->Close(); } else { rc = Error_NetBroken; } pCnn->DecRefCount(); pCnn = NULL; char tempCardNo[MAX_PATH] = ""; int cardNoLen = strlen(pszCardNo); if (cardNoLen > 8) { for (int i = 0; i < 4; i++) *(tempCardNo + i) = *(pszCardNo + i); for (int i = 4; i < cardNoLen - 4; i++) *(tempCardNo + i) = '*'; for (int i = cardNoLen - 4; i < cardNoLen; i++) *(tempCardNo + i) = *(pszCardNo + i); *(tempCardNo + cardNoLen) = '\0'; } else strcpy(tempCardNo, pszCardNo); if (rc == Error_Succeed) Dbg("Register swallow card succ,cardno: %s, reason: %s", tempCardNo, pszReasonCode); else Dbg("Register swallow card failed(%s),cardno: %s, reason: %s", SpStrError(rc), tempCardNo, pszReasonCode); return rc; } ErrorCodeEnum CGUIConsoleEntity::SyncMaterialCount(const char *pszDeviceNo, DWORD dwCardBoxNum, CAutoArray &arrMaintainFlag, CAutoArray &arrMaintainer, CAutoArray &arrMaintainTime, CAutoArray &arrCardBoxNo, CAutoArray &arrPsbCode, CAutoArray &arrPsbName, CAutoArray &arrCardInit, CAutoArray &arrCardRemains, CAutoArray &arrCardIssued, CAutoArray &arrCardMixed, CAutoArray &arrCardPercent) { Dbg("SyncMaterialCount DeviceNo = %s, CardBoxNum = %d", pszDeviceNo, dwCardBoxNum); if (pszDeviceNo == NULL || strlen(pszDeviceNo) == 0) { Dbg("param [pszDeviceNo] invalid"); return Error_Param; } if (dwCardBoxNum <= 0) { Dbg("param [dwCardBoxNum] invalid"); return Error_Param; } if (arrCardBoxNo.GetCount() != (dwCardBoxNum+1) || arrCardBoxNo.GetCount() != (dwCardBoxNum + 1) || arrPsbCode.GetCount() != (dwCardBoxNum + 1) || arrPsbName.GetCount() != (dwCardBoxNum + 1) || arrCardInit.GetCount() != (dwCardBoxNum + 1) || arrCardRemains.GetCount() != (dwCardBoxNum + 1) || arrCardIssued.GetCount() != (dwCardBoxNum + 1) || arrCardMixed.GetCount() != (dwCardBoxNum + 1) || arrCardPercent.GetCount() != (dwCardBoxNum + 1) || arrMaintainFlag.GetCount() != (dwCardBoxNum + 1) || arrMaintainer.GetCount() != (dwCardBoxNum + 1) || arrMaintainTime.GetCount() != (dwCardBoxNum + 1)) { Dbg("element count of array param invalid"); return Error_Param; } // invoke material branch service ErrorCodeEnum rc = Error_Succeed; CMaterialMgrCnn *pCnn = new CMaterialMgrCnn(this); if (pCnn->ConnectFromCentralSetting() && pCnn->IsConnectionOK()) { rc = pCnn->SyncMaterialCount(pszDeviceNo, dwCardBoxNum, arrMaintainFlag, arrMaintainer, arrMaintainTime, arrCardBoxNo, arrPsbCode, arrPsbName, arrCardInit, arrCardRemains, arrCardIssued, arrCardMixed, arrCardPercent); pCnn->Close(); } else { rc = Error_NetBroken; } pCnn->DecRefCount(); pCnn = NULL; if (rc == Error_Succeed) Dbg("计数同步成功"); else Dbg("计数同步失败:%s", SpStrError(rc)); return rc; } void CGUIConsoleSession::Handle_GetCurrentMaintainer(SpReqAnsContext::Pointer ctx) { auto rc = m_pEntity->GetCurrentMaintainer(ctx->Ans.strUserID, ctx->Ans.strUserName, ctx->Ans.strCurRight, ctx->Ans.strAuthorizer, ctx->Ans.dwBeginTime); ctx->Answer(rc); } void CGUIConsoleSession::Handle_Empower(SpReqAnsContext::Pointer ctx) { auto rc = m_pEntity->Empower(ctx->Req.strUserID); ctx->Answer(rc); } void CGUIConsoleSession::Handle_Takeover(SpReqAnsContext::Pointer ctx) { auto rc = m_pEntity->Takeover(ctx->Req.strUserID); ctx->Answer(rc); } void CGUIConsoleSession::Handle_ForceQuit(SpReqAnsContext::Pointer ctx) { auto rc = m_pEntity->ForceQuit(); ctx->Answer(rc); } void CGUIConsoleSession::Handle_AddMaterialCounter(SpReqAnsContext::Pointer ctx) { auto rc = m_pEntity->AddMaterialCounter(ctx->Req.strMaterialCode); ctx->Answer(rc); } void CGUIConsoleSession::Handle_AddTradeManage(SpReqAnsContext::Pointer ctx) { auto rc = m_pEntity->AddTradeManage(ctx->Req.terminalNo, ctx->Req.AgentID, ctx->Req.time); ctx->Answer(rc); } //查询物料信息 void CGUIConsoleSession::Handle_QueryMaterialInfo(SpReqAnsContext::Pointer ctx) { auto rc = m_pEntity->QueryMaterialInfo(ctx->Req.strDeviceNo, ctx->Ans.count, ctx->Ans.arrDeviceNo, ctx->Ans.arrMateriel, ctx->Ans.CardGroove, ctx->Ans.CardBoxNo, ctx->Ans.PsbCode, ctx->Ans.PsbName, ctx->Ans.CardInit, ctx->Ans.CardRemains, ctx->Ans.CardIssued, ctx->Ans.CardMixed, ctx->Ans.CardPercent, ctx->Ans.TerminalNo, ctx->Ans.Maintainer, ctx->Ans.MaintainTime, ctx->Ans.UpdateTime); ctx->Answer(rc); } void CGUIConsoleSession::Handle_GetMaterialCounter(SpReqAnsContext::Pointer ctx) { auto rc = m_pEntity->GetMaterialCounter(ctx->Req.strMaterialCode, ctx->Ans.dwLastCapacity, ctx->Ans.dwUsedCounter); ctx->Answer(rc); } void CGUIConsoleSession::Handle_ResetMaterialCounter(SpReqAnsContext::Pointer ctx) { auto rc = m_pEntity->ResetMaterialCounter(ctx->Req.strMaterialCode, ctx->Req.dwResetCapacity, ctx->Req.dwUsedCounter, ctx->Req.dwRemainCounter, ctx->Req.strComment); ctx->Answer(rc); } void CGUIConsoleSession::Handle_RegistSwallowedCard(SpReqAnsContext::Pointer ctx) { auto rc = m_pEntity->RegistSwallowedCard(ctx->Req.strCardNo, ctx->Req.strReasonCode, ctx->Req.strSwallowDate, ctx->Req.strSwallowTime, ctx->Req.strDeviceSciNo); ctx->Answer(rc); } void CGUIConsoleSession::Handle_SyncMaterialCount(SpOnewayCallContext::Pointer ctx) { LOG_FUNCTION(); m_pEntity->SyncMaterialCount(ctx->Info.strDeviceNo, ctx->Info.dwCardBoxNum, ctx->Info.arrMaintainFlag, ctx->Info.arrMaintainer, ctx->Info.arrMaintainTime, ctx->Info.arrCardBoxNo, ctx->Info.arrPsbCode, ctx->Info.arrPsbName, ctx->Info.arrCardInit, ctx->Info.arrCardRemains, ctx->Info.arrCardIssued, ctx->Info.arrCardMixed, ctx->Info.arrCardPercent); } SP_BEGIN_ENTITY_MAP() SP_ENTITY(CGUIConsoleEntity) SP_END_ENTITY_MAP()