You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
286 lines
9.0 KiB
C#
286 lines
9.0 KiB
C#
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<dynamic> listItems = new ObservableCollection<dynamic>();
|
|
private ObservableCollection<InstructionInfo> labelItems = new ObservableCollection<InstructionInfo>();
|
|
|
|
#region 参数定义
|
|
|
|
/// <summary>
|
|
/// 标签数量
|
|
/// </summary>
|
|
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)); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 日志打印ListBox
|
|
/// </summary>
|
|
private IEnumerable logInfoListBox;
|
|
public IEnumerable LogInfoListBox
|
|
{
|
|
get { return logInfoListBox; }
|
|
set { logInfoListBox = value; RaisePropertyChanged(() => LogInfoListBox); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 标签信息DataGrid
|
|
/// </summary>
|
|
private IEnumerable labelInfoDataGrid;
|
|
|
|
public IEnumerable LabelInfoDataGrid
|
|
{
|
|
get { return labelInfoDataGrid; }
|
|
set { labelInfoDataGrid = value; RaisePropertyChanged(() => LabelInfoDataGrid); }
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region 事件定义
|
|
|
|
/// <summary>
|
|
/// 打开串口
|
|
/// </summary>
|
|
public RelayCommand OpenSerialPortCommand { get; set; }
|
|
|
|
/// <summary>
|
|
/// 关闭串口
|
|
/// </summary>
|
|
public RelayCommand CloseSerialPortCommand { get; set; }
|
|
|
|
/// <summary>
|
|
/// 导出文件
|
|
/// </summary>
|
|
public RelayCommand ExportFilesCommand { get; set; }
|
|
#endregion
|
|
/// <summary>
|
|
/// 清空列表
|
|
/// </summary>
|
|
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<byte[]> dataPackages = _business.SplitPackets(buffer);
|
|
|
|
foreach (byte[] package in dataPackages)
|
|
{
|
|
_business.LabelHandle(package);
|
|
LabelCountParam += 1;
|
|
}
|
|
};
|
|
|
|
//加载串口通讯日志
|
|
_serialPortBusiness.RefreshLogMessageEvent += message =>
|
|
{
|
|
logger.Info(message);
|
|
PrintMessageToListBox(message);
|
|
};
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 打开串口通讯
|
|
/// </summary>
|
|
private void OpenSerialPort()
|
|
{
|
|
try
|
|
{
|
|
//LabelTest();
|
|
_serialPortBusiness.Open();
|
|
if (rfidDeviceStatus != 1)
|
|
{
|
|
RfidDeviceStatus = 1;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
PrintMessageToListBox($"串口通讯打开异常:{e.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭串口通讯
|
|
/// </summary>
|
|
private void CloseSerialPort()
|
|
{
|
|
try
|
|
{
|
|
_serialPortBusiness.Close();
|
|
if (rfidDeviceStatus != 2)
|
|
{
|
|
RfidDeviceStatus = 2;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
PrintMessageToListBox($"串口通讯关闭异常:{e.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 导出文件
|
|
/// </summary>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清空标签信息列表
|
|
/// </summary>
|
|
private void EmptyFiles()
|
|
{
|
|
labelItems.Clear();
|
|
LabelCountParam = 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加载标签信息DataGrid
|
|
/// </summary>
|
|
/// <param name="labelInfos"></param>
|
|
private void RefreshLabelInfoDataGrid(InstructionInfo instructionInfo)
|
|
{
|
|
if (instructionInfo == null) return;
|
|
App.Current.Dispatcher.BeginInvoke((Action)(() =>
|
|
{
|
|
labelItems.Add(instructionInfo);
|
|
|
|
LabelInfoDataGrid = labelItems;
|
|
|
|
LabelCountParam += 1;
|
|
}));
|
|
}
|
|
|
|
/// <summary>
|
|
/// listBox绑定日志
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 标签解析测试
|
|
/// </summary>
|
|
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<byte[]> dataPackages = _business.SplitPackets(receivedData);
|
|
|
|
// 打印处理后的数据包
|
|
foreach (byte[] package in dataPackages)
|
|
{
|
|
_business.LabelHandle(package);
|
|
//LabelCountParam += 1;
|
|
}
|
|
}
|
|
}
|
|
}
|