AddressFileLoader.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System.Collections.Concurrent;
  2. namespace ConfigOperator;
  3. public class AddressFileLoader(HardwareAddress addresses)
  4. {
  5. public void Load(string? folder = null)
  6. {
  7. folder ??= Paths.IOListFolder;
  8. if (!DierctoryHelper.GetAllFiles(folder, out List<string>? hardwareFiles) || hardwareFiles is null)
  9. return;
  10. foreach (var item in hardwareFiles)
  11. {
  12. if (!JsonHelper.ReadFile(item, out AddressSaveXML? output) || output is null)
  13. continue;
  14. if (output.Mini8 is null)
  15. continue;
  16. if (output.Channels is null)
  17. continue;
  18. addresses.Mini8sAddress[output.Mini8.Index] = output.Mini8;
  19. ConcurrentDictionary<byte, ChannelAddress> channels = [];
  20. foreach (var channel in output.Channels)
  21. channels[channel.ChannelIndex] = channel;
  22. addresses.Mini8ChannelsAddress[output.Mini8.Index] = channels;
  23. }
  24. }
  25. public void Save(string? folder = null)
  26. {
  27. if (addresses.Mini8ChannelsAddress is null || addresses.Mini8sAddress is null)
  28. return;
  29. folder ??= Paths.IOListFolder;
  30. foreach (var mini8 in addresses.Mini8sAddress)
  31. {
  32. AddressSaveXML saveItem = new()
  33. {
  34. Mini8 = mini8.Value,
  35. Channels = [.. addresses.Mini8ChannelsAddress[mini8.Key].Values]
  36. };
  37. string path = Path.Combine(folder, $"Mini8-{mini8.Value.Index}.json");
  38. JsonHelper.WriteFile(path, saveItem);
  39. }
  40. }
  41. public void LoadPLC(string? path = null, string? path2 = null)
  42. {
  43. path ??= Paths.PLCIOListFolder;
  44. path2 ??= Paths.PLCChannelIOListFolder;
  45. if (!DierctoryHelper.GetAllFiles(path, out List<string>? plcFile) || plcFile is null)
  46. return;
  47. foreach (var item in plcFile)
  48. {
  49. if (!JsonHelper.ReadFile(item, out PLCAddress? output) || output is null)
  50. continue;
  51. addresses.PLCAddress = output;
  52. break;
  53. }
  54. if (!DierctoryHelper.GetAllFiles(path2, out List<string>? plcChannelFile) || plcChannelFile is null)
  55. return;
  56. addresses.PLCChannelsAddresses = [];
  57. foreach (var item in plcChannelFile)
  58. {
  59. if (!JsonHelper.ReadFile(item, out List<ChannelAddressPLC>? output) || output is null)
  60. continue;
  61. byte mini8Index = output.First().Mini8Index;
  62. addresses.PLCChannelsAddresses[mini8Index] = [];
  63. foreach (ChannelAddressPLC channel in output)
  64. {
  65. if (channel is null)
  66. continue;
  67. addresses.PLCChannelsAddresses[mini8Index][channel.ChannelIndex] = channel;
  68. }
  69. }
  70. }
  71. public void SavePLC(string? folder = null, string? folder2 = null)
  72. {
  73. if (addresses.PLCAddress is null || addresses.PLCChannelsAddresses is null)
  74. return;
  75. folder ??= Paths.PLCIOListFolder;
  76. folder2 ??= Paths.PLCChannelIOListFolder;
  77. string path = Path.Combine(folder, $"PLC.json");
  78. JsonHelper.WriteFile(path, addresses.PLCAddress);
  79. foreach (var item in addresses.PLCChannelsAddresses)
  80. {
  81. List<ChannelAddressPLC> Channels = [];
  82. Channels.AddRange(item.Value.Values);
  83. string pathChannel = Path.Combine(folder2, $"PLC_Channel-{item.Key}.json");
  84. JsonHelper.WriteFile(pathChannel, Channels);
  85. }
  86. }
  87. public void Fake()
  88. {
  89. for (byte mini8Index = 1; mini8Index <= 8; mini8Index++)
  90. {
  91. if (mini8Index == 5)
  92. continue;
  93. Mini8Address mini8Address = new()
  94. {
  95. Index = mini8Index,
  96. Address = $"192.168.250.{10 + mini8Index}",
  97. Port = 502
  98. };
  99. addresses.Mini8sAddress.TryAdd(mini8Index, mini8Address);
  100. ConcurrentDictionary<byte, ChannelAddress> channels = [];
  101. for (byte channelIndex = 1; channelIndex <= 16; channelIndex++)
  102. {
  103. ushort move = (ushort)(channelIndex - 1);
  104. ChannelAddress address = new()
  105. {
  106. Mini8Index = mini8Index,
  107. ChannelIndex = channelIndex,
  108. PV = (ushort)(15500 + move * 2),
  109. WorkingOutput = (ushort)(15532 + move * 2),
  110. AutoTuneStatus = (ushort)(15564 + move),
  111. AutoTune_P = (ushort)(15596 + move * 2),
  112. AutoTune_I = (ushort)(15628 + move * 2),
  113. AutoTune_D = (ushort)(15660 + move * 2),
  114. SensorBreakAlarm1 = 15692,
  115. SensorBreakAlarm2 = 15693,
  116. SetPoint = (ushort)(15694 + move * 2),
  117. ActiveTuneSet = (ushort)(15726 + move),
  118. Running_P = (ushort)(15758 + move * 2),
  119. Running_I = (ushort)(15790 + move * 2),
  120. Running_D = (ushort)(15822 + move * 2),
  121. SetpointUpRate = (ushort)(15860 + move * 2),
  122. SetpointDownRate = (ushort)(15892 + move * 2),
  123. Caps = (ushort)(5193 + move * 4),
  124. Floor = (ushort)(5194 + move * 4),
  125. CapsWarning = (ushort)(5195 + move * 4),
  126. FloorWarning = (ushort)(5196 + move * 4),
  127. Inhibit = (ushort)(15924 + move * 2),
  128. ActiveAutoTune = (ushort)(15956 + move * 2),
  129. };
  130. channels[channelIndex] = address;
  131. }
  132. addresses.Mini8ChannelsAddress[mini8Index] = channels;
  133. }
  134. }
  135. public void FakePLC()
  136. {
  137. PLCAddress plcAddress = new()
  138. {
  139. IPAddress = "192.168.250.1",
  140. Port = 9600,
  141. Interval = 1000,
  142. };
  143. addresses.PLCAddress = plcAddress;
  144. for (byte mini8Index = 1; mini8Index <= 8; mini8Index++)
  145. {
  146. if (mini8Index == 5)
  147. continue;
  148. ConcurrentDictionary<byte, ChannelAddressPLC> channels = [];
  149. for (byte channelIndex = 1; channelIndex <= 16; channelIndex++)
  150. {
  151. ushort move = (ushort)(channelIndex - 1);
  152. ChannelAddressPLC address = new()
  153. {
  154. Mini8Index = mini8Index,
  155. ChannelIndex = channelIndex,
  156. PV = string.Empty,
  157. WorkingOutput = string.Empty,
  158. AutoTuneStatus = string.Empty,
  159. AutoTune_P = string.Empty,
  160. AutoTune_I = string.Empty,
  161. AutoTune_D = string.Empty,
  162. SensorBreakAlarm1 = string.Empty,
  163. SensorBreakAlarm2 = string.Empty,
  164. SetPoint = string.Empty,
  165. ActiveTuneSet = string.Empty,
  166. Running_P = string.Empty,
  167. Running_I = string.Empty,
  168. Running_D = string.Empty,
  169. SetpointUpRate = string.Empty,
  170. SetpointDownRate = string.Empty,
  171. Caps = string.Empty,
  172. Floor = string.Empty,
  173. CapsWarning = string.Empty,
  174. FloorWarning = string.Empty,
  175. Inhibit = string.Empty,
  176. ActiveAutoTune = string.Empty,
  177. };
  178. channels[channelIndex] = address;
  179. }
  180. addresses.PLCChannelsAddresses[mini8Index] = channels;
  181. }
  182. }
  183. }
  184. public class AddressSaveXML
  185. {
  186. public Mini8Address? Mini8 { get; set; }
  187. public List<ChannelAddress>? Channels { get; set; }
  188. }