App.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Windows;
  5. using System.Windows.Threading;
  6. using vStation.Module.Login;
  7. using vStation.Module.Status;
  8. using vStation.Module.SystemSetting;
  9. using vStation.Module.Workstation;
  10. using System.Runtime.InteropServices;
  11. using vStation.Infrastructure;
  12. using vStation.Utilities.Report;
  13. using Vinno.IUS.Common.Utilities;
  14. using System.Threading.Tasks;
  15. using vStation.PipeLine;
  16. using vStation.URMStationRelevant;
  17. using Newtonsoft.Json;
  18. using System.Collections.Generic;
  19. using URMServiceConfig;
  20. using JsonRpcLite.Rpc;
  21. using JsonRpcLite.Network;
  22. using WingInterfaceLibrary.Interface;
  23. using JsonRpcLite.Services;
  24. using NPOI.SS.Formula.Functions;
  25. using System.Threading;
  26. namespace vStation
  27. {
  28. /// <summary>
  29. /// App.xaml 的交互逻辑
  30. /// </summary>
  31. public partial class App : Application
  32. {
  33. /// <summary>
  34. /// 该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。
  35. /// </summary>
  36. /// <param name="hWnd">将被激活并被调入前台的窗口句柄。</param>
  37. /// <returns>如果窗口设入了前台,返回值为非零;如果窗口未被设入前台,返回值为零。</returns>
  38. [DllImport("User32.dll")]
  39. private static extern bool SetForegroundWindow(IntPtr hWnd);
  40. [DllImport("User32.dll")]
  41. private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  42. private DefaultLogEngine _defaultLogEngine;
  43. private UserLogEngine _userLogEngine;
  44. protected override void OnStartup(StartupEventArgs e)
  45. {
  46. CheckedOpenRepeatedly(e.Args);
  47. //NetworkException.RegisterConverter(new NetworkExceptionConverter());
  48. XmlHelper.RegisterXmlEngine(new XmlEngine());
  49. InitializeLanguage();
  50. IoHelper.RegisterEngine(new IoEngine(AppDomain.CurrentDomain.BaseDirectory));
  51. _defaultLogEngine = new DefaultLogEngine();
  52. _userLogEngine = new UserLogEngine();
  53. UserLogger.RegisterEngine(_userLogEngine);
  54. Logger.RegisterEngine(_defaultLogEngine);
  55. AppDomain.CurrentDomain.UnhandledException += OnDomainOnUnhandledException;
  56. Current.DispatcherUnhandledException += OnDispatcherUnhandledException;
  57. Current.Exit += OnExit;
  58. AppManager.Instance.RegisterFunction(typeof(IDbService), new DbService());
  59. AppManager.Instance.RegisterFunction(typeof(Module.Login.ILoginService), new LoginService());
  60. AppManager.Instance.RegisterFunction(typeof(ISystemSettingManager), new SystemSettingManager());
  61. AppManager.Instance.RegisterFunction(typeof(ISystemSettingExamPointFee), new SystemSettingExamPointFee());
  62. Config.IsURMStation = true;
  63. if (Config.IsURMStation)
  64. {
  65. IntiURMStation();
  66. }
  67. base.OnStartup(e);
  68. }
  69. private void IntiURMStation()
  70. {
  71. // "E:\\FISShellRev\\fis\\fis\\fis\\bin\\Debug\\net6.0";
  72. URMConfig.URMWorkingDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "URMWorkStation") ;
  73. RootObject root=null;
  74. var isSingle= URMConfig.GetIsSingleMode();
  75. if (File.Exists(URMConfig.ConfigPath))
  76. {
  77. try
  78. {
  79. var data = File.ReadAllText(URMConfig.ConfigPath);
  80. root = JsonConvert.DeserializeObject<RootObject>(data);
  81. }
  82. catch (Exception ex)
  83. {
  84. Logger.WriteLineError($"Error happened while init urm station{ex}");
  85. }
  86. }
  87. else {
  88. if (root == null)
  89. {
  90. root = new RootObject();
  91. root.flyinsonoServers = new List<string>();
  92. root.flyinsonoServers.Add("https://bj.flyinsono.com");
  93. root.common = new Common();
  94. root.server = new URMServiceConfig.Server();
  95. root.common.dateTimeFormat = "yyyy-MM-dd HH:mm";
  96. root.server.current = "http://127.0.0.1:8303";
  97. root.server.optionSource = new List<string>();
  98. root.server.optionSource.Add("http://127.0.0.1:8303");
  99. }
  100. }
  101. URMConfig.RPCAddress = root.server.current;
  102. if (isSingle)
  103. {
  104. root.server.current = "http://127.0.0.1:8303";
  105. var txt = JsonConvert.SerializeObject(root);
  106. URMConfig.ServerStatusChanged += OnServerStatusChanged;
  107. URMConfig.ServerStatus = ServerStatus.NeedStartServer;
  108. File.WriteAllText(URMConfig.ConfigPath, txt);
  109. }
  110. }
  111. private void OnServerStatusChanged(object sender, ServerStatus e)
  112. {
  113. if (e == ServerStatus.NeedStartServer)
  114. {
  115. GetServerState(URMConfig.RPCAddress);
  116. return;
  117. }
  118. if (e == ServerStatus.Starting)
  119. {
  120. ControlServer(true);
  121. return;
  122. }
  123. Logger.WriteLineVerbose($"Server starting result is:--- {e}");
  124. }
  125. private void ControlServer(bool isOpen, bool isRestart = false)
  126. {
  127. Logger.WriteLineInfo($"Server start batch file not exist!");
  128. var targetPath = isOpen ? URMConfig.StartServerBatchFile : URMConfig.CloseServerBatchFile;
  129. if (!File.Exists(targetPath))
  130. {
  131. Logger.WriteLineError($"Server start batch file not exist!");
  132. return;
  133. }
  134. ProcessStartInfo processInfo2 = new ProcessStartInfo(targetPath)
  135. {
  136. CreateNoWindow = true,
  137. UseShellExecute = false,
  138. RedirectStandardError = true,
  139. RedirectStandardOutput = true,
  140. Verb = "runas",
  141. // 不使用操作系统外壳程序启动进程
  142. };
  143. Process.Start(processInfo2);
  144. Task.Run
  145. (() =>
  146. {
  147. if (isOpen)
  148. {
  149. int count = 0;
  150. while (URMConfig.ServerStatus==ServerStatus.Starting)
  151. {
  152. count++;
  153. Thread.Sleep(2000);
  154. GetServerState(URMConfig.RPCAddress);
  155. if (count == 30)
  156. {
  157. URMConfig.ServerStatus = ServerStatus.StartingTimeout;
  158. break;
  159. }
  160. }
  161. }
  162. });
  163. }
  164. private void GetServerState(string address)
  165. {
  166. Task.Run(async () => {
  167. var isConnected = await ValidateServerAddress(address);
  168. if (isConnected)
  169. {
  170. URMConfig.ServerStatus = ServerStatus.Started;
  171. }
  172. else
  173. {
  174. URMConfig.ServerStatus=ServerStatus.Starting;
  175. }
  176. });
  177. }
  178. private async Task<bool> ValidateServerAddress(string address)
  179. {
  180. try
  181. {
  182. var client = new JsonRpcClient();
  183. var clientEngine = new JsonRpcHttpClientEngine(address);
  184. clientEngine.Timeout = 1000;
  185. client.UseEngine(clientEngine);
  186. var loginService = client?.CreateProxy<WingInterfaceLibrary.Interface.ILoginService>();
  187. if (loginService == null)
  188. {
  189. return false;
  190. }
  191. var result = await loginService.CommonLoginAsync(new WingInterfaceLibrary.Request.User.CommonLoginRequest()
  192. {
  193. });
  194. if (result == null)
  195. {
  196. return false;
  197. }
  198. if (result.LoginState == WingInterfaceLibrary.Enum.LoginStateEnum.SignOrLoginFail ||
  199. result.LoginState == WingInterfaceLibrary.Enum.LoginStateEnum.PasswordIncorrect
  200. )
  201. {
  202. return true;
  203. }
  204. }
  205. catch (RpcException ex)
  206. {
  207. return true;
  208. }
  209. catch (Exception ex)
  210. {
  211. return false;
  212. }
  213. finally
  214. {
  215. }
  216. return true;
  217. }
  218. /// <summary>
  219. /// 判断是否重复打开
  220. /// </summary>
  221. /// <param name="checkedIndex">判断次数,每次间隔1秒,判断结束,窗口依旧存在,则取消此次运行</param>
  222. private void CheckedOpenRepeatedly(string[] Args = null)
  223. {
  224. var thisProcess = Process.GetCurrentProcess();
  225. foreach (var item in Process.GetProcessesByName(thisProcess.ProcessName))
  226. {
  227. if (thisProcess.Id != item.Id)
  228. {
  229. if ((Args != null && Args.Length > 0 && Args[0] == "RestoreData")||string.IsNullOrEmpty(item.MainWindowTitle))
  230. {
  231. item.Close();
  232. }
  233. else
  234. {
  235. ShowWindow(item.MainWindowHandle, 9);
  236. SetForegroundWindow(item.MainWindowHandle);
  237. Current.Shutdown();
  238. }
  239. }
  240. }
  241. }
  242. private void OnExit(object sender, ExitEventArgs e)
  243. {
  244. _defaultLogEngine.Dispose();
  245. _userLogEngine.Dispose();
  246. var thisProcess = Process.GetCurrentProcess();
  247. thisProcess.Kill();
  248. }
  249. private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
  250. {
  251. Logger.WriteLineError($"Unhandled exception:{e.Exception}");
  252. e.Handled = true;
  253. ShowExceptionWindow(e.Exception.Message);
  254. }
  255. private void OnDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
  256. {
  257. Logger.WriteLineError($"Unhandled exception:{e.ExceptionObject}");
  258. var exception = e.ExceptionObject as Exception;
  259. if (exception == null)
  260. {
  261. ShowExceptionWindow(e.ExceptionObject.ToString());
  262. }
  263. else
  264. {
  265. ShowExceptionWindow(exception.ToString());
  266. }
  267. }
  268. private void ShowExceptionWindow(string exceptionMessage)
  269. {
  270. var statusService = AppManager.Instance.GetFunction<IStatusService>();
  271. var popupInfo = new PopupInfo(TranslateHelper.Translate("Error"), exceptionMessage, PopupIconType.Error);
  272. statusService?.ShowPopup(popupInfo);
  273. }
  274. private void InitializeLanguage()
  275. {
  276. var assemblyName = GetType().Assembly.GetName().Name;
  277. foreach (var supportedLanguage in TranslateHelper.SupportedLanguages)
  278. {
  279. var resourceName = $"{assemblyName}.Languages.{supportedLanguage}.json";
  280. using (var languageStream = GetType().Assembly.GetManifestResourceStream(resourceName))
  281. {
  282. if (languageStream != null)
  283. {
  284. var reader = new StreamReader(languageStream);
  285. var content = reader.ReadToEnd();
  286. TranslateHelper.AddLanguageResource(supportedLanguage, content);
  287. }
  288. }
  289. }
  290. }
  291. }
  292. public class RootObject
  293. {
  294. public Common common { get; set; }
  295. public URMServiceConfig.Server server { get; set; }
  296. public List<string> flyinsonoServers { get; set; }
  297. }
  298. public class Common
  299. {
  300. public string dateTimeFormat { get; set; }
  301. }
  302. }
  303. namespace URMServiceConfig
  304. {
  305. public class Server
  306. {
  307. public string current { get; set; }
  308. public List<string> optionSource { get; set; }
  309. }
  310. }