终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

132 строки
5.0 KiB

  1. using BPASmartClient.Helper;
  2. using BPASmartClient.JXJFoodBigStation.Model;
  3. using BPASmartClient.JXJFoodBigStation.Model.HK_PLC;
  4. using Microsoft.Toolkit.Mvvm.ComponentModel;
  5. using Microsoft.Toolkit.Mvvm.Input;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.ComponentModel;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace BPASmartClient.JXJFoodBigStation.ViewModel
  16. {
  17. public class HKPlcCommMonitorViewModel:ObservableObject
  18. {
  19. public HKPlcCommMonitorViewModel()
  20. {
  21. Type type = typeof(DB_Read);
  22. PropertyInfo[] properties = type.GetProperties();
  23. double Offset = 0.0;
  24. string addressPos = "";
  25. ThreadManage.GetInstance().StartLong(new Action(() =>
  26. {
  27. foreach (PropertyInfo mi in properties)
  28. {
  29. PropertyInfo a = GVL_BigStation.HKPlc_Read.GetType().GetProperty(mi.Name);
  30. object ab = a.GetValue(GVL_BigStation.HKPlc_Read, null);
  31. int index = Array.FindIndex(PlcInfo.ToArray(), p => p.Variable == mi.Name);
  32. if (index != -1)
  33. {
  34. PlcInfo.ElementAt(index).NowValue = ab.ToString();
  35. }
  36. }
  37. Thread.Sleep(100);
  38. }), "plc变量读取",true);
  39. if (PlcInfo.Count == 0)
  40. {
  41. foreach (PropertyInfo mi in properties)
  42. {
  43. PropertyInfo a = GVL_BigStation.HKPlc_Read.GetType().GetProperty(mi.Name);
  44. object ab = a.GetValue(GVL_BigStation.HKPlc_Read, null);
  45. if (mi.PropertyType.Name == "Int16")
  46. {
  47. addressPos = "DBW";
  48. Offset = Math.Ceiling(Offset);
  49. if (Offset / 2.0 - Math.Floor(Offset / 2.0) > 0.0)
  50. {
  51. Offset += 1.0;
  52. }
  53. }
  54. else if (mi.PropertyType.Name == "Boolean")
  55. {
  56. addressPos = "DBX";
  57. Offset = Math.Round(Offset, 1);
  58. if ((Offset - Math.Floor(Offset)) >= 0.8)
  59. {
  60. Offset = Math.Ceiling(Offset);
  61. }
  62. }
  63. else if (mi.PropertyType.Name == "Single")
  64. {
  65. addressPos = "DBD";
  66. Offset = Math.Ceiling(Offset);
  67. if (Offset / 2.0 - Math.Floor(Offset / 2.0) > 0.0)
  68. {
  69. Offset += 1.0;
  70. }
  71. }
  72. var num = GVL_BigStation.HKPlc_Read;
  73. var res = a.GetCustomAttribute<PlcCommAttribute>();
  74. string describe = "";
  75. if (res != null)
  76. {
  77. describe = res.Describe;
  78. }
  79. else
  80. {
  81. describe = "";
  82. }
  83. PlcInfo.Add(new PlcVarMonitor()
  84. {
  85. SerialNum = PlcInfo.Count + 1,
  86. Variable = mi.Name,
  87. Address = "DB98." + addressPos + string.Format("{0:N1}", Offset),
  88. Type = mi.PropertyType.Name,
  89. Describe = describe,
  90. NowValue = ab.ToString(),
  91. });
  92. if (mi.PropertyType.Name == "Int16")
  93. {
  94. Offset += 2;
  95. }
  96. else if (mi.PropertyType.Name == "Boolean")
  97. {
  98. Offset += 0.1;
  99. }
  100. else if (mi.PropertyType.Name == "Single")
  101. {
  102. Offset += 4;
  103. }
  104. }
  105. }
  106. SetValueCommand = new RelayCommand<object>((o) =>
  107. {
  108. /*if (o != null && o is String address)
  109. {
  110. int index = Array.FindIndex(PlcInfo.ToArray(), p => p.Address == address);
  111. if (index != -1)
  112. {
  113. if (ProcessControl.GetInstance.HKDevice.IsConnected) ProcessControl.GetInstance.HKDevice.HK_PLC_S7.Write(address, PlcInfo.ElementAt(index).SetValue);
  114. }
  115. }*/
  116. });
  117. }
  118. /*public static string GetPropertyValue<T>( T t,string PropertyName)
  119. {
  120. Type type = typeof(T);
  121. }*/
  122. public static ObservableCollection<PlcVarMonitor> PlcInfo { get; set; } = new ObservableCollection<PlcVarMonitor>();
  123. public RelayCommand<object> SetValueCommand { get; set; }
  124. }
  125. }