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 _logger2; private IBaseMaterialService? baseMaterialService; private ILogoIdentifyService? ocrVerfiyService; private ILogoConfigService? logoConfigService; private TcpServer? tcpServer; private PlcPool plcPool = null; public IndexViewModel() { logoIdentifyService = App.ServiceProvider.GetService(); LogoBusiness.RefreshMessageEvent += RefreshMessage; LogoBusiness.RefreshBoxInfoEvent += RefreshBoxInfo; LogoBusiness.RefreshPictureEvent += RefreshPicture; ResetCommand = new RelayCommand(Reset); RefreshDataGrid(); plcPool = App.ServiceProvider.GetService(); tcpServer = App.ServiceProvider.GetService(); ocrVerfiyService = App.ServiceProvider.GetService(); baseMaterialService = App.ServiceProvider.GetService(); logoConfigService = App.ServiceProvider.GetService(); _logger2 = App.ServiceProvider.GetService>(); logoBusiness = LogoBusiness.GetInstance(_logger2, logoConfigService, baseMaterialService, ocrVerfiyService, plcPool, tcpServer); //程序启动,打开绿色指示灯 gunHelper.SendData("OK"); } /// /// 复位方法 /// 复位PLC放行,复位报警灯 /// /// private void Reset() { //FileHelper.FindPhoto(DebugConfig.Instance.CameraFilePath); logoBusiness.Pass(); } /// /// 测试方法 /// /// 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 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 界面刷新 /// /// 刷新界面箱体信息 /// /// /// /// /// public void RefreshBoxInfo(string boxCode,string boxTime,string model,bool isSuccess) { BoxCode = boxCode; BoxTime = boxTime; ProductModel = model; RefreshResultColor(isSuccess); RefreshDataGrid(); } /// /// 刷新界面照片 /// /// /// /// /// public void RefreshPicture(byte[] imageData) { ImageData = imageData; } /// /// 刷新界面提示信息 /// /// 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 _LogoIdentifyDataGrid = new ObservableCollection(); public ObservableCollection 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 } }