| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- #pragma warning disable CS0436 // 类型与导入类型冲突
- using CardApi;
- using ConfigureWindow.domain;
- using log4net;
- using MaterialDesignThemes.Wpf;
- using Microsoft.Win32;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Configuration;
- using System.Configuration.Install;
- using System.IO;
- using System.Runtime.CompilerServices;
- using System.ServiceProcess;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- namespace ConfigureWindow
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- public static readonly ILog log = LogManager.GetLogger("zmjService");
- static string serviceFilePath = $"{AppDomain.CurrentDomain.BaseDirectory}\\zmjService.exe";
- static string serviceName = "ZmjService";
- private MainWindowViewModel viewModel;
- public MainWindow()
- {
- viewModel = new MainWindowViewModel();
- DataContext = viewModel;
- InitializeComponent();
- }
- private void mainWindow_Loaded(object sender, RoutedEventArgs e)
- {
- if (IsServiceExisted(serviceName))
- {
- viewModel.ServiceInstalled = true;
- }
- }
- private async void btn_install_Click(object sender, RoutedEventArgs e)
- {
- showLoading();
- await Task.Run(() =>
- {
- if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceFilePath);
- this.InstallService(serviceFilePath);
- viewModel.ServiceInstalled = true;
- });
- hideLoading();
- if (viewModel.ServiceInstalled)
- {
- toast("服务安装成功");
- }
- }
- private void btn_start_Click(object sender, RoutedEventArgs e)
- {
- if (this.IsServiceExisted(serviceName))
- {
- this.ServiceStart(serviceName);
- viewModel.ServiceStarted = true;
- toast("服务启动成功");
- }
- }
- private void btn_stop_Click(object sender, RoutedEventArgs e)
- {
- if (this.IsServiceExisted(serviceName))
- {
- this.ServiceStop(serviceName);
- viewModel.ServiceStarted = false; Mouse.OverrideCursor = Cursors.Arrow;
- toast("服务停止成功");
- }
- }
- private async void btn_uninstall_Click(object sender, RoutedEventArgs e)
- {
- if (this.IsServiceExisted(serviceName))
- {
- showLoading();
- await Task.Run(() =>
- {
- this.ServiceStop(serviceName);
- this.UninstallService(serviceFilePath);
- viewModel.ServiceStarted = false;
- viewModel.ServiceInstalled = false;
- });
- hideLoading();
- toast("服务卸载成功");
- }
- }
- //判断服务是否存在
- private bool IsServiceExisted(string serviceName)
- {
- ServiceController[] services = ServiceController.GetServices();
- foreach (ServiceController sc in services)
- {
- if (sc.ServiceName.ToLower() == serviceName.ToLower())
- {
- if (sc.Status == ServiceControllerStatus.Running || sc.Status == ServiceControllerStatus.StartPending)
- {
- viewModel.ServiceStarted = true;
- }
- return true;
- }
- }
- return false;
- }
- //安装服务
- private void InstallService(string serviceFilePath)
- {
- using (AssemblyInstaller installer = new AssemblyInstaller())
- {
- installer.UseNewContext = true;
- installer.Path = serviceFilePath;
- IDictionary savedState = new Hashtable();
- installer.Install(savedState);
- installer.Commit(savedState);
- }
- }
- //卸载服务
- private void UninstallService(string serviceFilePath)
- {
- using (AssemblyInstaller installer = new AssemblyInstaller())
- {
- installer.UseNewContext = true;
- installer.Path = serviceFilePath;
- installer.Uninstall(null);
- }
- }
- //启动服务
- private void ServiceStart(string serviceName)
- {
- using (ServiceController control = new ServiceController(serviceName))
- {
- if (control.Status == ServiceControllerStatus.Stopped)
- {
- control.Start();
- }
- }
- }
- //停止服务
- private void ServiceStop(string serviceName)
- {
- using (ServiceController control = new ServiceController(serviceName))
- {
- if (control.Status == ServiceControllerStatus.Running)
- {
- control.Stop();
- }
- }
- }
- private void btn_get_version_Click(object sender, RoutedEventArgs e)
- {
- toast(viewModel.version());
- }
- private string btoa2string(byte[] btoa)
- {
- for (int i = 0; i < btoa.Length; i++)
- {
- if (btoa[i] == 0)
- {
- return Encoding.Default.GetString(btoa, 0, i);
- }
- }
- return "";
- }
- private async void btn_read_Click(object sender, RoutedEventArgs e)
- {
- showLoading();
- try
- {
- object res = await Task.Run(() =>
- {
- return viewModel.readCard();
- });
- hideLoading();
- showMsg(res.ToString());
- }
- catch (Exception ex)
- {
- hideLoading();
- toast(ex.Message);
- }
- }
- private async void btn_write_Click(object sender, RoutedEventArgs e)
- {
- if (string.IsNullOrWhiteSpace(viewModel.DBPath ?? ""))
- {
- toast("请选择数据库文件");
- return;
- }
- if (!File.Exists(viewModel.DBPath))
- {
- toast("数据库文件不存在");
- return;
- }
- if (string.IsNullOrWhiteSpace(viewModel.CardFlag ?? ""))
- {
- toast("请先读取卡标识");
- return;
- }
- Dictionary<string, object> data = (Dictionary<string, object>)await DialogHost.Show(new InputDialog(viewModel.settings.Type ?? CardType.TYPE_1));
- if (data != null)
- {
- showLoading();
- try
- {
- await Task.Run(() =>
- {
- viewModel.writeCard(data);
- });
- hideLoading();
- toast("写卡成功");
- }
- catch (Exception ex)
- {
- hideLoading();
- toast(ex.Message);
- }
- }
- }
- private void btn_browse_Click(object sender, RoutedEventArgs e)
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Filter = "MDB文件|*.mdb";
- if (openFileDialog.ShowDialog() == true)
- {
- viewModel.DBPath = openFileDialog.FileName;
- }
- }
- private void mainWindow_Closed(object sender, EventArgs e)
- {
- Properties.Settings.Default.Save();
- }
- private void btn_test_Click(object sender, RoutedEventArgs e)
- {
- log.Info("btn test clicked!");
- }
- private async void btn_read_flag_Click(object sender, RoutedEventArgs e)
- {
- showLoading();
- try
- {
- await Task.Run(() =>
- {
- viewModel.readFlag();
- hideLoading();
- });
- }
- catch (Exception ex)
- {
- hideLoading();
- toast(ex.Message);
- }
- }
- private void showLoading()
- {
- StackPanel stackPanel = new StackPanel();
- stackPanel.VerticalAlignment = VerticalAlignment.Center;
- stackPanel.HorizontalAlignment = HorizontalAlignment.Center;
- ProgressBar progressBar = new ProgressBar();
- progressBar.Style = (Style)FindResource("MaterialDesignCircularProgressBar");
- progressBar.Width = 30;
- progressBar.Height = 30;
- progressBar.IsIndeterminate = true;
- progressBar.Margin = new Thickness(16);
- stackPanel.Children.Add(progressBar);
- DialogHost.Show(stackPanel);
- }
- private void hideLoading()
- {
- this.Dispatcher.Invoke(() =>
- {
- DialogHost.Close(null);
- });
- }
- private void showMsg(string msg)
- {
- Dispatcher.Invoke(() =>
- {
- StackPanel stackPanel = new StackPanel
- {
- VerticalAlignment = VerticalAlignment.Center,
- HorizontalAlignment = HorizontalAlignment.Right,
- Margin = new Thickness(8)
- };
- Label label = new Label
- {
- Content = msg
- };
- stackPanel.Children.Add(label);
- Button btnClose = new Button
- {
- HorizontalAlignment = HorizontalAlignment.Right,
- Width = 64,
- Margin = new Thickness(0, 8, 0, 0),
- Content = "关闭",
- Style = (Style)FindResource("MaterialDesignFlatButton"),
- Command = DialogHost.CloseDialogCommand
- };
- stackPanel.Children.Add(btnClose);
- DialogHost.Show(stackPanel);
- });
- }
- private void toast(string s)
- {
- snackBar.MessageQueue.Enqueue(s, "关闭", () => { });
- }
- }
- }
|