App.xaml.cs 10.0 KB

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