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.

640 lines
22 KiB
C#

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Win32;
using MiniExcelLibs;
using SlnMesnac.RfidUpload.NLog;
using SlnMesnac.RfidUpload.Business;
using SlnMesnac.RfidUpload.Common;
using SlnMesnac.RfidUpload.Model;
using SlnMesnac.RfidUpload.Model.apiParam;
using SlnMesnac.RfidUpload.Model.config;
using SlnMesnac.RfidUpload.TouchSocket;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using ApiServer = SlnMesnac.RfidUpload.TouchSocket.ApiServer;
using System.Windows;
using System.Threading;
namespace SlnMesnac.RfidUpload.UI.viewModel
{
public class MainWindowViewModel : ViewModelBase
{
TransferOrderViewModel transferOrderViewModel = new TransferOrderViewModel();
ContainerTransferOrderViewModel containerTransferOrderViewModel = new ContainerTransferOrderViewModel();
private readonly LogHelper logger = LogHelper.Instance;
private readonly HandleBusiness _business = HandleBusiness.Instance;
private readonly SerialPortBusiness _serialPortBusiness = SerialPortBusiness.Instance;
private ObservableCollection<dynamic> listItems = new ObservableCollection<dynamic>();
private ObservableCollection<DataGridEnt> labelItems = new ObservableCollection<DataGridEnt>();
private CsbHandleBusiness csbHandleBusiness = CsbHandleBusiness.Instance;
private WebApiServerApp webApiServerApp = WebApiServerApp.Instance;
private ApiServer apiServer = ApiServer.GetInstance();
private AppConfig appConfig = AppConfig.Instance;
private JsonChange jsonChange = JsonChange.Instance;
private int No = 1;
#region 参数定义
/// <summary>
/// 串口
/// </summary>
private ObservableCollection<string> _serialPortItems = new ObservableCollection<string>();
public ObservableCollection<string> SerialPortItems
{
get { return _serialPortItems; }
set { _serialPortItems = value; RaisePropertyChanged(() => SerialPortItems); }
}
private string _selectedSerialPortItems = string.Empty;
public string SelectedSerialPortItems
{
get { return _selectedSerialPortItems; }
set { _selectedSerialPortItems = value; RaisePropertyChanged(() => SelectedSerialPortItems); }
}
/// <summary>
/// 波特率
/// </summary>
private ObservableCollection<int> _baudRateItems = new ObservableCollection<int>() { 9600, 115200 };
public ObservableCollection<int> BaudRateItems
{
get { return _baudRateItems; }
set { _baudRateItems = value; RaisePropertyChanged(() => BaudRateItems); }
}
private int _selectedBaudRateItems = 115200;
public int SelectedBaudRateItems
{
get { return _selectedBaudRateItems; }
set { _selectedBaudRateItems = value; RaisePropertyChanged(() => SelectedBaudRateItems); }
}
/// <summary>
/// 数据位
/// </summary>
private ObservableCollection<int> _dataBitsItems = new ObservableCollection<int>() { 2, 4, 6, 8 };
public ObservableCollection<int> DataBitsItems
{
get { return _dataBitsItems; }
set { _dataBitsItems = value; RaisePropertyChanged(() => DataBitsItems); }
}
private int _selectedDataBitsItems = 8;
public int SelectedDataBitsItems
{
get { return _selectedDataBitsItems; }
set { _selectedDataBitsItems = value; RaisePropertyChanged(() => SelectedDataBitsItems); }
}
/// <summary>
///停止位
/// </summary>
private ObservableCollection<int> _stopBitsItems = new ObservableCollection<int>() { 0, 1, 2, 3, 4 };
public ObservableCollection<int> StopBitsItems
{
get { return _stopBitsItems; }
set { _stopBitsItems = value; RaisePropertyChanged(() => StopBitsItems); }
}
private int _selectedStopBitsItems = 1;
public int SelectedStopBitsItems
{
get { return _selectedStopBitsItems; }
set { _selectedStopBitsItems = value; RaisePropertyChanged(() => SelectedStopBitsItems); }
}
/// <summary>
///校验位
/// </summary>
private ObservableCollection<string> _parityItems = new ObservableCollection<string>() { "None", "Odd", "Even", "Mark", "Space" };
public ObservableCollection<string> ParityItems
{
get { return _parityItems; }
set { _parityItems = value; RaisePropertyChanged(() => ParityItems); }
}
private string _selectedParityItems = "None";
public string SelectedParityItems
{
get { return _selectedParityItems; }
set { _selectedParityItems = value; RaisePropertyChanged(() => SelectedParityItems); }
}
/// <summary>
/// 标签数量
/// </summary>
private int labelCountParam = 0;
public int LabelCountParam
{
get { return labelCountParam; }
set { labelCountParam = value;RaisePropertyChanged(() => LabelCountParam); }
}
/// <summary>
/// 主界面当前调拨单号
/// </summary>
private string _mainPageDbdh = "";
public string MainPageDbdh
{
get { return _mainPageDbdh; }
set { _mainPageDbdh = value; RaisePropertyChanged(() => MainPageDbdh); }
}
/// <summary>
/// 工作模式
/// </summary>
private int workModelParam = 0;
public int WorkModelParam
{
get { return workModelParam; }
set { workModelParam = value; RaisePropertyChanged(() => WorkModelParam); }
}
/// <summary>
/// 分发验证后的对象
/// </summary>
private TransferOrderInfo _transferOrderInfo = new TransferOrderInfo();
public TransferOrderInfo TransferOrderInfoItem
{
get { return _transferOrderInfo; }
set { _transferOrderInfo = value; RaisePropertyChanged(() => TransferOrderInfoItem); }
}
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 RelayCommand FinishCommand { get; set; }
public RelayCommand systemSetCommand { get; set; }
/// <summary>
/// 设置工作模式
/// </summary>
public RelayCommand<object> SetWorkModelCommand { get; set; }
public MainWindowViewModel()
{
containerStorageQueryByCsbViewModel.RefreshSubmitVerifyEvent += RefreshSubmitVerify;
TransferOrderViewModel.RefreshSubmitVerifyEvent += RefreshSubmitVerify;
SelectRouterWindow.RefreshSubmitVerifyEvent += RefreshSubmitVerify;
logger.Info("初始化启动");
EmptyFilesCommand = new RelayCommand(EmptyFiles);
ExportFilesCommand = new RelayCommand(ExportFiles);
OpenSerialPortCommand = new RelayCommand(OpenSerialPort);
CloseSerialPortCommand = new RelayCommand(CloseSerialPort);
SetWorkModelCommand = new RelayCommand<object>(SetWorkModel);
systemSetCommand = new RelayCommand(SystemSet);
FinishCommand = new RelayCommand(Finish);
this.Init();
}
private void Init()
{
WorkModelParam = Convert.ToInt32(appConfig.workModel);
//webApiServerApp.Init();
//apiServer.RefreshScanInfoInCenterStartEvent += () =>
//{
// OpenSerialPort();
//};
//apiServer.RefreshScanInfoInCenterStopEvent += () =>
//{
// CloseSerialPort();
//};
//apiServer.RefreshScanInfoOutCenterStartEvent += () =>
//{
// OpenSerialPort();
//};
//apiServer.RefreshScanInfoOutCenterStopEvent += () =>
//{
// CloseSerialPort();
//};
//加载标签信息
_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);
};
_serialPortBusiness.GetSerialPorts(out List<string> serialPorts);
SerialPortItems = new ObservableCollection<string>(serialPorts);
}
/// <summary>
/// 打开串口通讯
/// </summary>
private void OpenSerialPort()
{
try
{
LabelTest();
//_serialPortBusiness.OpenSerialPort();
this.GetParity(out int parity);
_serialPortBusiness.OpenSerialPort(_selectedSerialPortItems, _selectedBaudRateItems, parity, _selectedDataBitsItems, _selectedStopBitsItems);
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);
}
}
private static INIFile iNIFile = new INIFile(System.Environment.CurrentDirectory + "/App.InI");
private void Finish()
{
if (workModelParam == 1) //新容器入库
{
//取出托盘号并调用接口
var palletInfo= labelItems.FirstOrDefault();
if (palletInfo == null)
{
MessageBox.Show("请先放包裹读条码!");
return;
}
else
{
CsbResult res = csbHandleBusiness.scanAnyContainerCodeByCsb(palletInfo.result);
MessageBox.Show(res.message);
}
}
else if (workModelParam == 2) //封发-按调拨单
{
var info = labelItems.ToList();
var strList = info.Select(x => x.result).ToList();
string opBatch = Guid.NewGuid().ToString("N").Substring(0, 20);
CsbResult res = csbHandleBusiness.containerScanSealByCsb(TransferOrderInfoItem.ffjhNo, System.Guid.NewGuid().ToString("N").Substring(0, 20), TransferOrderInfoItem.ffjhscrq, opBatch, jsonChange.ModeToJson(strList));
MessageBox.Show(res.message);
Task.Run(() =>
{ //若干秒以后调反馈接口,确认是否和实际相符
Thread.Sleep(3000);
string opBatch = Guid.NewGuid().ToString("N").Substring(0, 20);
CsbResult res = csbHandleBusiness.containerScanSealResultByCsb(TransferOrderInfoItem.ffjhNo, opBatch);
MessageBox.Show(res.message);
});
}else if(WorkModelParam == 3) //库存容器封发配发 -- 6.13.容器直连配发-开始配发
{
string opBatch = Guid.NewGuid().ToString("N").Substring(0, 20);
csbHandleBusiness.containerAllotByCsb(TransferOrderInfoItem.ffjhNo,opBatch);
Task.Run(() =>
{
Thread.Sleep(3000);
//若干秒以后调反馈接口,确认是否和实际相符
string opBatch = Guid.NewGuid().ToString("N").Substring(0, 20);
CsbResult res = csbHandleBusiness.containerAllotResultByCsb(TransferOrderInfoItem.ffjhNo, opBatch);
MessageBox.Show(res.message);
});
}
else if(workModelParam == -1)
{
MessageBox.Show("工作模式错误"); return;
}
}
/// <summary>
/// 清空标签信息列表
/// </summary>
private void EmptyFiles()
{
No = 0;
labelItems.Clear();
LabelCountParam = 0;
}
private void SystemSet()
{
systemSetWindow systemSetWindow = new systemSetWindow();
systemSetWindow.ShowDialog();
}
private void SetWorkModel(object modelFlag)
{
// WorkModelParam = Convert.ToInt32(modelFlag);
int WorkModel = Convert.ToInt32(modelFlag);
// appConfig.workModel = this.workModelParam.ToString();
//TransferOrderWindow transferOrderWindow = new TransferOrderWindow(transferOrderViewModel);
//transferOrderWindow.ShowDialog();
if(WorkModel == 1) //新容器入库
{
containerStorageQueryByCsb storageQueryByCsb = new containerStorageQueryByCsb();
storageQueryByCsb.ShowDialog();
}
else if(WorkModel == 2) //容器封发-按调拨单
{
TransferOrderWindow transferOrderWindow = new TransferOrderWindow(transferOrderViewModel);
transferOrderWindow.ShowDialog();
}
else if (WorkModel == 3) //库存容器封发配发
{
ContainerTransferOrderWindow containerTransferOrderWindow = new ContainerTransferOrderWindow(containerTransferOrderViewModel);
containerTransferOrderWindow.ShowDialog();
}
else if (WorkModel == 4) //卸车容器封发配发
{
ContainerTransferOrderWindow containerTransferOrderWindow = new ContainerTransferOrderWindow(containerTransferOrderViewModel);
containerTransferOrderWindow.ShowDialog();
}else if (WorkModel == 5) //退出
{
System.Environment.Exit(0);
}
}
/// <summary>
/// 加载标签信息DataGrid
/// </summary>
/// <param name="labelInfos"></param>
private void RefreshLabelInfoDataGrid(InstructionInfo instructionInfo)
{
if (instructionInfo == null) return;
App.Current.Dispatcher.BeginInvoke((Action)(() =>
{
instructionInfo.no = No++;
DataGridEnt dataGridEnt = new DataGridEnt();
dataGridEnt.dbdh = _transferOrderInfo.dbdh;
dataGridEnt.result = instructionInfo.data;
labelItems.Add(dataGridEnt);
LabelInfoDataGrid = labelItems;
LabelCountParam += 1;
}));
Task.Run(() =>
{
if (workModelParam == 101)
{
var info = BatchOpenQuery.Instance;
info.rqtm = instructionInfo.data;
_business.upload(101, jsonChange.ModeToJson(info), "新容器入库");
}
});
}
/// <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;
}
}
/// <summary>
/// 转换校验位
/// </summary>
/// <param name="parity"></param>
private void GetParity(out int parity)
{
switch (_selectedParityItems)
{
case "None":
parity = 0;
break;
case "Odd":
parity = 1;
break;
case "Even":
parity = 2;
break;
case "Mark":
parity = 3;
break;
case "Space":
parity = 4;
break;
default:
parity = 0;
break;
}
}
/// <summary>
///
/// model -当前工作模式
/// msg -接口返回信息
/// dbdh -调拨单号
/// </summary>
/// <param name="model"></param>
/// <param name="msg"></param>
/// <param name="dbdh"></param>
private void RefreshSubmitVerify(int model,string msg,TransferOrderInfo transferOrderInfo=null)
{
appConfig.workModel = model.ToString();
WorkModelParam = model ;
if (transferOrderInfo != null)
{
MainPageDbdh = transferOrderInfo.dbdh;
TransferOrderInfoItem = transferOrderInfo;
}
}
}
}
class DataGridEnt
{
public int no { get; set; }
public string dbdh { get; set; }
public string result { get; set; }
public DateTime recordtime { get; set; }
}