mod_ResourceWatcher.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #pragma once
  2. #include "modVer.h"
  3. #include "SpTest.h"
  4. #include "ResourceWatcherFSM.h"
  5. #include "UOSTools.hpp"
  6. class ResourceWatcherEntity;
  7. #define ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE 1
  8. #define ENT_TIMERINTERVAL_CHECK_SOGOU_INPUT_INSTALLED_STATE 60 * 1000
  9. #define ENT_TIMERID_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE 3
  10. #define ENT_TIMERINTERVAL_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE 30 * 1000
  11. class ResourceWatcherServiceSession : public ResourceWatcherService_ServerSessionBase
  12. {
  13. public:
  14. ResourceWatcherServiceSession(ResourceWatcherEntity* pEntity) : m_pEntity(pEntity) {}
  15. virtual ~ResourceWatcherServiceSession() {}
  16. virtual void Handle_Fetch(SpReqAnsContext<ResourceWatcherService_Fetch_Req,
  17. ResourceWatcherService_Fetch_Ans>::Pointer ctx);
  18. virtual void Handle_GetDevInfo(SpReqAnsContext<ResourceWatcherService_GetDevInfo_Req,
  19. ResourceWatcherService_GetDevInfo_Ans>::Pointer ctx);
  20. virtual void Handle_GetCardSwiper(SpReqAnsContext<ResourceWatcherService_GetCardSwiper_Req, ResourceWatcherService_GetCardSwiper_Ans>::Pointer ctx);
  21. virtual void Handle_GetCpuType(SpReqAnsContext<ResourceWatcherService_GetCpuType_Req, ResourceWatcherService_GetCpuType_Ans>::Pointer ctx);
  22. virtual void Handle_OperateFile(SpReqAnsContext<ResourceWatcherService_OperateFile_Req, ResourceWatcherService_OperateFile_Ans>::Pointer ctx);
  23. virtual void Handle_ExtractEventLog(SpReqAnsContext<ResourceWatcherService_ExtractEventLog_Req, ResourceWatcherService_ExtractEventLog_Ans>::Pointer ctx);
  24. virtual void Handle_UpdateDNS(SpReqAnsContext<ResourceWatcherService_UpdateDNS_Req, ResourceWatcherService_UpdateDNS_Ans>::Pointer ctx);
  25. virtual void Handle_GetNetworkInfo(SpReqAnsContext<ResourceWatcherService_GetNetworkInfo_Req, ResourceWatcherService_GetNetworkInfo_Ans>::Pointer ctx);
  26. virtual void Handle_GetThirdPartyInstallState(SpReqAnsContext<ResourceWatcherService_GetThirdPartyInstallState_Req, ResourceWatcherService_GetThirdPartyInstallState_Ans>::Pointer ctx);
  27. virtual void Handle_InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req, ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
  28. private:
  29. ResourceWatcherEntity* m_pEntity;
  30. };
  31. class ResourceWatcherEntity : public CEntityBase, public IBroadcastListener, public ITimerListener
  32. {
  33. public:
  34. ResourceWatcherEntity():m_bInitMode(FALSE) {}
  35. virtual ~ResourceWatcherEntity() {}
  36. virtual const char* GetEntityName() const { return "ResourceWatcher"; }
  37. const char* GetEntityVersion() const { return MODULE_VERSION_FULL; }
  38. ON_ENTITYT_TEST()
  39. virtual CServerSessionBase* OnNewSession(const char* , const char * )
  40. {
  41. LOG_FUNCTION();
  42. return new ResourceWatcherServiceSession(this);
  43. }
  44. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,
  45. CSmartPointer<ITransactionContext> pTransactionContext)
  46. {
  47. LOG_FUNCTION();
  48. ErrorCodeEnum errorCode = Error_Succeed;
  49. CSimpleStringA strtermState;
  50. GetFunction()->GetSysVar("TerminalStage", strtermState);
  51. if (strtermState.IsStartWith("Z=")) {
  52. Dbg("aaa in config mode");
  53. m_bInitMode = TRUE;
  54. }
  55. errorCode = m_fsm.Init(this);
  56. if (errorCode == Error_Succeed) {
  57. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  58. CSystemStaticInfo staticInfo;
  59. spEntityFunction->GetSystemStaticInfo(staticInfo);
  60. if (!staticInfo.strMachineType.IsNullOrEmpty() && !staticInfo.strMachineType.Compare("RVC.Pad", true)) {
  61. errorCode = spEntityFunction->SubscribeBroadcast("CardSwiper", NULL, this, m_uiCardSwiperStatusListener);
  62. if (errorCode != Error_Succeed) {
  63. LOG_TRACE("Subscribe CardSwiper evt failed 0x%x", errorCode);
  64. pTransactionContext->SendAnswer(errorCode);
  65. return;
  66. }
  67. Dbg("SubScribe CardSwiper status broadcast suc.");
  68. }
  69. }
  70. pTransactionContext->SendAnswer(errorCode);
  71. }
  72. void OnStarted()
  73. {
  74. m_fsm.AfterInit();
  75. ///**TODO(Gifur@10/21/2021): ´Ë½Ó¿ÚÓÐÎÊÌ⣬´ý²é£¡£¡£¡ */
  76. //m_bInitMode = m_fsm.IsInitMode();
  77. //Dbg("%s: %u", __FUNCTION__, m_bInitMode);
  78. if (!m_bInitMode) {
  79. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  80. spEntityFunction->SetTimer(ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE,
  81. this, ENT_TIMERINTERVAL_CHECK_SOGOU_INPUT_INSTALLED_STATE);
  82. }
  83. }
  84. /*ignore*/
  85. virtual void OnPrePause(CSmartPointer<ITransactionContext> pTransactionContext)
  86. {
  87. UOS::MonitorInfo info;
  88. info.name = "VGA-0";
  89. info.nResolutionX = 1280;
  90. info.nResolutionY = 1024;
  91. info.isPrimary = true;
  92. ConfigMonitorSetting(info);
  93. pTransactionContext->SendAnswer(Error_Succeed);
  94. }
  95. /*ignore*/
  96. virtual void OnPreContinue(CSmartPointer<ITransactionContext> pTransactionContext)
  97. {
  98. UOS::MonitorInfo info;
  99. info.name = "HDMI-A-0";
  100. info.nResolutionX = 1024;
  101. info.nResolutionY = 768;
  102. info.isPrimary = false;
  103. info.other = "VGA-0";
  104. info.posDirecttion = 2;
  105. ConfigMonitorSetting(info);
  106. pTransactionContext->SendAnswer(Error_Succeed);
  107. }
  108. /*invoke fsm.onExit(), invoked when entity will be closed*/
  109. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,
  110. CSmartPointer<ITransactionContext>pTransactionContext)
  111. {
  112. ErrorCodeEnum errorCode = m_fsm.OnExit();
  113. pTransactionContext->SendAnswer(errorCode);
  114. }
  115. virtual void OnSelfTest(EntityTestEnum eTestType,
  116. CSmartPointer<ITransactionContext>pTransactionContext)
  117. {
  118. m_fsm.SelfTest(eTestType, pTransactionContext);
  119. }
  120. virtual bool IsService() const
  121. {
  122. return true;
  123. }
  124. virtual bool IsMultiThread() const
  125. {
  126. return false;
  127. }
  128. void Fetch(SpReqAnsContext<ResourceWatcherService_Fetch_Req,
  129. ResourceWatcherService_Fetch_Ans>::Pointer ctx)
  130. {
  131. FetchEvent* evt = new FetchEvent();
  132. evt->ctx = ctx;
  133. m_fsm.PostEventFIFO(evt);
  134. }
  135. void GetDevInfo(SpReqAnsContext<ResourceWatcherService_GetDevInfo_Req,
  136. ResourceWatcherService_GetDevInfo_Ans>::Pointer ctx)
  137. {
  138. ctx->Answer(Error_NotImpl);
  139. }
  140. void GetCSwiperStatus(SpReqAnsContext<ResourceWatcherService_GetCardSwiper_Req,
  141. ResourceWatcherService_GetCardSwiper_Ans>::Pointer ctx)
  142. {
  143. ctx->Ans.status = m_fsm.GetCardSwiperVal();
  144. ctx->Answer(Error_Succeed);
  145. }
  146. void GetCPUType(SpReqAnsContext<ResourceWatcherService_GetCpuType_Req,
  147. ResourceWatcherService_GetCpuType_Ans>::Pointer ctx)
  148. {
  149. m_fsm.GetCpuType(ctx);
  150. }
  151. void OperateFile(
  152. SpReqAnsContext<ResourceWatcherService_OperateFile_Req,
  153. ResourceWatcherService_OperateFile_Ans>::Pointer ctx)
  154. {
  155. OperateFileEvent* evt = new OperateFileEvent();
  156. evt->m_ctx = ctx;
  157. m_fsm.PostEventFIFO(evt);
  158. }
  159. void RetrieveEventLog(SpReqAnsContext<ResourceWatcherService_ExtractEventLog_Req,
  160. ResourceWatcherService_ExtractEventLog_Ans>::Pointer ctx)
  161. {
  162. LOG_FUNCTION();
  163. #if defined(RVC_OS_WIN)
  164. m_fsm.PostEventFIFO(new GetEventLogEvent(ctx));
  165. #else
  166. ctx->Answer(Error_NotSupport);
  167. #endif //RVC_OS_WIN
  168. }
  169. void UpdateDNS(SpReqAnsContext<ResourceWatcherService_UpdateDNS_Req, ResourceWatcherService_UpdateDNS_Ans>::Pointer ctx);
  170. void GetNetworkInfo(SpReqAnsContext<ResourceWatcherService_GetNetworkInfo_Req, ResourceWatcherService_GetNetworkInfo_Ans>::Pointer ctx);
  171. void GetThirdPartyInstallState(SpReqAnsContext<ResourceWatcherService_GetThirdPartyInstallState_Req, ResourceWatcherService_GetThirdPartyInstallState_Ans>::Pointer ctx);
  172. void InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req, ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
  173. #if defined(_MSC_VER)
  174. SP_BEGIN_MSG_DISPATCH_MAP(ResourceWatcherEntity)
  175. SP_BEGIN_ENTITY_MSG("CardSwiper")
  176. SP_MSG_HANDLE_NS(CardSwiper, ConnectStatus, OnCardSwiperConnectStatus)
  177. SP_END_ENTITY_MSG()
  178. SP_END_MSG_DISPATCH_MAP()
  179. #else
  180. virtual void OnBroadcastEvent(CUUID SubID, const char* pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, CAutoBuffer Buffer)
  181. {
  182. }
  183. #endif //_MSC_VER
  184. private:
  185. void OnCardSwiperConnectStatus(const char *pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, CardSwiper::ConnectStatus &evt)
  186. {
  187. Dbg("OnCardSwiperConnectStatus %d", evt.status);
  188. QueryCardSwiper cardswiperStatus;
  189. cardswiperStatus.status = evt.status;
  190. m_fsm.m_cardswiperStatus = evt.status;
  191. SpSendBroadcast(GetFunction(), SP_MSG_OF(QueryCardSwiper), SP_MSG_SIG_OF(QueryCardSwiper),
  192. cardswiperStatus);
  193. }
  194. ErrorCodeEnum DoCheckInstallStateJob();
  195. void DoCheckSogouProcessStatus();
  196. void OnTimeout(DWORD dwTimerID)
  197. {
  198. if(dwTimerID == ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE) {
  199. CSimpleStringA strState;
  200. auto err = GetFunction()->GetSysVar("UIState",strState);
  201. /** 让进入首页之后才获取输入法信息 [Gifur@20211212]*/
  202. if(/*TRUE || */err == Error_Succeed && strState == "M") {
  203. Dbg("to check Sogou input install state...");
  204. if(Error_Succeed == DoCheckInstallStateJob()) {
  205. GetFunction()->KillTimer(ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE);
  206. GetFunction()->SetTimer(ENT_TIMERID_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE,
  207. this, ENT_TIMERINTERVAL_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE);
  208. }
  209. } else {
  210. Dbg("Get UIState result: %s, %s", SpStrError(err), strState.GetData());
  211. }
  212. } else if (dwTimerID == ENT_TIMERID_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE) {
  213. DoCheckSogouProcessStatus();
  214. }
  215. else {
  216. Dbg("Unkonwn timer id: %u", dwTimerID);
  217. }
  218. }
  219. ErrorCodeEnum ConfigMonitorSetting(const UOS::MonitorInfo& config);
  220. private:
  221. ResourceWatcherFSM m_fsm;
  222. CUUID m_uiCardSwiperStatusListener;
  223. BOOL m_bInitMode;
  224. };