终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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