using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.Input; using BPASmart.Model; using System.Diagnostics; using Microsoft.EntityFrameworkCore; using BPA.Helper; using System.Text.Json.Serialization; using Microsoft.EntityFrameworkCore.Metadata.Internal; using Ubiety.Dns.Core.Records; using System.Windows; using BPA.Communication; using System.Threading; namespace BPASmart.VariableManager.ViewModels { public class VariableConfigViewModel : NoticeBase { private int varialeInfosIndex = -1; ICommunicationDevice DeviceType; public VariableConfigViewModel(string o) { ServiceCenter.GetInstance.Init(); DataInit(o); VarNameChanged(); SaveCommand = new RelayCommand(() => { ServiceCenter.GetInstance.VarSave(); }); GenerateEntityClassCommand = new RelayCommand(() => { GenerateEntityClass(); }); StartMotionCommand = new RelayCommand(() => { switch (ButtonContext) { case "开始监控": TabName = "当前值"; CurrentVisibility = Visibility.Visible; RemoveButVisiblity = Visibility.Collapsed; ButtonContext = "停止监控"; IsEnable = false; Motion(); break; case "停止监控": TabName = "操作"; CurrentVisibility = Visibility.Collapsed; RemoveButVisiblity = Visibility.Visible; ButtonContext = "开始监控"; IsEnable = true; ThreadManage.GetInstance().StopTask($"{DeviceType} 初始化连接"); ThreadManage.GetInstance().StopTask($"{DeviceType} 设备数据采集"); break; default: break; } }); RemoveCommand = new RelayCommand((o) => { if (o != null && o is VariableInfo variable) { varialeInfos.Remove(variable); } for (int i = 0; i < varialeInfos.Count; i++) { varialeInfos.ElementAt(i).ID = i + 1; } }); } private void GenerateEntityClass() { StringBuilder sb = new StringBuilder(); string deviceName = Json.Data.CommunicationDevices.ElementAt(varialeInfosIndex).DeviceName; sb.Append("using System;\r\n"); sb.Append("using System.Text;\r\n"); sb.Append("using System.Linq;\r\n"); sb.Append("namespace ProjectName\r\n"); sb.Append("{\r\n"); sb.Append($"public class {deviceName}\r\n"); sb.Append("{\r\n"); Json.Data.CommunicationDevices.ElementAt(varialeInfosIndex).VarTableModels.ToList().ForEach(item => { if (!string.IsNullOrEmpty(item.VarName)) { sb.Append("/// \r\n"); sb.Append($"/// {item.VarName}\r\n"); sb.Append("/// \r\n"); sb.Append($"public {item.DataType.ToEnum().ToSystemData()} {item.VarName}"); sb.Append("{get;set;}\r\n \r\n"); } }); sb.Append("}\r\n"); sb.Append("}\r\n"); TextHelper.GetInstance.WriteTextInfo(sb.ToString(), $"{deviceName}.cs", "EntityClass"); } private void DataInit(string o) { varialeInfosIndex = Array.FindIndex(Json.Data.CommunicationDevices.ToArray(), p => p.DeviceName == o); if (varialeInfosIndex >= 0 && varialeInfosIndex < Json.Data.CommunicationDevices.Count) { DeviceType = Json.Data.CommunicationDevices.ElementAt(varialeInfosIndex).CommDevice; GlobalVar.DeviceType = DeviceType; varialeInfos = Json.Data.CommunicationDevices.ElementAt(varialeInfosIndex).VarTableModels; if (varialeInfos.Count <= 0) AddRow(); } ComboBoxItemInit(); ActionManage.GetInstance.Register(new Action((p) => { if (p != null && p.Length == 2) { NameWidth = Convert.ToDouble(p[0]); AddressWidth = Convert.ToDouble(p[1]); } }), "TabGridSizeChanged"); } private void VarNameChanged() { DelegationNotifi.GetInstance.VarNameChanged = new Action((p) => { var result = varialeInfos.GroupBy(P => P.VarName).ToList(); if (result != null && result.Count < varialeInfos.Count) { result.ForEach(x => { if (x.Key.Length > 0) { if (x.Count() > 1) { x.ToList().ForEach(item => { int index = Array.FindIndex(varialeInfos.ToArray(), p => p.VarName == item.VarName && p.ID == item.ID); if (index >= 0 && index < varialeInfos.Count) { varialeInfos.ElementAt(index).IsRedundant = true; } }); } else if (x.Count() == 1) { int index = Array.FindIndex(varialeInfos.ToArray(), p => p.VarName == x.ElementAt(0).VarName && p.ID == x.ElementAt(0).ID); if (index >= 0 && index < varialeInfos.Count) { varialeInfos.ElementAt(index).IsRedundant = false; } } } }); } else { for (int i = 0; i < varialeInfos.Count; i++) { varialeInfos.ElementAt(i).IsRedundant = false; } } if ((p - 1) >= 0) { if (varialeInfos?.Count >= p) { if (varialeInfos.ElementAt(p - 1).VarName?.Length > 0) { if (varialeInfos.Count == p) AddRow(); } } } }); } private void Motion() { ThreadManage.GetInstance().Start(new Action(() => { switch (DeviceType) { case BPASmart.Model.ModbusRtu _modbusRtu: break; case BPASmart.Model.ModbusTcp _modbusTcp: BPA.Communication.ModbusTcp modbusTcpMaster = new BPA.Communication.ModbusTcp(); modbusTcpMaster.ConnectOk = new Action(() => { ThreadManage.GetInstance().StartLong(new Action(() => { varialeInfos.GetReadDataModels().ToList()?.ForEach(temp => { Array ResultArray = null; temp.Value?.ForEach(value => { switch (temp.Key) { case EDataType.Bool: ResultArray = modbusTcpMaster.Read(value.StartAddress.ToString(), value.Length)?.Content; break; case EDataType.Byte: break; case EDataType.Int: ResultArray = modbusTcpMaster.Read(value.StartAddress.ToString(), value.Length)?.Content; break; case EDataType.Word: ResultArray = modbusTcpMaster.Read(value.StartAddress.ToString(), value.Length)?.Content; break; case EDataType.Dint: ResultArray = modbusTcpMaster.Read(value.StartAddress.ToString(), value.Length)?.Content; break; case EDataType.Dword: ResultArray = modbusTcpMaster.Read(value.StartAddress.ToString(), value.Length)?.Content; break; case EDataType.Float: ResultArray = modbusTcpMaster.Read(value.StartAddress.ToString(), value.Length)?.Content; break; default: break; } SetValue(ResultArray, value, temp.Key); }); }); Thread.Sleep(100); }), $"{DeviceType} 设备数据采集"); }); modbusTcpMaster.IsReconnect = true; modbusTcpMaster.ModbusTcpConnect(_modbusTcp.IP, _modbusTcp.PortNum); break; case BPASmart.Model.Siemens _siemens: break; default: break; } }), $"{DeviceType} 初始化连接"); } /// /// 添加行 /// private void AddRow() { App.Current.Dispatcher.Invoke(new Action(() => { if (varialeInfosIndex >= 0 && varialeInfosIndex < Json.Data.CommunicationDevices.Count) { varialeInfos.Add(new VariableInfo(DeviceType) { ID = varialeInfos.Count + 1 }); } })); } #region 属性 public double NameWidth { get { return _mNameWidth; } set { _mNameWidth = value; OnPropertyChanged(); } } public static double _mNameWidth; public double AddressWidth { get { return _mAddressWidth; } set { _mAddressWidth = value; OnPropertyChanged(); } } private static double _mAddressWidth; public string TabName { get { return _mTabName; } set { _mTabName = value; OnPropertyChanged(); } } private static string _mTabName = "操作"; public string ButtonContext { get { return _mButtonContext; } set { _mButtonContext = value; OnPropertyChanged(); } } private static string _mButtonContext = "开始监控"; public Visibility CurrentVisibility { get { return _mCurrentVisibility; } set { _mCurrentVisibility = value; OnPropertyChanged(); } } private static Visibility _mCurrentVisibility = Visibility.Collapsed; public Visibility RemoveButVisiblity { get { return _mRemoveButVisiblity; } set { _mRemoveButVisiblity = value; OnPropertyChanged(); } } private static Visibility _mRemoveButVisiblity = Visibility.Visible; public bool IsEnable { get { return _mIsEnable; } set { _mIsEnable = value; OnPropertyChanged(); } } private bool _mIsEnable = true; #endregion #region 数据列表 /// /// 数据类型下拉列表 /// public ObservableCollection dataType { get; set; } = new ObservableCollection(); /// /// 报警设置窗离散量报警类型 /// public ObservableCollection PopupDiscreteAlarmType { get; set; } = new ObservableCollection(); /// /// 变量信息 /// public ObservableCollection varialeInfos { get; set; } #endregion #region Command public RelayCommand SaveCommand { get; set; } public RelayCommand StartMotionCommand { get; set; } public RelayCommand RemoveCommand { get; set; } public RelayCommand GenerateEntityClassCommand { get; set; } #endregion private void SetValue(Array arrays, ReadDataModel readDataModel, EDataType eDataType) { if (arrays != null) { ushort by = eDataType.GetEDataSize(); for (int i = 0; i < arrays.Length; i++) { int varIndex = Array.FindIndex(varialeInfos.ToArray(), p => p.RealAddress == (readDataModel.StartAddress + (i * by)).ToString()); if (varIndex >= 0 && varIndex < varialeInfos.Count) { varialeInfos.ElementAt(varIndex).CurrentValue = arrays.GetValue(i)?.ToString(); } } } } /// /// 下拉列表初始化 /// private void ComboBoxItemInit() { dataType.Clear(); PopupDiscreteAlarmType.Clear(); string[] DataTypeNames = Enum.GetNames(typeof(EDataType)); foreach (var item in DataTypeNames) { dataType.Add(item); } string[] PopupAlarmTypes = Enum.GetNames(typeof(EAlongTriggerType)); foreach (var item in PopupAlarmTypes) { PopupDiscreteAlarmType.Add(item); } } } }