|
- 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<object>((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<CommunicationPar>.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<CommunicationPar>.Data.CommunicationDevices.ElementAt(varialeInfosIndex).VarTableModels.ToList().ForEach(item =>
- {
- if (!string.IsNullOrEmpty(item.VarName))
- {
- sb.Append("/// <summary>\r\n");
- sb.Append($"/// {item.VarName}\r\n");
- sb.Append("/// <summary>\r\n");
- sb.Append($"public {item.DataType.ToEnum<EDataType>().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<CommunicationPar>.Data.CommunicationDevices.ToArray(), p => p.DeviceName == o);
- if (varialeInfosIndex >= 0 && varialeInfosIndex < Json<CommunicationPar>.Data.CommunicationDevices.Count)
- {
- DeviceType = Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(varialeInfosIndex).CommDevice;
- GlobalVar.DeviceType = DeviceType;
- varialeInfos = Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(varialeInfosIndex).VarTableModels;
- if (varialeInfos.Count <= 0) AddRow();
- }
- ComboBoxItemInit();
- ActionManage.GetInstance.Register(new Action<object[]>((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<int>((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<bool[]>(value.StartAddress.ToString(), value.Length)?.Content;
- break;
- case EDataType.Byte:
- break;
- case EDataType.Int:
- ResultArray = modbusTcpMaster.Read<short[]>(value.StartAddress.ToString(), value.Length)?.Content;
- break;
- case EDataType.Word:
- ResultArray = modbusTcpMaster.Read<ushort[]>(value.StartAddress.ToString(), value.Length)?.Content;
- break;
- case EDataType.Dint:
- ResultArray = modbusTcpMaster.Read<int[]>(value.StartAddress.ToString(), value.Length)?.Content;
- break;
- case EDataType.Dword:
- ResultArray = modbusTcpMaster.Read<uint[]>(value.StartAddress.ToString(), value.Length)?.Content;
- break;
- case EDataType.Float:
- ResultArray = modbusTcpMaster.Read<float[]>(value.StartAddress.ToString(), value.Length)?.Content;
- break;
- default:
- break;
- }
- SetValue(ResultArray, value, temp.Key);
- });
- });
-
- Thread.Sleep(100);
- }), $"{DeviceType} 设备数据采集");
- });
- modbusTcpMaster.IsReconnect = true;
- modbusTcpMaster.Connect(new ConfigurationOptions()
- {
- IpAddress = _modbusTcp.IP,
- Port = _modbusTcp.PortNum
- });
- break;
- case BPASmart.Model.Siemens _siemens:
- break;
- default:
- break;
- }
- }), $"{DeviceType} 初始化连接");
- }
-
- /// <summary>
- /// 添加行
- /// </summary>
- private void AddRow()
- {
- App.Current.Dispatcher.Invoke(new Action(() =>
- {
- if (varialeInfosIndex >= 0 && varialeInfosIndex < Json<CommunicationPar>.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 数据列表
- /// <summary>
- /// 数据类型下拉列表
- /// </summary>
- public ObservableCollection<string> dataType { get; set; } = new ObservableCollection<string>();
-
- /// <summary>
- /// 报警设置窗离散量报警类型
- /// </summary>
- public ObservableCollection<string> PopupDiscreteAlarmType { get; set; } = new ObservableCollection<string>();
-
- /// <summary>
- /// 变量信息
- /// </summary>
- public ObservableCollection<VariableInfo> varialeInfos { get; set; }
-
- #endregion
-
- #region Command
- public RelayCommand SaveCommand { get; set; }
-
- public RelayCommand StartMotionCommand { get; set; }
-
- public RelayCommand<object> 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();
- }
- }
- }
- }
-
- /// <summary>
- /// 下拉列表初始化
- /// </summary>
- 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); }
- }
-
- }
- }
|