guitask.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. #include "stdafx2.h"
  2. #include "SpBase.h"
  3. #include "guitask.h"
  4. #include "mainfrm.h"
  5. #include "SpIni.h"
  6. #include "path.h"
  7. #include "AccessAuthorization_client_g.h"
  8. using namespace AccessAuthorization;
  9. #include "mod_CenterSetting/Event.h"
  10. int level = 2;
  11. GUITask::GUITask() : m_pEntity(NULL), m_hWndMainFrame(NULL), m_hThread(NULL), m_hThread2(NULL), m_hWndStartPage(NULL),
  12. m_idleNum(0), m_totalNum(0)
  13. {
  14. m_hEventStartReady = ::CreateEventA(NULL, TRUE, FALSE, NULL);
  15. m_hEventStopReady = ::CreateEventA(NULL, TRUE, FALSE, NULL);
  16. m_hWndStartPage =NULL;
  17. }
  18. GUITask::~GUITask()
  19. {
  20. CloseHandle(m_hEventStopReady);
  21. CloseHandle(m_hEventStartReady);
  22. }
  23. ErrorCodeEnum GUITask::Kickoff(CEntityBase *pEntity)
  24. {
  25. LOG_FUNCTION();
  26. #if defined(RVC_OS_WIN)
  27. if (m_hThread) {
  28. return Error_Duplication;
  29. }
  30. m_pEntity = pEntity;
  31. m_pEntity->GetFunction()->RegistSysVarEvent("UIState", this);
  32. CSmartPointer<IEntityFunction> pFunc = m_pEntity->GetFunction();
  33. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  34. ErrorCodeEnum errCode = pFuncPrivilege->RegistEntityStateEvent(NULL,this);
  35. if (errCode != Error_Succeed)
  36. {
  37. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("RegistEntityState failed(%d).",errCode);
  38. return Error_Unexpect;
  39. }
  40. m_totalNum = GetWillWorkEntityCountFromHealthmanager();
  41. if (m_totalNum == 0) {
  42. CAutoArray<EntityEntry> Entities;
  43. ErrorCodeEnum err = GetAllEntity(Entities);
  44. m_totalNum = Entities.GetCount();
  45. }
  46. m_hThread = (HANDLE)_beginthreadex(0, 0, &__Process, this, 0, 0);
  47. if (!m_hThread) {
  48. return Error_Resource;
  49. }
  50. WaitForSingleObject(m_hEventStartReady, INFINITE);
  51. //SubscribeLog();
  52. return RegistEntityStateEvent();
  53. #else
  54. return Error_NotSupport;
  55. #endif //RVC_OS_WIN
  56. }
  57. ErrorCodeEnum GUITask::Close()
  58. {
  59. #if defined(RVC_OS_WIN)
  60. if (!m_hThread) {
  61. return Error_NotInit;
  62. }
  63. UnSubscribeLog();
  64. UnregistEntityStateEvent();
  65. if (m_hWndMainFrame) {
  66. PostMessageA(m_hWndMainFrame, WM_CLOSE, -1, -1);
  67. WaitForSingleObject(m_hEventStopReady, INFINITE);
  68. }
  69. WaitForSingleObject(m_hThread, INFINITE);
  70. CloseHandle(m_hThread);
  71. m_hThread = 0;
  72. #endif //RVC_OS_WIN
  73. return Error_Succeed;
  74. }
  75. #if defined(RVC_OS_WIN)
  76. CAppModule _Module;
  77. #endif //RVC_OS_WIN
  78. void GUITask::Process()
  79. {
  80. #if defined(RVC_OS_WIN)
  81. ResetEvent(m_hEventStartReady);
  82. m_pEntity->GetFunction()->InitLogCurrentThread();
  83. HRESULT hRes = ::CoInitialize(NULL);
  84. // If you are running on NT 4.0 or higher you can use the following call instead to
  85. // make the EXE free threaded. This means that calls come in on a random RPC thread.
  86. // HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
  87. ATLASSERT(SUCCEEDED(hRes));
  88. // this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
  89. ::DefWindowProc(NULL, 0, 0, 0L);
  90. AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES); // add flags to support other controls
  91. hRes = _Module.Init(NULL, ModuleBase::GetModuleBase()->GetInstance());
  92. ATLASSERT(SUCCEEDED(hRes));
  93. ATL::AtlAxWinInit();
  94. CMessageLoop theLoop;
  95. _Module.AddMessageLoop(&theLoop);
  96. CMainFrame wndMain(this);
  97. if(wndMain.CreateEx() == NULL)
  98. {
  99. ATLTRACE(_T("Main window creation failed!\n"));
  100. return;
  101. }
  102. m_hWndMainFrame = wndMain.m_hWnd;
  103. CSmartPointer<IConfigInfo> pConfig;
  104. auto rc = m_pEntity->GetFunction()->OpenConfig(Config_CenterSetting, pConfig);
  105. CSimpleStringA csimpleStrMachineTypeCfg(true);
  106. StartPageDlg dlg(this);
  107. if (IS_SUCCEED(rc)) {
  108. rc = pConfig->ReadConfigValue("NonExclusive", "NewStartPage", csimpleStrMachineTypeCfg);
  109. CSystemStaticInfo staticInfo;
  110. m_pEntity->GetFunction()->GetSystemStaticInfo(staticInfo);
  111. if (csimpleStrMachineTypeCfg.IndexOf(staticInfo.strMachineType) >= 0) {
  112. TerminalBaseInfo info;
  113. info.strMachineType = staticInfo.strMachineType;
  114. info.strTerminalNo = staticInfo.strTerminalID;
  115. info.strSoftwareVersion = staticInfo.InstallVersion.ToString();
  116. dlg.SetTerminalBasicInfo(info);
  117. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Enter newpage...");
  118. if (dlg.Create(NULL) == NULL) {
  119. ATLTRACE(_T("Main window creation failed!\n"));
  120. return;
  121. }
  122. m_hWndStartPage = dlg.m_hWnd;
  123. }
  124. }
  125. SetEvent(m_hEventStartReady);
  126. wndMain.ShowWindow(SW_MINIMIZE);
  127. if(m_hWndStartPage)
  128. dlg.ShowWindow(SW_SHOW);
  129. else
  130. ShowOutputConsole();
  131. int nRet = theLoop.Run();
  132. _Module.RemoveMessageLoop();
  133. _Module.Term();
  134. ::CoUninitialize();
  135. SetEvent(m_hEventStopReady);
  136. m_hWndMainFrame = NULL;
  137. #endif //RVC_OS_WIN
  138. }
  139. ErrorCodeEnum GUITask::RegistEntityStateEvent()
  140. {
  141. CSmartPointer<IEntityFunction> pFunc = m_pEntity->GetFunction();
  142. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  143. if (pFuncPrivilege != NULL) {
  144. return pFuncPrivilege->RegistEntityStateEvent(NULL, this); // NULL to register for all
  145. } else {
  146. return Error_NoPrivilege;
  147. }
  148. }
  149. ErrorCodeEnum GUITask::UnregistEntityStateEvent()
  150. {
  151. CSmartPointer<IEntityFunction> pFunc = m_pEntity->GetFunction();
  152. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  153. if (pFuncPrivilege != NULL) {
  154. return pFuncPrivilege->UnregistEntityStateEvent(NULL);
  155. } else {
  156. return Error_NoPrivilege;
  157. }
  158. }
  159. ErrorCodeEnum GUITask::ReqFrameworkQuit(bool bRebootOS)
  160. {
  161. CSmartPointer<IEntityFunction> pFunc = m_pEntity->GetFunction();
  162. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  163. if (pFuncPrivilege != NULL)
  164. {
  165. CSmartPointer<IConfigInfo> pConfig;
  166. auto rc = m_pEntity->GetFunction()->OpenConfig(Config_CenterSetting, pConfig);
  167. int iNoRestart;
  168. CSimpleStringA csimpleStrMachineTypeCfg;
  169. rc = pConfig->ReadConfigValue("NonExclusive", "NonGuardian", csimpleStrMachineTypeCfg);
  170. CSystemStaticInfo staticInfo;
  171. m_pEntity->GetFunction()->GetSystemStaticInfo(staticInfo);
  172. if(csimpleStrMachineTypeCfg.IndexOf(staticInfo.strMachineType)>=0)
  173. return pFuncPrivilege->Reboot(RebootTrigger_ManualLocal, bRebootOS ? RebootWay_OS : RebootWay_Power );
  174. return pFuncPrivilege->Reboot(RebootTrigger_ManualLocal, bRebootOS ? RebootWay_OS : RebootWay_Framework );
  175. }
  176. return Error_Unexpect;
  177. }
  178. ErrorCodeEnum GUITask::SubscribeLog()
  179. {
  180. #if defined(_MSC_VER)
  181. return m_pEntity->GetFunction()->SubscribeLog(m_logSubID, this, Log_Ignore, Severity_None, Error_IgnoreAll, -1, NULL, false);
  182. #else
  183. return Error_NotSupport;
  184. #endif //_MSC_VER
  185. }
  186. ErrorCodeEnum GUITask::UnSubscribeLog()
  187. {
  188. if (HasSubscibeLog())
  189. {
  190. m_pEntity->GetFunction()->UnsubscribeLog(m_logSubID);
  191. m_logSubID = 0;
  192. }
  193. return Error_Succeed;
  194. }
  195. ErrorCodeEnum GUITask::SetDebugLevel(const char *pszEntity, int nLevel)
  196. {
  197. return m_pEntity->GetFunction()->GetPrivilegeFunction()->SetSysDebugLevel(pszEntity, (DebugLevelEnum)nLevel, true);
  198. }
  199. ErrorCodeEnum GUITask::GetShellDebugLevel(int &nLevel)
  200. {
  201. CSystemRunInfo info ={};
  202. auto rc = m_pEntity->GetFunction()->GetSystemRunInfo(info);
  203. if (rc == Error_Succeed)
  204. nLevel = info.eDebugLevel;
  205. return rc;
  206. }
  207. ErrorCodeEnum GUITask::BeginAccessAuth()
  208. {
  209. AccessAuthService_ClientBase *pClient = new AccessAuthService_ClientBase(m_pEntity);
  210. auto rc = pClient->Connect();
  211. if (rc == Error_Succeed)
  212. {
  213. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("connect to entity [AccessAuthorization] succeed, start access auth now");
  214. rc = (*pClient)(EntityResource::getLink().upgradeLink())->Regist();
  215. pClient->GetFunction()->CloseSession();
  216. }
  217. else
  218. {
  219. LogWarn(Severity_Low, rc, 0, "connect to entity [AccessAuthorization] fail");
  220. pClient->SafeDelete();
  221. }
  222. return rc;
  223. }
  224. ErrorCodeEnum GUITask::BeginInitialize()
  225. {
  226. return Error_NotImpl;
  227. }
  228. #include "mod_GuiConsole.h"
  229. ErrorCodeEnum GUITask::DeleteKeySet()
  230. {
  231. #if defined(_MSC_VER)
  232. HCRYPTPROV hProv(0);
  233. if (!CryptAcquireContext(&hProv, "RVC", MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET | CRYPT_DELETEKEYSET)) {
  234. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("delete keyset fail: %d", GetLastError());
  235. return Error_Unexpect;
  236. } else {
  237. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("delete keyset succ");
  238. return Error_Succeed;
  239. }
  240. #else
  241. return Error_NotImpl;
  242. #endif //_MSC_VER
  243. }
  244. ErrorCodeEnum GUITask::GetFrameworkInfo(CSimpleStringA &strInfo)
  245. {
  246. CSystemStaticInfo info;
  247. memset(&info, 0, sizeof(info));
  248. auto rc = m_pEntity->GetFunction()->GetSystemStaticInfo(info);
  249. if (rc == Error_Succeed) {
  250. strInfo = CSimpleStringA::Format("[%s] [%s]", (const char*) info.strTerminalID, (const char*)info.InstallVersion.ToString());
  251. }
  252. return rc;
  253. }
  254. ErrorCodeEnum GUITask::AsyncStartEntity(const char *entity_name, const char *cmdline, void *pData)
  255. {
  256. CSmartPointer<IEntityFunction> pFunc = m_pEntity->GetFunction();
  257. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  258. if (pFuncPrivilege != NULL) {
  259. CSmartPointer<IAsynWaitSp> spWait;
  260. ErrorCodeEnum Error = pFuncPrivilege->StartEntity(entity_name, cmdline, 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_START_ENTITY;
  267. spWait->SetCallback(this, entry);
  268. }
  269. return Error;
  270. } else {
  271. return Error_NoPrivilege;
  272. }
  273. }
  274. ErrorCodeEnum GUITask::AsyncStopEntity(const char *entity_name, void *pData)
  275. {
  276. CSmartPointer<IEntityFunction> pFunc = m_pEntity->GetFunction();
  277. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  278. if (pFuncPrivilege != NULL) {
  279. CSmartPointer<IAsynWaitSp> spWait;
  280. ErrorCodeEnum Error = pFuncPrivilege->StopEntity(entity_name, spWait);
  281. if (Error == Error_Succeed) {
  282. callback_entry *entry = new callback_entry();
  283. entry->pRawData = pData;
  284. entry->EntityName = entity_name;
  285. entry->ErrorResult = Error_Unexpect;
  286. entry->op = OP_STOP_ENTITY;
  287. spWait->SetCallback(this, entry);
  288. }
  289. return Error;
  290. } else {
  291. return Error_NoPrivilege;
  292. }
  293. }
  294. ErrorCodeEnum GUITask::AsyncPauseEntity(const char *entity_name, void *pData)
  295. {
  296. CSmartPointer<IEntityFunction> pFunc = m_pEntity->GetFunction();
  297. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  298. if (pFuncPrivilege != NULL) {
  299. CSmartPointer<IAsynWaitSp> spWait;
  300. ErrorCodeEnum Error = pFuncPrivilege->PauseEntity(entity_name, spWait);
  301. if (Error == Error_Succeed) {
  302. callback_entry *entry = new callback_entry();
  303. entry->pRawData = pData;
  304. entry->EntityName = entity_name;
  305. entry->ErrorResult = Error_Unexpect;
  306. entry->op = OP_PAUSE_ENTITY;
  307. spWait->SetCallback(this, entry);
  308. }
  309. return Error;
  310. } else {
  311. return Error_NoPrivilege;
  312. }
  313. }
  314. ErrorCodeEnum GUITask::AsyncContinueEntity(const char *entity_name, void *pData)
  315. {
  316. CSmartPointer<IEntityFunction> pFunc = m_pEntity->GetFunction();
  317. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  318. if (pFuncPrivilege != NULL) {
  319. CSmartPointer<IAsynWaitSp> spWait;
  320. ErrorCodeEnum Error = pFuncPrivilege->ContinueEntity(entity_name, spWait);
  321. if (Error == Error_Succeed) {
  322. callback_entry *entry = new callback_entry();
  323. entry->pRawData = pData;
  324. entry->EntityName = entity_name;
  325. entry->ErrorResult = Error_Unexpect;
  326. entry->op = OP_CONTINUE_ENTITY;
  327. spWait->SetCallback(this, entry);
  328. }
  329. return Error;
  330. } else {
  331. return Error_NoPrivilege;
  332. }
  333. }
  334. ErrorCodeEnum GUITask::AsyncTerminateEntity(const char *entity_name, void *pData)
  335. {
  336. CSmartPointer<IEntityFunction> pFunc = m_pEntity->GetFunction();
  337. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  338. if (pFuncPrivilege != NULL) {
  339. CSmartPointer<IAsynWaitSp> spWait;
  340. ErrorCodeEnum Error = pFuncPrivilege->TerminateEntity(entity_name, spWait);
  341. if (Error == Error_Succeed) {
  342. callback_entry *entry = new callback_entry();
  343. entry->pRawData = pData;
  344. entry->EntityName = entity_name;
  345. entry->ErrorResult = Error_Unexpect;
  346. entry->op = OP_TERMINATE_ENTITY;
  347. spWait->SetCallback(this, entry);
  348. }
  349. return Error;
  350. } else {
  351. return Error_NoPrivilege;
  352. }
  353. }
  354. UINT CalcKeyCountUnderSection(const char* pszFile, const char* pszSection)
  355. {
  356. UINT result(0);
  357. #if defined(_MSC_VER)
  358. DWORD dwInitialCapacity = 16;
  359. DWORD dwSize;
  360. char* p = NULL;
  361. const int tries = 70;
  362. ErrorCodeEnum Error = Error_Succeed;
  363. int i;
  364. for (i = 0; i < tries; ++i) {
  365. p = (char*)realloc(p, dwInitialCapacity);
  366. dwSize = ::GetPrivateProfileStringA(pszSection, NULL, NULL, p, dwInitialCapacity, pszFile);
  367. if (dwSize == dwInitialCapacity - 2) { // buffer is too small
  368. dwInitialCapacity = dwInitialCapacity * 2;
  369. } else {
  370. break;
  371. }
  372. }
  373. if (i < tries) {
  374. int cnt = 0;
  375. char* t = p;
  376. size_t n;
  377. while ((n = strlen(t)) > 0) {
  378. cnt++;
  379. t += n + 1;
  380. }
  381. result = cnt;
  382. }
  383. free(p);
  384. #endif //_MSC_VER
  385. return result;
  386. }
  387. UINT GUITask::GetWillWorkEntityCountFromHealthmanager()
  388. {
  389. UINT count(0);
  390. CSystemStaticInfo sysInfo;
  391. CSimpleStringA cfgPath = "", strCoreBoot("CoreBoot"), strSafeLoad("SafeLoad"), strOp("Operating");
  392. m_pEntity->GetFunction()->GetPath("cfg", cfgPath);
  393. m_pEntity->GetFunction()->GetSystemStaticInfo(sysInfo);
  394. strCoreBoot = strCoreBoot + "." + sysInfo.strMachineType;
  395. strSafeLoad = strSafeLoad + "." + sysInfo.strMachineType;
  396. strOp = strOp + "." + sysInfo.strMachineType;
  397. cfgPath = cfgPath + "\\HealthManager.ini";
  398. count += CalcKeyCountUnderSection(cfgPath, strCoreBoot);
  399. count += CalcKeyCountUnderSection(cfgPath, strSafeLoad);
  400. count += CalcKeyCountUnderSection(cfgPath, strOp);
  401. return count;
  402. }
  403. ErrorCodeEnum GUITask::GetAllEntity(CAutoArray<EntityEntry> &Entities)
  404. {
  405. CSmartPointer<IEntityFunction> spFunc = m_pEntity->GetFunction();
  406. CAutoArray<CSimpleStringA> strEntityNames;
  407. CAutoArray<WORD> wDevelopIDs;
  408. ErrorCodeEnum Error = spFunc->GetAllRegistedEntity(strEntityNames, wDevelopIDs);
  409. if (Error == Error_Succeed) {
  410. Entities.Init(strEntityNames.GetCount());
  411. for (int i = 0; i < strEntityNames.GetCount(); ++i) {
  412. Error = GetEntity(strEntityNames[i], Entities[i]);
  413. }
  414. }
  415. return Error;
  416. }
  417. ErrorCodeEnum GUITask::GetEntity(const char *entity_name, EntityEntry &e)
  418. {
  419. CSmartPointer<IEntityFunction> spFunc = m_pEntity->GetFunction();
  420. LOG_ASSERT(spFunc != NULL);
  421. CEntityStaticInfo StaticInfo;
  422. CEntityRunInfo RunInfo;
  423. ErrorCodeEnum Error = spFunc->GetEntityStaticInfo(entity_name, StaticInfo);
  424. if (Error == Error_Succeed) {
  425. Error = spFunc->GetEntityRunInfo(entity_name, RunInfo);
  426. if (Error == Error_Succeed) {
  427. e.Name = entity_name;
  428. e.Id = RunInfo.dwEntityInstanceID;
  429. e.ModuleName = _GetFileName(StaticInfo.strSpFileName);
  430. e.State = RunInfo.eState;
  431. e.Type = StaticInfo.bStartedByShell ? 0 : 1;
  432. e.Pid = (int)RunInfo.dwProcessID;
  433. e.DevelopID = (int)StaticInfo.wEntityDevelopID;
  434. e.DebugLevel = RunInfo.eDebugLevel;
  435. }
  436. }
  437. return Error;
  438. }
  439. ErrorCodeEnum GUITask::GetCustomizeStartMenuList(CAutoArray<StartMenuEntry> &StartItems)
  440. {
  441. int cnt = 0;
  442. StartItems.Clear();
  443. return Error_Deprecated;
  444. }
  445. void GUITask::OnAnswer(CSmartPointer<IAsynWaitSp> pAsynWaitSp)
  446. {
  447. #if defined(_MSC_VER)
  448. if (m_hWndMainFrame) {
  449. CSmartPointer<ICallbackListener> spCallback;
  450. CSmartPointer<IReleasable> pData;
  451. pAsynWaitSp->GetCallback(spCallback, pData);
  452. LOG_ASSERT(pData.GetRawPointer() != NULL);
  453. callback_entry* entry = static_cast<callback_entry*>((IReleasable*)pData.GetRawPointer());
  454. entry->ErrorResult = pAsynWaitSp->AsyncGetAnswer();
  455. callback_entry* new_entry = new callback_entry();
  456. new_entry->EntityName = entry->EntityName;
  457. new_entry->ErrorResult = entry->ErrorResult;
  458. new_entry->op = entry->op;
  459. new_entry->state = entry->state;
  460. PostMessage(m_hWndMainFrame, WM_GUICONSOLE, 0, (LPARAM)new_entry);
  461. }
  462. #endif //_MSC_VER
  463. }
  464. ErrorCodeEnum GUITask::OutputMsg(const char *pMsg)
  465. {
  466. #if defined(_MSC_VER)
  467. if (m_hWndMainFrame) {
  468. PostMessage(m_hWndMainFrame, WM_SHOW_MSG, 0, (LPARAM)strdup(pMsg));
  469. return Error_Succeed;
  470. } else
  471. return Error_NotExist;
  472. #else
  473. return Error_NotSupport;
  474. #endif //_MSC_VER
  475. }
  476. void GUITask::OnEntityStateHook(const char *pszEntityName,const char *pszTriggerEntity,EntityStateEnum eState,EntityStateEnum eLastState)
  477. {
  478. #if defined(_MSC_VER)
  479. if (m_hWndMainFrame) {
  480. callback_entry* entry = new callback_entry();
  481. entry->EntityName = pszEntityName;
  482. entry->ErrorResult = Error_Succeed;
  483. entry->op = OP_FIRE_ENTITY_STATE;
  484. entry->state = eState;
  485. DWORD dwProcessID = 0;
  486. //if (entry->state != EntityState_Close) // 关闭状态进程号清0置后
  487. {
  488. CEntityRunInfo Info;
  489. CSmartPointer<IEntityFunction> pFunc = m_pEntity->GetFunction();
  490. pFunc->GetEntityRunInfo(entry->EntityName, Info);
  491. dwProcessID = Info.dwProcessID;
  492. }
  493. PostMessage(m_hWndMainFrame, WM_GUICONSOLE, dwProcessID, (LPARAM)entry);
  494. }
  495. SYSTEMTIME st;
  496. GetLocalTime(&st);
  497. CSimpleStringA msg;
  498. if(eState == EntityState_Starting)
  499. msg = CSimpleString::Format("[%04d-%02d-%02d %02d:%02d:%02d] 正在启动实体%s...", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, pszEntityName);
  500. if(eState == EntityState_Idle)
  501. {
  502. msg = CSimpleString::Format("[%04d-%02d-%02d %02d:%02d:%02d] %s实体启动成功", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, pszEntityName);
  503. m_idleNum++;
  504. }
  505. if(eLastState == EntityState_Idle && eState != EntityState_Idle )
  506. {
  507. msg = CSimpleString::Format("[%04d-%02d-%02d %02d:%02d:%02d] %s实体异常,重新启动...", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, pszEntityName);
  508. m_idleNum--;
  509. }
  510. PostMessage(m_hWndStartPage, WM_NUMCHANGE, 0, 0);
  511. PostMessage(m_hWndStartPage, WM_SHOW_MSG, (WPARAM)strdup(msg), (LPARAM)RGB(171, 171, 171));
  512. #endif //_MSC_VER
  513. }
  514. void GUITask::OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  515. const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  516. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage, const linkContext& pLinkInfo)
  517. {
  518. // 忽略GPIO事件
  519. if (dwUserCode == 0x2090000A || dwUserCode == 0x20900009)
  520. return;
  521. else if (dwUserCode == EVENT_MOD_CENTERSETTING_UPDATE_CENTER)
  522. {
  523. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("检测到集中配置更新了");
  524. auto generateCenterSettingPath = []() ->std::string {
  525. char pathbuf[1024] = "";
  526. int pathlen = ::GetModuleFileNameA(NULL, pathbuf, 1024);//获得GuiConsole路径
  527. // 替换掉单杠
  528. int times = 0;
  529. while (pathlen > 0)
  530. {
  531. if (pathbuf[pathlen--] == SPLIT_SLASH)
  532. times++;
  533. if (2 == times)
  534. break;
  535. }
  536. pathbuf[++pathlen] = '\0';
  537. std::string dstPath = pathbuf;
  538. #if defined(_MSC_VER)
  539. dstPath.append("\\cfg\\CenterSetting.ini");
  540. #else
  541. dstPath.append("/cfg/CenterSetting.LAN.ini");
  542. #endif //_MSC_VER
  543. return dstPath;
  544. };
  545. //实时更新实体打印的日志等级,这个功能在信创下需要调整,实现方式不一样 gifur@20230816
  546. auto centersettingPath = generateCenterSettingPath();
  547. load_debugLevelInCentersetting(centersettingPath.c_str());
  548. }
  549. if (pszMessage!=NULL && strlen(pszMessage)>2)
  550. {
  551. CSimpleStringA str = pszMessage;
  552. if (str[str.GetLength()-2] == '\r' && str[str.GetLength()-1]=='\n')
  553. str[str.GetLength()-2] = 0;
  554. if (eLogType == Log_Error)
  555. {
  556. str = CSimpleStringA::Format("[%s] Error:{%s}(sc:0x%X, uc:0x%X)\r\n", pszEntityName, (const char*)str, dwSysError, dwUserCode);
  557. #if defined(_MSC_VER)
  558. PostMessage(m_hWndStartPage, WM_SHOW_MSG, (WPARAM)strdup(str), (LPARAM)RGB(255, 132, 32));
  559. #endif //_MSC_VER
  560. }
  561. else if(eLogType == Log_Warning)
  562. {
  563. str = CSimpleStringA::Format("[%s] Warn:{%s}(sc:0x%X, uc:0x%X)\r\n", pszEntityName, (const char*)str, dwSysError, dwUserCode);
  564. if(eLevel == Severity_High)
  565. {
  566. #if defined(_MSC_VER)
  567. PostMessage(m_hWndStartPage, WM_SHOW_MSG, (WPARAM)strdup(str), (LPARAM)RGB(255, 132, 32));
  568. #endif //_MSC_VER
  569. }
  570. }
  571. else if(eLogType == Log_Event)
  572. str = CSimpleStringA::Format("[%s] Event:{%s}(uc:0x%X)\r\n", pszEntityName, (const char*)str, dwUserCode);
  573. else if (eLogType == Log_Debug)
  574. str = CSimpleStringA::Format("[%s] Debug:{%s}\r\n", pszEntityName, (const char*)str);
  575. else
  576. return;
  577. OutputMsg(str);
  578. }
  579. }
  580. CEntityBase* GUITask::GetEntity()
  581. {
  582. return m_pEntity;
  583. }
  584. ErrorCodeEnum GUITask::ShowMaintainView(bool bShow, bool bHighLevel)
  585. {
  586. #if defined(_MSC_VER)
  587. if (m_hWndMainFrame) {
  588. PostMessage(m_hWndMainFrame, WM_SHOW_MAINTAIN_VIEW, bShow ? 1 : 0, bHighLevel ? 1 : 0);
  589. return Error_Succeed;
  590. } else
  591. return Error_NotExist;
  592. #else
  593. return Error_NotSupport;
  594. #endif //_MSC_VER
  595. }
  596. ErrorCodeEnum GUITask::SetWindowPosition(bool bTop)
  597. {
  598. #if defined(_MSC_VER)
  599. if (m_hWndMainFrame) {
  600. if (bTop) {
  601. ShowWindow(m_hWndMainFrame, SW_NORMAL);
  602. SetWindowPos(m_hWndMainFrame, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  603. SetWindowPos(m_hWndMainFrame, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  604. } else {
  605. SetWindowPos(m_hWndMainFrame, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  606. SetWindowPos(m_hWndMainFrame, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  607. ShowWindow(m_hWndMainFrame, SW_MINIMIZE);
  608. }
  609. return Error_Succeed;
  610. } else
  611. return Error_NotExist;
  612. #else
  613. return Error_NotSupport;
  614. #endif //_MSC_VER
  615. }
  616. ErrorCodeEnum GUITask::ShowBeginInit(bool bShow)
  617. {
  618. #if defined(_MSC_VER)
  619. if (m_hWndMainFrame) {
  620. PostMessage(m_hWndMainFrame, WM_SHOW_BEGININIT_VIEW, bShow ? 1 : 0, -1);
  621. return Error_Succeed;
  622. } else
  623. return Error_NotExist;
  624. #else
  625. return Error_NotSupport;
  626. #endif //_MSC_VER
  627. }
  628. ErrorCodeEnum GUITask::EnableMobileDialMenu(bool bEnable)
  629. {
  630. #if defined(_MSC_VER)
  631. if (m_hWndMainFrame) {
  632. PostMessage(m_hWndMainFrame, WM_SHOW_MOBILEDIAL_VIEW, bEnable ? 1 : 0, -1);
  633. return Error_Succeed;
  634. } else {
  635. return Error_NotExist;
  636. }
  637. #else
  638. return Error_NotSupport;
  639. #endif //_MSC_VER
  640. }
  641. void GUITask::CloseSoftKeyboard()
  642. {
  643. #if defined(_MSC_VER)
  644. HANDLE hProcessHandle;
  645. ULONG nProcessID;
  646. HWND kbWindow;
  647. kbWindow = ::FindWindow(NULL, _T("屏幕键盘"));
  648. if (NULL != kbWindow) {
  649. ::GetWindowThreadProcessId(kbWindow, &nProcessID);
  650. hProcessHandle = ::OpenProcess(PROCESS_TERMINATE, FALSE, nProcessID);
  651. ::TerminateProcess(hProcessHandle, 4);
  652. }
  653. #endif //_MSC_VER
  654. }
  655. ErrorCodeEnum GUITask::ShowBlueScreen()
  656. {
  657. auto pEntityFunc = m_pEntity->GetFunction();
  658. auto pPrevFunc = pEntityFunc->GetPrivilegeFunction();
  659. const char *pMsg = "System Failure!System Failure!System Failure!System Failure!System Failure!System Failure!System Failure!System Failure!\r\n系统启动失败,请联系总行开发人员解决!";
  660. pPrevFunc->DisplayBlueScreen(pMsg);
  661. return Error_Succeed;
  662. }
  663. ErrorCodeEnum GUITask::ShowFatalError()
  664. {
  665. auto pEntityFunc = m_pEntity->GetFunction();
  666. const char *pMsg = "开放系统管理员初级培训及认证报名通知.开放系统管理员初级培训及认证报名通知.开放系统管理员初级培训及认证报名通知.开放系统管理员初级培训及认证报名通知.开放系统管理员初级培训及认证报名通知.";
  667. pEntityFunc->ShowFatalError(pMsg);
  668. return Error_Succeed;
  669. }
  670. ErrorCodeEnum GUITask::HideBlueScreen()
  671. {
  672. auto pEntityFunc = m_pEntity->GetFunction();
  673. auto pPrevFunc = pEntityFunc->GetPrivilegeFunction();
  674. return pPrevFunc->UndisplayBlueScreen();
  675. }
  676. ErrorCodeEnum GUITask::ShowOutputConsole()
  677. {
  678. auto pFunc = m_pEntity->GetFunction()->GetPrivilegeFunction();
  679. return pFunc->ShowOuputConsole();
  680. }
  681. ErrorCodeEnum GUITask::CloseOutputConsole()
  682. {
  683. auto pFunc = m_pEntity->GetFunction()->GetPrivilegeFunction();
  684. return pFunc->CloseOuputConsole();
  685. }
  686. void GUITask::OnSysVarEvent(const char *pszKey,
  687. const char *pszValue, const char *pszOldValue, const char *pszEntityName)
  688. {
  689. if ((_strnicmp(pszKey, "UIState", strlen("UIState")) == 0))
  690. {
  691. if (_strnicmp(pszValue, "M", strlen("M")) == 0)
  692. {
  693. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("close start page");
  694. #if defined(_MSC_VER)
  695. if (m_hWndStartPage) {
  696. PostMessageA(m_hWndStartPage, WM_CLOSE, -1, -1);
  697. }
  698. #endif //_MSC_VER
  699. m_pEntity->GetFunction()->UnregistSysVarEvent("UIState");
  700. }
  701. }
  702. }
  703. ErrorCodeEnum GUITask::HideGuiConsole()
  704. {
  705. #if defined(_MSC_VER)
  706. ShowWindow(m_hWndMainFrame, SW_HIDE);
  707. #endif //_MSC_VER
  708. return Error_Succeed;
  709. }