AddressFileLoader.cs 7.4 KB

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