mod_guiconsole.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. #include "mod_GuiConsole.h"
  2. #include "publicFunExport.h"
  3. #include <EventCode.h>
  4. #include "GUIConsole_msg_g.h"
  5. #include <chrono>
  6. bool isPad = false;
  7. void CGUIConsoleEntity::OnPreStart(CAutoArray<CSimpleStringA> strArgs, CSmartPointer<ITransactionContext> pTransactionContext)
  8. {
  9. pTransactionContext->SendAnswer(Error_Succeed);
  10. }
  11. void CGUIConsoleEntity::OnPreClose(EntityCloseCauseEnum eCloseCause, CSmartPointer<ITransactionContext> pTransactionContext)
  12. {
  13. pTransactionContext->SendAnswer(Error_Succeed);
  14. }
  15. void CGUIConsoleEntity::OnLog(const CAutoArray<CUUID>& SubIDs, const CUUID nLogID, const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  16. const DWORD dwSysError, const DWORD dwUserCode, const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  17. const CAutoArray<DWORD>& Param, const char* pszEntityName, const char* pszModuleName, const char* pszMessage, const linkContext& pLinkInfo)
  18. {
  19. // 忽略GPIO事件
  20. if (dwUserCode == 0x2090000A || dwUserCode == 0x20900009)
  21. return;
  22. //did not open log sender
  23. if (!m_isOpenLogSender)
  24. return;
  25. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("OnLog %s %s", pszEntityName, pszMessage);
  26. if (pszMessage != NULL && strlen(pszMessage) > 2)
  27. {
  28. CSimpleStringA str = pszMessage;
  29. LogInfo msg;
  30. msg.sysError = dwSysError;
  31. msg.userCode = dwUserCode;
  32. msg.timeStr = GenerateTimeStr().c_str();
  33. msg.entityName = pszEntityName;
  34. msg.LogType = LogTypeEnumToString(eLogType).c_str();
  35. msg.message = str;
  36. SpSendBroadcast(GetFunction(), eMsg_LogInfo, eMsgSig_LogInfo, msg);
  37. }
  38. }
  39. void CGUIConsoleEntity::OnEntityStateHook(const char* pszEntityName, const char* pszTriggerEntity, EntityStateEnum eState, EntityStateEnum eLastState)
  40. {
  41. //did not open log sender
  42. if (m_isOpenLogSender)
  43. {
  44. LogInfo msg;
  45. if (eLastState == EntityState_Idle && eState != EntityState_Idle)
  46. {
  47. msg.sysError = ErrorCodeEnum::Error_Closed;
  48. msg.userCode = ErrorCodeEnum::Error_Closed;
  49. msg.message = CSimpleString::Format("%s实体异常,重新启动...", pszEntityName);
  50. }
  51. else
  52. {
  53. msg.sysError = ErrorCodeEnum::Error_Succeed;
  54. msg.userCode = ErrorCodeEnum::Error_Succeed;
  55. if (eState == EntityState_Starting)
  56. msg.message = CSimpleString::Format("正在启动实体%s...", pszEntityName);
  57. else if (eState == EntityState_Idle)
  58. msg.message = CSimpleString::Format("%s 实体启动成功", pszEntityName);
  59. }
  60. msg.timeStr = GenerateTimeStr().c_str();
  61. msg.entityName = pszEntityName;
  62. SpSendBroadcast(GetFunction(), eMsg_LogInfo, eMsgSig_LogInfo, msg);
  63. }
  64. if (m_isOpenEntityMonitor)
  65. PostEntityVary(pszEntityName);//if registered, while entity status changed, send to web
  66. }
  67. void CGUIConsoleEntity::OnTimeout(DWORD dwTimerID)
  68. {
  69. }
  70. std::pair<DWORD, std::string> CGUIConsoleEntity::openLogSender(SpReqAnsContext<GUIConsoleService_OpenLogSender_Req, GUIConsoleService_OpenLogSender_Ans>::Pointer ctx)
  71. {
  72. if ((__int64)m_logSubID != 0)
  73. {
  74. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_AlreadyExist, WARN_GUICONSOLE_LOG_ALREADY_REGISTER, "openLogSender: already register");
  75. m_isOpenLogSender = true;
  76. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_LOG_ALREADY_REGISTER, "openLogSender: already register");
  77. }
  78. else
  79. {
  80. auto logType = StringToLogTypeEnum(ctx->Req.LogType.GetData());
  81. //check valid input for this function
  82. if (logType != LogTypeEnum::Log_Ignore && logType != LogTypeEnum::Log_Event && logType != LogTypeEnum::Log_Warning && logType != LogTypeEnum::Log_Error)
  83. {
  84. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_NotSupport, WARN_GUICONSOLE_LOGSENDER_UNVAILD_INPUT,
  85. CSimpleString::Format("openLogSender: not valid input: %s, use default warning", ctx->Req.LogType.GetData()).GetData());
  86. m_sendLogType = LogTypeEnum::Log_Warning;
  87. }
  88. else
  89. m_sendLogType = logType;
  90. auto rc = GetFunction()->SubscribeLog(m_logSubID, this, (LogTypeEnum)m_sendLogType, Severity_None, Error_IgnoreAll, -1, NULL, false);
  91. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("openLogSender SubscribeLog %s %s", ctx->Req.LogType.GetData(),
  92. (rc == ErrorCodeEnum::Error_Succeed ? "success" : "failed"));
  93. m_isOpenLogSender = (rc == ErrorCodeEnum::Error_Succeed);
  94. return std::pair<DWORD, std::string>(rc, "");
  95. }
  96. }
  97. std::pair<DWORD, std::string> CGUIConsoleEntity::closeLogSender()
  98. {
  99. if ((__int64)m_logSubID == 0)
  100. {
  101. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_AlreadyExist, WARN_GUICONSOLE_LOG_ALREADY_UNREGISTER, "closeLogSender: already unregister");
  102. m_isOpenLogSender = false;
  103. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_LOG_ALREADY_UNREGISTER, "closeLogSender: already unregister");
  104. }
  105. else
  106. {
  107. auto rc = GetFunction()->UnsubscribeLog(m_logSubID);
  108. if(rc == ErrorCodeEnum::Error_Succeed)
  109. m_logSubID = 0;
  110. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("closeLogSender UnsubscribeLog %s", (rc == ErrorCodeEnum::Error_Succeed ? "success" : "failed"));
  111. m_isOpenLogSender = !(rc == ErrorCodeEnum::Error_Succeed);
  112. return std::pair<DWORD, std::string>(rc, "");
  113. }
  114. }
  115. std::pair<DWORD, std::string> CGUIConsoleEntity::openEntityMonitor(SpReqAnsContext<GUIConsoleService_OpenEntityMonitor_Req, GUIConsoleService_OpenEntityMonitor_Ans>::Pointer ctx)
  116. {
  117. m_isOpenEntityMonitor = true;
  118. if (ctx->Req.longestFrequence <= 1)
  119. m_longestEntityMonitor = 1;
  120. else
  121. m_longestEntityMonitor = ctx->Req.longestFrequence;
  122. if (ctx->Req.leastFrequence <= 1)
  123. m_leastEntityMonitor = 1;
  124. else
  125. m_leastEntityMonitor = ctx->Req.leastFrequence;
  126. m_entityMonitor_shouldwork = true;
  127. int rc_errorenum = ErrorCodeEnum::Error_Succeed;
  128. if (!GetFunction()->HasPrivilege())
  129. {
  130. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_NoPrivilege, WARN_GUICONSOLE_ENTITYMONITOR_NO_PRIVILEGE, "openEntityMonitor no privilige");
  131. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_ENTITYMONITOR_NO_PRIVILEGE, "");
  132. }
  133. if (ErrorCodeEnum::Error_Succeed != (rc_errorenum = GetFunction()->GetPrivilegeFunction()->RegistEntityStateEvent(NULL, this)))
  134. {
  135. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_NoPrivilege, WARN_GUICONSOLE_ENTITYMONITOR_REGISTER,
  136. CSimpleStringA::Format("openEntityMonitor RegistEntityStateEvent failed, %d", rc_errorenum));
  137. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_ENTITYMONITOR_REGISTER, "");
  138. }
  139. auto entityMonitorSender = [this] {
  140. while (m_isOpenEntityMonitor)
  141. {
  142. if(m_entityMonitor_shouldwork || (std::chrono::steady_clock::now() - m_entityMonitorlast_notify_time >= std::chrono::seconds(m_longestEntityMonitor)))
  143. {
  144. m_entityMonitorlast_notify_time = std::chrono::steady_clock::now();
  145. m_entityMonitor_shouldwork = false;
  146. EntityStatus cur;
  147. CAutoArray<EntityEntry> Entities;
  148. auto ret = GetAllEntity(Entities);
  149. if (ret.first != ErrorCodeEnum::Error_Succeed)
  150. {
  151. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_AlreadyExist, WARN_GUICONSOLE_ENTITYMONITOR_GETINFO_FAILED,
  152. CSimpleStringA::Format("openEntityMonitor - GetAllEntity failed, %d", ret.first));
  153. continue;
  154. }
  155. cur.entityName.Init(Entities.GetCount());
  156. cur.status.Init(Entities.GetCount());
  157. cur.processId.Init(Entities.GetCount());
  158. cur.versionNo.Init(Entities.GetCount());
  159. cur.lastStartTime.Init(Entities.GetCount());
  160. for (int i = 0; i < Entities.GetCount(); ++i) {
  161. cur.entityName[i] = Entities[i].Name;
  162. cur.status[i] = EntityEntry::GetStateName(Entities[i].State);
  163. cur.processId[i] = std::to_string(Entities[i].Pid).c_str();
  164. cur.versionNo[i] = getRunVersion();
  165. cur.lastStartTime[i] = Entities[i].lastStartTime.ToTimeString();
  166. }
  167. SpSendBroadcast(GetFunction(), eMsg_EntityStatus, eMsgSig_EntityStatus, cur);
  168. }
  169. std::this_thread::sleep_for(std::chrono::seconds(m_leastEntityMonitor));
  170. }
  171. };
  172. if (thread_entityMonitor != NULL)
  173. {
  174. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_AlreadyExist, WARN_GUICONSOLE_ENTITYMONITOR_EXISTED,
  175. "openEntityMonitor, thread_entityMonitor already existed");
  176. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_ENTITYMONITOR_EXISTED, "");
  177. }
  178. thread_entityMonitor = new std::thread(entityMonitorSender);
  179. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("openEntityMonitor create entityMonitor sender thread %s, leastFrequence:%d, m_longestEntityMonitor:%d",
  180. (thread_entityMonitor != NULL ? "success" : "failed"), m_leastEntityMonitor, m_longestEntityMonitor);
  181. return std::pair<DWORD, std::string>(Error_Succeed, "");
  182. }
  183. std::pair<DWORD, std::string> CGUIConsoleEntity::closeEntityMonitor()
  184. {
  185. m_isOpenEntityMonitor = false;
  186. int rc_errorenum = ErrorCodeEnum::Error_Succeed;
  187. if (!GetFunction()->HasPrivilege())
  188. {
  189. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_NoPrivilege, WARN_GUICONSOLE_ENTITYMONITOR_NO_PRIVILEGE, "closeEntityMonitor no privilige");
  190. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_ENTITYMONITOR_NO_PRIVILEGE, "");
  191. }
  192. if (ErrorCodeEnum::Error_Succeed != (rc_errorenum = GetFunction()->GetPrivilegeFunction()->UnregistEntityStateEvent(NULL)))
  193. {
  194. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_NoPrivilege, WARN_GUICONSOLE_ENTITYMONITOR_UNREGISTER,
  195. CSimpleStringA::Format("closeEntityMonitor unRegistEntityStateEvent failed, %d", rc_errorenum));
  196. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_ENTITYMONITOR_UNREGISTER, "");
  197. }
  198. if (thread_entityMonitor == NULL)
  199. {
  200. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_NotExist, WARN_GUICONSOLE_ENTITYMONITOR_NOTEXISTED,
  201. "closeEntityMonitor, thread_entityMonitor not exist");
  202. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_ENTITYMONITOR_NOTEXISTED, "");
  203. }
  204. thread_entityMonitor->join();
  205. delete thread_entityMonitor;
  206. thread_entityMonitor = NULL;
  207. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("closeEntityMonitor close entityMonitor sender thread success");
  208. return std::pair<DWORD, std::string>(Error_Succeed, "");
  209. }
  210. std::pair<DWORD, std::string> CGUIConsoleEntity::openPerformanceSender(SpReqAnsContext<GUIConsoleService_OpenPerformanceSender_Req, GUIConsoleService_OpenPerformanceSender_Ans>::Pointer ctx)
  211. {
  212. m_isOpenPerformanceSender = true;
  213. if (ctx->Req.sendFrequence <= 1)
  214. m_performanceSenderFrequence = 1;
  215. else
  216. m_performanceSenderFrequence = ctx->Req.sendFrequence;
  217. auto performanceSender = [this] {
  218. while (m_isOpenPerformanceSender)
  219. {
  220. system_monitor_status curStatus;
  221. auto pullret = PollProcessList(m_performanceSenderFrequence, curStatus);
  222. PerformanceList dst;
  223. dst.NameArr.Init(curStatus.processList.size());
  224. dst.pidArr.Init(curStatus.processList.size());
  225. dst.cpuArr.Init(curStatus.processList.size());
  226. dst.memoryArr.Init(curStatus.processList.size());
  227. dst.cmdlineArr.Init(curStatus.processList.size());
  228. dst.handleNumArr.Init(curStatus.processList.size());
  229. for (int i = 0; i < curStatus.processList.size(); i++)
  230. {
  231. dst.NameArr[i] = curStatus.processList[i].ExeName;
  232. dst.pidArr[i] = curStatus.processList[i].ID;
  233. dst.cpuArr[i] = curStatus.processList[i].PercentProcessorTime;
  234. dst.memoryArr[i] = curStatus.processList[i].PercentMemory;
  235. dst.cmdlineArr[i] = "";
  236. dst.handleNumArr[i] = curStatus.processList[i].ThreadCount;
  237. }
  238. SpSendBroadcast(GetFunction(), eMsg_PerformanceList, eMsgSig_PerformanceList, dst);
  239. std::this_thread::sleep_for(std::chrono::seconds(m_performanceSenderFrequence));
  240. }
  241. };
  242. if (thread_performanceSend != NULL)
  243. {
  244. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_AlreadyExist, WARN_GUICONSOLE_PERFORMANCE_EXISTED,
  245. "openPerformanceSender, thread_performanceSend already existed");
  246. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_PERFORMANCE_EXISTED, "");
  247. }
  248. thread_performanceSend = new std::thread(performanceSender);
  249. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("openPerformanceSender create performance sender thread %s, sendFrequence:%d",
  250. (thread_performanceSend != NULL ? "success" : "failed"), m_performanceSenderFrequence);
  251. return std::pair<DWORD, std::string>(Error_Succeed, "");
  252. }
  253. std::pair<DWORD, std::string> CGUIConsoleEntity::closePerformanceSender()
  254. {
  255. m_isOpenPerformanceSender = false;
  256. if(thread_performanceSend == NULL)
  257. {
  258. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_NotExist, WARN_GUICONSOLE_PERFORMANCE_NOTEXISTED,
  259. "closePerformanceSender, thread_performanceSend not exist");
  260. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_PERFORMANCE_NOTEXISTED, "");
  261. }
  262. thread_performanceSend->join();
  263. delete thread_performanceSend;
  264. thread_performanceSend = NULL;
  265. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("closePerformanceSender close performance sender thread success");
  266. return std::pair<DWORD, std::string>(Error_Succeed, "");
  267. }
  268. std::pair<DWORD, std::string> CGUIConsoleEntity::VTMSystemControl(SpReqAnsContext<GUIConsoleService_VTMSystemControl_Req, GUIConsoleService_VTMSystemControl_Ans>::Pointer ctx)
  269. {
  270. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  271. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  272. if (pFuncPrivilege == NULL)
  273. {
  274. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_NoPrivilege, WARN_GUICONSOLE_NO_PRIVILIGE, "VTMSystemControl no privilige");
  275. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_NO_PRIVILIGE, "");
  276. }
  277. DWORD rc = Error_Succeed;
  278. if(!ctx->Req.rebootFunction.Compare("RestartApp", true))
  279. rc = pFuncPrivilege->Reboot(RebootTrigger_ManualLocal, RebootWayEnum::RebootWay_Framework);
  280. else if (!ctx->Req.rebootFunction.Compare("RestartOS", true))
  281. rc = pFuncPrivilege->Reboot(RebootTrigger_ManualLocal, RebootWayEnum::RebootWay_OS);
  282. else if (!ctx->Req.rebootFunction.Compare("ShutdownOS", true))
  283. rc = pFuncPrivilege->Reboot(RebootTrigger_ManualLocal, RebootWayEnum::RebootWay_Power);
  284. else if (!ctx->Req.rebootFunction.Compare("ExitApp", true))
  285. rc = pFuncPrivilege->Reboot(RebootTrigger_ManualLocal, RebootWayEnum::RebootWay_Unknown);
  286. else
  287. {
  288. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_NoDefine, WARN_GUICONSOLE_REBOOT_UNKNOWN_REASON,
  289. CSimpleStringA::Format("VTMSystemControl receive unknown param %s, do nothing", ctx->Req.rebootFunction.GetData()).GetData());
  290. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_REBOOT_UNKNOWN_REASON, "");
  291. }
  292. return std::pair<DWORD, std::string>(rc, "");
  293. }
  294. std::pair<DWORD, std::string> CGUIConsoleEntity::VTM_controlEntity(SpReqAnsContext<GUIConsoleService_VTM_controlEntity_Req, GUIConsoleService_VTM_controlEntity_Ans>::Pointer ctx)
  295. {
  296. ErrorCodeEnum ret = ErrorCodeEnum::Error_Succeed;
  297. if (!ctx->Req.operation.Compare("Start", true))
  298. ret = AsyncStartEntity(ctx->Req.entityName, NULL, NULL);
  299. else if (!ctx->Req.operation.Compare("Stop", true))
  300. ret = AsyncStopEntity(ctx->Req.entityName, NULL);
  301. else if (!ctx->Req.operation.Compare("Pause", true))
  302. ret = AsyncPauseEntity(ctx->Req.entityName, NULL);
  303. else if (!ctx->Req.operation.Compare("Continue", true))
  304. ret = AsyncContinueEntity(ctx->Req.entityName, NULL);
  305. else if (!ctx->Req.operation.Compare("Terminate", true))
  306. ret = AsyncTerminateEntity(ctx->Req.entityName, NULL);
  307. if (m_isOpenLogSender)
  308. {
  309. LogInfo msg;
  310. msg.sysError = ret;
  311. msg.userCode = ret;
  312. msg.timeStr = GenerateTimeStr().c_str();
  313. msg.entityName = ctx->Req.entityName;
  314. msg.message = CSimpleString::Format("%s %s %s, ret:%d", ctx->Req.entityName, ctx->Req.operation
  315. , (ret == ErrorCodeEnum::Error_Succeed ? "Success" : "Failed"), ret);
  316. SpSendBroadcast(GetFunction(), eMsg_LogInfo, eMsgSig_LogInfo, msg);
  317. }
  318. return std::pair<DWORD, std::string>(ret, "");
  319. }
  320. std::pair<DWORD, std::string> CGUIConsoleEntity::GetBasicInformation(SpReqAnsContext<GUIConsoleService_GetBasicInformation_Req, GUIConsoleService_GetBasicInformation_Ans>::Pointer ctx)
  321. {
  322. //get idle entityNum;
  323. int idleNum = 0, totalNum = 0;
  324. CAutoArray<EntityEntry> Entities;
  325. auto ret = ErrorCodeEnum::Error_Succeed;
  326. auto entity_ret = GetAllEntity(Entities);
  327. if (ErrorCodeEnum::Error_Succeed != entity_ret.first)
  328. {
  329. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_Unexpect, WARN_GUICONSOLE_BASICINFO_GETENTITY
  330. , CSimpleString::Format("GetBasicInformation-GetAllEntity get entity failed, %d", entity_ret.first).GetData());
  331. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_BASICINFO_GETENTITY, "");
  332. }
  333. totalNum = Entities.GetCount();
  334. for (int i = 0; i < Entities.GetCount(); i++)
  335. {
  336. CSimpleString curState = EntityEntry::GetStateName(Entities[i].State);
  337. if (!curState.Compare("Idle", true))
  338. idleNum++;
  339. }
  340. ctx->Ans.total_entityNum = totalNum;
  341. ctx->Ans.Idle_entityNum = idleNum;
  342. CSystemStaticInfo m_terminalInfo;
  343. if (ErrorCodeEnum::Error_Succeed != (ret = GetFunction()->GetSystemStaticInfo(m_terminalInfo)))
  344. {
  345. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_Unexpect, WARN_GUICONSOLE_BASICINFO_SYSTEMINFO_FAILED
  346. , CSimpleString::Format("GetBasicInformation-GetSystemStaticInfo failed, %d", ret).GetData());
  347. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_BASICINFO_SYSTEMINFO_FAILED, "");
  348. }
  349. ctx->Ans.terminalNo = m_terminalInfo.strTerminalID;
  350. ctx->Ans.machineType = m_terminalInfo.strMachineType;
  351. ctx->Ans.manufacturer = m_terminalInfo.strManufacturer;
  352. ctx->Ans.machineModel = m_terminalInfo.strMachineModel;
  353. ctx->Ans.versionNo = getRunVersion();
  354. #ifdef DEVOPS_ON_ST /*DevOps流水线编译,ST环境*/
  355. ctx->Ans.Env = "ST";
  356. #elif defined(DEVOPS_ON_UAT)/*DevOps流水线编译,UAT环境*/
  357. ctx->Ans.Env = "UAT";
  358. #elif defined(DEVOPS_ON_PRD)/*DevOps流水线编译,PRD环境*/
  359. ctx->Ans.Env = "PRD";
  360. #else/*本地编译等非DevOps环境编译的版本*/
  361. ctx->Ans.Env = "DEV";
  362. #endif
  363. CSystemRunInfo sysRunInfo;
  364. if (ErrorCodeEnum::Error_Succeed != (ret = GetFunction()->GetSystemRunInfo(sysRunInfo)))
  365. {
  366. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_Unexpect, WARN_GUICONSOLE_BASICINFO_RUNINFO_FAILED
  367. , CSimpleString::Format("GetBasicInformation-GetSystemRunInfo failed, %d", ret).GetData());
  368. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_BASICINFO_RUNINFO_FAILED, "");
  369. }
  370. ctx->Ans.vtm_startupTime = sysRunInfo.tmStart.ToTimeString();
  371. ctx->Ans.basicconfig_status = sysRunInfo.bBasicCfgWork;
  372. ctx->Ans.errmsg_status = sysRunInfo.bErrCfgWork;
  373. return std::pair<DWORD, std::string>(ErrorCodeEnum::Error_Succeed, "");
  374. }
  375. std::pair<DWORD, std::string> CGUIConsoleEntity::GetHelpInformationArr(SpReqAnsContext<GUIConsoleService_GetHelpInformationArr_Req, GUIConsoleService_GetHelpInformationArr_Ans>::Pointer ctx)
  376. {
  377. return std::pair<DWORD, std::string>();
  378. }
  379. std::pair<DWORD, std::string> CGUIConsoleEntity::GetHelpDetail(SpReqAnsContext<GUIConsoleService_GetHelpDetail_Req, GUIConsoleService_GetHelpDetail_Ans>::Pointer ctx)
  380. {
  381. return std::pair<DWORD, std::string>();
  382. }
  383. CGUIConsoleEntity::CGUIConsoleEntity()
  384. :m_isOpenLogSender(false), m_isOpenEntityMonitor(false), m_isOpenPerformanceSender(false),
  385. m_sendLogType(LogTypeEnum::Log_Warning), thread_performanceSend(NULL), thread_entityMonitor(NULL),
  386. m_longestEntityMonitor(60), m_leastEntityMonitor(2)
  387. {
  388. }
  389. CGUIConsoleEntity::~CGUIConsoleEntity()
  390. {
  391. m_isOpenPerformanceSender = false;
  392. m_isOpenEntityMonitor = false;
  393. m_isOpenLogSender = false;
  394. if (thread_performanceSend)
  395. {
  396. thread_performanceSend->detach();
  397. delete thread_performanceSend;
  398. thread_performanceSend = NULL;
  399. }
  400. if (thread_entityMonitor)
  401. {
  402. thread_entityMonitor->detach();
  403. delete thread_entityMonitor;
  404. thread_entityMonitor = NULL;
  405. }
  406. }
  407. void CGUIConsoleSession::Handle_OpenLogSender(SpReqAnsContext<GUIConsoleService_OpenLogSender_Req, GUIConsoleService_OpenLogSender_Ans>::Pointer ctx)
  408. {
  409. DbgToBeidou(ctx->link, __FUNCTION__)();
  410. auto ret = m_pEntity->openLogSender(ctx);
  411. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  412. }
  413. void CGUIConsoleSession::Handle_CloseLogSender(SpReqAnsContext<GUIConsoleService_CloseLogSender_Req, GUIConsoleService_CloseLogSender_Ans>::Pointer ctx)
  414. {
  415. DbgToBeidou(ctx->link, __FUNCTION__)();
  416. auto ret = m_pEntity->closeLogSender();
  417. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  418. }
  419. void CGUIConsoleSession::Handle_OpenEntityMonitor(SpReqAnsContext<GUIConsoleService_OpenEntityMonitor_Req, GUIConsoleService_OpenEntityMonitor_Ans>::Pointer ctx)
  420. {
  421. DbgToBeidou(ctx->link, __FUNCTION__)();
  422. auto ret = m_pEntity->openEntityMonitor(ctx);
  423. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  424. }
  425. void CGUIConsoleSession::Handle_CloseEntityMonitor(SpReqAnsContext<GUIConsoleService_CloseEntityMonitor_Req, GUIConsoleService_CloseEntityMonitor_Ans>::Pointer ctx)
  426. {
  427. DbgToBeidou(ctx->link, __FUNCTION__)();
  428. auto ret = m_pEntity->closeEntityMonitor();
  429. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  430. }
  431. void CGUIConsoleSession::Handle_OpenPerformanceSender(SpReqAnsContext<GUIConsoleService_OpenPerformanceSender_Req, GUIConsoleService_OpenPerformanceSender_Ans>::Pointer ctx)
  432. {
  433. DbgToBeidou(ctx->link, __FUNCTION__)();
  434. auto ret = m_pEntity->openPerformanceSender(ctx);
  435. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  436. }
  437. void CGUIConsoleSession::Handle_ClosePerformanceSender(SpReqAnsContext<GUIConsoleService_ClosePerformanceSender_Req, GUIConsoleService_ClosePerformanceSender_Ans>::Pointer ctx)
  438. {
  439. DbgToBeidou(ctx->link, __FUNCTION__)();
  440. auto ret = m_pEntity->closePerformanceSender();
  441. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  442. }
  443. void CGUIConsoleSession::Handle_VTMSystemControl(SpReqAnsContext<GUIConsoleService_VTMSystemControl_Req, GUIConsoleService_VTMSystemControl_Ans>::Pointer ctx)
  444. {
  445. DbgToBeidou(ctx->link, __FUNCTION__)();
  446. auto ret = m_pEntity->VTMSystemControl(ctx);
  447. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  448. }
  449. void CGUIConsoleSession::Handle_VTM_controlEntity(SpReqAnsContext<GUIConsoleService_VTM_controlEntity_Req, GUIConsoleService_VTM_controlEntity_Ans>::Pointer ctx)
  450. {
  451. DbgToBeidou(ctx->link, __FUNCTION__)();
  452. auto ret = m_pEntity->VTM_controlEntity(ctx);
  453. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  454. }
  455. void CGUIConsoleSession::Handle_GetAllEntityList(SpReqAnsContext<GUIConsoleService_GetAllEntityList_Req, GUIConsoleService_GetAllEntityList_Ans>::Pointer ctx)
  456. {
  457. DbgToBeidou(ctx->link, __FUNCTION__)();
  458. CAutoArray<EntityEntry> Entities;
  459. auto ret = m_pEntity->GetAllEntity(Entities);
  460. if (ret.first != ErrorCodeEnum::Error_Succeed)
  461. {
  462. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  463. return;
  464. }
  465. ctx->Ans.entityName.Init(Entities.GetCount());
  466. ctx->Ans.status.Init(Entities.GetCount());
  467. ctx->Ans.processId.Init(Entities.GetCount());
  468. ctx->Ans.versionNo.Init(Entities.GetCount());
  469. ctx->Ans.lastStartTime.Init(Entities.GetCount());
  470. for (int i = 0; i < Entities.GetCount(); ++i) {
  471. ctx->Ans.entityName[i] = Entities[i].Name;
  472. ctx->Ans.status[i] = EntityEntry::GetStateName(Entities[i].State);
  473. ctx->Ans.processId[i] = std::to_string(Entities[i].Pid).c_str();
  474. ctx->Ans.versionNo[i] = m_pEntity->getRunVersion();
  475. ctx->Ans.lastStartTime[i] = Entities[i].lastStartTime.ToTimeString();
  476. }
  477. ctx->Answer(Error_Succeed);
  478. }
  479. void CGUIConsoleSession::Handle_GetBasicInformation(SpReqAnsContext<GUIConsoleService_GetBasicInformation_Req, GUIConsoleService_GetBasicInformation_Ans>::Pointer ctx)
  480. {
  481. DbgToBeidou(ctx->link, __FUNCTION__)();
  482. auto ret = m_pEntity->GetBasicInformation(ctx);
  483. ctx->Answer((ErrorCodeEnum)ret.first, ret.first);
  484. }
  485. void CGUIConsoleSession::Handle_GetHelpInformationArr(SpReqAnsContext<GUIConsoleService_GetHelpInformationArr_Req, GUIConsoleService_GetHelpInformationArr_Ans>::Pointer ctx)
  486. {
  487. DbgToBeidou(ctx->link, __FUNCTION__)();
  488. //m_pEntity->GetHelpInformationArr(ctx);
  489. ctx->Answer(ErrorCodeEnum::Error_Succeed);
  490. }
  491. void CGUIConsoleSession::Handle_GetHelpDetail(SpReqAnsContext<GUIConsoleService_GetHelpDetail_Req, GUIConsoleService_GetHelpDetail_Ans>::Pointer ctx)
  492. {
  493. DbgToBeidou(ctx->link, __FUNCTION__)();
  494. //m_pEntity->GetHelpDetail(ctx);
  495. ctx->Answer(ErrorCodeEnum::Error_Succeed);
  496. }
  497. CSimpleString CGUIConsoleEntity::getRunVersion()
  498. {
  499. static CSimpleStringA activeVer;
  500. if(activeVer.GetLength() == 0)
  501. GetFunction()->GetRunningVersion(activeVer);
  502. return activeVer;
  503. }
  504. ErrorCodeEnum CGUIConsoleEntity::AsyncStartEntity(const char* entity_name, const char* cmdline, void* pData)
  505. {
  506. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  507. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  508. if (pFuncPrivilege != NULL) {
  509. CSmartPointer<IAsynWaitSp> spWait;
  510. ErrorCodeEnum Error = pFuncPrivilege->StartEntity(entity_name, cmdline, spWait);
  511. if (Error == Error_Succeed) {
  512. callback_entry* entry = new callback_entry();
  513. entry->pRawData = pData;
  514. entry->EntityName = entity_name;
  515. entry->ErrorResult = Error_Unexpect;
  516. entry->op = OP_START_ENTITY;
  517. spWait->SetCallback(this, entry);
  518. }
  519. return Error;
  520. }
  521. else {
  522. return Error_NoPrivilege;
  523. }
  524. }
  525. ErrorCodeEnum CGUIConsoleEntity::AsyncStopEntity(const char* entity_name, void* pData)
  526. {
  527. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  528. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  529. if (pFuncPrivilege != NULL) {
  530. CSmartPointer<IAsynWaitSp> spWait;
  531. ErrorCodeEnum Error = pFuncPrivilege->StopEntity(entity_name, spWait);
  532. if (Error == Error_Succeed) {
  533. callback_entry* entry = new callback_entry();
  534. entry->pRawData = pData;
  535. entry->EntityName = entity_name;
  536. entry->ErrorResult = Error_Unexpect;
  537. entry->op = OP_STOP_ENTITY;
  538. spWait->SetCallback(this, entry);
  539. }
  540. return Error;
  541. }
  542. else {
  543. return Error_NoPrivilege;
  544. }
  545. }
  546. ErrorCodeEnum CGUIConsoleEntity::AsyncPauseEntity(const char* entity_name, void* pData)
  547. {
  548. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  549. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  550. if (pFuncPrivilege != NULL) {
  551. CSmartPointer<IAsynWaitSp> spWait;
  552. ErrorCodeEnum Error = pFuncPrivilege->PauseEntity(entity_name, spWait);
  553. if (Error == Error_Succeed) {
  554. callback_entry* entry = new callback_entry();
  555. entry->pRawData = pData;
  556. entry->EntityName = entity_name;
  557. entry->ErrorResult = Error_Unexpect;
  558. entry->op = OP_PAUSE_ENTITY;
  559. spWait->SetCallback(this, entry);
  560. }
  561. return Error;
  562. }
  563. else {
  564. return Error_NoPrivilege;
  565. }
  566. }
  567. ErrorCodeEnum CGUIConsoleEntity::AsyncContinueEntity(const char* entity_name, void* pData)
  568. {
  569. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  570. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  571. if (pFuncPrivilege != NULL) {
  572. CSmartPointer<IAsynWaitSp> spWait;
  573. ErrorCodeEnum Error = pFuncPrivilege->ContinueEntity(entity_name, spWait);
  574. if (Error == Error_Succeed) {
  575. callback_entry* entry = new callback_entry();
  576. entry->pRawData = pData;
  577. entry->EntityName = entity_name;
  578. entry->ErrorResult = Error_Unexpect;
  579. entry->op = OP_CONTINUE_ENTITY;
  580. spWait->SetCallback(this, entry);
  581. }
  582. return Error;
  583. }
  584. else {
  585. return Error_NoPrivilege;
  586. }
  587. }
  588. ErrorCodeEnum CGUIConsoleEntity::AsyncTerminateEntity(const char* entity_name, void* pData)
  589. {
  590. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  591. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  592. if (pFuncPrivilege != NULL) {
  593. CSmartPointer<IAsynWaitSp> spWait;
  594. ErrorCodeEnum Error = pFuncPrivilege->TerminateEntity(entity_name, spWait);
  595. if (Error == Error_Succeed) {
  596. callback_entry* entry = new callback_entry();
  597. entry->pRawData = pData;
  598. entry->EntityName = entity_name;
  599. entry->ErrorResult = Error_Unexpect;
  600. entry->op = OP_TERMINATE_ENTITY;
  601. spWait->SetCallback(this, entry);
  602. }
  603. return Error;
  604. }
  605. else {
  606. return Error_NoPrivilege;
  607. }
  608. }
  609. ErrorCodeEnum CGUIConsoleEntity::GetEntity(const char* entity_name, EntityEntry& e)
  610. {
  611. CSmartPointer<IEntityFunction> spFunc = GetFunction();
  612. LOG_ASSERT(spFunc != NULL);
  613. CEntityStaticInfo StaticInfo;
  614. CEntityRunInfo RunInfo;
  615. ErrorCodeEnum Error = spFunc->GetEntityStaticInfo(entity_name, StaticInfo);
  616. if (Error == Error_Succeed) {
  617. Error = spFunc->GetEntityRunInfo(entity_name, RunInfo);
  618. if (Error == Error_Succeed) {
  619. e.Name = entity_name;
  620. e.Id = RunInfo.dwEntityInstanceID;
  621. e.ModuleName = _GetFileName(StaticInfo.strSpFileName);
  622. e.State = RunInfo.eState;
  623. e.Type = StaticInfo.bStartedByShell ? 0 : 1;
  624. e.Pid = (int)RunInfo.dwProcessID;
  625. e.DevelopID = (int)StaticInfo.wEntityDevelopID;
  626. e.DebugLevel = RunInfo.eDebugLevel;
  627. e.lastStartTime = RunInfo.tmLastStart;
  628. }
  629. }
  630. return Error;
  631. }
  632. void CGUIConsoleEntity::PostEntityVary(const char* entity_name)
  633. {
  634. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("PostEntityVary %s ", entity_name);
  635. EntityEntry ret;
  636. DWORD rc;
  637. if (ErrorCodeEnum::Error_Succeed != (rc = GetEntity(entity_name, ret)))
  638. {
  639. LogWarn(SeverityLevelEnum::Severity_Middle, (ErrorCodeEnum)rc, WARN_GUICONSOLE_GETENTITY_FAILED
  640. , CSimpleStringA::Format("PostEntityVary-GetEntity failed, reason:%d", rc).GetData());
  641. return;
  642. }
  643. m_entityMonitor_shouldwork = true;
  644. /*
  645. EntityStatus cur;
  646. cur.entityName = entity_name;
  647. cur.status = EntityEntry::GetStateName(ret.State);
  648. cur.processId = std::to_string(ret.Pid).c_str();
  649. cur.versionNo = getRunVersion();
  650. cur.lastStartTime = ret.lastStartTime.ToTimeString();
  651. SpSendBroadcast(GetFunction(), eMsg_EntityStatus, eMsgSig_EntityStatus, cur);
  652. */
  653. }
  654. std::pair<DWORD, std::string> CGUIConsoleEntity::GetAllEntity(CAutoArray<EntityEntry>& Entities)
  655. {
  656. CSmartPointer<IEntityFunction> spFunc = GetFunction();
  657. CAutoArray<CSimpleStringA> strEntityNames;
  658. CAutoArray<WORD> wDevelopIDs;
  659. ErrorCodeEnum Error = spFunc->GetAllRegistedEntity(strEntityNames, wDevelopIDs);
  660. if (Error_Succeed != Error)
  661. {
  662. LogWarn(SeverityLevelEnum::Severity_Middle, Error, WARN_GUICONSOLE_GETALLENTITY_REGISTERED, "GetAllEntity-GetAllRegistedEntity failed");
  663. return std::make_pair(WARN_GUICONSOLE_GETALLENTITY_REGISTERED, "");
  664. }
  665. CSimpleString ErrInfo;
  666. Entities.Init(strEntityNames.GetCount());
  667. for (int i = 0; i < strEntityNames.GetCount(); ++i) {
  668. Error = GetEntity(strEntityNames[i], Entities[i]);
  669. if (Error != Error_Succeed)
  670. ErrInfo += CSimpleString::Format("|%s %d|", strEntityNames[i].GetData(), Error);
  671. }
  672. if (ErrInfo.GetLength() > 0)
  673. {
  674. LogWarn(SeverityLevelEnum::Severity_Middle, Error, WARN_GUICONSOLE_GETALLENTITY_GETENTITY, ErrInfo.GetData());
  675. return std::make_pair(WARN_GUICONSOLE_GETALLENTITY_GETENTITY, "");
  676. }
  677. return std::make_pair(ErrorCodeEnum::Error_Succeed, "");
  678. }
  679. void CGUIConsoleEntity::OnAnswer(CSmartPointer<IAsynWaitSp> pAsynWaitSp)
  680. {
  681. CSmartPointer<ICallbackListener> spCallback;
  682. CSmartPointer<IReleasable> pData;
  683. pAsynWaitSp->GetCallback(spCallback, pData);
  684. LOG_ASSERT(pData.GetRawPointer() != NULL);
  685. callback_entry* entry = static_cast<callback_entry*>((IReleasable*)pData.GetRawPointer());
  686. entry->ErrorResult = pAsynWaitSp->AsyncGetAnswer();
  687. if(m_isOpenEntityMonitor)
  688. PostEntityVary(entry->EntityName);//if registered, while entity status changed, send to web
  689. }
  690. SP_BEGIN_ENTITY_MAP()
  691. SP_ENTITY(CGUIConsoleEntity)
  692. SP_END_ENTITY_MAP()