using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Microsoft.Win32; using MiniExcelLibs; using NLog; using SlnMesnac.RfidUpload.Business; using SlnMesnac.RfidUpload.Model; using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Runtime.CompilerServices; namespace SlnMesnac.RfidUpload.UI.viewModel { public class MainWindowViewModel : ViewModelBase { private readonly Logger logger = LogManager.GetCurrentClassLogger(); private readonly HandleBusiness _business = HandleBusiness.Instance; private readonly SerialPortBusiness _serialPortBusiness = SerialPortBusiness.Instance; private ObservableCollection listItems = new ObservableCollection(); private ObservableCollection labelItems = new ObservableCollection(); #region 参数定义 /// /// 标签数量 /// private int labelCountParam = 0; public int LabelCountParam { get { return labelCountParam; } set { labelCountParam = value;RaisePropertyChanged(() => LabelCountParam); } } private int rfidDeviceStatus = 0; public int RfidDeviceStatus { get { return rfidDeviceStatus; } set { rfidDeviceStatus = value; RaisePropertyChanged(nameof(RfidDeviceStatus)); } } /// /// 日志打印ListBox /// private IEnumerable logInfoListBox; public IEnumerable LogInfoListBox { get { return logInfoListBox; } set { logInfoListBox = value; RaisePropertyChanged(() => LogInfoListBox); } } /// /// 标签信息DataGrid /// private IEnumerable labelInfoDataGrid; public IEnumerable LabelInfoDataGrid { get { return labelInfoDataGrid; } set { labelInfoDataGrid = value; RaisePropertyChanged(() => LabelInfoDataGrid); } } #endregion #region 事件定义 /// /// 打开串口 /// public RelayCommand OpenSerialPortCommand { get; set; } /// /// 关闭串口 /// public RelayCommand CloseSerialPortCommand { get; set; } /// /// 导出文件 /// public RelayCommand ExportFilesCommand { get; set; } #endregion /// /// 清空列表 /// public RelayCommand EmptyFilesCommand { get; set; } public MainWindowViewModel() { logger.Info("初始化启动"); EmptyFilesCommand = new RelayCommand(EmptyFiles); ExportFilesCommand = new RelayCommand(ExportFiles); OpenSerialPortCommand = new RelayCommand(OpenSerialPort); CloseSerialPortCommand = new RelayCommand(CloseSerialPort); this.Init(); } private void Init() { //加载标签信息 _business.InstructionInfoDataGridEvent += info => { RefreshLabelInfoDataGrid(info); }; //加载解析日志 _business.RefreshLogMessageEvent += message => { logger.Info(message); PrintMessageToListBox(message); }; //接收串口数据 _serialPortBusiness.ReceivedDataEvent += buffer => { //粘包数据处理 List dataPackages = _business.SplitPackets(buffer); foreach (byte[] package in dataPackages) { _business.LabelHandle(package); LabelCountParam += 1; } }; //加载串口通讯日志 _serialPortBusiness.RefreshLogMessageEvent += message => { logger.Info(message); PrintMessageToListBox(message); }; } /// /// 打开串口通讯 /// private void OpenSerialPort() { try { //LabelTest(); _serialPortBusiness.Open(); if (rfidDeviceStatus != 1) { RfidDeviceStatus = 1; } } catch (Exception e) { PrintMessageToListBox($"串口通讯打开异常:{e.Message}"); } } /// /// 关闭串口通讯 /// private void CloseSerialPort() { try { _serialPortBusiness.Close(); if (rfidDeviceStatus != 2) { RfidDeviceStatus = 2; } } catch (Exception e) { PrintMessageToListBox($"串口通讯关闭异常:{e.Message}"); } } /// /// 导出文件 /// private void ExportFiles() { try { var info = labelItems.ToList(); // 创建 SaveFileDialog 对象 SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Excel文件 (*.xlsx)|*.xlsx|所有文件 (*.*)|*.*"; saveFileDialog.Title = "保存文件"; saveFileDialog.FileName = $"标签信息{DateTime.Now.Date:yyyy-MM-dd}.xlsx"; if (saveFileDialog.ShowDialog() == true) { string filePath = saveFileDialog.FileName; MiniExcel.SaveAs(filePath, info); PrintMessageToListBox($"标签数据导出成功:{filePath}"); } } catch(Exception ex) { PrintMessageToListBox($"标签数据导出异常:{ex.Message}"); logger.Error($"标签数据导出异常", ex); } } /// /// 清空标签信息列表 /// private void EmptyFiles() { labelItems.Clear(); LabelCountParam = 0; } /// /// 加载标签信息DataGrid /// /// private void RefreshLabelInfoDataGrid(InstructionInfo instructionInfo) { if (instructionInfo == null) return; App.Current.Dispatcher.BeginInvoke((Action)(() => { labelItems.Add(instructionInfo); LabelInfoDataGrid = labelItems; LabelCountParam += 1; })); } /// /// listBox绑定日志 /// /// private void PrintMessageToListBox(string message) { lock (string.Empty) { try { listItems.Add($"{DateTime.Now.ToString("HH:mm:ss")}==>{message}"); LogInfoListBox = listItems.OrderByDescending(x => x); } catch (Exception ex) { logger.Error("日志数据绑定异常", ex); } } } /// /// 标签解析测试 /// private void LabelTest() { // 模拟接收到的粘包数据 byte[] receivedData = { 0xAA, 0x55, 0x00, 0x1F, 0x31, 0x03, 0x45, 0x32, 0x30, 0x30, 0x33, 0x30, 0x33, 0x32, 0x37, 0x38, 0x31, 0x30, 0x30, 0x31, 0x35, 0x30, 0x30, 0x35, 0x39, 0x30, 0x44, 0x35, 0x35, 0x36, 0x2C, 0xAA, 0x55, 0x00, 0x1F, 0x31, 0x03, 0x45, 0x32, 0x30, 0x30, 0x33, 0x30, 0x33, 0x32, 0x37, 0x38, 0x31, 0x30, 0x30, 0x31, 0x35, 0x30, 0x30, 0x35, 0x39, 0x30, 0x44, 0x35, 0x35, 0x36, 0x2C, 0xAA, 0x55, 0x00, 0x1F, 0x31, 0x03, 0x45, 0x32, 0x30, 0x30, 0x33, 0x30, 0x33, 0x32, 0x37, 0x38, 0x31, 0x30, 0x30, 0x31, 0x35, 0x30, 0x30, 0x35, 0x39, 0x30, 0x44, 0x35, 0x35, 0x36, 0x2C, 0xAA, 0x55, 0x00, 0x1F, 0x31, 0x03, 0x45, 0x32, 0x30, 0x30, 0x33, 0x30, 0x33, 0x32, 0x37, 0x38, 0x31, 0x30, 0x30, 0x31, 0x35, 0x30, 0x30, 0x35, 0x39, 0x30, 0x44, 0x35, 0x35, 0x36, 0x2C, 0xAA, 0x55, 0x00, 0x27, 0x31, 0x03, 0x46, 0x43, 0x31, 0x30, 0x30, 0x38, 0x32, 0x30, 0x32, 0x30, 0x30 }; //粘包数据的处理 List dataPackages = _business.SplitPackets(receivedData); // 打印处理后的数据包 foreach (byte[] package in dataPackages) { _business.LabelHandle(package); //LabelCountParam += 1; } } } }