From f7f7f34943f9b37c20f08e952120fee0861d1a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=84=8F=20=E5=BD=AD?= <2417589739@qq.com> Date: Sat, 17 Sep 2022 12:28:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=B8=AD=E5=BF=83=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BPASmart.Model/ExpandMethod.cs | 110 +++++ BPASmart.Server/CommunicationServer.cs | 392 +++++++++--------- .../ViewModels/VariableConfigViewModel.cs | 325 ++++++++------- .../Views/VariableConfig.xaml | 4 +- 4 files changed, 478 insertions(+), 353 deletions(-) create mode 100644 BPASmart.Model/ExpandMethod.cs diff --git a/BPASmart.Model/ExpandMethod.cs b/BPASmart.Model/ExpandMethod.cs new file mode 100644 index 00000000..fdd08064 --- /dev/null +++ b/BPASmart.Model/ExpandMethod.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BPASmart.Model +{ + public static class ExpandMethod + { + /// + /// 获取数据类型的大小 + /// + /// + /// + public static ushort GetEDataSize(this EDataType eDataType) + { + switch (eDataType) + { + case EDataType.Bool: + case EDataType.Byte: + case EDataType.Int: + case EDataType.Word: + return 1; + case EDataType.Dint: + case EDataType.Dword: + case EDataType.Float: + return 2; + case EDataType.Double: + break; + case EDataType.String: + break; + default: + break; + } + return 1; + } + + /// + /// 获取Modbus Tcp 连续变量数据的组对象 + /// + /// + /// + /// + public static List GetDataGroup(this IGrouping variableInfos, int by = 1) + { + List ReturnValue = new List(); + var res = variableInfos?.OrderBy(p => p.RealAddress).ToList(); + List RealAddresss = new List(); + variableInfos.ToList()?.ForEach(item => { if (int.TryParse(item.RealAddress, out int add)) RealAddresss.Add(add); }); + int count = 0; + if (res != null) + { + int address = RealAddresss.Min(); + int startAddress = address; + for (int i = 0; i < res.Count; i++) + { + if (int.TryParse(res.ElementAt(i).RealAddress, out int TempAddress)) + { + if (TempAddress == address) + { + count++; + address += by; + } + else + { + ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); + count = 1; + address = TempAddress + by; + startAddress = TempAddress; + } + } + + } + ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); + } + return ReturnValue; + } + + public static Dictionary> GetReadDataModels(this ObservableCollection varialeInfos) + { + Dictionary> readDataModels = new Dictionary>(); + varialeInfos.GroupBy(p => p.DataType)?.ToList()?.ForEach(tempVar => + { + if (tempVar.Key != null && tempVar.Key.Length > 0) + { + EDataType dataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.Key); + switch (dataType) + { + case EDataType.Bool: + case EDataType.Byte: + case EDataType.Int: + case EDataType.Word: + if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, tempVar.GetDataGroup()); + break; + case EDataType.Dint: + case EDataType.Dword: + case EDataType.Float: + if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, tempVar.GetDataGroup(2)); + break; + default: + break; + } + } + }); + return readDataModels; + } + } +} diff --git a/BPASmart.Server/CommunicationServer.cs b/BPASmart.Server/CommunicationServer.cs index cbe53ec2..59c2b0ba 100644 --- a/BPASmart.Server/CommunicationServer.cs +++ b/BPASmart.Server/CommunicationServer.cs @@ -34,83 +34,83 @@ namespace BPASmart.Server CommunicationDevices.TryAdd(item.DeviceName, modbusTcpMaster); ThreadManage.GetInstance().StartLong(new Action(() => { - GetReadDataModels(item).ToList()?.ForEach(temp => - { - //switch (temp.Key) - //{ - // case EDataType.Bool: - // temp.Value?.ForEach(value => - // { - // //var res = modbusTcpMaster.ReadBool(value.StartAddress.ToString(), value.Length); - // var res = modbusTcpMaster.Read(value.StartAddress.ToString(), value.Length); - // SetValue(res.Content, item.DeviceName, value, 1); - // }); - // break; - // case EDataType.Byte: - // break; - // case EDataType.Int: - // break; - // case EDataType.Word: - // temp.Value?.ForEach(value => - // { - // //var res = modbusTcpMaster.ReadUshort(value.StartAddress.ToString(), value.Length); - // var res = modbusTcpMaster.Read(value.StartAddress.ToString(), value.Length); - // SetValue(res.Content, item.DeviceName, value, 1); - // }); - // break; - // case EDataType.Dint: - // break; - // case EDataType.Dword: - // temp.Value?.ForEach(value => - // { - // //var res = modbusTcpMaster.ReadUint(value.StartAddress.ToString(), value.Length); - // var res = modbusTcpMaster.Read(value.StartAddress.ToString(), value.Length); - // SetValue(res.Content, item.DeviceName, value, 2); - // }); - // break; - // case EDataType.Float: - // temp.Value?.ForEach(value => - // { - // //var res = modbusTcpMaster.ReadFloat(value.StartAddress.ToString(), value.Length); - // var res = modbusTcpMaster.Read(value.StartAddress.ToString(), value.Length); - // SetValue(res.Content, item.DeviceName, value, 2); - // }); - // break; - // default: - // break; - //} + item.VarTableModels.GetReadDataModels().ToList()?.ForEach(temp => + { + //switch (temp.Key) + //{ + // case EDataType.Bool: + // temp.Value?.ForEach(value => + // { + // //var res = modbusTcpMaster.ReadBool(value.StartAddress.ToString(), value.Length); + // var res = modbusTcpMaster.Read(value.StartAddress.ToString(), value.Length); + // SetValue(res.Content, item.DeviceName, value, 1); + // }); + // break; + // case EDataType.Byte: + // break; + // case EDataType.Int: + // break; + // case EDataType.Word: + // temp.Value?.ForEach(value => + // { + // //var res = modbusTcpMaster.ReadUshort(value.StartAddress.ToString(), value.Length); + // var res = modbusTcpMaster.Read(value.StartAddress.ToString(), value.Length); + // SetValue(res.Content, item.DeviceName, value, 1); + // }); + // break; + // case EDataType.Dint: + // break; + // case EDataType.Dword: + // temp.Value?.ForEach(value => + // { + // //var res = modbusTcpMaster.ReadUint(value.StartAddress.ToString(), value.Length); + // var res = modbusTcpMaster.Read(value.StartAddress.ToString(), value.Length); + // SetValue(res.Content, item.DeviceName, value, 2); + // }); + // break; + // case EDataType.Float: + // temp.Value?.ForEach(value => + // { + // //var res = modbusTcpMaster.ReadFloat(value.StartAddress.ToString(), value.Length); + // var res = modbusTcpMaster.Read(value.StartAddress.ToString(), value.Length); + // SetValue(res.Content, item.DeviceName, value, 2); + // }); + // break; + // default: + // break; + //} - 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, item.DeviceName, value, temp.Key); - }); - }); + 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, item.DeviceName, value, temp.Key); + }); + }); Thread.Sleep(100); }), $"{item.DeviceName} 设备数据采集"); @@ -213,69 +213,69 @@ namespace BPASmart.Server } } - private void SetValue(TArray[] arrays, string DeviceName, ReadDataModel readDataModel, ushort by) - { - if (arrays != null) - { - int index = Array.FindIndex(Json.Data.CommunicationDevices.ToArray(), p => p.DeviceName == DeviceName);//获取设备所在集合位置 - if (index >= 0 && index < Json.Data.CommunicationDevices.Count) - { - var tempArray = Json.Data.CommunicationDevices.ElementAt(index).VarTableModels.ToArray(); - for (int i = 0; i < arrays.Length; i++) - { - int varIndex = Array.FindIndex(tempArray, p => p.RealAddress == (readDataModel.StartAddress + (i * by)).ToString()); - if (varIndex >= 0 && varIndex < tempArray.Length) - { - Json.Data.CommunicationDevices.ElementAt(index).VarTableModels.ElementAt(varIndex).CurrentValue = arrays[i].ToString(); - } - } - var Devicename = Json.Data.CommunicationDevices[index].DeviceName; - List reeisDataModels = new List(); - Json.Data.CommunicationDevices[index].VarTableModels.ToList().ForEach(tempVar => - { - if (tempVar.VarName.Length > 0) - { - reeisDataModels.Add(new ReeisDataModel() - { - VarName = tempVar.VarName, - VarVaule = tempVar.CurrentValue, - DataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.DataType) - }); - } - }); - RedisHelper.GetInstance.SetValue($"{Devicename}", reeisDataModels); - } - } - } + //private void SetValue(TArray[] arrays, string DeviceName, ReadDataModel readDataModel, ushort by) + //{ + // if (arrays != null) + // { + // int index = Array.FindIndex(Json.Data.CommunicationDevices.ToArray(), p => p.DeviceName == DeviceName);//获取设备所在集合位置 + // if (index >= 0 && index < Json.Data.CommunicationDevices.Count) + // { + // var tempArray = Json.Data.CommunicationDevices.ElementAt(index).VarTableModels.ToArray(); + // for (int i = 0; i < arrays.Length; i++) + // { + // int varIndex = Array.FindIndex(tempArray, p => p.RealAddress == (readDataModel.StartAddress + (i * by)).ToString()); + // if (varIndex >= 0 && varIndex < tempArray.Length) + // { + // Json.Data.CommunicationDevices.ElementAt(index).VarTableModels.ElementAt(varIndex).CurrentValue = arrays[i].ToString(); + // } + // } + // var Devicename = Json.Data.CommunicationDevices[index].DeviceName; + // List reeisDataModels = new List(); + // Json.Data.CommunicationDevices[index].VarTableModels.ToList().ForEach(tempVar => + // { + // if (tempVar.VarName.Length > 0) + // { + // reeisDataModels.Add(new ReeisDataModel() + // { + // VarName = tempVar.VarName, + // VarVaule = tempVar.CurrentValue, + // DataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.DataType) + // }); + // } + // }); + // RedisHelper.GetInstance.SetValue($"{Devicename}", reeisDataModels); + // } + // } + //} - private ushort GetBySize(EDataType eDataType) - { - switch (eDataType) - { - case EDataType.Bool: - case EDataType.Byte: - case EDataType.Int: - case EDataType.Word: - return 1; - case EDataType.Dint: - case EDataType.Dword: - case EDataType.Float: - return 2; - case EDataType.Double: - break; - case EDataType.String: - break; - default: - break; - } - return 1; - } + //private ushort GetBySize(EDataType eDataType) + //{ + // switch (eDataType) + // { + // case EDataType.Bool: + // case EDataType.Byte: + // case EDataType.Int: + // case EDataType.Word: + // return 1; + // case EDataType.Dint: + // case EDataType.Dword: + // case EDataType.Float: + // return 2; + // case EDataType.Double: + // break; + // case EDataType.String: + // break; + // default: + // break; + // } + // return 1; + //} private void SetValue(Array arrays, string DeviceName, ReadDataModel readDataModel, EDataType eDataType) { if (arrays != null) { - ushort by = GetBySize(eDataType); + ushort by = eDataType.GetEDataSize(); int index = Array.FindIndex(Json.Data.CommunicationDevices.ToArray(), p => p.DeviceName == DeviceName);//获取设备所在集合位置 if (index >= 0 && index < Json.Data.CommunicationDevices.Count) { @@ -309,70 +309,70 @@ namespace BPASmart.Server - private Dictionary> GetReadDataModels(CommunicationModel communicationModel) - { - Dictionary> readDataModels = new Dictionary>(); - communicationModel.VarTableModels.GroupBy(p => p.DataType)?.ToList()?.ForEach(tempVar => - { - if (tempVar.Key != null && tempVar.Key.Length > 0) - { - //int address = tempVar.Min(p => p.RealAddress); - EDataType dataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.Key); - switch (dataType) - { - case EDataType.Bool: - case EDataType.Byte: - case EDataType.Int: - case EDataType.Word: - if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar)); - break; - case EDataType.Dint: - case EDataType.Dword: - case EDataType.Float: - if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar, 2)); - break; - default: - break; - } - } - }); - return readDataModels; - } + //private Dictionary> GetReadDataModels(CommunicationModel communicationModel) + //{ + // Dictionary> readDataModels = new Dictionary>(); + // communicationModel.VarTableModels.GroupBy(p => p.DataType)?.ToList()?.ForEach(tempVar => + // { + // if (tempVar.Key != null && tempVar.Key.Length > 0) + // { + // //int address = tempVar.Min(p => p.RealAddress); + // EDataType dataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.Key); + // switch (dataType) + // { + // case EDataType.Bool: + // case EDataType.Byte: + // case EDataType.Int: + // case EDataType.Word: + // if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar)); + // break; + // case EDataType.Dint: + // case EDataType.Dword: + // case EDataType.Float: + // if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar, 2)); + // break; + // default: + // break; + // } + // } + // }); + // return readDataModels; + //} - private List GetDataGroup(IGrouping variableInfos, int by = 1) - { - List ReturnValue = new List(); - var res = variableInfos?.OrderBy(p => p.RealAddress).ToList(); - List RealAddresss = new List(); - variableInfos.ToList()?.ForEach(item => { if (int.TryParse(item.RealAddress, out int add)) RealAddresss.Add(add); }); - int count = 0; - if (res != null) - { - //int address = variableInfos.Min(p => p.RealAddress); - int address = RealAddresss.Min(); - int startAddress = address; - for (int i = 0; i < res.Count; i++) - { - if (int.TryParse(res.ElementAt(i).RealAddress, out int TempAddress)) - { - if (TempAddress == address) - { - count++; - address += by; - } - else - { - ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); - count = 1; - address = TempAddress + by; - startAddress = TempAddress; - } - } + //private List GetDataGroup(IGrouping variableInfos, int by = 1) + //{ + // List ReturnValue = new List(); + // var res = variableInfos?.OrderBy(p => p.RealAddress).ToList(); + // List RealAddresss = new List(); + // variableInfos.ToList()?.ForEach(item => { if (int.TryParse(item.RealAddress, out int add)) RealAddresss.Add(add); }); + // int count = 0; + // if (res != null) + // { + // //int address = variableInfos.Min(p => p.RealAddress); + // int address = RealAddresss.Min(); + // int startAddress = address; + // for (int i = 0; i < res.Count; i++) + // { + // if (int.TryParse(res.ElementAt(i).RealAddress, out int TempAddress)) + // { + // if (TempAddress == address) + // { + // count++; + // address += by; + // } + // else + // { + // ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); + // count = 1; + // address = TempAddress + by; + // startAddress = TempAddress; + // } + // } - } - ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); - } - return ReturnValue; - } + // } + // ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); + // } + // return ReturnValue; + //} } } \ No newline at end of file diff --git a/BPASmart.VariableManager/ViewModels/VariableConfigViewModel.cs b/BPASmart.VariableManager/ViewModels/VariableConfigViewModel.cs index 780f30a5..6d35a3a0 100644 --- a/BPASmart.VariableManager/ViewModels/VariableConfigViewModel.cs +++ b/BPASmart.VariableManager/ViewModels/VariableConfigViewModel.cs @@ -27,32 +27,33 @@ namespace BPASmart.VariableManager.ViewModels { DataInit(o); VarNameChanged(); + DelegationNotifi.GetInstance.VariableSave = new Action(() => { Json.Save(); }); 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; - } - - }); + { + 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) => { @@ -157,52 +158,42 @@ namespace BPASmart.VariableManager.ViewModels { ThreadManage.GetInstance().StartLong(new Action(() => { - GetReadDataModels().ToList()?.ForEach(temp => + varialeInfos.GetReadDataModels().ToList()?.ForEach(temp => { - switch (temp.Key) + Array ResultArray = null; + temp.Value?.ForEach(value => { - case EDataType.Bool: - temp.Value?.ForEach(value => - { - var res = modbusTcpMaster.ReadCoils(value.StartAddress, value.Length); - SetValue(res, value, 1); - }); - break; - case EDataType.Byte: - break; - case EDataType.Int: - break; - case EDataType.Word: - temp.Value?.ForEach(value => - { - var res = modbusTcpMaster.ReadHoldingRegisters(value.StartAddress, value.Length); - SetValue(res, value, 1); - }); - break; - case EDataType.Dint: - break; - case EDataType.Dword: - temp.Value?.ForEach(value => - { - var res = modbusTcpMaster.ReadHoldingRegisters(value.StartAddress, value.Length); - SetValue(res, value, 2); - }); - break; - case EDataType.Float: - temp.Value?.ForEach(value => - { - var res = modbusTcpMaster.ReadHoldingRegisters(value.StartAddress, value.Length); - SetValue(res, value, 2); - }); - break; - default: - break; - } + 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} 设备数据采集"); - //var DeviceModel = item; }); modbusTcpMaster.IsReconnect = true; modbusTcpMaster.ModbusTcpConnect(_modbusTcp.IP, _modbusTcp.PortNum); @@ -284,109 +275,131 @@ namespace BPASmart.VariableManager.ViewModels public RelayCommand RemoveCommand { get; set; } #endregion - private void SetValue(TArray[] arrays, ReadDataModel readDataModel, ushort by) - { - 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[i].ToString(); - } - } - } + //private void SetValue(TArray[] arrays, ReadDataModel readDataModel, ushort by) + //{ + // 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[i].ToString(); + // } + // } - private Dictionary> GetReadDataModels() + //} + + private void SetValue(Array arrays, ReadDataModel readDataModel, EDataType eDataType) { - Dictionary> readDataModels = new Dictionary>(); - varialeInfos.GroupBy(p => p.DataType)?.ToList()?.ForEach(tempVar => + if (arrays != null) { - if (tempVar.Key != null && tempVar.Key.Length > 0) + ushort by = eDataType.GetEDataSize(); + for (int i = 0; i < arrays.Length; i++) { - //int address = tempVar.Min(p => p.RealAddress); - EDataType dataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.Key); - switch (dataType) + int varIndex = Array.FindIndex(varialeInfos.ToArray(), p => p.RealAddress == (readDataModel.StartAddress + (i * by)).ToString()); + if (varIndex >= 0 && varIndex < varialeInfos.Count) { - case EDataType.Bool: - case EDataType.Byte: - case EDataType.Int: - case EDataType.Word: - if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar)); - break; - case EDataType.Dint: - case EDataType.Dword: - case EDataType.Float: - if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar, 2)); - break; - default: - break; + varialeInfos.ElementAt(varIndex).CurrentValue = arrays.GetValue(i)?.ToString(); } } - }); - return readDataModels; - } - private List GetDataGroup(IGrouping variableInfos, int by = 1) - { - //List ReturnValue = new List(); - //var res = variableInfos?.OrderBy(p => p.RealAddress).ToList(); - //int count = 0; - //if (res != null) - //{ - // int address = variableInfos.Min(p => p.RealAddress); - // int startAddress = address; - // for (int i = 0; i < res.Count; i++) - // { - // if (res.ElementAt(i).RealAddress == address) - // { - // count++; - // address += by; - // } - // else - // { - // ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); - // count = 1; - // address = res.ElementAt(i).RealAddress + by; - // startAddress = res.ElementAt(i).RealAddress; - // } - // } - // ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); - //} - //return ReturnValue; - - List ReturnValue = new List(); - var res = variableInfos?.OrderBy(p => p.RealAddress).ToList(); - List RealAddresss = new List(); - variableInfos.ToList()?.ForEach(item => { if (int.TryParse(item.RealAddress, out int add)) RealAddresss.Add(add); }); - int count = 0; - if (res != null) - { - int address = RealAddresss.Min(); - int startAddress = address; - for (int i = 0; i < res.Count; i++) - { - if (int.TryParse(res.ElementAt(i).RealAddress, out int TempAddress)) - { - if (TempAddress == address) - { - count++; - address += by; - } - else - { - ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); - count = 1; - address = TempAddress + by; - startAddress = TempAddress; - } - } - } - ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); + + + //int index = Array.FindIndex(Json.Data.CommunicationDevices.ToArray(), p => p.DeviceName == DeviceName);//获取设备所在集合位置 + //if (index >= 0 && index < Json.Data.CommunicationDevices.Count) + //{ + // var tempArray = Json.Data.CommunicationDevices.ElementAt(index).VarTableModels.ToArray(); + // for (int i = 0; i < arrays.Length; i++) + // { + // int varIndex = Array.FindIndex(tempArray, p => p.RealAddress == (readDataModel.StartAddress + (i * by)).ToString()); + // if (varIndex >= 0 && varIndex < tempArray.Length) + // { + // Json.Data.CommunicationDevices.ElementAt(index).VarTableModels.ElementAt(varIndex).CurrentValue = arrays.GetValue(i)?.ToString(); + // } + // } + // var Devicename = Json.Data.CommunicationDevices[index].DeviceName; + // List reeisDataModels = new List(); + // Json.Data.CommunicationDevices[index].VarTableModels.ToList().ForEach(tempVar => + // { + // if (tempVar.VarName.Length > 0) + // { + // reeisDataModels.Add(new ReeisDataModel() + // { + // VarName = tempVar.VarName, + // VarVaule = tempVar.CurrentValue, + // DataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.DataType) + // }); + // } + // }); + // RedisHelper.GetInstance.SetValue($"{Devicename}", reeisDataModels); + //} } - return ReturnValue; } + //private Dictionary> GetReadDataModels() + //{ + // Dictionary> readDataModels = new Dictionary>(); + // varialeInfos.GroupBy(p => p.DataType)?.ToList()?.ForEach(tempVar => + // { + // if (tempVar.Key != null && tempVar.Key.Length > 0) + // { + // EDataType dataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.Key); + // switch (dataType) + // { + // case EDataType.Bool: + // case EDataType.Byte: + // case EDataType.Int: + // case EDataType.Word: + // if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar)); + // break; + // case EDataType.Dint: + // case EDataType.Dword: + // case EDataType.Float: + // if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar, 2)); + // break; + // default: + // break; + // } + // } + // }); + // return readDataModels; + //} + + //private List GetDataGroup(IGrouping variableInfos, int by = 1) + //{ + // List ReturnValue = new List(); + // var res = variableInfos?.OrderBy(p => p.RealAddress).ToList(); + // List RealAddresss = new List(); + // variableInfos.ToList()?.ForEach(item => { if (int.TryParse(item.RealAddress, out int add)) RealAddresss.Add(add); }); + // int count = 0; + // if (res != null) + // { + // int address = RealAddresss.Min(); + // int startAddress = address; + // for (int i = 0; i < res.Count; i++) + // { + // if (int.TryParse(res.ElementAt(i).RealAddress, out int TempAddress)) + // { + // if (TempAddress == address) + // { + // count++; + // address += by; + // } + // else + // { + // ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); + // count = 1; + // address = TempAddress + by; + // startAddress = TempAddress; + // } + // } + + // } + // ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); + // } + // return ReturnValue; + //} + /// diff --git a/BPASmart.VariableManager/Views/VariableConfig.xaml b/BPASmart.VariableManager/Views/VariableConfig.xaml index bf1ddc19..d7dd36cd 100644 --- a/BPASmart.VariableManager/Views/VariableConfig.xaml +++ b/BPASmart.VariableManager/Views/VariableConfig.xaml @@ -248,7 +248,7 @@ - + @@ -274,6 +274,7 @@