#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
{
///
/// MainWindow.xaml 的交互逻辑
///
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 data = (Dictionary)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, "关闭", () => { });
}
}
}