App.xaml.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. ProcessHelper.FinishProcessByName(SonopostConstants.UpgradeAppName);
  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 OnCurrentNetworkChanged(object sender, NetworkInterfaceInfo e)
  87. {
  88. if (e != null)
  89. {
  90. var ip = e.IpAddress.Value;
  91. var mac = e.MacAddress;
  92. Logger.WriteLineInfo($"Current IP:{ip}");
  93. Logger.WriteLineInfo($"Current Mac:{mac}");
  94. if (!string.IsNullOrEmpty(ip) && e.OperationalStatus == OperationStatus.Connected)
  95. {
  96. _oledManager.ShowMessages(1, MessageType.DisplayEnglishMediumWithPageNum, ip, 1);
  97. }
  98. else
  99. {
  100. _oledManager.ShowMessages(1, MessageType.DisplayEnglishMediumWithPageNum, mac, 1);
  101. }
  102. _isWifi = e.NetworkInterfaceType == NetworkInterfaceType.Wireless80211;
  103. if (!_isWifi)
  104. {
  105. _oledManager.ClearImage(1, 104, 4, 24, 24);
  106. }
  107. }
  108. }
  109. private void OnCurrentWifiLevelChanged(object sender, int e)
  110. {
  111. if (_isWifi)
  112. {
  113. var levelPic = $"wifi_{e}.png";
  114. _oledManager.ShowImage(1, MessageType.DisplayImageInCurrentPage, 104, 4, levelPic);
  115. }
  116. }
  117. private void SetNetwork()
  118. {
  119. try
  120. {
  121. if (SonopostUserDefinedSettings.Instance.NetworkSetting.NeedDoubleNetwork)
  122. {
  123. var outsideAddress = SonopostUserDefinedSettings.Instance.NetworkSetting.OutsideNetworkMacAddress;
  124. var outsideInterface = _networkManager.GetAllNetworkInterfaceInfos().FirstOrDefault(c => c.MacAddress == outsideAddress);
  125. if (outsideInterface == null)
  126. return;
  127. var result = _networkManager.SetDualNetwork(outsideInterface.MacAddress);
  128. Logger.WriteLineInfo(result ? "SetDualNetworkEnable Success!" : "SetDualNetworkEnable Fail!");
  129. }
  130. }
  131. catch (Exception e)
  132. {
  133. Logger.WriteLineError($"SetNetwork Error:{e}");
  134. }
  135. }
  136. private void AutoLogin()
  137. {
  138. Logger.WriteLineInfo($"Auto Login:{SonopostUserDefinedSettings.Instance.ServerSetting.IsAutoLogin}");
  139. if (SonopostUserDefinedSettings.Instance.ServerSetting.IsAutoLogin)
  140. {
  141. AppManager.Instance.GetManager<ILoginManager>().Login();
  142. }
  143. }
  144. protected override void OnExit(ExitEventArgs e)
  145. {
  146. Logger.WriteLineWarn("OnExit Invoke");
  147. Dispose();
  148. base.OnExit(e);
  149. }
  150. private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
  151. {
  152. Logger.WriteLineError($"DispatcherUnhandledException:{e.Exception}");
  153. _oledManager?.ShowStatus(OledMessage.Error);
  154. Thread.Sleep(2000);
  155. Process.GetCurrentProcess().Kill();
  156. }
  157. private void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
  158. {
  159. Logger.WriteLineError($"UnhandledException:{e.ExceptionObject}");
  160. _oledManager?.ShowStatus(OledMessage.Error);
  161. Thread.Sleep(2000);
  162. Process.GetCurrentProcess().Kill();
  163. }
  164. /// <summary>
  165. /// 判断是否重复打开
  166. /// </summary>
  167. private void CheckedOpenRepeatedly()
  168. {
  169. var thisProcess = Process.GetCurrentProcess();
  170. if (Process.GetProcessesByName(thisProcess.ProcessName).Where(x => x.ProcessName == thisProcess.ProcessName).Count() > 1)
  171. {
  172. thisProcess.Kill();
  173. }
  174. }
  175. private void OnSessionEnded(object sender, SessionEndedEventArgs e)
  176. {
  177. try
  178. {
  179. Logger.WriteLineWarn($"Sonopost OnSessionEnded Invoke");
  180. _oledManager?.ShowStatus(OledMessage.ShutDown);
  181. Dispose();
  182. }
  183. catch (Exception ex)
  184. {
  185. Logger.WriteLineError($"Sonopost OnSessionEnded Invoke Error:{ex}");
  186. }
  187. finally
  188. {
  189. Logger.WriteLineWarn("=======================Sonopost Shutdown=======================");
  190. Process.GetCurrentProcess().Kill();
  191. }
  192. }
  193. private void Dispose()
  194. {
  195. if (_isDisposed)
  196. {
  197. return;
  198. }
  199. Logger.WriteLineWarn("Sonopost Dispose Invoke");
  200. try
  201. {
  202. Task.Run(() =>
  203. {
  204. try
  205. {
  206. _networkManager.CurrentNetworkChanged -= OnCurrentNetworkChanged;
  207. _networkManager.CurrentWifiLevelChanged -= OnCurrentWifiLevelChanged;
  208. AppDomain.CurrentDomain.UnhandledException -= CurrentDomainUnhandledException;
  209. SystemEvents.SessionEnded -= OnSessionEnded;
  210. _webHost?.Dispose();
  211. AppManager.Instance.DisposeAllManagers();
  212. }
  213. catch (Exception ex)
  214. {
  215. Logger.WriteLineError($"Sonopost Dispose Error:{ex}");
  216. }
  217. });
  218. _manualResetEvent.WaitOne(3000);
  219. _isDisposed = true;
  220. }
  221. catch (Exception ex)
  222. {
  223. Logger.WriteLineError($"Sonopost Dispose Error:{ex}");
  224. }
  225. }
  226. }
  227. }