using BPASmartClient.Helper; using BPASmartClient.JXJFoodBigStation.Model; using BPASmartClient.JXJFoodBigStation.Model.HK_PLC; using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.Input; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; namespace BPASmartClient.JXJFoodBigStation.ViewModel { public class HKPlcCommMonitorViewModel:ObservableObject { public HKPlcCommMonitorViewModel() { Type type = typeof(DB_Read); PropertyInfo[] properties = type.GetProperties(); double Offset = 0.0; string addressPos = ""; ThreadManage.GetInstance().StartLong(new Action(() => { foreach (PropertyInfo mi in properties) { PropertyInfo a = GVL_BigStation.HKPlc_Read.GetType().GetProperty(mi.Name); object ab = a.GetValue(GVL_BigStation.HKPlc_Read, null); int index = Array.FindIndex(PlcInfo.ToArray(), p => p.Variable == mi.Name); if (index != -1) { PlcInfo.ElementAt(index).NowValue = ab.ToString(); } } Thread.Sleep(100); }), "plc变量读取",true); if (PlcInfo.Count == 0) { foreach (PropertyInfo mi in properties) { PropertyInfo a = GVL_BigStation.HKPlc_Read.GetType().GetProperty(mi.Name); object ab = a.GetValue(GVL_BigStation.HKPlc_Read, null); if (mi.PropertyType.Name == "Int16") { addressPos = "DBW"; Offset = Math.Ceiling(Offset); if (Offset / 2.0 - Math.Floor(Offset / 2.0) > 0.0) { Offset += 1.0; } } else if (mi.PropertyType.Name == "Boolean") { addressPos = "DBX"; Offset = Math.Round(Offset, 1); if ((Offset - Math.Floor(Offset)) >= 0.8) { Offset = Math.Ceiling(Offset); } } else if (mi.PropertyType.Name == "Single") { addressPos = "DBD"; Offset = Math.Ceiling(Offset); if (Offset / 2.0 - Math.Floor(Offset / 2.0) > 0.0) { Offset += 1.0; } } var num = GVL_BigStation.HKPlc_Read; var res = a.GetCustomAttribute(); string describe = ""; if (res != null) { describe = res.Describe; } else { describe = ""; } PlcInfo.Add(new PlcVarMonitor() { SerialNum = PlcInfo.Count + 1, Variable = mi.Name, Address = "DB98." + addressPos + string.Format("{0:N1}", Offset), Type = mi.PropertyType.Name, Describe = describe, NowValue = ab.ToString(), }); if (mi.PropertyType.Name == "Int16") { Offset += 2; } else if (mi.PropertyType.Name == "Boolean") { Offset += 0.1; } else if (mi.PropertyType.Name == "Single") { Offset += 4; } } } SetValueCommand = new RelayCommand((o) => { /*if (o != null && o is String address) { int index = Array.FindIndex(PlcInfo.ToArray(), p => p.Address == address); if (index != -1) { if (ProcessControl.GetInstance.HKDevice.IsConnected) ProcessControl.GetInstance.HKDevice.HK_PLC_S7.Write(address, PlcInfo.ElementAt(index).SetValue); } }*/ }); } /*public static string GetPropertyValue( T t,string PropertyName) { Type type = typeof(T); }*/ public static ObservableCollection PlcInfo { get; set; } = new ObservableCollection(); public RelayCommand SetValueCommand { get; set; } } }