终端一体化运控平台
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

345 Zeilen
14 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Microsoft.Toolkit.Mvvm.ComponentModel;
  8. using Microsoft.Toolkit.Mvvm.Input;
  9. using BPASmart.Model;
  10. using System.Diagnostics;
  11. using Microsoft.EntityFrameworkCore;
  12. using BPA.Helper;
  13. using System.Text.Json.Serialization;
  14. using Microsoft.EntityFrameworkCore.Metadata.Internal;
  15. using Ubiety.Dns.Core.Records;
  16. using System.Windows;
  17. using BPA.Communication;
  18. using System.Threading;
  19. namespace BPASmart.VariableManager.ViewModels
  20. {
  21. public class VariableConfigViewModel : NoticeBase
  22. {
  23. private int varialeInfosIndex = -1;
  24. ICommunicationDevice DeviceType;
  25. public VariableConfigViewModel(string o)
  26. {
  27. ServiceCenter.GetInstance.Init();
  28. DataInit(o);
  29. VarNameChanged();
  30. SaveCommand = new RelayCommand(() => { ServiceCenter.GetInstance.VarSave(); });
  31. GenerateEntityClassCommand = new RelayCommand(() => { GenerateEntityClass(); });
  32. StartMotionCommand = new RelayCommand(() =>
  33. {
  34. switch (ButtonContext)
  35. {
  36. case "开始监控":
  37. TabName = "当前值";
  38. CurrentVisibility = Visibility.Visible;
  39. RemoveButVisiblity = Visibility.Collapsed;
  40. ButtonContext = "停止监控";
  41. IsEnable = false;
  42. Motion();
  43. break;
  44. case "停止监控":
  45. TabName = "操作";
  46. CurrentVisibility = Visibility.Collapsed;
  47. RemoveButVisiblity = Visibility.Visible;
  48. ButtonContext = "开始监控";
  49. IsEnable = true;
  50. ThreadManage.GetInstance().StopTask($"{DeviceType} 初始化连接");
  51. ThreadManage.GetInstance().StopTask($"{DeviceType} 设备数据采集");
  52. break;
  53. default:
  54. break;
  55. }
  56. });
  57. RemoveCommand = new RelayCommand<object>((o) =>
  58. {
  59. if (o != null && o is VariableInfo variable)
  60. {
  61. varialeInfos.Remove(variable);
  62. }
  63. for (int i = 0; i < varialeInfos.Count; i++)
  64. {
  65. varialeInfos.ElementAt(i).ID = i + 1;
  66. }
  67. });
  68. }
  69. private void GenerateEntityClass()
  70. {
  71. StringBuilder sb = new StringBuilder();
  72. string deviceName = Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(varialeInfosIndex).DeviceName;
  73. sb.Append("using System;\r\n");
  74. sb.Append("using System.Text;\r\n");
  75. sb.Append("using System.Linq;\r\n");
  76. sb.Append("namespace ProjectName\r\n");
  77. sb.Append("{\r\n");
  78. sb.Append($"public class {deviceName}\r\n");
  79. sb.Append("{\r\n");
  80. Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(varialeInfosIndex).VarTableModels.ToList().ForEach(item =>
  81. {
  82. if (!string.IsNullOrEmpty(item.VarName))
  83. {
  84. sb.Append("/// <summary>\r\n");
  85. sb.Append($"/// {item.VarName}\r\n");
  86. sb.Append("/// <summary>\r\n");
  87. sb.Append($"public {item.DataType.ToEnum<EDataType>().ToSystemData()} {item.VarName}");
  88. sb.Append("{get;set;}\r\n \r\n");
  89. }
  90. });
  91. sb.Append("}\r\n");
  92. sb.Append("}\r\n");
  93. TextHelper.GetInstance.WriteTextInfo(sb.ToString(), $"{deviceName}.cs", "EntityClass");
  94. }
  95. private void DataInit(string o)
  96. {
  97. varialeInfosIndex = Array.FindIndex(Json<CommunicationPar>.Data.CommunicationDevices.ToArray(), p => p.DeviceName == o);
  98. if (varialeInfosIndex >= 0 && varialeInfosIndex < Json<CommunicationPar>.Data.CommunicationDevices.Count)
  99. {
  100. DeviceType = Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(varialeInfosIndex).CommDevice;
  101. GlobalVar.DeviceType = DeviceType;
  102. varialeInfos = Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(varialeInfosIndex).VarTableModels;
  103. if (varialeInfos.Count <= 0) AddRow();
  104. }
  105. ComboBoxItemInit();
  106. ActionManage.GetInstance.Register(new Action<object[]>((p) =>
  107. {
  108. if (p != null && p.Length == 2)
  109. {
  110. NameWidth = Convert.ToDouble(p[0]);
  111. AddressWidth = Convert.ToDouble(p[1]);
  112. }
  113. }), "TabGridSizeChanged");
  114. }
  115. private void VarNameChanged()
  116. {
  117. DelegationNotifi.GetInstance.VarNameChanged = new Action<int>((p) =>
  118. {
  119. var result = varialeInfos.GroupBy(P => P.VarName).ToList();
  120. if (result != null && result.Count < varialeInfos.Count)
  121. {
  122. result.ForEach(x =>
  123. {
  124. if (x.Key.Length > 0)
  125. {
  126. if (x.Count() > 1)
  127. {
  128. x.ToList().ForEach(item =>
  129. {
  130. int index = Array.FindIndex(varialeInfos.ToArray(), p => p.VarName == item.VarName && p.ID == item.ID);
  131. if (index >= 0 && index < varialeInfos.Count)
  132. {
  133. varialeInfos.ElementAt(index).IsRedundant = true;
  134. }
  135. });
  136. }
  137. else if (x.Count() == 1)
  138. {
  139. int index = Array.FindIndex(varialeInfos.ToArray(), p => p.VarName == x.ElementAt(0).VarName && p.ID == x.ElementAt(0).ID);
  140. if (index >= 0 && index < varialeInfos.Count)
  141. {
  142. varialeInfos.ElementAt(index).IsRedundant = false;
  143. }
  144. }
  145. }
  146. });
  147. }
  148. else
  149. {
  150. for (int i = 0; i < varialeInfos.Count; i++)
  151. {
  152. varialeInfos.ElementAt(i).IsRedundant = false;
  153. }
  154. }
  155. if ((p - 1) >= 0)
  156. {
  157. if (varialeInfos?.Count >= p)
  158. {
  159. if (varialeInfos.ElementAt(p - 1).VarName?.Length > 0)
  160. {
  161. if (varialeInfos.Count == p) AddRow();
  162. }
  163. }
  164. }
  165. });
  166. }
  167. private void Motion()
  168. {
  169. ThreadManage.GetInstance().Start(new Action(() =>
  170. {
  171. switch (DeviceType)
  172. {
  173. case BPASmart.Model.ModbusRtu _modbusRtu:
  174. break;
  175. case BPASmart.Model.ModbusTcp _modbusTcp:
  176. BPA.Communication.ModbusTcp modbusTcpMaster = new BPA.Communication.ModbusTcp();
  177. modbusTcpMaster.ConnectOk = new Action(() =>
  178. {
  179. ThreadManage.GetInstance().StartLong(new Action(() =>
  180. {
  181. varialeInfos.GetReadDataModels().ToList()?.ForEach(temp =>
  182. {
  183. Array ResultArray = null;
  184. temp.Value?.ForEach(value =>
  185. {
  186. switch (temp.Key)
  187. {
  188. case EDataType.Bool:
  189. ResultArray = modbusTcpMaster.Read<bool[]>(value.StartAddress.ToString(), value.Length)?.Content;
  190. break;
  191. case EDataType.Byte:
  192. break;
  193. case EDataType.Int:
  194. ResultArray = modbusTcpMaster.Read<short[]>(value.StartAddress.ToString(), value.Length)?.Content;
  195. break;
  196. case EDataType.Word:
  197. ResultArray = modbusTcpMaster.Read<ushort[]>(value.StartAddress.ToString(), value.Length)?.Content;
  198. break;
  199. case EDataType.Dint:
  200. ResultArray = modbusTcpMaster.Read<int[]>(value.StartAddress.ToString(), value.Length)?.Content;
  201. break;
  202. case EDataType.Dword:
  203. ResultArray = modbusTcpMaster.Read<uint[]>(value.StartAddress.ToString(), value.Length)?.Content;
  204. break;
  205. case EDataType.Float:
  206. ResultArray = modbusTcpMaster.Read<float[]>(value.StartAddress.ToString(), value.Length)?.Content;
  207. break;
  208. default:
  209. break;
  210. }
  211. SetValue(ResultArray, value, temp.Key);
  212. });
  213. });
  214. Thread.Sleep(100);
  215. }), $"{DeviceType} 设备数据采集");
  216. });
  217. modbusTcpMaster.IsReconnect = true;
  218. modbusTcpMaster.ModbusTcpConnect(_modbusTcp.IP, _modbusTcp.PortNum);
  219. break;
  220. case BPASmart.Model.Siemens _siemens:
  221. break;
  222. default:
  223. break;
  224. }
  225. }), $"{DeviceType} 初始化连接");
  226. }
  227. /// <summary>
  228. /// 添加行
  229. /// </summary>
  230. private void AddRow()
  231. {
  232. App.Current.Dispatcher.Invoke(new Action(() =>
  233. {
  234. if (varialeInfosIndex >= 0 && varialeInfosIndex < Json<CommunicationPar>.Data.CommunicationDevices.Count)
  235. {
  236. varialeInfos.Add(new VariableInfo(DeviceType) { ID = varialeInfos.Count + 1 });
  237. }
  238. }));
  239. }
  240. #region 属性
  241. public double NameWidth { get { return _mNameWidth; } set { _mNameWidth = value; OnPropertyChanged(); } }
  242. public static double _mNameWidth;
  243. public double AddressWidth { get { return _mAddressWidth; } set { _mAddressWidth = value; OnPropertyChanged(); } }
  244. private static double _mAddressWidth;
  245. public string TabName { get { return _mTabName; } set { _mTabName = value; OnPropertyChanged(); } }
  246. private static string _mTabName = "操作";
  247. public string ButtonContext { get { return _mButtonContext; } set { _mButtonContext = value; OnPropertyChanged(); } }
  248. private static string _mButtonContext = "开始监控";
  249. public Visibility CurrentVisibility { get { return _mCurrentVisibility; } set { _mCurrentVisibility = value; OnPropertyChanged(); } }
  250. private static Visibility _mCurrentVisibility = Visibility.Collapsed;
  251. public Visibility RemoveButVisiblity { get { return _mRemoveButVisiblity; } set { _mRemoveButVisiblity = value; OnPropertyChanged(); } }
  252. private static Visibility _mRemoveButVisiblity = Visibility.Visible;
  253. public bool IsEnable { get { return _mIsEnable; } set { _mIsEnable = value; OnPropertyChanged(); } }
  254. private bool _mIsEnable = true;
  255. #endregion
  256. #region 数据列表
  257. /// <summary>
  258. /// 数据类型下拉列表
  259. /// </summary>
  260. public ObservableCollection<string> dataType { get; set; } = new ObservableCollection<string>();
  261. /// <summary>
  262. /// 报警设置窗离散量报警类型
  263. /// </summary>
  264. public ObservableCollection<string> PopupDiscreteAlarmType { get; set; } = new ObservableCollection<string>();
  265. /// <summary>
  266. /// 变量信息
  267. /// </summary>
  268. public ObservableCollection<VariableInfo> varialeInfos { get; set; }
  269. #endregion
  270. #region Command
  271. public RelayCommand SaveCommand { get; set; }
  272. public RelayCommand StartMotionCommand { get; set; }
  273. public RelayCommand<object> RemoveCommand { get; set; }
  274. public RelayCommand GenerateEntityClassCommand { get; set; }
  275. #endregion
  276. private void SetValue(Array arrays, ReadDataModel readDataModel, EDataType eDataType)
  277. {
  278. if (arrays != null)
  279. {
  280. ushort by = eDataType.GetEDataSize();
  281. for (int i = 0; i < arrays.Length; i++)
  282. {
  283. int varIndex = Array.FindIndex(varialeInfos.ToArray(), p => p.RealAddress == (readDataModel.StartAddress + (i * by)).ToString());
  284. if (varIndex >= 0 && varIndex < varialeInfos.Count)
  285. {
  286. varialeInfos.ElementAt(varIndex).CurrentValue = arrays.GetValue(i)?.ToString();
  287. }
  288. }
  289. }
  290. }
  291. /// <summary>
  292. /// 下拉列表初始化
  293. /// </summary>
  294. private void ComboBoxItemInit()
  295. {
  296. dataType.Clear();
  297. PopupDiscreteAlarmType.Clear();
  298. string[] DataTypeNames = Enum.GetNames(typeof(EDataType));
  299. foreach (var item in DataTypeNames) { dataType.Add(item); }
  300. string[] PopupAlarmTypes = Enum.GetNames(typeof(EAlongTriggerType));
  301. foreach (var item in PopupAlarmTypes) { PopupDiscreteAlarmType.Add(item); }
  302. }
  303. }
  304. }