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; } } }