SystemControl.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. using DBModel;
  2. using DBNames;
  3. using log4net;
  4. using SCADA.CommonLib;
  5. using SCADA.CommonLib.Helper;
  6. using SCADA_DAQ.Plugin.Core;
  7. using SCADA_DAQ.Plugin.Core.License;
  8. using SCADA_DAQ.Plugin.CoreUI;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Reflection;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18. using System.Windows.Media.Imaging;
  19. namespace SCADA_DAQ
  20. {
  21. internal class SystemControl
  22. {
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. bool isShutDown = false;
  27. ILog Log { get; set; } = null;
  28. private EventWaitHandle evh;
  29. public App App { get; private set; }
  30. private static SystemControl systemControl;
  31. private static readonly object syncObj = new object();
  32. private readonly List<AdvanceTimer> Timers = new List<AdvanceTimer>();
  33. private readonly Task initTask;
  34. private readonly Task initServer;
  35. private ServiceHost host = null;
  36. public string UidVersion { get; }
  37. private string SoftWareId { get; set; }
  38. private SystemControl(App app)
  39. {
  40. App = app;
  41. PowerOnTime = DateTime.Now;
  42. SoftWareId = MD5Helper.GetFileMD5(GetType().Assembly.Location);
  43. UidVersion = $"{ApplicationHelper.GetAppVersion()}.{Convert.ToUInt16(SoftWareId.Substring(0, 3), 16):D4}";
  44. app.UidVersion = UidVersion;
  45. initTask = Task.Factory.StartNew(() =>
  46. {
  47. bool createdNew = true;
  48. if (Env.SingleModel.Value == true)
  49. {
  50. evh = new EventWaitHandle(false, EventResetMode.AutoReset, App.ProductId, out createdNew);
  51. var currentProcess = Process.GetCurrentProcess();
  52. var ps = Process.GetProcessesByName(
  53. Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().ManifestModule.Name))
  54. .Where(t => t.Id != currentProcess.Id);
  55. if (createdNew) //信号锁创建成功,且是单例模式
  56. {
  57. foreach (Process p in ps)
  58. {
  59. p.Kill();
  60. }
  61. }
  62. else
  63. {
  64. if (ps.Count() == 1 && ps.First().MainWindowHandle != IntPtr.Zero)
  65. {
  66. evh?.Set();
  67. app.Dispatcher.Invoke(new Action(() =>
  68. {
  69. isShutDown = true;
  70. App.Shutdown(1);
  71. }));
  72. return;
  73. }
  74. else
  75. {
  76. foreach (Process p in ps) //单例模式下如果主界面没有出现,或者有多个进程,直接全部kill掉
  77. {
  78. p.Kill();
  79. }
  80. }
  81. }
  82. }
  83. Console.WriteLine($"开始获取电脑ID {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}");
  84. LicenseManage.GenerateID(App.ProductId, out var str);
  85. Console.WriteLine($"检查释放系统组件 {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}");
  86. ReleaseRunTime();
  87. try
  88. {
  89. var dtClass = DataTableHelper.DtToList<App_Classes>(Env.DAL.App_Classes.GetData($"{T_Col_Name.App_Classes.ClassType_Int}=2"));
  90. if (dtClass != null)
  91. {
  92. Env.Schedual.Classes.Clear();
  93. foreach (var appClassese in dtClass)
  94. {
  95. var startTime = TimeSpan.Parse(appClassese.StartTime_Str);
  96. var endTime = TimeSpan.Parse(appClassese.EndTime_Str);
  97. Env.Schedual.Classes.Add(new WorkShift(startTime,
  98. endTime, appClassese.ClassesName_Str));
  99. }
  100. }
  101. }
  102. catch (Exception)
  103. {
  104. Env.Schedual.Classes.Add(new WorkShift(new TimeSpan(7, 30, 0), new TimeSpan(19, 30, 0), "白班"));
  105. Env.Schedual.Classes.Add(new WorkShift(new TimeSpan(19, 30, 0), new TimeSpan(7, 30, 0), "夜班"));
  106. }
  107. Plugin.CoreUI.SystemApp.SystemStatictics.AppStart();
  108. CreateBackGroundTask(ReportOnLine, 10 * 1000 * 60, 10 * 1000, true, "ReportAppInfo");
  109. CreateBackGroundTask(Plugin.CoreUI.SystemApp.SystemStatictics.UpdateLastOnLineTime, 1 * 1000 * 60, 10 * 1000, true, "UpdateLastTime");
  110. CreateBackGroundTask(Plugin.CoreUI.SystemApp.SystemStatictics.StatisticsRunTime, 30 * 1000, 15 * 1000, true, "StatisticsRunTime");
  111. });
  112. initServer = Task.Factory.StartNew(() =>
  113. {
  114. });
  115. }
  116. private DateTime PowerOnTime { get; set; }
  117. public static SystemControl Create(App app)
  118. {
  119. lock (syncObj)
  120. {
  121. if (systemControl == null)
  122. {
  123. systemControl = new SystemControl(app);
  124. }
  125. }
  126. return systemControl;
  127. }
  128. public static SystemControl GetInstance()
  129. {
  130. return systemControl;
  131. }
  132. public void SystemInit()
  133. {
  134. App.DispatcherUnhandledException += Application_DispatcherUnhandledException;
  135. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  136. TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
  137. Task.Factory.StartNew(() =>
  138. {
  139. Task.WaitAll(initTask);
  140. if (isShutDown) return;
  141. var st = new System.Diagnostics.Stopwatch();
  142. st.Start();
  143. host = ServiceHost.Instance;
  144. host.Start();
  145. (App as IApp).ServiceHost = host;
  146. try
  147. {
  148. Env.StartTime = DateTime.Now;
  149. Log = SCADA.CommonLib.LoggerHelper.Logger.CreatLogger(typeof(App));
  150. Dictionary<SCADA.CommonLib.LoggerHelper.LogLevel, log4net.Core.Level> logLevel =
  151. new Dictionary<SCADA.CommonLib.LoggerHelper.LogLevel, log4net.Core.Level>()
  152. {
  153. { SCADA.CommonLib.LoggerHelper.LogLevel.All,log4net.Core.Level.All},
  154. { SCADA.CommonLib.LoggerHelper.LogLevel.Debug,log4net.Core.Level.Debug},
  155. { SCADA.CommonLib.LoggerHelper.LogLevel.Info,log4net.Core.Level.Info},
  156. { SCADA.CommonLib.LoggerHelper.LogLevel.Warn,log4net.Core.Level.Warn},
  157. { SCADA.CommonLib.LoggerHelper.LogLevel.Error,log4net.Core.Level.Error},
  158. { SCADA.CommonLib.LoggerHelper.LogLevel.Fatal,log4net.Core.Level.Fatal},
  159. { SCADA.CommonLib.LoggerHelper.LogLevel.None,log4net.Core.Level.Off},
  160. };
  161. LogManager.GetRepository().Threshold = logLevel[Env.LogLevel.Value];
  162. Log.Info($"App {App.ProductId} ({UidVersion}) is starting ……");
  163. Task.WaitAll(initServer);
  164. Console.WriteLine($"系统初始化完成 {DateTime.Now:yyyy-MM-dd HH:mm:ss,fff}");
  165. Func<bool> action = null;
  166. LoadLicense();
  167. App.Dispatcher.Invoke(new Action(() =>
  168. {
  169. if (App.MainWindow is MainWindow window)
  170. {
  171. action = window.GetLicense;
  172. }
  173. }));
  174. if (!LicenseManage.CheckID(App.ProductId, out var str, action))
  175. {
  176. App.Dispatcher.Invoke(new Action(() =>
  177. {
  178. isShutDown = true;
  179. App.Shutdown(100);
  180. }));
  181. return;
  182. }
  183. else
  184. {
  185. Env.LicenseInfo = LicenseManage.GetLicenseInfo(App.ProductId);
  186. }
  187. Env.ComputerId.Value = Env.LicenseInfo.Computer.ComputerId;
  188. App.Dispatcher.Invoke(new Action(() =>
  189. {
  190. if (App.MainWindow is MainWindow window)
  191. {
  192. window.LicenseInfo = Env.LicenseInfo;
  193. Log.Debug($"系统初始化完成,即将开启");
  194. window.SystemInited();
  195. Env.SetTheme();
  196. Env.SetLanguage();
  197. Env.SetFont();
  198. }
  199. }));
  200. }
  201. catch (Exception ex)
  202. {
  203. FalatReport(ex);
  204. Log.Error(ex);
  205. }
  206. new Thread(() =>
  207. {
  208. while (!isShutDown && evh != null)
  209. {
  210. if (evh.WaitOne(-1))
  211. {
  212. if (isShutDown)
  213. {
  214. return;
  215. }
  216. App.Dispatcher.Invoke(new Action(() =>
  217. {
  218. App.MainWindow.Topmost = true;
  219. App.MainWindow.WindowState = WindowState.Maximized;
  220. App.MainWindow.Topmost = false;
  221. }));
  222. }
  223. }
  224. }).Start();
  225. });
  226. }
  227. /// <summary>
  228. /// 重新加载授权文件
  229. /// </summary>
  230. internal void LoadLicense()
  231. {
  232. Env.LicenseInfo = LicenseManage.GetLicenseInfo(App.ProductId);
  233. if (isShutDown) return;
  234. App.Dispatcher.Invoke(new Action(() =>
  235. {
  236. if (App.MainWindow is MainWindow window)
  237. {
  238. window.MainWindowViewModel.CopyRightInfo = $"{Env.AuthorInfo.Value}({Env.LicenseInfo.LicenseType})";
  239. }
  240. }));
  241. ReportAppInfo(true);
  242. }
  243. private DateTime lastReportSuccessTime = DateTime.MinValue;
  244. private void ReportOnLine()
  245. {
  246. if ((DateTime.Now - lastReportSuccessTime).TotalSeconds > 590)
  247. {
  248. ReportAppInfo(true);
  249. }
  250. }
  251. private void ReportAppInfo(bool isOnline = true)
  252. {
  253. if (EicpCoreManage.ReportAppInfo(new
  254. {
  255. SessionID = App.SessionId,
  256. AppName = Env.LicenseInfo?.AppName,
  257. Version = UidVersion,
  258. CustomerTag = Env.ComputerTag.Value,
  259. ComputerId = Env.LicenseInfo?.Computer.ComputerId,
  260. LicenseId = Env.LicenseInfo?.LicenseId,
  261. LicenseType = Env.LicenseInfo?.LicenseType.ToString(),
  262. ActiveTime = Env.LicenseInfo?.ActiveTime,
  263. ExpireDate = Env.LicenseInfo?.ActiveTime.AddDays(Env.LicenseInfo.ExpireDays),
  264. TotalOutputPcs = Env.TotalOutputPcs.Value,
  265. TotalOutputQty = Env.TotalOutputQty.Value,
  266. IsOnline = isOnline
  267. }, out var appName))
  268. {
  269. lastReportSuccessTime = DateTime.Now;
  270. Env.ComputerTag.Value = appName;
  271. }
  272. }
  273. private void FalatReport(Exception exception)
  274. {
  275. try
  276. {
  277. EicpCoreManage.FalatReport(new
  278. {
  279. SessionID = App.SessionId,
  280. AppName = Env.LicenseInfo?.AppName,
  281. Version = UidVersion,
  282. ComputerId = Env.LicenseInfo?.Computer?.ComputerId,
  283. SoftwareID = SoftWareId,
  284. Customer = System.Windows.Forms.Application.CompanyName,
  285. Message = exception.Message,
  286. PowerOnTime = PowerOnTime,
  287. RunTime = DateTime.Now - PowerOnTime,
  288. Exception = exception?.ToString()
  289. });
  290. }
  291. catch (Exception ex)
  292. {
  293. Log?.Error(ex);
  294. }
  295. //Task.Factory.StartNew(() =>
  296. //{
  297. // //var webApi = new WebApiHelper($"http://localhost:4082/AppManage/AppFatalReport");
  298. // var webApi = new WebApiHelper($"http://{Env.WebServerAddress.Value}/AppManage/AppFatalReport");
  299. // var res = webApi.POST<string>();
  300. //});
  301. }
  302. public bool RestartHost()
  303. {
  304. return host.Restart();
  305. }
  306. private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  307. {
  308. Log?.Error(e.Exception);
  309. e.Handled = true;
  310. FalatReport(e.Exception);
  311. }
  312. private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  313. {
  314. FalatReport(e.ExceptionObject as Exception);
  315. Log?.Fatal(e.ExceptionObject);
  316. LogManager.Flush(10);
  317. ExitHandler(e.ExceptionObject.ToString());
  318. App?.Dispatcher.BeginInvoke(new Action(() =>
  319. {
  320. MessageBox.Show(e.ExceptionObject.ToString());
  321. }));
  322. }
  323. /// <summary>
  324. /// 释放运行时压缩包
  325. /// </summary>
  326. private void ReleaseRunTime()
  327. {
  328. var runTimeLibDic = new Dictionary<string, string>()
  329. {
  330. {"Content.zip","Content" },
  331. {"HCNETSDKLib.zip",null },
  332. {"RunTime.zip",null },
  333. {"SyntecLib_v4.zip",null},
  334. //{"FocasLib.zip",AppDomain.CurrentDomain.BaseDirectory },
  335. {"x86_x64.zip",null },
  336. };
  337. foreach (var item in runTimeLibDic)
  338. {
  339. try
  340. {
  341. if (System.IO.File.Exists(item.Key))
  342. {
  343. ZipHelper.UnZip(item.Key, item.Value);
  344. FileHelper.DeleteFile(item.Key);
  345. }
  346. }
  347. catch (Exception ex)
  348. {
  349. Log.Error(ex);
  350. }
  351. }
  352. }
  353. private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
  354. {
  355. Log.Fatal(e.Exception);
  356. FalatReport(e.Exception);
  357. //App?.Dispatcher.BeginInvoke(
  358. // new Action(
  359. // () =>
  360. // {
  361. // MessageBox.Show("程序出现异常(task):" + e.Exception.ToString());
  362. // }));
  363. ExitHandler(e.Exception.Message);
  364. }
  365. private void ExitHandler(string exitMsg)
  366. {
  367. Plugin.CoreUI.SystemApp.SystemStatictics.AppExit(exitMsg);
  368. ReportAppInfo(false);
  369. }
  370. public void ApplicationExit(int exitCode)
  371. {
  372. isShutDown = true;
  373. evh?.Set();
  374. evh?.Dispose();
  375. }
  376. public void ApplicationDispose(int exitCode)
  377. {
  378. if (host != null) host.Stop();
  379. foreach (var item in Timers)
  380. {
  381. item.Dispose();
  382. }
  383. var exitReason = "";
  384. if (ExitCode.ContainsKey(exitCode))
  385. {
  386. exitReason = ExitCode[exitCode];
  387. }
  388. ExitHandler(exitReason);
  389. if (Log != null)
  390. {
  391. Log.Info($"{App.ProductId}({UidVersion}) is shutdown by {exitReason}");
  392. SCADA.CommonLib.LoggerHelper.Logger.Dispose();
  393. }
  394. }
  395. public void ApplicationSessionEnding(object sender, SessionEndingCancelEventArgs e)
  396. {
  397. Log.Error($"计算机已注销或关闭,原因:{e.ReasonSessionEnding}");
  398. }
  399. /// <summary>
  400. /// 创建一个后台任务
  401. /// </summary>
  402. /// <param name="task"></param>
  403. /// <param name="cycleTime">重复执行间隔 毫秒</param>
  404. /// <param name="delayTime">延迟时间</param>
  405. /// <param name="backgroundContinue">当转到后台时继续执行</param>
  406. /// <param name="taskName"></param>
  407. public void CreateBackGroundTask(ThreadStart task, int cycleTime, int delayTime = 0, bool backgroundContinue = false, string taskName = null)
  408. {
  409. Timers.Add(new AdvanceTimer(task, taskName, cycleTime, delayTime)
  410. {
  411. IsBackground = backgroundContinue,
  412. IsActived = true,
  413. TimerName = taskName
  414. });
  415. }
  416. private readonly Dictionary<int, string> ExitCode = new Dictionary<int, string>()
  417. {
  418. {0,"User Exit"},
  419. {100,"invalid license"},
  420. };
  421. }
  422. }