终端一体化运控平台
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.
 
 
 

130 lines
4.9 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. namespace BPASmart.Model
  8. {
  9. public static class ExpandMethod
  10. {
  11. /// <summary>
  12. /// 获取数据类型的大小
  13. /// </summary>
  14. /// <param name="eDataType"></param>
  15. /// <returns></returns>
  16. public static ushort GetEDataSize(this EDataType eDataType)
  17. {
  18. switch (eDataType)
  19. {
  20. case EDataType.Bool:
  21. case EDataType.Byte:
  22. case EDataType.Int:
  23. case EDataType.Word:
  24. return 1;
  25. case EDataType.Dint:
  26. case EDataType.Dword:
  27. case EDataType.Float:
  28. return 2;
  29. case EDataType.Double:
  30. break;
  31. case EDataType.String:
  32. break;
  33. default:
  34. break;
  35. }
  36. return 1;
  37. }
  38. public static string ToSystemData(this EDataType eData)
  39. {
  40. switch (eData)
  41. {
  42. case EDataType.Bool: return "bool";
  43. case EDataType.Byte: return "byte";
  44. case EDataType.Int: return "short";
  45. case EDataType.Word: return "ushort";
  46. case EDataType.Dint: return "int";
  47. case EDataType.Dword: return "uint";
  48. case EDataType.Float: return "float";
  49. case EDataType.Double: return "double";
  50. case EDataType.String: return "string";
  51. default:
  52. break;
  53. }
  54. return default;
  55. }
  56. /// <summary>
  57. /// 获取Modbus Tcp 连续变量数据的组对象
  58. /// </summary>
  59. /// <param name="variableInfos"></param>
  60. /// <param name="by"></param>
  61. /// <returns></returns>
  62. public static List<ReadDataModel> GetDataGroup(this IGrouping<string, VariableInfo> variableInfos, int by = 1)
  63. {
  64. List<ReadDataModel> ReturnValue = new List<ReadDataModel>();
  65. var res = variableInfos?.OrderBy(p => p.RealAddress).ToList();
  66. List<int> RealAddresss = new List<int>();
  67. variableInfos.ToList()?.ForEach(item => { if (int.TryParse(item.RealAddress, out int add)) RealAddresss.Add(add); });
  68. int count = 0;
  69. if (res != null)
  70. {
  71. int address = RealAddresss.Min();
  72. int startAddress = address;
  73. for (int i = 0; i < res.Count; i++)
  74. {
  75. if (int.TryParse(res.ElementAt(i).RealAddress, out int TempAddress))
  76. {
  77. if (TempAddress == address)
  78. {
  79. count++;
  80. address += by;
  81. }
  82. else
  83. {
  84. ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count });
  85. count = 1;
  86. address = TempAddress + by;
  87. startAddress = TempAddress;
  88. }
  89. }
  90. }
  91. ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count });
  92. }
  93. return ReturnValue;
  94. }
  95. public static Dictionary<EDataType, List<ReadDataModel>> GetReadDataModels(this ObservableCollection<VariableInfo> varialeInfos)
  96. {
  97. Dictionary<EDataType, List<ReadDataModel>> readDataModels = new Dictionary<EDataType, List<ReadDataModel>>();
  98. varialeInfos.GroupBy(p => p.DataType)?.ToList()?.ForEach(tempVar =>
  99. {
  100. if (tempVar.Key != null && tempVar.Key.Length > 0)
  101. {
  102. EDataType dataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.Key);
  103. switch (dataType)
  104. {
  105. case EDataType.Bool:
  106. case EDataType.Byte:
  107. case EDataType.Int:
  108. case EDataType.Word:
  109. if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, tempVar.GetDataGroup());
  110. break;
  111. case EDataType.Dint:
  112. case EDataType.Dword:
  113. case EDataType.Float:
  114. if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, tempVar.GetDataGroup(2));
  115. break;
  116. default:
  117. break;
  118. }
  119. }
  120. });
  121. return readDataModels;
  122. }
  123. }
  124. }