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.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.ServiceModel.Channels; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; using static System.Net.Mime.MediaTypeNames; 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 LightHelper lightHelper = LightHelper.Instance; 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; LogoBusiness.RefreshDataGridEvent += RefreshDataGrid; LightHelper.RefreshMessageEvent += RefreshMessage; 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"); OKIsVis = Visibility.Hidden; NGIsVis = Visibility.Hidden; } /// /// 复位方法 /// 复位PLC放行,复位报警灯 /// /// private void Reset() { Message = ""; logoBusiness.Pass(); lightHelper.SendData("CLOSE"); } public async void RefreshDataGrid() { try { await Task.Run(async() => { // 查询条件初始化 DateTime BeginDate = DateTime.Now.Date; // 当天的 0:00:00 DateTime EndDate = DateTime.Now.Date.AddDays(2).AddSeconds(-1); // 当天的 23:59:59 List list = await logoIdentifyService.QueryAllByTime(BeginDate,EndDate); if (list == null || list.Count == 0) return; #region 计算通过率 int totalCount = list.Count; int passCount = list.Where(x => x.isChecked == 1).Count(); ScanCount = totalCount.ToString(); PassCount = passCount.ToString(); int NeedCheckPassCount = list.Where(x => x.isChecked == 1 && x.Result == 1).Count(); // 计算通过率并保留两位小数,添加百分号 double passRate = totalCount == 0 ? 0 : (double)NeedCheckPassCount / passCount * 100; PassRate = passRate.ToString("F2") + "%"; #endregion await App.Current.Dispatcher.BeginInvoke((Action)(() => { LogoIdentifyDataGrid.Clear(); LogoIdentifyDataGrid.Clear(); list = list.OrderByDescending(x => x.RecordTime).ToList(); foreach (LogoIdentify verify in list) { LogoIdentifyDataGrid.Add(verify); } })); }); } catch (Exception ex) { Console.WriteLine(ex.Message); } } #region 界面刷新 /// /// 刷新界面箱体信息 /// /// /// /// /// public void RefreshBoxInfo(string boxCode,string boxTime,string model,bool isSuccess) { BoxCode = boxCode; BoxTime = boxTime; ProductModel = model; RefreshResultColor(isSuccess); if (!isSuccess) { ImageData = null; } } /// /// 刷新界面照片 /// /// /// /// /// 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 _ScanCount; public string ScanCount { get { return _ScanCount; } set { _ScanCount = value; RaisePropertyChanged(nameof(ScanCount)); } } #endregion #region 通过数 private string _PassCount; public string PassCount { get { return _PassCount; } set { _PassCount = value; RaisePropertyChanged(nameof(PassCount)); } } #endregion #region 通过率 private string _PassRate; public string PassRate { get { return _PassRate; } set { _PassRate = value; RaisePropertyChanged(nameof(PassRate)); } } #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)); // } //} #region 识别结果ok\ng展示 private Visibility _OKIsVis; public Visibility OKIsVis { get { return _OKIsVis; } set { _OKIsVis = value; RaisePropertyChanged(nameof(OKIsVis)); } } private Visibility _NGIsVis; public Visibility NGIsVis { get { return _NGIsVis; } set { _NGIsVis = value; RaisePropertyChanged(nameof(NGIsVis)); } } #endregion public void RefreshResultColor(bool isSuccess) { if (isSuccess) { OKIsVis = Visibility.Visible; NGIsVis = Visibility.Collapsed; //ResultTxt = "OK"; //ResultColor = System.Windows.Media.Brushes.LimeGreen; } else { OKIsVis = Visibility.Collapsed; NGIsVis = Visibility.Visible; //ResultTxt = "NG"; //ResultColor = System.Windows.Media.Brushes.Red; } } #endregion #endregion } }