mod_vtmloader.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "stdafx.h"
  2. #include "mod_vtmloader.h"
  3. #include "EventCode.h"
  4. #include "VtmLoader_UserCode.h"
  5. #include "publicFunExport.h"
  6. //#pragma comment(lib, "libpublicFun.lib")
  7. #include "tokenDefine.h"
  8. #include <thread>
  9. #include <SpIni.h>
  10. void VtmLoaderServerSession::Handle_SetTerminalNo(SpReqAnsContext<VtmLoaderService_SetTerminalNo_Req, VtmLoaderService_SetTerminalNo_Ans>::Pointer ctx)
  11. {
  12. m_pEntity->SetTerminalNo(ctx);
  13. }
  14. void VtmLoaderServerSession::Handle_NoticeAppReady(SpReqAnsContext<VtmLoaderService_NoticeAppReady_Req, VtmLoaderService_NoticeAppReady_Ans>::Pointer ctx)
  15. {
  16. //m_pEntity->NoticeAppReady(ctx);
  17. }
  18. void VtmLoaderServerSession::Handle_CheckDeviceEntity(SpReqAnsContext<VtmLoaderService_CheckDeviceEntity_Req, VtmLoaderService_CheckDeviceEntity_Ans>::Pointer ctx)
  19. {
  20. m_pEntity->CheckDeviceEntity(ctx);
  21. }
  22. void VtmLoaderServerSession::Handle_SaveTerminalNo(SpReqAnsContext<VtmLoaderService_SaveTerminalNo_Req, VtmLoaderService_SaveTerminalNo_Ans>::Pointer ctx)
  23. {
  24. m_pEntity->SaveTerminalNo(ctx);
  25. }
  26. void VtmLoaderServerSession::Handle_DealWithOldEvent(SpReqAnsContext<VtmLoaderService_DealWithOldEvent_Req, VtmLoaderService_DealWithOldEvent_Ans>::Pointer ctx)
  27. {
  28. m_pEntity->DealWithOldEvent(ctx);
  29. }
  30. void CVtmLoaderEntity::OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  31. {
  32. m_FSM.Init(this);
  33. //CloseHandle(CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&thread_testEvent, this, 0, NULL));
  34. pTransactionContext->SendAnswer(Error_Succeed) ;
  35. }
  36. void CVtmLoaderEntity::OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  37. {
  38. //m_task.Close();
  39. m_FSM.PostExitEvent();
  40. UnsubscribeEntitysEvents();
  41. pTransactionContext->SendAnswer(Error_Succeed);
  42. }
  43. void CVtmLoaderEntity::OnLog(const CAutoArray<CUUID>& SubIDs, const CUUID nLogID, const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  44. const DWORD dwSysError, const DWORD dwUserCode, const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  45. const CAutoArray<DWORD>& Param, const char* pszEntityName, const char* pszModuleName, const char* pszMessage, const linkContext& pLinkInfo)
  46. {
  47. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("dwUserCode:%x", dwUserCode);
  48. switch (dwUserCode) {
  49. case LOG_EVT_MOD_MEDIACONTROLLER_STARTED_SUCCESS:
  50. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("set MediaController entity have checked setting");
  51. m_FSM.SetVideoEntityOK();
  52. break;
  53. default:
  54. break;
  55. }
  56. }
  57. //only process healthmanager lost
  58. void CVtmLoaderEntity::OnEntityStateHook(const char* pszEntityName, const char* pszTriggerEntity, EntityStateEnum eState, EntityStateEnum eLastState)
  59. {
  60. if (_strnicmp("HealthManager", pszEntityName, strlen("HealthManager")) != 0 || eState != EntityState_Lost)
  61. return;
  62. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  63. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  64. if (pFuncPrivilege == NULL)
  65. {
  66. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("NoPrivilege");
  67. return;
  68. }
  69. switch (eState)
  70. {
  71. case EntityState_Lost:
  72. {
  73. CSimpleStringA tmpWarnMsg = CSimpleStringA::Format("%s lost. trig entity:%s,lastState:%d", pszEntityName, pszTriggerEntity, eLastState);
  74. CEntityStaticInfo esi = { 0 };
  75. ErrorCodeEnum ec = GetFunction()->GetEntityStaticInfo(pszEntityName, esi);
  76. //0x101 0x21D
  77. int iByteHigh, iByteLow;
  78. iByteHigh = (esi.wEntityDevelopID & 0xF00) >> 8;
  79. iByteLow = esi.wEntityDevelopID & 0xFF;
  80. if (iByteHigh < 1 || iByteHigh > 9)
  81. {
  82. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Unexpected entity id:%x", esi.wEntityDevelopID);
  83. break;
  84. }
  85. CSimpleStringA csResultCode(true);
  86. //实体lost的RTAxxxx的后两位xx,规则同RTAxxxx的前两位xx(实体标识码),由实体编号自动生成。健康实体本身的自定义RTAxxxx应该避开这些自定义
  87. //'0':0x30 'A':0x41
  88. if (iByteLow > 0 && iByteLow < 10)
  89. csResultCode = CSimpleStringA::Format("RTA51%c%c", iByteHigh + 0x30, iByteLow + 0x30);
  90. else
  91. csResultCode = CSimpleStringA::Format("RTA51%c%c", iByteHigh + 0x30, iByteLow - 10 + 0x41);
  92. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("resultcode:%s", csResultCode.GetData());
  93. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode(csResultCode.GetData())(tmpWarnMsg.GetData());
  94. EntityLostProcTask* task = new EntityLostProcTask(this, pszEntityName);
  95. GetFunction()->PostThreadPoolTask(task);
  96. }
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. void CVtmLoaderEntity::EntityLostProc(CSimpleStringA entityName)
  103. {
  104. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  105. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  106. CSmartPointer<IAsynWaitSp> spWait;
  107. ErrorCodeEnum eErrCode = pFuncPrivilege->TerminateEntity(entityName, spWait);
  108. if (eErrCode == Error_Succeed)
  109. {
  110. eErrCode = spWait->WaitAnswer(10000);
  111. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("on lost kill wait %d", eErrCode);
  112. }
  113. else
  114. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("on lost kill %d", eErrCode);
  115. Sleep(5000);
  116. eErrCode = pFuncPrivilege->StartEntity(entityName, NULL, spWait);
  117. if (eErrCode == Error_Succeed)
  118. {
  119. eErrCode = spWait->WaitAnswer(10000);
  120. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("on lost start wait %d", eErrCode);
  121. }
  122. else
  123. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("on lost start %d", eErrCode);
  124. }
  125. ErrorCodeEnum CVtmLoaderEntity::UnsubscribeEntitysEvents()
  126. {
  127. auto pFunc = GetFunction();
  128. if ((__int64)m_SubLogID2 != 0)
  129. pFunc->UnsubscribeLog(m_SubLogID2);
  130. if ((__int64)m_SubLogID3 != 0)
  131. pFunc->UnsubscribeLog(m_SubLogID3);
  132. return Error_Succeed;
  133. }
  134. void CVtmLoaderEntity::DealWithOldEvent(SpReqAnsContext<VtmLoaderService_DealWithOldEvent_Req, VtmLoaderService_DealWithOldEvent_Ans>::Pointer ctx)
  135. {
  136. /*
  137. DealWithOldEvent_Event* pEvt = new DealWithOldEvent_Event();
  138. pEvt->ctx = ctx;
  139. m_FSM.PostEventFIFO(pEvt);
  140. */
  141. static SpReqAnsContext<VtmLoaderService_DealWithOldEvent_Req, VtmLoaderService_DealWithOldEvent_Ans>::Pointer s_ctx;
  142. s_ctx = ctx;
  143. auto dealOldEventThread = [&] {
  144. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("DealWithOldEvent called eventId, %d", s_ctx->Req.eventId);
  145. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("DealWithOldEvent called reqStrRev1, %s", s_ctx->Req.reqStrRev1);
  146. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("DealWithOldEvent called reqStrRev2, %s", s_ctx->Req.reqStrRev2);
  147. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("DealWithOldEvent called reqIntRev1, %d", s_ctx->Req.reqIntRev1);
  148. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("DealWithOldEvent called reqIntRev2, %d", s_ctx->Req.reqIntRev2);
  149. ErrorCodeEnum eErr = ErrorCodeEnum::Error_Succeed;
  150. switch (s_ctx->Req.eventId)
  151. {
  152. case EVENT_CONSOLE_REQ_SHUTDOWN_SHELL:
  153. LogWarn(Severity_Low, Error_Unexpect, 0x50100215, CSimpleStringA::Format("%x, reason:%s", EVENT_CONSOLE_REQ_SHUTDOWN_SHELL, s_ctx->Req.reqStrRev1.GetData()));
  154. GetFunction()->GetPrivilegeFunction()->Reboot(RebootTrigger_DeadForever , RebootWayEnum::RebootWay_Framework);
  155. break;
  156. case LOG_EVT_IEBROWSER_RESET_SITE_RESTART:
  157. GetFunction()->GetPrivilegeFunction()->Reboot(RebootTrigger_ManualLocal, RebootWayEnum::RebootWay_Framework);
  158. break;
  159. default:
  160. break;
  161. }
  162. s_ctx->Answer(eErr);
  163. };
  164. std::thread(dealOldEventThread).detach();
  165. }
  166. SP_BEGIN_ENTITY_MAP()
  167. SP_ENTITY(CVtmLoaderEntity)
  168. SP_END_ENTITY_MAP()