using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using Microsoft.Extensions.DependencyInjection; using SlnMesnac.Business; using SlnMesnac.Business.@base; using SlnMesnac.Model.domain; using SlnMesnac.Model.dto; using SlnMesnac.Plc; using SlnMesnac.WPF.Model; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; #region << 版 本 注 释 >> /*-------------------------------------------------------------------- * 版权所有 (c) 2024 WenJY 保留所有权利。 * CLR版本:4.0.30319.42000 * 机器名称:LAPTOP-E0N2L34V * 命名空间:SlnMesnac.WPF.ViewModel * 唯一标识:14008fcc-0a31-4f1e-bc80-9f9ea84d3de5 * * 创建者:WenJY * 电子邮箱:wenjy@mesnac.com * 创建时间:2024-04-10 16:18:57 * 版本:V1.0.0 * 描述: * *-------------------------------------------------------------------- * 修改人: * 时间: * 修改说明: * * 版本:V1.0.0 *--------------------------------------------------------------------*/ #endregion << 版 本 注 释 >> namespace SlnMesnac.WPF.ViewModel { /// /// 设备参数监控 /// internal class DevMonitorViewModel : ViewModelBase { private BaseBusiness baseBusiness = null; private PlcAbsractFactory plc = null; // private readonly PalletStowBusiness _palletStowBusiness; // private readonly ProdCompletionBusiness _prodCompletionBusiness; public DevMonitorViewModel() { baseBusiness = App.ServiceProvider.GetService(); plc = baseBusiness.GetPlcByKey("plc"); // _palletStowBusiness = App.ServiceProvider.GetService(); // _prodCompletionBusiness = App.ServiceProvider.GetService(); Task.Run(() => { while (true) { RefreshHotDataGrid(); RefreshUnPackDataGrid(); RefreshMagNetDataGrid(); Thread.Sleep(50); } }); //var palletTask = App.ServiceProvider.GetService>(); //RefreshPalletTaskDataGrid(palletTask); // _palletStowBusiness.RefreshPalletTaskListEvent += RefreshPalletTaskDataGrid; //var barCodeTasks = App.ServiceProvider.GetService>(); //RefreshBarCodeTaskDataGrid(barCodeTasks); // _prodCompletionBusiness.RefreshBarCodeTaskListEvent += RefreshBarCodeTaskDataGrid; } #region 参数定义 /// /// 烘干机DataGrid /// private ObservableCollection hotDataGrid; public ObservableCollection HotDataGrid { get { return hotDataGrid; } set { hotDataGrid = value; RaisePropertyChanged(() => HotDataGrid); } } /// /// 拆包机DataGrid /// private ObservableCollection unPackDataGrid; public ObservableCollection UnPackDataGrid { get { return unPackDataGrid; } set { unPackDataGrid = value; RaisePropertyChanged(() => UnPackDataGrid); } } /// /// 磁选机DataGrid /// private ObservableCollection magNetDataGrid; public ObservableCollection MagNetDataGrid { get { return magNetDataGrid; } set { magNetDataGrid = value; RaisePropertyChanged(() => MagNetDataGrid); } } ///// ///// 条码队列DataGrid ///// //private ObservableCollection barCodeTaskDataGrid; //public ObservableCollection BarCodeTaskDataGrid //{ // get { return barCodeTaskDataGrid; } // set { barCodeTaskDataGrid = value; RaisePropertyChanged(() => BarCodeTaskDataGrid); } //} #endregion ///// ///// 刷新托盘队列信息 ///// ///// //private void RefreshPalletTaskDataGrid(List palletTasks) //{ // hotDataGrid = new ObservableCollection(); // foreach (var task in palletTasks) // { // hotDataGrid.Add(task); // } //} ///// ///// 刷新条码队列信息 ///// ///// //private void RefreshBarCodeTaskDataGrid(List barCodeTasks) //{ // BarCodeTaskDataGrid = new ObservableCollection(); // foreach (var task in barCodeTasks) // { // BarCodeTaskDataGrid.Add(task); // } //} #region 烘干机参数监控 private async void RefreshHotDataGrid() { try { HotDataGrid = new ObservableCollection(); int hotStatus = plc.readByteByAddress(baseBusiness.GetPlcAddressByConfigKey("烘干机风机状态")); int hotStatus1 = plc.readByteByAddress(baseBusiness.GetPlcAddressByConfigKey("烘干机风机状态")); int hotStatus2 = plc.readByteByAddress(baseBusiness.GetPlcAddressByConfigKey("烘干机风机状态")); await App.Current.Dispatcher.BeginInvoke((Action)(() => { HotDataGrid.Add(new PLCMachineModel() { Name = "烘干机风机状态", Value = ChangeHotStatus(hotStatus) }); })); }catch (Exception ex) { MessageBox.Show(ex.Message); } } private string ChangeHotStatus(int status) { switch (status) { case 0: return "未知";break; case 1: return "运行";break; case 2: return "停止";break; case 3: return "故障";break; default: return "未知";break; } } #endregion #region 拆包机参数监控 private async void RefreshUnPackDataGrid() { try { UnPackDataGrid = new ObservableCollection(); int unPackStatus = plc.readByteByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机运行状态系统")); bool signal0 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机MES屏蔽")); bool signal1 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程启动")); bool signal2 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程停止")); bool signal3 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程急停")); bool signal4 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("设备叫料")); bool signal5 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("物料到位")); bool signal6 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("设备投料")); bool signal7 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机请求移走空托盘")); await App.Current.Dispatcher.BeginInvoke((Action)(() => { UnPackDataGrid.Add(new PLCMachineModel() { Name = "拆包机运行状态系统", Value = ChangeUnPackStatus(unPackStatus) }); UnPackDataGrid.Add(new PLCMachineModel() { Name = "拆包机MES屏蔽", Value = signal0.ToString() }) ; UnPackDataGrid.Add(new PLCMachineModel() { Name = "拆包机远程启动", Value = signal1.ToString() }); UnPackDataGrid.Add(new PLCMachineModel() { Name = "拆包机远程停止", Value = signal2.ToString() }); UnPackDataGrid.Add(new PLCMachineModel() { Name = "拆包机远程急停", Value = signal3.ToString() }); UnPackDataGrid.Add(new PLCMachineModel() { Name = "设备叫料", Value = signal4.ToString() }); UnPackDataGrid.Add(new PLCMachineModel() { Name = "物料到位", Value = signal5.ToString() }); UnPackDataGrid.Add(new PLCMachineModel() { Name = "设备投料", Value = signal6.ToString() }); UnPackDataGrid.Add(new PLCMachineModel() { Name = "拆包机请求移走空托盘", Value = signal7.ToString() }); })); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private string ChangeUnPackStatus(int status) { switch (status) { case 0: return "停止中"; break; case 1: return "正在进料"; break; case 2: return "正在提升"; break; case 3: return "正在出料"; break; case 4: return "正在复位"; break; default: return "未知"; break; } } #endregion #region 磁选机参数监控 private async void RefreshMagNetDataGrid() { try { MagNetDataGrid = new ObservableCollection(); bool signal0 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机MES屏蔽")); bool signal1 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机设备故障反馈")); bool signal2 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机震动启动")); bool signal3 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机磁选启动")); bool signal4 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机心跳包")); await App.Current.Dispatcher.BeginInvoke((Action)(() => { MagNetDataGrid.Add(new PLCMachineModel() { Name = "磁选机MES屏蔽", Value = signal0.ToString() }) ; MagNetDataGrid.Add(new PLCMachineModel() { Name = "磁选机设备故障反馈", Value = signal1.ToString() }); MagNetDataGrid.Add(new PLCMachineModel() { Name = "磁选机震动启动", Value = signal2.ToString() }); MagNetDataGrid.Add(new PLCMachineModel() { Name = "磁选机磁选启动", Value = signal3.ToString() }); MagNetDataGrid.Add(new PLCMachineModel() { Name = "磁选机心跳包", Value = signal4.ToString() }); })); } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion } }