|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using BPA.Helper;
- using BPASmartClient.JXJFoodBigStation.Model;
- using BPASmartClient.JXJFoodBigStation.Model.HK_PLC;
- using BPA.Helper;
-
- 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:NotifyBase
- {
- public HKPlcCommMonitorViewModel()
- {
- Type type = typeof(DB_Read);
- PropertyInfo[] properties = type.GetProperties();
- double Offset = 0.0;
- string addressPos = "";
- TaskManage.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<PlcCommAttribute>();
- 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 BPARelayCommand<object>((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 t,string PropertyName)
- {
- Type type = typeof(T);
-
- }*/
- public static ObservableCollection<PlcVarMonitor> PlcInfo { get; set; } = new ObservableCollection<PlcVarMonitor>();
-
- public BPARelayCommand<object> SetValueCommand { get; set; }
- }
- }
|