mod_guiconsole.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #include "mod_GuiConsole.h"
  2. #include "publicFunExport.h"
  3. #include <EventCode.h>
  4. #include <mod_guiconsole/GUIConsole_msg_g.h>
  5. bool isPad = false;
  6. void CGUIConsoleEntity::OnPreStart(CAutoArray<CSimpleStringA> strArgs, CSmartPointer<ITransactionContext> pTransactionContext)
  7. {
  8. pTransactionContext->SendAnswer(Error_Succeed);
  9. }
  10. void CGUIConsoleEntity::OnPreClose(EntityCloseCauseEnum eCloseCause, CSmartPointer<ITransactionContext> pTransactionContext)
  11. {
  12. pTransactionContext->SendAnswer(Error_Succeed);
  13. }
  14. void CGUIConsoleEntity::OnLog(const CAutoArray<CUUID>& SubIDs, const CUUID nLogID, const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  15. const DWORD dwSysError, const DWORD dwUserCode, const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  16. const CAutoArray<DWORD>& Param, const char* pszEntityName, const char* pszModuleName, const char* pszMessage, const linkContext& pLinkInfo)
  17. {
  18. // 忽略GPIO事件
  19. if (dwUserCode == 0x2090000A || dwUserCode == 0x20900009)
  20. return;
  21. //did not open log sender
  22. if (!m_isOpenLogSender)
  23. return;
  24. if (pszMessage != NULL && strlen(pszMessage) > 2)
  25. {
  26. CSimpleStringA str = pszMessage;
  27. LogInfo msg;
  28. msg.sysError = dwSysError;
  29. msg.userCode = dwUserCode;
  30. msg.timeStr = GenerateTimeStr().c_str();
  31. msg.entityName = pszEntityName;
  32. msg.message = str;
  33. SpSendBroadcast(GetFunction(), eMsg_LogInfo, eMsgSig_LogInfo, msg);
  34. }
  35. }
  36. void CGUIConsoleEntity::OnEntityStateHook(const char* pszEntityName, const char* pszTriggerEntity, EntityStateEnum eState, EntityStateEnum eLastState)
  37. {
  38. //did not open log sender
  39. if (!m_isOpenLogSender)
  40. return;
  41. LogInfo msg;
  42. if (eLastState == EntityState_Idle && eState != EntityState_Idle)
  43. {
  44. msg.sysError = ErrorCodeEnum::Error_Closed;
  45. msg.userCode = ErrorCodeEnum::Error_Closed;
  46. msg.message = CSimpleString::Format("%s实体异常,重新启动...", pszEntityName);
  47. }
  48. else
  49. {
  50. msg.sysError = ErrorCodeEnum::Error_Succeed;
  51. msg.userCode = ErrorCodeEnum::Error_Succeed;
  52. if (eState == EntityState_Starting)
  53. msg.message = CSimpleString::Format("正在启动实体%s...", pszEntityName);
  54. else if(eState == EntityState_Idle)
  55. msg.message = CSimpleString::Format("%s 实体启动成功", pszEntityName);
  56. }
  57. msg.timeStr = GenerateTimeStr().c_str();
  58. msg.entityName = pszEntityName;
  59. SpSendBroadcast(GetFunction(), eMsg_LogInfo, eMsgSig_LogInfo, msg);
  60. }
  61. void CGUIConsoleEntity::OnTimeout(DWORD dwTimerID)
  62. {
  63. }
  64. std::pair<DWORD, std::string> CGUIConsoleEntity::openLogSender()
  65. {
  66. if ((__int64)m_logSubID != 0)
  67. {
  68. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_AlreadyExist, WARN_GUICONSOLE_LOG_ALREADY_REGISTER, "openLogSender: already register");
  69. m_isOpenLogSender = true;
  70. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_LOG_ALREADY_REGISTER, "openLogSender: already register");
  71. }
  72. else
  73. {
  74. auto rc = GetFunction()->SubscribeLog(m_logSubID, this, Log_Ignore, Severity_None, Error_IgnoreAll, -1, NULL, false);
  75. m_isOpenLogSender = (rc == ErrorCodeEnum::Error_Succeed);
  76. return std::pair<DWORD, std::string>(rc, "");
  77. }
  78. }
  79. std::pair<DWORD, std::string> CGUIConsoleEntity::closeLogSender()
  80. {
  81. if ((__int64)m_logSubID == 0)
  82. {
  83. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_AlreadyExist, WARN_GUICONSOLE_LOG_ALREADY_UNREGISTER, "openLogSender: already unregister");
  84. m_isOpenLogSender = false;
  85. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_LOG_ALREADY_UNREGISTER, "openLogSender: already register");
  86. }
  87. else
  88. {
  89. auto rc = GetFunction()->UnsubscribeLog(m_logSubID);
  90. if(rc == ErrorCodeEnum::Error_Succeed)
  91. m_logSubID = 0;
  92. m_isOpenLogSender = !(rc == ErrorCodeEnum::Error_Succeed);
  93. return std::pair<DWORD, std::string>(rc, "");
  94. }
  95. }
  96. std::pair<DWORD, std::string> CGUIConsoleEntity::openEntityMonitor()
  97. {
  98. m_isOpenEntityMonitor = true;
  99. return std::pair<DWORD, std::string>(Error_Succeed, "");
  100. }
  101. std::pair<DWORD, std::string> CGUIConsoleEntity::closeEntityMonitor()
  102. {
  103. m_isOpenEntityMonitor = false;
  104. return std::pair<DWORD, std::string>(Error_Succeed, "");
  105. }
  106. std::pair<DWORD, std::string> CGUIConsoleEntity::openPerformanceSender()
  107. {
  108. m_isOpenPerformanceSender = true;
  109. return std::pair<DWORD, std::string>(Error_Succeed, "");
  110. }
  111. std::pair<DWORD, std::string> CGUIConsoleEntity::closePerformanceSender()
  112. {
  113. m_isOpenPerformanceSender = false;
  114. return std::pair<DWORD, std::string>(Error_Succeed, "");
  115. }
  116. std::pair<DWORD, std::string> CGUIConsoleEntity::VTMSystemControl()
  117. {
  118. return std::pair<DWORD, std::string>(Error_Succeed, "");
  119. }
  120. std::pair<DWORD, std::string> CGUIConsoleEntity::VTM_controlEntity()
  121. {
  122. return std::pair<DWORD, std::string>(Error_Succeed, "");
  123. }
  124. CGUIConsoleEntity::CGUIConsoleEntity()
  125. :m_isOpenLogSender(false), m_isOpenEntityMonitor(false), m_isOpenPerformanceSender(false)
  126. {
  127. }
  128. CGUIConsoleEntity::~CGUIConsoleEntity()
  129. {}
  130. void CGUIConsoleSession::Handle_OpenLogSender(SpReqAnsContext<GUIConsoleService_OpenLogSender_Req, GUIConsoleService_OpenLogSender_Ans>::Pointer ctx)
  131. {
  132. auto ret = m_pEntity->openLogSender();
  133. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  134. }
  135. void CGUIConsoleSession::Handle_CloseLogSender(SpReqAnsContext<GUIConsoleService_CloseLogSender_Req, GUIConsoleService_CloseLogSender_Ans>::Pointer ctx)
  136. {
  137. auto ret = m_pEntity->closeLogSender();
  138. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  139. }
  140. void CGUIConsoleSession::Handle_OpenEntityMonitor(SpReqAnsContext<GUIConsoleService_OpenEntityMonitor_Req, GUIConsoleService_OpenEntityMonitor_Ans>::Pointer ctx)
  141. {
  142. auto ret = m_pEntity->openEntityMonitor();
  143. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  144. }
  145. void CGUIConsoleSession::Handle_CloseEntityMonitor(SpReqAnsContext<GUIConsoleService_CloseEntityMonitor_Req, GUIConsoleService_CloseEntityMonitor_Ans>::Pointer ctx)
  146. {
  147. auto ret = m_pEntity->closeEntityMonitor();
  148. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  149. }
  150. void CGUIConsoleSession::Handle_OpenPerformanceSender(SpReqAnsContext<GUIConsoleService_OpenPerformanceSender_Req, GUIConsoleService_OpenPerformanceSender_Ans>::Pointer ctx)
  151. {
  152. auto ret = m_pEntity->openPerformanceSender();
  153. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  154. }
  155. void CGUIConsoleSession::Handle_ClosePerformanceSender(SpReqAnsContext<GUIConsoleService_ClosePerformanceSender_Req, GUIConsoleService_ClosePerformanceSender_Ans>::Pointer ctx)
  156. {
  157. auto ret = m_pEntity->closePerformanceSender();
  158. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  159. }
  160. void CGUIConsoleSession::Handle_VTMSystemControl(SpReqAnsContext<GUIConsoleService_VTMSystemControl_Req, GUIConsoleService_VTMSystemControl_Ans>::Pointer ctx)
  161. {
  162. auto ret = m_pEntity->VTMSystemControl();
  163. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  164. }
  165. void CGUIConsoleSession::Handle_VTM_controlEntity(SpReqAnsContext<GUIConsoleService_VTM_controlEntity_Req, GUIConsoleService_VTM_controlEntity_Ans>::Pointer ctx)
  166. {
  167. auto ret = m_pEntity->VTM_controlEntity();
  168. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  169. }
  170. ErrorCodeEnum CGUIConsoleEntity::AsyncStartEntity(const char* entity_name, const char* cmdline, void* pData)
  171. {
  172. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  173. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  174. if (pFuncPrivilege != NULL) {
  175. CSmartPointer<IAsynWaitSp> spWait;
  176. ErrorCodeEnum Error = pFuncPrivilege->StartEntity(entity_name, cmdline, spWait);
  177. if (Error == Error_Succeed) {
  178. callback_entry* entry = new callback_entry();
  179. entry->pRawData = pData;
  180. entry->EntityName = entity_name;
  181. entry->ErrorResult = Error_Unexpect;
  182. entry->op = OP_START_ENTITY;
  183. spWait->SetCallback(this, entry);
  184. }
  185. return Error;
  186. }
  187. else {
  188. return Error_NoPrivilege;
  189. }
  190. }
  191. ErrorCodeEnum CGUIConsoleEntity::AsyncStopEntity(const char* entity_name, void* pData)
  192. {
  193. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  194. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  195. if (pFuncPrivilege != NULL) {
  196. CSmartPointer<IAsynWaitSp> spWait;
  197. ErrorCodeEnum Error = pFuncPrivilege->StopEntity(entity_name, spWait);
  198. if (Error == Error_Succeed) {
  199. callback_entry* entry = new callback_entry();
  200. entry->pRawData = pData;
  201. entry->EntityName = entity_name;
  202. entry->ErrorResult = Error_Unexpect;
  203. entry->op = OP_STOP_ENTITY;
  204. spWait->SetCallback(this, entry);
  205. }
  206. return Error;
  207. }
  208. else {
  209. return Error_NoPrivilege;
  210. }
  211. }
  212. ErrorCodeEnum CGUIConsoleEntity::AsyncPauseEntity(const char* entity_name, void* pData)
  213. {
  214. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  215. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  216. if (pFuncPrivilege != NULL) {
  217. CSmartPointer<IAsynWaitSp> spWait;
  218. ErrorCodeEnum Error = pFuncPrivilege->PauseEntity(entity_name, spWait);
  219. if (Error == Error_Succeed) {
  220. callback_entry* entry = new callback_entry();
  221. entry->pRawData = pData;
  222. entry->EntityName = entity_name;
  223. entry->ErrorResult = Error_Unexpect;
  224. entry->op = OP_PAUSE_ENTITY;
  225. spWait->SetCallback(this, entry);
  226. }
  227. return Error;
  228. }
  229. else {
  230. return Error_NoPrivilege;
  231. }
  232. }
  233. ErrorCodeEnum CGUIConsoleEntity::AsyncContinueEntity(const char* entity_name, void* pData)
  234. {
  235. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  236. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  237. if (pFuncPrivilege != NULL) {
  238. CSmartPointer<IAsynWaitSp> spWait;
  239. ErrorCodeEnum Error = pFuncPrivilege->ContinueEntity(entity_name, spWait);
  240. if (Error == Error_Succeed) {
  241. callback_entry* entry = new callback_entry();
  242. entry->pRawData = pData;
  243. entry->EntityName = entity_name;
  244. entry->ErrorResult = Error_Unexpect;
  245. entry->op = OP_CONTINUE_ENTITY;
  246. spWait->SetCallback(this, entry);
  247. }
  248. return Error;
  249. }
  250. else {
  251. return Error_NoPrivilege;
  252. }
  253. }
  254. ErrorCodeEnum CGUIConsoleEntity::AsyncTerminateEntity(const char* entity_name, void* pData)
  255. {
  256. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  257. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  258. if (pFuncPrivilege != NULL) {
  259. CSmartPointer<IAsynWaitSp> spWait;
  260. ErrorCodeEnum Error = pFuncPrivilege->TerminateEntity(entity_name, spWait);
  261. if (Error == Error_Succeed) {
  262. callback_entry* entry = new callback_entry();
  263. entry->pRawData = pData;
  264. entry->EntityName = entity_name;
  265. entry->ErrorResult = Error_Unexpect;
  266. entry->op = OP_TERMINATE_ENTITY;
  267. spWait->SetCallback(this, entry);
  268. }
  269. return Error;
  270. }
  271. else {
  272. return Error_NoPrivilege;
  273. }
  274. }
  275. ErrorCodeEnum CGUIConsoleEntity::GetEntity(const char* entity_name, EntityEntry& e)
  276. {
  277. CSmartPointer<IEntityFunction> spFunc = GetFunction();
  278. LOG_ASSERT(spFunc != NULL);
  279. CEntityStaticInfo StaticInfo;
  280. CEntityRunInfo RunInfo;
  281. ErrorCodeEnum Error = spFunc->GetEntityStaticInfo(entity_name, StaticInfo);
  282. if (Error == Error_Succeed) {
  283. Error = spFunc->GetEntityRunInfo(entity_name, RunInfo);
  284. if (Error == Error_Succeed) {
  285. e.Name = entity_name;
  286. e.Id = RunInfo.dwEntityInstanceID;
  287. e.ModuleName = _GetFileName(StaticInfo.strSpFileName);
  288. e.State = RunInfo.eState;
  289. e.Type = StaticInfo.bStartedByShell ? 0 : 1;
  290. e.Pid = (int)RunInfo.dwProcessID;
  291. e.DevelopID = (int)StaticInfo.wEntityDevelopID;
  292. e.DebugLevel = RunInfo.eDebugLevel;
  293. }
  294. }
  295. return Error;
  296. }
  297. ErrorCodeEnum CGUIConsoleEntity::GetAllEntity(CAutoArray<EntityEntry>& Entities)
  298. {
  299. CSmartPointer<IEntityFunction> spFunc = GetFunction();
  300. CAutoArray<CSimpleStringA> strEntityNames;
  301. CAutoArray<WORD> wDevelopIDs;
  302. ErrorCodeEnum Error = spFunc->GetAllRegistedEntity(strEntityNames, wDevelopIDs);
  303. if (Error == Error_Succeed) {
  304. Entities.Init(strEntityNames.GetCount());
  305. for (int i = 0; i < strEntityNames.GetCount(); ++i) {
  306. Error = GetEntity(strEntityNames[i], Entities[i]);
  307. }
  308. }
  309. return Error;
  310. }
  311. void CGUIConsoleEntity::OnAnswer(CSmartPointer<IAsynWaitSp> pAsynWaitSp)
  312. {
  313. /*
  314. if (m_hWndMainFrame) {
  315. CSmartPointer<ICallbackListener> spCallback;
  316. CSmartPointer<IReleasable> pData;
  317. pAsynWaitSp->GetCallback(spCallback, pData);
  318. LOG_ASSERT(pData.GetRawPointer() != NULL);
  319. callback_entry* entry = static_cast<callback_entry*>((IReleasable*)pData.GetRawPointer());
  320. entry->ErrorResult = pAsynWaitSp->AsyncGetAnswer();
  321. callback_entry* new_entry = new callback_entry();
  322. new_entry->EntityName = entry->EntityName;
  323. new_entry->ErrorResult = entry->ErrorResult;
  324. new_entry->op = entry->op;
  325. new_entry->state = entry->state;
  326. PostMessage(m_hWndMainFrame, WM_GUICONSOLE, 0, (LPARAM)new_entry);
  327. }
  328. */
  329. }
  330. SP_BEGIN_ENTITY_MAP()
  331. SP_ENTITY(CGUIConsoleEntity)
  332. SP_END_ENTITY_MAP()