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.

307 lines
9.0 KiB
C#

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using HslCommunication.Enthernet;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Win32;
using SlnMesnac.Business;
using SlnMesnac.Common;
using SlnMesnac.Config;
using SlnMesnac.Model.domain;
using SlnMesnac.Plc;
using SlnMesnac.Repository.service;
using SlnMesnac.TouchSocket;
using SlnMesnac.WPF.Views;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media;
namespace SlnMesnac.WPF.ViewModel
{
public partial class IndexViewModel: ObservableObject
{
private ILogoIdentifyService logoIdentifyService;
private GunHelper gunHelper = GunHelper.Instance;
public RelayCommand ResetCommand { get; set;}
private LogoBusiness logoBusiness = null;
private readonly ILogger<LogoBusiness> _logger2;
private IBaseMaterialService? baseMaterialService;
private ILogoIdentifyService? ocrVerfiyService;
private ILogoConfigService? logoConfigService;
private TcpServer? tcpServer;
private PlcPool plcPool = null;
public IndexViewModel()
{
logoIdentifyService = App.ServiceProvider.GetService<ILogoIdentifyService>();
LogoBusiness.RefreshMessageEvent += RefreshMessage;
LogoBusiness.RefreshBoxInfoEvent += RefreshBoxInfo;
LogoBusiness.RefreshPictureEvent += RefreshPicture;
ResetCommand = new RelayCommand(Reset);
RefreshDataGrid();
plcPool = App.ServiceProvider.GetService<PlcPool>();
tcpServer = App.ServiceProvider.GetService<TcpServer>();
ocrVerfiyService = App.ServiceProvider.GetService<ILogoIdentifyService>();
baseMaterialService = App.ServiceProvider.GetService<IBaseMaterialService>();
logoConfigService = App.ServiceProvider.GetService<ILogoConfigService>();
_logger2 = App.ServiceProvider.GetService<ILogger<LogoBusiness>>();
logoBusiness = LogoBusiness.GetInstance(_logger2, logoConfigService, baseMaterialService, ocrVerfiyService, plcPool, tcpServer);
//程序启动,打开绿色指示灯
gunHelper.SendData("OK");
}
/// <summary>
/// 复位方法
/// 复位PLC放行复位报警灯
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void Reset()
{
//FileHelper.FindPhoto(DebugConfig.Instance.CameraFilePath);
logoBusiness.Pass();
}
/// <summary>
/// 测试方法
/// </summary>
/// <returns></returns>
public byte[] OpenAndConvertImageToBytes()
{
byte[] imageData = null;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Image files (*.jpg, *.jpeg, *.png)|*.jpg;*.jpeg;*.png";
if (openFileDialog.ShowDialog() == true)
{
string filePath = openFileDialog.FileName;
try
{
using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
imageData = new byte[stream.Length];
stream.Read(imageData, 0, (int)stream.Length);
}
}
catch (Exception ex)
{
// 处理异常
Console.WriteLine("Error: " + ex.Message);
}
}
return imageData;
}
public async void RefreshDataGrid()
{
List<LogoIdentify> list = await logoIdentifyService.GetAllRecordAsync();
if (list == null || list.Count == 0) return;
await App.Current.Dispatcher.BeginInvoke((Action)(() =>
{
LogoIdentifyDataGrid.Clear();
list = list.OrderByDescending(x=>x.RecordTime).ToList();
foreach (LogoIdentify verify in list)
{
LogoIdentifyDataGrid.Add(verify);
}
}));
}
#region 界面刷新
/// <summary>
/// 刷新界面箱体信息
/// </summary>
/// <param name="boxCode"></param>
/// <param name="boxTime"></param>
/// <param name="model"></param>
/// <param name="imageData"></param>
public void RefreshBoxInfo(string boxCode,string boxTime,string model,bool isSuccess)
{
BoxCode = boxCode;
BoxTime = boxTime;
ProductModel = model;
RefreshResultColor(isSuccess);
RefreshDataGrid();
}
/// <summary>
/// 刷新界面照片
/// </summary>
/// <param name="boxCode"></param>
/// <param name="boxTime"></param>
/// <param name="model"></param>
/// <param name="imageData"></param>
public void RefreshPicture(byte[] imageData)
{
ImageData = imageData;
}
/// <summary>
/// 刷新界面提示信息
/// </summary>
/// <param name="message"></param>
public void RefreshMessage(string message,bool isWarning = false)
{
if (isWarning)
{
MsgColor = System.Windows.Media.Brushes.Red;
}
else
{
MsgColor = System.Windows.Media.Brushes.White;
}
Message = $"{DateTime.Now.ToString("HH:mm:ss")}:{message}";
}
#endregion
#region 界面参数定义
#region 初始化datagrid
private ObservableCollection<LogoIdentify> _LogoIdentifyDataGrid = new ObservableCollection<LogoIdentify>();
public ObservableCollection<LogoIdentify> LogoIdentifyDataGrid
{
get { return _LogoIdentifyDataGrid; }
set
{
_LogoIdentifyDataGrid = value;
RaisePropertyChanged();//属性通知
}
}
#endregion
#region 箱体码
private string _BoxCode;
public string BoxCode
{
get { return _BoxCode; }
set { _BoxCode = value; RaisePropertyChanged(nameof(BoxCode)); }
}
#endregion
#region 箱体码扫描时间
private string _BoxTime;
public string BoxTime
{
get { return _BoxTime; }
set { _BoxTime = value; RaisePropertyChanged(nameof(BoxTime)); }
}
#endregion
#region 产品型号
private string _ProductModel;
public string ProductModel
{
get { return _ProductModel; }
set { _ProductModel = value; RaisePropertyChanged(nameof(ProductModel)); }
}
#endregion
#region 提示信息
private string _Message;
public string Message
{
get { return _Message; }
set { _Message = value; RaisePropertyChanged(nameof(Message)); }
}
private System.Windows.Media.Brush _msgColor;
public System.Windows.Media.Brush MsgColor
{
get { return _msgColor; }
set
{
_msgColor = value;
RaisePropertyChanged(nameof(MsgColor));
}
}
#endregion
#region Logo照片
private byte[] _imageData;
public byte[] ImageData
{
get => _imageData;
set
{
_imageData = value;
RaisePropertyChanged(nameof(ImageData)); // 属性改变时触发通知
}
}
#endregion
#region Logo识别结果
private string _resultTxt;
public string ResultTxt
{
get { return _resultTxt; }
set
{
_resultTxt = value;
RaisePropertyChanged(nameof(ResultTxt));
}
}
private System.Windows.Media.Brush _resultColor;
public System.Windows.Media.Brush ResultColor
{
get { return _resultColor; }
set
{
_resultColor = value;
RaisePropertyChanged(nameof(ResultColor));
}
}
public void RefreshResultColor(bool isSuccess)
{
if (isSuccess)
{
ResultTxt = "OK";
ResultColor = System.Windows.Media.Brushes.LimeGreen;
}
else
{
ResultTxt = "NG";
ResultColor = System.Windows.Media.Brushes.Red;
}
}
#endregion
#endregion
}
}