MainWindow.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #pragma warning disable CS0436 // 类型与导入类型冲突
  2. using CardApi;
  3. using ConfigureWindow.domain;
  4. using log4net;
  5. using MaterialDesignThemes.Wpf;
  6. using Microsoft.Win32;
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Configuration;
  12. using System.Configuration.Install;
  13. using System.IO;
  14. using System.Runtime.CompilerServices;
  15. using System.ServiceProcess;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Input;
  21. namespace ConfigureWindow
  22. {
  23. /// <summary>
  24. /// MainWindow.xaml 的交互逻辑
  25. /// </summary>
  26. public partial class MainWindow : Window
  27. {
  28. public static readonly ILog log = LogManager.GetLogger("zmjService");
  29. static string serviceFilePath = $"{AppDomain.CurrentDomain.BaseDirectory}\\zmjService.exe";
  30. static string serviceName = "ZmjService";
  31. private MainWindowViewModel viewModel;
  32. public MainWindow()
  33. {
  34. viewModel = new MainWindowViewModel();
  35. DataContext = viewModel;
  36. InitializeComponent();
  37. }
  38. private void mainWindow_Loaded(object sender, RoutedEventArgs e)
  39. {
  40. if (IsServiceExisted(serviceName))
  41. {
  42. viewModel.ServiceInstalled = true;
  43. }
  44. }
  45. private async void btn_install_Click(object sender, RoutedEventArgs e)
  46. {
  47. showLoading();
  48. await Task.Run(() =>
  49. {
  50. if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceFilePath);
  51. this.InstallService(serviceFilePath);
  52. viewModel.ServiceInstalled = true;
  53. });
  54. hideLoading();
  55. if (viewModel.ServiceInstalled)
  56. {
  57. toast("服务安装成功");
  58. }
  59. }
  60. private void btn_start_Click(object sender, RoutedEventArgs e)
  61. {
  62. if (this.IsServiceExisted(serviceName))
  63. {
  64. this.ServiceStart(serviceName);
  65. viewModel.ServiceStarted = true;
  66. toast("服务启动成功");
  67. }
  68. }
  69. private void btn_stop_Click(object sender, RoutedEventArgs e)
  70. {
  71. if (this.IsServiceExisted(serviceName))
  72. {
  73. this.ServiceStop(serviceName);
  74. viewModel.ServiceStarted = false; Mouse.OverrideCursor = Cursors.Arrow;
  75. toast("服务停止成功");
  76. }
  77. }
  78. private async void btn_uninstall_Click(object sender, RoutedEventArgs e)
  79. {
  80. if (this.IsServiceExisted(serviceName))
  81. {
  82. showLoading();
  83. await Task.Run(() =>
  84. {
  85. this.ServiceStop(serviceName);
  86. this.UninstallService(serviceFilePath);
  87. viewModel.ServiceStarted = false;
  88. viewModel.ServiceInstalled = false;
  89. });
  90. hideLoading();
  91. toast("服务卸载成功");
  92. }
  93. }
  94. //判断服务是否存在
  95. private bool IsServiceExisted(string serviceName)
  96. {
  97. ServiceController[] services = ServiceController.GetServices();
  98. foreach (ServiceController sc in services)
  99. {
  100. if (sc.ServiceName.ToLower() == serviceName.ToLower())
  101. {
  102. if (sc.Status == ServiceControllerStatus.Running || sc.Status == ServiceControllerStatus.StartPending)
  103. {
  104. viewModel.ServiceStarted = true;
  105. }
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. //安装服务
  112. private void InstallService(string serviceFilePath)
  113. {
  114. using (AssemblyInstaller installer = new AssemblyInstaller())
  115. {
  116. installer.UseNewContext = true;
  117. installer.Path = serviceFilePath;
  118. IDictionary savedState = new Hashtable();
  119. installer.Install(savedState);
  120. installer.Commit(savedState);
  121. }
  122. }
  123. //卸载服务
  124. private void UninstallService(string serviceFilePath)
  125. {
  126. using (AssemblyInstaller installer = new AssemblyInstaller())
  127. {
  128. installer.UseNewContext = true;
  129. installer.Path = serviceFilePath;
  130. installer.Uninstall(null);
  131. }
  132. }
  133. //启动服务
  134. private void ServiceStart(string serviceName)
  135. {
  136. using (ServiceController control = new ServiceController(serviceName))
  137. {
  138. if (control.Status == ServiceControllerStatus.Stopped)
  139. {
  140. control.Start();
  141. }
  142. }
  143. }
  144. //停止服务
  145. private void ServiceStop(string serviceName)
  146. {
  147. using (ServiceController control = new ServiceController(serviceName))
  148. {
  149. if (control.Status == ServiceControllerStatus.Running)
  150. {
  151. control.Stop();
  152. }
  153. }
  154. }
  155. private void btn_get_version_Click(object sender, RoutedEventArgs e)
  156. {
  157. toast(viewModel.version());
  158. }
  159. private string btoa2string(byte[] btoa)
  160. {
  161. for (int i = 0; i < btoa.Length; i++)
  162. {
  163. if (btoa[i] == 0)
  164. {
  165. return Encoding.Default.GetString(btoa, 0, i);
  166. }
  167. }
  168. return "";
  169. }
  170. private async void btn_read_Click(object sender, RoutedEventArgs e)
  171. {
  172. showLoading();
  173. try
  174. {
  175. object res = await Task.Run(() =>
  176. {
  177. return viewModel.readCard();
  178. });
  179. hideLoading();
  180. showMsg(res.ToString());
  181. }
  182. catch (Exception ex)
  183. {
  184. hideLoading();
  185. toast(ex.Message);
  186. }
  187. }
  188. private async void btn_write_Click(object sender, RoutedEventArgs e)
  189. {
  190. if (string.IsNullOrWhiteSpace(viewModel.DBPath ?? ""))
  191. {
  192. toast("请选择数据库文件");
  193. return;
  194. }
  195. if (!File.Exists(viewModel.DBPath))
  196. {
  197. toast("数据库文件不存在");
  198. return;
  199. }
  200. if (string.IsNullOrWhiteSpace(viewModel.CardFlag ?? ""))
  201. {
  202. toast("请先读取卡标识");
  203. return;
  204. }
  205. Dictionary<string, object> data = (Dictionary<string, object>)await DialogHost.Show(new InputDialog(viewModel.settings.Type ?? CardType.TYPE_1));
  206. if (data != null)
  207. {
  208. showLoading();
  209. try
  210. {
  211. await Task.Run(() =>
  212. {
  213. viewModel.writeCard(data);
  214. });
  215. hideLoading();
  216. toast("写卡成功");
  217. }
  218. catch (Exception ex)
  219. {
  220. hideLoading();
  221. toast(ex.Message);
  222. }
  223. }
  224. }
  225. private void btn_browse_Click(object sender, RoutedEventArgs e)
  226. {
  227. OpenFileDialog openFileDialog = new OpenFileDialog();
  228. openFileDialog.Filter = "MDB文件|*.mdb";
  229. if (openFileDialog.ShowDialog() == true)
  230. {
  231. viewModel.DBPath = openFileDialog.FileName;
  232. }
  233. }
  234. private void mainWindow_Closed(object sender, EventArgs e)
  235. {
  236. Properties.Settings.Default.Save();
  237. }
  238. private void btn_test_Click(object sender, RoutedEventArgs e)
  239. {
  240. log.Info("btn test clicked!");
  241. }
  242. private async void btn_read_flag_Click(object sender, RoutedEventArgs e)
  243. {
  244. showLoading();
  245. try
  246. {
  247. await Task.Run(() =>
  248. {
  249. viewModel.readFlag();
  250. hideLoading();
  251. });
  252. }
  253. catch (Exception ex)
  254. {
  255. hideLoading();
  256. toast(ex.Message);
  257. }
  258. }
  259. private void showLoading()
  260. {
  261. StackPanel stackPanel = new StackPanel();
  262. stackPanel.VerticalAlignment = VerticalAlignment.Center;
  263. stackPanel.HorizontalAlignment = HorizontalAlignment.Center;
  264. ProgressBar progressBar = new ProgressBar();
  265. progressBar.Style = (Style)FindResource("MaterialDesignCircularProgressBar");
  266. progressBar.Width = 30;
  267. progressBar.Height = 30;
  268. progressBar.IsIndeterminate = true;
  269. progressBar.Margin = new Thickness(16);
  270. stackPanel.Children.Add(progressBar);
  271. DialogHost.Show(stackPanel);
  272. }
  273. private void hideLoading()
  274. {
  275. this.Dispatcher.Invoke(() =>
  276. {
  277. DialogHost.Close(null);
  278. });
  279. }
  280. private void showMsg(string msg)
  281. {
  282. Dispatcher.Invoke(() =>
  283. {
  284. StackPanel stackPanel = new StackPanel
  285. {
  286. VerticalAlignment = VerticalAlignment.Center,
  287. HorizontalAlignment = HorizontalAlignment.Right,
  288. Margin = new Thickness(8)
  289. };
  290. Label label = new Label
  291. {
  292. Content = msg
  293. };
  294. stackPanel.Children.Add(label);
  295. Button btnClose = new Button
  296. {
  297. HorizontalAlignment = HorizontalAlignment.Right,
  298. Width = 64,
  299. Margin = new Thickness(0, 8, 0, 0),
  300. Content = "关闭",
  301. Style = (Style)FindResource("MaterialDesignFlatButton"),
  302. Command = DialogHost.CloseDialogCommand
  303. };
  304. stackPanel.Children.Add(btnClose);
  305. DialogHost.Show(stackPanel);
  306. });
  307. }
  308. private void toast(string s)
  309. {
  310. snackBar.MessageQueue.Enqueue(s, "关闭", () => { });
  311. }
  312. }
  313. }