VRBoardNozzleConfig.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace VaporRecoveryOnlineWatchHubApp.Config
  7. {
  8. public class VRBoardNozzleConfig
  9. {
  10. public int SiteLevelNozzleId { get; set; }
  11. public int SiteLevelDispenserId { get; set; }
  12. public string SiteLevelDispenserDescription { get; set; }
  13. public byte? DataCollectorNozzleNumber { get; set; }
  14. public int? DataCollectorDeviceId { get; set; }
  15. public override string ToString()
  16. {
  17. return GetType().GetProperties().Aggregate(string.Empty,
  18. (result, next) => result + $"{next.Name} : {(next.GetValue(this)?.ToString()) ?? string.Empty} ");
  19. }
  20. }
  21. public class VRBoardNozzleConfigs
  22. {
  23. public List<VRBoardNozzleConfig> data { get; set; } = new List<VRBoardNozzleConfig>();
  24. public VRBoardNozzleConfig DeviceInfoToSiteNozzleConfig(int deviceId, int addressNo) =>
  25. data.Where(c => c.DataCollectorDeviceId == deviceId && c.DataCollectorNozzleNumber == addressNo).
  26. FirstOrDefault();
  27. public int? DeviceInfoToSiteNozzleId(int deviceId, int addressNo) =>
  28. data.Where(c => c.DataCollectorDeviceId == deviceId && c.DataCollectorNozzleNumber == addressNo).
  29. FirstOrDefault()?.SiteLevelNozzleId;
  30. public List<int> DeviceInfoToSiteNozzleIdList(int deviceId) =>
  31. data.Where(c => c.DataCollectorDeviceId == deviceId).Select(c => c.SiteLevelNozzleId).ToList();
  32. public int? DeviceInfoToSiteDispenserId(int deviceId, int addressNo) =>
  33. data.Where(c => c.DataCollectorDeviceId == deviceId && c.DataCollectorNozzleNumber == addressNo).
  34. FirstOrDefault()?.SiteLevelDispenserId;
  35. public string DeviceInfoToSiteDispenserDescription(int deviceId, int addressNo) =>
  36. data.Where(c => c.DataCollectorDeviceId == deviceId && c.DataCollectorNozzleNumber == addressNo).
  37. FirstOrDefault()?.SiteLevelDispenserDescription;
  38. public VRBoardNozzleConfig SiteNozzleIdToConfig(int siteNozzleId) =>
  39. data.Where(c => c.SiteLevelNozzleId == siteNozzleId).FirstOrDefault();
  40. }
  41. //public static class NozzleConfigsExtension
  42. //{
  43. // public static VRBoardNozzleConfigs Sort(this VRBoardNozzleConfigs self)
  44. // {
  45. // self.data = self.data.OrderBy(x => x.SiteLevelNozzleId).ToList();
  46. // return self;
  47. // }
  48. // public static VRBoardNozzleConfigs CreateNozzleConfigs(this VRBoardNozzleConfigs nozzleConfigs, string nozzleDeviceMapping, string nozzleDispenserMapping)
  49. // {
  50. // var configs = nozzleConfigs.data;
  51. // var groupDelimeter = ";";
  52. // var elementDelimeter = ":";
  53. // if (!string.IsNullOrEmpty(nozzleDispenserMapping))
  54. // {
  55. // var mappings = nozzleDispenserMapping.Split(groupDelimeter);
  56. // if (mappings.Length >= 0)
  57. // {
  58. // foreach (var m in mappings)
  59. // {
  60. // // format of mapping should like 1号油机:1:1,2,3,4 which represents dispenser description:dispenser id:site nozzle list
  61. // var mapping = m.Split(elementDelimeter);
  62. // if (mapping.Length == 3 &&
  63. // int.TryParse(mapping[1], out int dispenserId))
  64. // {
  65. // var dispenserDesc = mapping[0];
  66. // var nozzleList = mapping[2].Split(",");
  67. // foreach (var n in nozzleList)
  68. // {
  69. // if (int.TryParse(n, out int nozzleId))
  70. // {
  71. // var nozzleConfig = new VRBoardNozzleConfig
  72. // {
  73. // SiteLevelDispenserDescription = dispenserDesc,
  74. // SiteLevelDispenserId = dispenserId,
  75. // SiteLevelNozzleId = nozzleId
  76. // };
  77. // if (configs.Where(i => i.SiteLevelNozzleId == nozzleId).FirstOrDefault() == null)
  78. // {
  79. // configs.Add(nozzleConfig);
  80. // }
  81. // else
  82. // {
  83. // throw new ArgumentNullException($"Duplicated nozzle id {nozzleId} found");
  84. // }
  85. // }
  86. // }
  87. // }
  88. // }
  89. // }
  90. // }
  91. // if (!string.IsNullOrEmpty(nozzleDeviceMapping))
  92. // {
  93. // var mappings = nozzleDeviceMapping.Split(groupDelimeter);
  94. // if (mappings.Length >= 0)
  95. // {
  96. // foreach (var m in mappings)
  97. // {
  98. // // format of mapping should like 1:1:1 which represents deviceId:addressNo:siteNozzleNo
  99. // var mapping = m.Split(elementDelimeter);
  100. // if (mapping.Length == 3 &&
  101. // int.TryParse(mapping[0], out int deviceId) &&
  102. // byte.TryParse(mapping[1], out byte addressNo) &&
  103. // int.TryParse(mapping[2], out int siteNozzleNo))
  104. // {
  105. // configs.ForEach(x =>
  106. // {
  107. // if (x.SiteLevelNozzleId == siteNozzleNo)
  108. // {
  109. // x.DataCollectorDeviceId = deviceId;
  110. // x.DataCollectorNozzleNumber = addressNo;
  111. // }
  112. // });
  113. // }
  114. // }
  115. // }
  116. // }
  117. // return nozzleConfigs.Sort();
  118. // }
  119. //}
  120. }