终端一体化运控平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

349 rindas
15 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.Connect(new ConfigurationOptions()
  219. {
  220. IpAddress = _modbusTcp.IP,
  221. Port = _modbusTcp.PortNum
  222. });
  223. break;
  224. case BPASmart.Model.Siemens _siemens:
  225. break;
  226. default:
  227. break;
  228. }
  229. }), $"{DeviceType} 初始化连接");
  230. }
  231. /// <summary>
  232. /// 添加行
  233. /// </summary>
  234. private void AddRow()
  235. {
  236. App.Current.Dispatcher.Invoke(new Action(() =>
  237. {
  238. if (varialeInfosIndex >= 0 && varialeInfosIndex < Json<CommunicationPar>.Data.CommunicationDevices.Count)
  239. {
  240. varialeInfos.Add(new VariableInfo(DeviceType) { ID = varialeInfos.Count + 1 });
  241. }
  242. }));
  243. }
  244. #region 属性
  245. public double NameWidth { get { return _mNameWidth; } set { _mNameWidth = value; OnPropertyChanged(); } }
  246. public static double _mNameWidth;
  247. public double AddressWidth { get { return _mAddressWidth; } set { _mAddressWidth = value; OnPropertyChanged(); } }
  248. private static double _mAddressWidth;
  249. public string TabName { get { return _mTabName; } set { _mTabName = value; OnPropertyChanged(); } }
  250. private static string _mTabName = "操作";
  251. public string ButtonContext { get { return _mButtonContext; } set { _mButtonContext = value; OnPropertyChanged(); } }
  252. private static string _mButtonContext = "开始监控";
  253. public Visibility CurrentVisibility { get { return _mCurrentVisibility; } set { _mCurrentVisibility = value; OnPropertyChanged(); } }
  254. private static Visibility _mCurrentVisibility = Visibility.Collapsed;
  255. public Visibility RemoveButVisiblity { get { return _mRemoveButVisiblity; } set { _mRemoveButVisiblity = value; OnPropertyChanged(); } }
  256. private static Visibility _mRemoveButVisiblity = Visibility.Visible;
  257. public bool IsEnable { get { return _mIsEnable; } set { _mIsEnable = value; OnPropertyChanged(); } }
  258. private bool _mIsEnable = true;
  259. #endregion
  260. #region 数据列表
  261. /// <summary>
  262. /// 数据类型下拉列表
  263. /// </summary>
  264. public ObservableCollection<string> dataType { get; set; } = new ObservableCollection<string>();
  265. /// <summary>
  266. /// 报警设置窗离散量报警类型
  267. /// </summary>
  268. public ObservableCollection<string> PopupDiscreteAlarmType { get; set; } = new ObservableCollection<string>();
  269. /// <summary>
  270. /// 变量信息
  271. /// </summary>
  272. public ObservableCollection<VariableInfo> varialeInfos { get; set; }
  273. #endregion
  274. #region Command
  275. public RelayCommand SaveCommand { get; set; }
  276. public RelayCommand StartMotionCommand { get; set; }
  277. public RelayCommand<object> RemoveCommand { get; set; }
  278. public RelayCommand GenerateEntityClassCommand { get; set; }
  279. #endregion
  280. private void SetValue(Array arrays, ReadDataModel readDataModel, EDataType eDataType)
  281. {
  282. if (arrays != null)
  283. {
  284. ushort by = eDataType.GetEDataSize();
  285. for (int i = 0; i < arrays.Length; i++)
  286. {
  287. int varIndex = Array.FindIndex(varialeInfos.ToArray(), p => p.RealAddress == (readDataModel.StartAddress + (i * by)).ToString());
  288. if (varIndex >= 0 && varIndex < varialeInfos.Count)
  289. {
  290. varialeInfos.ElementAt(varIndex).CurrentValue = arrays.GetValue(i)?.ToString();
  291. }
  292. }
  293. }
  294. }
  295. /// <summary>
  296. /// 下拉列表初始化
  297. /// </summary>
  298. private void ComboBoxItemInit()
  299. {
  300. dataType.Clear();
  301. PopupDiscreteAlarmType.Clear();
  302. string[] DataTypeNames = Enum.GetNames(typeof(EDataType));
  303. foreach (var item in DataTypeNames) { dataType.Add(item); }
  304. string[] PopupAlarmTypes = Enum.GetNames(typeof(EAlongTriggerType));
  305. foreach (var item in PopupAlarmTypes) { PopupDiscreteAlarmType.Add(item); }
  306. }
  307. }
  308. }