App.xaml.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using SCADA.CommonCtrl.WpfControl;
  2. using SCADA.CommonLib;
  3. using SCADA.CommonLib.Helper;
  4. using SCADA.CommonLib.Service;
  5. using SCADA_DAQ.Plugin.Core.AutoUpdater;
  6. using SCADA_DAQ.Plugin.CoreUI;
  7. using System;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Media.Imaging;
  17. using System.Xml.Linq;
  18. namespace SCADA_DAQ
  19. {
  20. /// <summary>
  21. /// App.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class App : Application, IApp
  24. {
  25. private static SystemControl systemControl;
  26. private string _sessionid;
  27. internal Task updateCheckTask;
  28. /// <summary>
  29. /// </summary>
  30. public IServiceHost ServiceHost { get; set; }
  31. /// <summary>
  32. ///
  33. /// </summary>
  34. public ThemeConfig ThemeConfig { get => AutoSaveParameterItem.Create("App.ThemeConfig", new ThemeConfig()); }
  35. /// <summary>
  36. /// </summary>
  37. public SystemConfig SystemConfig { get => AutoSaveParameterItem.Create("App.SystemConfig", new SystemConfig()); }
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. public RuntimeInfo RuntimeInfo { get; set; } = new RuntimeInfo();
  42. private App()
  43. {
  44. //RuntimeInfo.ProductId=ApplicationHelper.GetProductTitle();
  45. RuntimeInfo.ProductId = System.Windows.Forms.Application.ProductName;
  46. RuntimeInfo.SessionId = _sessionid ?? (_sessionid = Guid.NewGuid().ToString());
  47. RuntimeInfo.ExeName = Path.GetFileName(Assembly.GetExecutingAssembly().CodeBase);
  48. RuntimeInfo.PuginPaths = "plugin";
  49. RuntimeInfo.DllNameMatch = "SCADA_DAQ.Plugin.*.dll";
  50. RuntimeInfo.MenuNameMatch = "UctFrm";
  51. Console.WriteLine($"App Start {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}");
  52. if (string.IsNullOrEmpty(SystemConfig.WebServer))
  53. {
  54. SystemConfig.WebServer = "www.skdscada.com";
  55. }
  56. if (string.IsNullOrEmpty(SystemConfig.ProductTitle))
  57. {
  58. SystemConfig.ProductTitle = ApplicationHelper.GetProductTitle();
  59. }
  60. if (string.IsNullOrEmpty(SystemConfig.AuthorInfo))
  61. {
  62. SystemConfig.AuthorInfo = System.Windows.Forms.Application.CompanyName;
  63. }
  64. if (string.IsNullOrEmpty(SystemConfig.DefaultUser))
  65. {
  66. SystemConfig.DefaultUser = "Operator";
  67. }
  68. try
  69. {
  70. var appServer = SystemConfig.WebServer;
  71. Trace.WriteLine($"Server:{appServer}");
  72. var task = Task.Factory.StartNew(() =>
  73. {
  74. Env.ParameterInit(this);
  75. });
  76. var win = new MainWindow();
  77. MainWindow = win;
  78. win.FrmClosingWait += Win_FrmClosingWait;
  79. systemControl = SystemControl.Create(this);
  80. var iconPath = "Content/Img/favicon.ico";
  81. if (File.Exists(iconPath))
  82. {
  83. MainWindow.Icon = new BitmapImage(new Uri(iconPath, UriKind.RelativeOrAbsolute));
  84. }
  85. task.Wait();
  86. systemControl.SystemInit();
  87. }
  88. catch (Exception ex)
  89. {
  90. //updateCheckTask?.Wait(10 * 1000);
  91. Trace.TraceError(ex.ToString());
  92. }
  93. }
  94. private void Win_FrmClosingWait(object sender, EventArgs e)
  95. {
  96. SystemControl.GetInstance().ApplicationDispose(0);
  97. }
  98. private void Application_Startup(object sender, StartupEventArgs e)
  99. {
  100. MainWindow.Show();
  101. }
  102. private void Application_SessionEnding(object sender, SessionEndingCancelEventArgs e)
  103. {
  104. systemControl.ApplicationSessionEnding(this, e);
  105. }
  106. private void Application_Exit(object sender, ExitEventArgs e)
  107. {
  108. systemControl.ApplicationExit(e.ApplicationExitCode);
  109. }
  110. /// <summary>
  111. ///
  112. /// </summary>
  113. /// <returns></returns>
  114. public bool RestartHost()
  115. {
  116. return systemControl.RestartHost();
  117. }
  118. /// <summary>
  119. ///
  120. /// </summary>
  121. /// <param name="runAs"></param>
  122. public void Restart(bool runAs = false)
  123. {
  124. MainWindow.Close();
  125. Exit += (s, e) =>
  126. {
  127. new Thread(() =>
  128. {
  129. Thread.Sleep(1000);
  130. string strAppFileName = Process.GetCurrentProcess().MainModule.FileName;
  131. Process myNewProcess = new Process();
  132. myNewProcess.StartInfo.FileName = strAppFileName;
  133. myNewProcess.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
  134. if (runAs == true)
  135. {
  136. myNewProcess.StartInfo.Verb = "runas";
  137. }
  138. myNewProcess.Start();
  139. }).Start();
  140. };
  141. }
  142. /// <summary>
  143. ///
  144. /// </summary>
  145. public void Restart()
  146. {
  147. Dispatcher.Invoke(new Action(() =>
  148. {
  149. Restart(false);
  150. }));
  151. }
  152. /// <summary>
  153. ///
  154. /// </summary>
  155. public void ReLoadLicense()
  156. {
  157. systemControl.LoadLicense();
  158. }
  159. /// <summary>
  160. /// 显示toast
  161. /// </summary>
  162. /// <param name="message">toast内容</param>
  163. /// <param name="toastType"></param>
  164. /// <param name="showTime">toast 时间 短:2500 长:3500</param>
  165. public void ShowToast(object message, ControlStyle toastType = ControlStyle.Default, int showTime = 2000)
  166. {
  167. Dispatcher.BeginInvoke(new Action(() =>
  168. {
  169. var toastItem = new ToastItem() { Message = message, ControlStyle = toastType };
  170. var showPannel = SCADA.CommonCtrl.WpfHelper.VisualHelper.GetChildObject<Panel>(MainWindow, "PART_ToastLayer");
  171. toastItem.Show(showPannel, showTime);
  172. }));
  173. }
  174. /// <summary>
  175. /// 显示短toast
  176. /// </summary>
  177. /// <param name="message">toast内容</param>
  178. /// <param name="toastType"></param>
  179. public void ShowShortToast(object message, ControlStyle toastType)
  180. {
  181. ShowToast(message, toastType, 2000);
  182. }
  183. /// <summary>
  184. /// 显示长toast
  185. /// </summary>
  186. /// <param name="message">toast内容</param>
  187. /// <param name="toastType"></param>
  188. public void ShowLongToast(object message, ControlStyle toastType)
  189. {
  190. ShowToast(message, toastType, 3500);
  191. }
  192. }
  193. }