App.xaml.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net.NetworkInformation;
  7. using System.Reflection;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Threading;
  12. using Vinno.FIS.Sonopost.Common;
  13. using Vinno.FIS.Sonopost.Features.Config;
  14. using Vinno.FIS.Sonopost.Features.Network;
  15. using Vinno.FIS.Sonopost.Features.Oled;
  16. using Vinno.FIS.Sonopost.Helpers;
  17. using Vinno.FIS.Sonopost.Managers;
  18. using Vinno.FIS.Sonopost.Managers.Interfaces;
  19. using Vinno.IUS.Common.Log;
  20. namespace Vinno.FIS.Sonopost
  21. {
  22. /// <summary>
  23. /// App.xaml 的交互逻辑
  24. /// </summary>
  25. public partial class App : Application
  26. {
  27. private LogEngine _logEngine;
  28. private WebHost _webHost;
  29. private IOledManager _oledManager;
  30. private bool _isWifi;
  31. private bool _isDisposed;
  32. private readonly ManualResetEvent _manualResetEvent = new ManualResetEvent(false);
  33. private INetworkManager _networkManager;
  34. protected override void OnStartup(StartupEventArgs e)
  35. {
  36. try
  37. {
  38. base.OnStartup(e);
  39. CheckedOpenRepeatedly();
  40. var absolutePath = Path.GetPathRoot(AppDomain.CurrentDomain.BaseDirectory);
  41. var path = Path.Combine(absolutePath, "Sonopost");
  42. DirectoryHelper.CreateDirectory(path);
  43. SonopostConstants.DataFolder = path;
  44. _logEngine = new SonopostLogEngine();
  45. Logger.RegisterEngine(_logEngine);
  46. Logger.WriteLineWarn("=======================Sonopost BootUp=======================");
  47. FISIMPL.FISIMPL.InitLogEngine(_logEngine);
  48. var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
  49. Logger.WriteLineInfo($"Current Version:{version}");
  50. AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
  51. this.DispatcherUnhandledException += OnDispatcherUnhandledException;
  52. SystemEvents.SessionEnded += OnSessionEnded;
  53. ClearUp();
  54. AppManager.Instance.Initialize();
  55. Init();
  56. _webHost = new WebHost();
  57. _webHost.Start();
  58. }
  59. catch (Exception ex)
  60. {
  61. Logger.WriteLineError($"Sonopost Start Up Error:{ex}");
  62. Process.GetCurrentProcess().Kill();
  63. }
  64. }
  65. private void Init()
  66. {
  67. AppManager.Instance.GetManager<ILoginManager>().Init();
  68. AppManager.Instance.GetManager<IDeviceManager>().Init();
  69. AppManager.Instance.GetManager<ILiveVideoManager>().Init();
  70. SystemHelper.Init();
  71. _networkManager = AppManager.Instance.GetManager<INetworkManager>();
  72. _networkManager.CurrentNetworkChanged += OnCurrentNetworkChanged;
  73. _networkManager.CurrentWifiLevelChanged += OnCurrentWifiLevelChanged;
  74. _oledManager = AppManager.Instance.GetManager<IOledManager>();
  75. _oledManager.ShowStatus(OledMessage.Started);
  76. SetNetwork();
  77. OnCurrentNetworkChanged(null, _networkManager.CurrentNetwork);
  78. OnCurrentWifiLevelChanged(null, _networkManager.CurrentWifiLevel);
  79. // CheckAvailableDevices();
  80. AutoLogin();
  81. if (SonopostUserDefinedSettings.Instance.CaptureSetting.RealTimeCaptureEnabled)
  82. {
  83. AppManager.Instance.GetManager<IKeyBoardListenManager>().StartKeyBoardListen();
  84. }
  85. }
  86. private void ClearUp()
  87. {
  88. var path = AppDomain.CurrentDomain.BaseDirectory;
  89. string newUpgrade = Path.Combine(path, SonopostConstants.NewUpgradeAppFileName);
  90. string oldUpgrade = Path.Combine(path, SonopostConstants.UpgradeAppFileName);
  91. if (File.Exists(newUpgrade))
  92. {
  93. if (File.Exists(oldUpgrade))
  94. {
  95. var result = ProcessHelper.FinishProcessByName(SonopostConstants.UpgradeAppName);
  96. if (result == false)
  97. {
  98. Logger.WriteLineError("Finish Upgrade Process Error!");
  99. return;
  100. }
  101. Thread.Sleep(100);
  102. FileHelper.DeleteFile(oldUpgrade);
  103. }
  104. File.Move(newUpgrade, oldUpgrade);
  105. }
  106. }
  107. private void OnCurrentNetworkChanged(object sender, NetworkInterfaceInfo e)
  108. {
  109. if (e != null)
  110. {
  111. var ip = e.IpAddress.Value;
  112. var mac = e.MacAddress;
  113. Logger.WriteLineInfo($"Current IP:{ip}");
  114. Logger.WriteLineInfo($"Current Mac:{mac}");
  115. if (!string.IsNullOrEmpty(ip) && e.OperationalStatus == OperationStatus.Connected)
  116. {
  117. _oledManager.ShowMessages(1, MessageType.DisplayEnglishMediumWithPageNum, ip, 1);
  118. }
  119. else
  120. {
  121. _oledManager.ShowMessages(1, MessageType.DisplayEnglishMediumWithPageNum, mac, 1);
  122. }
  123. _isWifi = e.NetworkInterfaceType == NetworkInterfaceType.Wireless80211;
  124. if (!_isWifi)
  125. {
  126. _oledManager.ClearImage(1, 104, 4, 24, 24);
  127. }
  128. }
  129. }
  130. private void OnCurrentWifiLevelChanged(object sender, int e)
  131. {
  132. if (_isWifi)
  133. {
  134. var levelPic = $"wifi_{e}.png";
  135. _oledManager.ShowImage(1, MessageType.DisplayImageInCurrentPage, 104, 4, levelPic);
  136. }
  137. }
  138. private void SetNetwork()
  139. {
  140. try
  141. {
  142. if (SonopostUserDefinedSettings.Instance.NetworkSetting.NeedDoubleNetwork)
  143. {
  144. var outsideAddress = SonopostUserDefinedSettings.Instance.NetworkSetting.OutsideNetworkMacAddress;
  145. var outsideInterface = _networkManager.GetAllNetworkInterfaceInfos().FirstOrDefault(c => c.MacAddress == outsideAddress);
  146. if (outsideInterface == null)
  147. return;
  148. var result = _networkManager.SetDualNetwork(outsideInterface.MacAddress);
  149. Logger.WriteLineInfo(result ? "SetDualNetworkEnable Success!" : "SetDualNetworkEnable Fail!");
  150. }
  151. }
  152. catch (Exception e)
  153. {
  154. Logger.WriteLineError($"SetNetwork Error:{e}");
  155. }
  156. }
  157. private void AutoLogin()
  158. {
  159. Logger.WriteLineInfo($"Auto Login:{SonopostUserDefinedSettings.Instance.ServerSetting.IsAutoLogin}");
  160. if (SonopostUserDefinedSettings.Instance.ServerSetting.IsAutoLogin)
  161. {
  162. AppManager.Instance.GetManager<ILoginManager>().Login();
  163. }
  164. }
  165. protected override void OnExit(ExitEventArgs e)
  166. {
  167. Logger.WriteLineWarn("OnExit Invoke");
  168. Dispose();
  169. base.OnExit(e);
  170. }
  171. private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
  172. {
  173. Logger.WriteLineError($"DispatcherUnhandledException:{e.Exception}");
  174. _oledManager?.ShowStatus(OledMessage.Error);
  175. Thread.Sleep(2000);
  176. Process.GetCurrentProcess().Kill();
  177. }
  178. private void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
  179. {
  180. Logger.WriteLineError($"UnhandledException:{e.ExceptionObject}");
  181. _oledManager?.ShowStatus(OledMessage.Error);
  182. Thread.Sleep(2000);
  183. Process.GetCurrentProcess().Kill();
  184. }
  185. /// <summary>
  186. /// 判断是否重复打开
  187. /// </summary>
  188. private void CheckedOpenRepeatedly()
  189. {
  190. var thisProcess = Process.GetCurrentProcess();
  191. if (Process.GetProcessesByName(thisProcess.ProcessName).Where(x => x.ProcessName == thisProcess.ProcessName).Count() > 1)
  192. {
  193. thisProcess.Kill();
  194. }
  195. }
  196. private void OnSessionEnded(object sender, SessionEndedEventArgs e)
  197. {
  198. try
  199. {
  200. Logger.WriteLineWarn($"Sonopost OnSessionEnded Invoke");
  201. _oledManager?.ShowStatus(OledMessage.ShutDown);
  202. Dispose();
  203. }
  204. catch (Exception ex)
  205. {
  206. Logger.WriteLineError($"Sonopost OnSessionEnded Invoke Error:{ex}");
  207. }
  208. finally
  209. {
  210. Logger.WriteLineWarn("=======================Sonopost Shutdown=======================");
  211. Process.GetCurrentProcess().Kill();
  212. }
  213. }
  214. private void Dispose()
  215. {
  216. if (_isDisposed)
  217. {
  218. return;
  219. }
  220. Logger.WriteLineWarn("Sonopost Dispose Invoke");
  221. try
  222. {
  223. Task.Run(() =>
  224. {
  225. try
  226. {
  227. _networkManager.CurrentNetworkChanged -= OnCurrentNetworkChanged;
  228. _networkManager.CurrentWifiLevelChanged -= OnCurrentWifiLevelChanged;
  229. AppDomain.CurrentDomain.UnhandledException -= CurrentDomainUnhandledException;
  230. SystemEvents.SessionEnded -= OnSessionEnded;
  231. _webHost?.Dispose();
  232. AppManager.Instance.DisposeAllManagers();
  233. }
  234. catch (Exception ex)
  235. {
  236. Logger.WriteLineError($"Sonopost Dispose Error:{ex}");
  237. }
  238. });
  239. _manualResetEvent.WaitOne(3000);
  240. _isDisposed = true;
  241. }
  242. catch (Exception ex)
  243. {
  244. Logger.WriteLineError($"Sonopost Dispose Error:{ex}");
  245. }
  246. }
  247. }
  248. }