SystemControl.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. using DBModel;
  2. using DBNames;
  3. using log4net;
  4. using SCADA.CommonLib;
  5. using SCADA.CommonLib.Helper;
  6. using SCADA.CommonLib.License;
  7. using SCADA_DAQ.Plugin.Core;
  8. using SCADA_DAQ.Plugin.Core.License;
  9. using SCADA_DAQ.Plugin.CoreUI;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Diagnostics;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Reflection;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  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.RuntimeInfo.UidVersion = UidVersion;
  45. initTask = Task.Factory.StartNew(() =>
  46. {
  47. bool createdNew = true;
  48. if (app.SystemConfig.SingleModel == true)
  49. {
  50. evh = new EventWaitHandle(false, EventResetMode.AutoReset, App.RuntimeInfo.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.RuntimeInfo.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. CreateBackGroundTask(ReportOnLine, 10 * 1000 * 60, 10 * 1000, true, "ReportAppInfo");
  108. var systemStatics = Plugin.CoreUI.SystemApp.SystemStatictics.Create();
  109. systemStatics.AppRuntime = App;
  110. systemStatics.Start();
  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 Stopwatch();
  142. st.Start();
  143. EicpCoreManage.AppRuntime = App;
  144. host = ServiceHost.Instance;
  145. (App as IApp).ServiceHost = host;
  146. host.Start(App);
  147. try
  148. {
  149. App.RuntimeInfo.StartTime = DateTime.Now;
  150. Log = SCADA.CommonLib.LoggerHelper.Logger.CreateLogger(typeof(App));
  151. Dictionary<SCADA.CommonLib.LoggerHelper.LogLevel, log4net.Core.Level> logLevel =
  152. new Dictionary<SCADA.CommonLib.LoggerHelper.LogLevel, log4net.Core.Level>()
  153. {
  154. { SCADA.CommonLib.LoggerHelper.LogLevel.All,log4net.Core.Level.All},
  155. { SCADA.CommonLib.LoggerHelper.LogLevel.Debug,log4net.Core.Level.Debug},
  156. { SCADA.CommonLib.LoggerHelper.LogLevel.Info,log4net.Core.Level.Info},
  157. { SCADA.CommonLib.LoggerHelper.LogLevel.Warn,log4net.Core.Level.Warn},
  158. { SCADA.CommonLib.LoggerHelper.LogLevel.Error,log4net.Core.Level.Error},
  159. { SCADA.CommonLib.LoggerHelper.LogLevel.Fatal,log4net.Core.Level.Fatal},
  160. { SCADA.CommonLib.LoggerHelper.LogLevel.None,log4net.Core.Level.Off},
  161. };
  162. LogManager.GetRepository().Threshold = logLevel[App.SystemConfig.LogLevel];
  163. Log.Info($"App {App.RuntimeInfo.ProductId} ({UidVersion}) is starting ……");
  164. Task.WaitAll(initServer);
  165. Console.WriteLine($"系统初始化完成 {DateTime.Now:yyyy-MM-dd HH:mm:ss,fff}");
  166. Func<bool> action = null;
  167. LoadLicense();
  168. App.Dispatcher.Invoke(new Action(() =>
  169. {
  170. if (App.MainWindow is MainWindow window)
  171. {
  172. action = window.GetLicense;
  173. }
  174. }));
  175. if (!LicenseManage.CheckID(App as IApp, out var str, action))
  176. {
  177. App.Dispatcher.Invoke(new Action(() =>
  178. {
  179. isShutDown = true;
  180. App.Shutdown(100);
  181. }));
  182. return;
  183. }
  184. else
  185. {
  186. App.RuntimeInfo.LicenseInfo = LicenseManage.GetLicenseInfo(App.RuntimeInfo.ProductId);
  187. }
  188. App.RuntimeInfo.ComputerId = App.RuntimeInfo.LicenseInfo.Computer.ComputerId;
  189. App.Dispatcher.Invoke(new Action(() =>
  190. {
  191. if (App.MainWindow is MainWindow window)
  192. {
  193. window.LicenseInfo = App.RuntimeInfo.LicenseInfo;
  194. Log.Debug($"系统初始化完成,即将开启");
  195. Env.SetLanguage();
  196. window.SystemInited();
  197. Env.SetTheme();
  198. Env.SetFont();
  199. }
  200. }));
  201. }
  202. catch (Exception ex)
  203. {
  204. FalatReport(ex);
  205. Log.Error(ex);
  206. }
  207. new Thread(() =>
  208. {
  209. while (!isShutDown && evh != null)
  210. {
  211. if (evh.WaitOne(-1))
  212. {
  213. if (isShutDown)
  214. {
  215. return;
  216. }
  217. App.Dispatcher.Invoke(new Action(() =>
  218. {
  219. App.MainWindow.Topmost = true;
  220. App.MainWindow.WindowState = WindowState.Maximized;
  221. App.MainWindow.Topmost = false;
  222. }));
  223. }
  224. }
  225. }).Start();
  226. });
  227. }
  228. /// <summary>
  229. /// 重新加载授权文件
  230. /// </summary>
  231. internal void LoadLicense()
  232. {
  233. App.RuntimeInfo.LicenseInfo = LicenseManage.GetLicenseInfo(App.RuntimeInfo.ProductId);
  234. if (isShutDown || App.RuntimeInfo.LicenseInfo == null) return;
  235. if (!App.Dispatcher.HasShutdownStarted)
  236. {
  237. App.Dispatcher.Invoke(new Action(() =>
  238. {
  239. if (App.MainWindow is MainWindow window)
  240. {
  241. window.MainWindowViewModel.CopyRightInfo = $"{App.SystemConfig.AuthorInfo}({App.RuntimeInfo.LicenseInfo.LicenseType})";
  242. }
  243. }));
  244. ReportAppInfo(true);
  245. }
  246. }
  247. private DateTime lastReportSuccessTime = DateTime.MinValue;
  248. private void ReportOnLine()
  249. {
  250. if ((DateTime.Now - lastReportSuccessTime).TotalSeconds > 590)
  251. {
  252. ReportAppInfo(true);
  253. }
  254. }
  255. private void ReportAppInfo(bool isOnline = true)
  256. {
  257. if (App.RuntimeInfo.LicenseInfo != null && (App.RuntimeInfo.LicenseInfo.LicenseType == LicenseType.TempLicense || App.RuntimeInfo.LicenseInfo.LicenseType == LicenseType.Permanentlicense))
  258. {
  259. var reportInfo = new
  260. {
  261. SessionID = App.RuntimeInfo.SessionId,
  262. AppName = App.RuntimeInfo.LicenseInfo?.AppName,
  263. Version = UidVersion,
  264. CustomerTag = App.SystemConfig.ComputerTag,
  265. ComputerId = App.RuntimeInfo.LicenseInfo?.Computer.ComputerId,
  266. LicenseId = App.RuntimeInfo.LicenseInfo?.LicenseId,
  267. LicenseType = App.RuntimeInfo.LicenseInfo?.LicenseType.ToString(),
  268. ActiveTime = App.RuntimeInfo.LicenseInfo?.ActiveTime,
  269. ExpireDate = App.RuntimeInfo.LicenseInfo?.ActiveTime.AddDays(App.RuntimeInfo.LicenseInfo.ExpireDays),
  270. TotalOutputPcs = Env.TotalOutputPcs.Value,
  271. TotalOutputQty = Env.TotalOutputQty.Value,
  272. IsOnline = isOnline
  273. };
  274. if (EicpCoreManage.ReportAppInfo(reportInfo, out var appName))
  275. {
  276. lastReportSuccessTime = DateTime.Now;
  277. App.SystemConfig.ComputerTag = appName;
  278. }
  279. }
  280. }
  281. private void FalatReport(Exception exception)
  282. {
  283. if (isShutDown) return;
  284. if (App.RuntimeInfo.LicenseInfo != null &&
  285. (App.RuntimeInfo.LicenseInfo.LicenseType == LicenseType.TempLicense ||
  286. App.RuntimeInfo.LicenseInfo.LicenseType == LicenseType.Permanentlicense))
  287. {
  288. try
  289. {
  290. EicpCoreManage.FalatReport(new
  291. {
  292. SessionID = App.RuntimeInfo.SessionId,
  293. AppName = App.RuntimeInfo.LicenseInfo?.AppName,
  294. Version = UidVersion,
  295. ComputerId = App.RuntimeInfo.LicenseInfo?.Computer?.ComputerId,
  296. SoftwareID = SoftWareId,
  297. Customer = System.Windows.Forms.Application.CompanyName,
  298. Message = exception.Message,
  299. PowerOnTime = PowerOnTime,
  300. RunTime = DateTime.Now - PowerOnTime,
  301. Exception = exception?.ToString()
  302. });
  303. }
  304. catch (Exception ex)
  305. {
  306. Log?.Error(ex);
  307. }
  308. }
  309. }
  310. public bool RestartHost()
  311. {
  312. return host.Restart();
  313. }
  314. private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  315. {
  316. Log?.Error(e.Exception);
  317. e.Handled = true;
  318. if (!isShutDown)
  319. {
  320. FalatReport(e.Exception);
  321. }
  322. }
  323. private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  324. {
  325. FalatReport(e.ExceptionObject as Exception);
  326. Log?.Fatal(e.ExceptionObject);
  327. LogManager.Flush(10);
  328. ExitHandler(e.ExceptionObject.ToString());
  329. App?.Dispatcher.BeginInvoke(new Action(() =>
  330. {
  331. MessageBox.Show(e.ExceptionObject.ToString());
  332. }));
  333. }
  334. /// <summary>
  335. /// 释放运行时压缩包
  336. /// </summary>
  337. private void ReleaseRunTime()
  338. {
  339. var runTimeLibDic = new Dictionary<string, string>()
  340. {
  341. {"Content.zip","Content" },
  342. {"EicpWeb.zip","Content" },
  343. {"EicpCustomer.zip","Content" },
  344. {"HCNETSDKLib.zip",null },
  345. {"RunTime.zip",null },
  346. {"SyntecLib_v4.zip",null},
  347. {"FocasLib.zip",AppDomain.CurrentDomain.BaseDirectory },
  348. {"DeviceCfg.zip",null},
  349. {"x86_x64.zip",null },
  350. };
  351. foreach (var item in runTimeLibDic)
  352. {
  353. try
  354. {
  355. if (System.IO.File.Exists(item.Key))
  356. {
  357. ZipHelper.UnZip(item.Key, item.Value);
  358. FileHelper.DeleteFile(item.Key);
  359. }
  360. }
  361. catch (Exception ex)
  362. {
  363. Log.Error(ex);
  364. }
  365. }
  366. }
  367. private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
  368. {
  369. Log?.Fatal(e.Exception);
  370. FalatReport(e.Exception);
  371. ExitHandler(e.Exception.Message);
  372. }
  373. private void ExitHandler(string exitMsg)
  374. {
  375. Plugin.CoreUI.SystemApp.SystemStatictics.Create().AppExit(exitMsg);
  376. ReportAppInfo(false);
  377. }
  378. public void ApplicationExit(int exitCode)
  379. {
  380. isShutDown = true;
  381. evh?.Set();
  382. evh?.Dispose();
  383. }
  384. public void ApplicationDispose(int exitCode)
  385. {
  386. isShutDown = true;
  387. if (host != null) host.Stop();
  388. foreach (var item in Timers)
  389. {
  390. item.Dispose();
  391. }
  392. var exitReason = "";
  393. if (ExitCode.ContainsKey(exitCode))
  394. {
  395. exitReason = ExitCode[exitCode];
  396. }
  397. ExitHandler(exitReason);
  398. if (Log != null)
  399. {
  400. Log.Info($"{App.RuntimeInfo.ProductId}({UidVersion}) is shutdown by {exitReason}");
  401. SCADA.CommonLib.LoggerHelper.Logger.Dispose();
  402. }
  403. }
  404. public void ApplicationSessionEnding(object sender, SessionEndingCancelEventArgs e)
  405. {
  406. Log.Error($"计算机已注销或关闭,原因:{e.ReasonSessionEnding}");
  407. }
  408. /// <summary>
  409. /// 创建一个后台任务
  410. /// </summary>
  411. /// <param name="task"></param>
  412. /// <param name="cycleTime">重复执行间隔 毫秒</param>
  413. /// <param name="delayTime">延迟时间</param>
  414. /// <param name="backgroundContinue">当转到后台时继续执行</param>
  415. /// <param name="taskName"></param>
  416. public void CreateBackGroundTask(ThreadStart task, int cycleTime, int delayTime = 0, bool backgroundContinue = false, string taskName = null)
  417. {
  418. Timers.Add(new AdvanceTimer(task, taskName, cycleTime, delayTime)
  419. {
  420. IsBackground = backgroundContinue,
  421. IsActived = true,
  422. TimerName = taskName
  423. });
  424. }
  425. private readonly Dictionary<int, string> ExitCode = new Dictionary<int, string>()
  426. {
  427. {0,"User Exit"},
  428. {100,"invalid license"},
  429. };
  430. }
  431. }