mod_healthmanager.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #ifndef __MOD_HEALTHMANAGER_H
  2. #define __MOD_HEALTHMANAGER_H
  3. #include <map>
  4. #include "SpBase.h"
  5. #include "HealthManagerFSM.h"
  6. #include "EventCode.h"
  7. #include "HealthManager_server_g.h"
  8. using namespace HealthManager;
  9. #include "UpgradeManager_client_g.h"
  10. using namespace std;
  11. using namespace UpgradeManager;
  12. #include "GuardianBase.h"
  13. typedef int (*pfShakeHands)(WorkStateEnum &eState);
  14. typedef int (*pfPushUpdateTask)(const char *pszPackName);
  15. typedef bool (*pfIsInstalling)();
  16. typedef int (*pfUpgradeRestart)(const DWORD dwParam1,const DWORD dwParam2);
  17. typedef int (*pfFrameworkQuit)(RebootTriggerEnum eReason);
  18. #define DEPLOYSTATE_NOTINIT 0
  19. #define DEPLOYSTATE_DONE 1
  20. #define DEPLOYSTATE_FAILED 2
  21. //提示页面类型
  22. enum WarmPageTypeEnum {
  23. WarmPageType_None = 0, //无提示页
  24. WarmPageType_TerminalUpgrade, //终端应用升级
  25. WarmPageType_DeviceAdapterUpgrade, //适配器升级
  26. WarmPageType_HeartBeatRollBack, //心跳回退
  27. WarmPageType_UpgradeRollBack, //升级任务回退
  28. WarmPageType_ThirdPartyUpgrade, //体系外升级
  29. };
  30. struct ModuleRunInfo
  31. {
  32. DWORD dwStart;
  33. int abnormalCount;
  34. bool bAbnormalBusy;
  35. };
  36. struct RestartModeInfo
  37. {
  38. DWORD dwTimes;
  39. DWORD dwInternal;
  40. };
  41. #include <winpr/sysinfo.h>
  42. static void GetLocalTimeRVC(SYSTEMTIME& stTime)
  43. {
  44. GetLocalTime(&stTime);
  45. }
  46. class HealthManagerSession : public HealthManagerService_ServerSessionBase
  47. {
  48. public:
  49. HealthManagerSession(CHealthManagerEntity *pEntity):m_pEntity(pEntity){}
  50. virtual ~HealthManagerSession(){}
  51. virtual void Handle_EnterState(SpReqAnsContext<HealthManagerService_EnterState_Req, HealthManagerService_EnterState_Ans>::Pointer ctx);
  52. virtual void Handle_ExitState(SpReqAnsContext<HealthManagerService_ExitState_Req, HealthManagerService_ExitState_Ans>::Pointer ctx);
  53. virtual void Handle_DoEvent(SpReqAnsContext<HealthManagerService_DoEvent_Req, HealthManagerService_DoEvent_Ans>::Pointer ctx);
  54. virtual void Handle_RealCheck(SpReqAnsContext<HealthManagerService_RealCheck_Req, HealthManagerService_RealCheck_Ans>::Pointer ctx);
  55. virtual void Handle_GetNetworkState(SpReqAnsContext<HealthManagerService_GetNetworkState_Req, HealthManagerService_GetNetworkState_Ans>::Pointer ctx);
  56. virtual void Handle_QueryHardwareInfo(SpReqAnsContext<HealthManagerService_QueryHardwareInfo_Req, HealthManagerService_QueryHardwareInfo_Ans>::Pointer ctx);
  57. virtual void Handle_ReadCenterConfigStr(SpReqAnsContext<HealthManagerService_ReadCenterConfigStr_Req, HealthManagerService_ReadCenterConfigStr_Ans>::Pointer ctx);
  58. virtual void Handle_GetAuthErrMsg(SpReqAnsContext<HealthManagerService_GetAuthErrMsg_Req, HealthManagerService_GetAuthErrMsg_Ans>::Pointer ctx);
  59. virtual void Handle_QueryCenterCfg(SpReqAnsContext<HealthManagerService_QueryCenterCfg_Req, HealthManagerService_QueryCenterCfg_Ans>::Pointer ctx);
  60. private:
  61. CHealthManagerEntity *m_pEntity;
  62. };
  63. class CHealthManagerEntity : public CEntityBase, public ILogListener, public IEntityStateListener, public ISysVarListener, public IBroadcastListener
  64. ,public ITimerListener, public ICallbackListener
  65. {
  66. public:
  67. CHealthManagerEntity() :
  68. m_bWaitRestartPC(false)
  69. , m_bScreenLock(false)
  70. , m_bSayIdle(false), m_bGuardianRun(false), m_ullGuardShakeCount(0)
  71. , m_menuChoice(""), m_menuPre(""), m_netState("N")
  72. , m_bInMainPage(false), m_stopSelfCheck(0)
  73. ,m_bNeedAuthRetry(false), m_bNeedGuardianRestart(true), m_bNeedGuardian(true)
  74. , m_bToRestartByCenterSetting(false){}
  75. virtual ~CHealthManagerEntity() {}
  76. virtual const char *GetEntityName() const { return "HealthManager"; }
  77. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  78. {
  79. ErrorCodeEnum eErrCode = m_fsm.Init(this);
  80. if (eErrCode == Error_Succeed) {
  81. DoRestart();
  82. }
  83. GetFunction()->SubscribeLog(m_uuidUpdate,this,Log_Event,Severity_Middle,Error_IgnoreAll,-1,"UpgradeManager");
  84. GetFunction()->SubscribeLog(m_uuidAccessAuth,this,Log_Event,Severity_Middle,Error_IgnoreAll,-1,"AccessAuthorization");
  85. GetFunction()->SubscribeLog(m_uuidAccessAuth,this,Log_Error,Severity_Middle,Error_IgnoreAll,-1,"AccessAuthorization");
  86. GetFunction()->SubscribeLog(m_uuidFaceTracking,this,Log_Error,Severity_High,Error_IgnoreAll,-1,"FaceTracking");
  87. GetFunction()->SubscribeLog(m_uuidMediaController,this,Log_Error,Severity_High,Error_IgnoreAll,-1,"MediaController");
  88. GetFunction()->SubscribeLog(m_uuidRemoteController,this,Log_Event,Severity_Middle,Error_IgnoreAll,-1,"RemoteController");
  89. GetFunction()->SubscribeLog(m_uuidGUIConsole,this,Log_Event,Severity_Middle,Error_IgnoreAll,-1,"GUIConsole");
  90. GetFunction()->SubscribeLog(m_uuidHeartBeat,this,Log_Event,Severity_Middle,Error_IgnoreAll,-1,"HeartBeat",false);
  91. GetFunction()->SubscribeLog(m_uuidCenterS, this, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "CenterSetting");
  92. GetFunction()->SubscribeLog(m_uuidPublic, this, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "Chromium");
  93. eErrCode = GetFunction()->SubscribeLog(m_uuidVtmLoader, this, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "VtmLoader");
  94. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("SubscribeLog VtmLoader eErrCode:%d", eErrCode);
  95. GetFunction()->RegistSysVarEvent("UIState", this);
  96. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  97. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  98. eErrCode = pFuncPrivilege->RegistEntityStateEvent(NULL, this);
  99. if (eErrCode != Error_Succeed)
  100. {
  101. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("RegistEntityState failed(%d).", eErrCode);
  102. pTransactionContext->SendAnswer(eErrCode);
  103. return;
  104. }
  105. //UpgradeManager
  106. #if defined(RVC_OS_WIN)
  107. GetFunction()->SubscribeLog(m_uuidPublic, this, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "PinPad");
  108. GetFunction()->SubscribeLog(m_uuid4SIPPhone, this, Log_Event, Severity_High, Error_IgnoreAll, -1, "SIPPhone");
  109. #endif //RVC_OS_WIN
  110. pTransactionContext->SendAnswer(eErrCode) ;
  111. }
  112. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  113. {
  114. pTransactionContext->SendAnswer(Error_Succeed);
  115. }
  116. virtual void OnPrePause(CSmartPointer<ITransactionContext> pTransactionContext)
  117. {
  118. pTransactionContext->SendAnswer(Error_Succeed);
  119. }
  120. virtual void OnPreContinue(CSmartPointer<ITransactionContext> pTransactionContext)
  121. {
  122. LOG_FUNCTION();
  123. pTransactionContext->SendAnswer(Error_Succeed);
  124. m_fsm.QuitFrameworkAndSaveInfo(RebootTrigger_FrameUpgrade, RebootWay_Framework);
  125. }
  126. void EnterState(SpReqAnsContext<HealthManagerService_EnterState_Req, HealthManagerService_EnterState_Ans>::Pointer ctx)
  127. {
  128. if (ctx->Req.state == "M")
  129. {
  130. FSMEvent *pEvt = new FSMEvent(USER_EVT_MAITAIN);
  131. m_fsm.PostEventFIFO(pEvt);
  132. }
  133. ctx->Answer(Error_Succeed);
  134. }
  135. void ExitState(SpReqAnsContext<HealthManagerService_ExitState_Req, HealthManagerService_ExitState_Ans>::Pointer ctx)
  136. {
  137. if (ctx->Req.state == "M")
  138. {
  139. FSMEvent *pEvt = new FSMEvent(USER_EVT_MAITAIN_FINISHED);
  140. m_fsm.PostEventFIFO(pEvt);
  141. }
  142. ctx->Answer(Error_Succeed);
  143. }
  144. void DoEvent(SpReqAnsContext<HealthManagerService_DoEvent_Req, HealthManagerService_DoEvent_Ans>::Pointer ctx)
  145. {
  146. if (ctx->Req.code == Event_VtmLoader_EntityLoad_Finished || ctx->Req.code == Event_VtmLoader_SIPPhoneEntityLoad_Failed
  147. || ctx->Req.code == Event_VtmLoader_OtherSyncEntityLoad_Failed)
  148. {
  149. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER)("receive %x,entityList:%s", ctx->Req.code,ctx->Req.entityList.GetData());
  150. if (!ctx->Req.entityList.IsNullOrEmpty())
  151. m_fsm.SetLoadEntityList(ctx->Req.entityList);
  152. FSMEvent* pEvt = new FSMEvent(USER_EVT_VTMLOADER_FINISHED);
  153. if (ctx->Req.code == Event_VtmLoader_EntityLoad_Finished)
  154. {
  155. pEvt->param1 = 0;
  156. //to get entity list
  157. if (!ctx->Req.entityList.IsNullOrEmpty())
  158. {
  159. m_activeEntity.Init(100);
  160. m_activeEntity = ctx->Req.entityList.Split('|');
  161. for (int i = 0; i < m_activeEntity.GetCount(); ++i)
  162. {
  163. m_modRunInfo[m_activeEntity[i]].abnormalCount = 0;
  164. m_modRunInfo[m_activeEntity[i]].bAbnormalBusy = false;
  165. }
  166. }
  167. }
  168. else if (ctx->Req.code == Event_VtmLoader_SIPPhoneEntityLoad_Failed)
  169. pEvt->param1 = 1;
  170. else
  171. pEvt->param1 = 2;
  172. m_fsm.PostEventFIFO(pEvt);
  173. }
  174. ctx->Answer(Error_Succeed);
  175. }
  176. void GetNetworkState(SpReqAnsContext<HealthManagerService_GetNetworkState_Req, HealthManagerService_GetNetworkState_Ans>::Pointer ctx)
  177. {
  178. ctx->Ans.netDeviceState = m_netState;
  179. ctx->Ans.reserved1 = m_fsm.QueryAccessAuthStatus();
  180. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_USER).setLogCode(HealthManagerService_LogCode_GetNetworkState)("current net state %s,auth state:%d", (LPCTSTR)m_netState, ctx->Ans.reserved1);
  181. ctx->Answer(Error_Succeed);
  182. }
  183. virtual CServerSessionBase *OnNewSession(const char* /*pszRemoteEntityName*/, const char * /*pszParam*/)
  184. {
  185. return new HealthManagerSession(this);
  186. }
  187. virtual bool IsService()const{return true;}
  188. virtual bool IsMultiThread()const{return true;}
  189. void QueryHardwareInfo(SpReqAnsContext<HealthManagerService_QueryHardwareInfo_Req, HealthManagerService_QueryHardwareInfo_Ans>::Pointer ctx)
  190. {
  191. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode("QLR0402501Q0")();
  192. QueryHardwareInfoTask* pTask = new QueryHardwareInfoTask(&m_fsm);
  193. pTask->ctx = ctx;
  194. GetFunction()->PostThreadPoolTask(pTask);
  195. }
  196. void ReadCenterConfigStr(SpReqAnsContext<HealthManagerService_ReadCenterConfigStr_Req, HealthManagerService_ReadCenterConfigStr_Ans>::Pointer ctx);
  197. void GetAuthErrMsg(SpReqAnsContext<HealthManagerService_GetAuthErrMsg_Req, HealthManagerService_GetAuthErrMsg_Ans>::Pointer ctx);
  198. void QueryCenterCfg(SpReqAnsContext<HealthManagerService_QueryCenterCfg_Req, HealthManagerService_QueryCenterCfg_Ans>::Pointer ctx);
  199. bool StartManagerDesktopPage(CSimpleStringA pageName);
  200. void WarnAndRestartFunc(RebootTriggerEnum eTrigger, RebootWayEnum eWay, WarmPageTypeEnum eWarmPageType, bool needVerctrl = false);
  201. void RealCheck(SpReqAnsContext<HealthManagerService_RealCheck_Req, HealthManagerService_RealCheck_Ans>::Pointer ctx);
  202. void OnAnswer(CSmartPointer<IAsynWaitSp> pAsynWaitSp);
  203. ErrorCodeEnum ExceptionErrorProcess(const char* pszEntityName, ErrorCodeEnum eTestResult);
  204. ErrorCodeEnum CheckEntity(const char* pszEntityName, EntityTestEnum eTestType);
  205. void EntityLostProc(CSimpleStringA entityName);
  206. virtual void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  207. const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  208. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage, const linkContext &pLinkInfo);
  209. virtual void OnSysVarEvent(const char *pszKey,
  210. const char *pszValue, const char *pszOldValue, const char *pszEntityName);
  211. virtual void OnBroadcastEvent(CUUID SubID, const char *pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, CAutoBuffer Buffer);
  212. virtual void OnTimeout(DWORD dwTimerID);
  213. virtual void OnEntityStateHook(const char* pszEntityName, const char* pszTriggerEntity, EntityStateEnum eState, EntityStateEnum eLastState);
  214. virtual void OnUserStateHook(const char* pszEntityName, DWORD dwState, DWORD dwLastState) { return; }
  215. virtual void OnCeateConnection(const char* pszCallerEntity, const char* pszServiceEntity) { return; }
  216. virtual void OnCloseConnection(const char* pszCallerEntity, const char* pszServiceEntity) { return; }
  217. private:
  218. CHealthManagerFSM m_fsm;
  219. //the following is useless?just one var is ok? oiltmp
  220. CUUID m_uuidUpdate,m_uuidMediaController,m_uuidFaceTracking,m_uuidAccessAuth
  221. , m_uuidRemoteController, m_uuidGUIConsole, m_uuidHeartBeat, m_uuidIE, m_uuidCenterS, m_uuidVtmLoader;
  222. CUUID m_uuidPublic, m_uuid4SIPPhone;
  223. int m_stopSelfCheck;
  224. int m_restartHour, m_restartMinute, m_lastHour, m_maxAbnormalTimes, m_rebootHourBegin, m_rebootHourEnd;;
  225. bool m_bWaitRestartPC, m_bScreenLock, m_bSayIdle
  226. , m_bGuardianRun, m_bInMainPage, m_bToRestartByCenterSetting;
  227. bool m_bNeedAuthRetry/*需要重试准入*/, m_bNeedGuardianRestart/*需要重启*/, m_bNeedGuardian;
  228. DWORD m_dwTimeOfAuthSuc;
  229. int m_maxWaitMainpageTime;
  230. ULONGLONG m_OSRunTicks, m_ullGuardShakeCount;
  231. CSimpleStringA m_menuChoice,m_currentVer,m_menuPre,m_netState, m_versionEx;
  232. map<CSimpleStringA,ModuleRunInfo> m_modRunInfo;
  233. CAutoArray<CSimpleStringA> m_activeEntity;
  234. pfShakeHands m_pfShake;
  235. pfUpgradeRestart m_pfUpgradeRestart;
  236. CSystemStaticInfo m_sysStaticInfo;
  237. /** 启动限制配置读取以及监护模块初始化*/
  238. bool DoRestart();
  239. void OnCheckTimeTimeout();
  240. void OnSelfCheckTimeout();
  241. ErrorCodeEnum RestartModule(const char* pEntityName);
  242. void AfterWaitRestartPC();
  243. bool SaveCurrentVersion();
  244. bool SaveFrameStartTimeForUpgrade();
  245. //reture value:
  246. //0:no need guardian; 1:guardian is running; -1:guardian is not running
  247. int CheckGuardianIsRun(bool bStart=false);
  248. bool StopGuardian();
  249. bool StartGuardian();
  250. CSimpleStringA __ReadCenterConfigStr(CSimpleStringA key, CSimpleStringA entityName);
  251. void ToCalcRebootHourAndMinute(int restartBegin, int restartEnd);
  252. bool CheckIfHaveEnterMainPageEver();
  253. };
  254. #endif // __MOD_HEALTHMANAGER_H
  255. struct FrameworkRestartTask : public ITaskSp
  256. {
  257. RebootTriggerEnum eTrigger;
  258. RebootWayEnum eWay;
  259. WarmPageTypeEnum eWarmPageType;
  260. bool needVerctrl;
  261. CHealthManagerEntity* m_entity;
  262. FrameworkRestartTask(CHealthManagerEntity* entity) : m_entity(entity), eWarmPageType(WarmPageType_None), needVerctrl(false) {}
  263. void Process()
  264. {
  265. m_entity->WarnAndRestartFunc(eTrigger, eWay, eWarmPageType, needVerctrl);
  266. }
  267. };
  268. struct WarnPromptTask : public ITaskSp
  269. {
  270. CHealthManagerEntity* m_entity;
  271. WarnPromptTask(CHealthManagerEntity* entity) : m_entity(entity) {}
  272. void Process()
  273. {
  274. m_entity->StartManagerDesktopPage("warnPrompt.html");
  275. }
  276. };
  277. struct EntityLostProcTask : public ITaskSp
  278. {
  279. CHealthManagerEntity* pEntity;
  280. CSimpleStringA entityName;
  281. EntityLostProcTask(CHealthManagerEntity* entity,CSimpleStringA entityName) : pEntity(entity),entityName(entityName) {}
  282. void Process()
  283. {
  284. pEntity->EntityLostProc(entityName);
  285. }
  286. };