ConfigUpdater.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. namespace MinicsConsole.Helper;
  2. public class ConfigUpdater(Hardwares hardwares, ConfigFiles configFiles, HardWareMonitor hardWareMonitor, ILog log)
  3. {
  4. public bool SetConfigFile(TemperatureConfig file, bool sendToMini8, out Dictionary<byte, Dictionary<byte, bool>> sendResult)
  5. {
  6. sendResult = [];
  7. if (Checker.IsNull(file, file.Mini8sConfig))
  8. return false;
  9. if (!sendToMini8)
  10. goto UpdateConfigInMemory;
  11. //Send to Mini8
  12. foreach (KeyValuePair<byte, Mini8Config> mini8 in file!.Mini8sConfig!)
  13. {
  14. sendResult[mini8.Key] = [];
  15. if (mini8.Value.ChannelConfig is null)
  16. continue;
  17. if (!hardwares.Mini8s.TryGetValue(mini8.Key, out Mini8Data? mini8Data) || mini8Data is null)
  18. {
  19. log.Warning($"SetConfigFile - mini8 {mini8.Key} Not Found");
  20. continue;
  21. }
  22. if (mini8Data.IsLocked)
  23. {
  24. log.Warning($"SetConfigFile - try switch config while mini8 {mini8.Key} is locked");
  25. continue;
  26. }
  27. if (!hardWareMonitor.Mini8Communicators.TryGetValueNotNull(mini8.Key, out IMini8Communicator communicator))
  28. {
  29. log.Warning($"SetConfigFile - try switch config while mini8 {mini8.Key} communicator not found");
  30. continue;
  31. }
  32. foreach (KeyValuePair<byte, ChannelConfig> channel in mini8.Value.ChannelConfig)
  33. {
  34. //if channel is disabled or not in use, whether sending other Mini8Input needed to be confirmed
  35. if (channel.Value.Inhibit == Inhibit.Disable ||
  36. channel.Value.ChannelMode == ChannelMode.UnUsed)
  37. {
  38. communicator.EnableChannel(channel.Key, Inhibit.Disable);
  39. continue;
  40. }
  41. Mini8Input input = new();
  42. channel.Value.Adapt(input);
  43. if (!communicator.SendMini8Data(channel.Key, input) ||
  44. !communicator.EnableChannel(channel.Key, channel.Value.Inhibit))
  45. {
  46. sendResult[mini8.Key][channel.Key] = false;
  47. continue;
  48. }
  49. sendResult[mini8.Key][channel.Key] = true;
  50. }
  51. }
  52. //Update Memory Mini8 Channels
  53. UpdateConfigInMemory:
  54. foreach (KeyValuePair<byte, Mini8Config> mini8 in file!.Mini8sConfig!)
  55. {
  56. if (mini8.Value is null || mini8.Value.ChannelConfig is null)
  57. continue;
  58. if (!hardwares.Mini8Channels.TryGetValueNotNull(mini8.Key, out ConcurrentDictionary<byte, ChannelData> channels))
  59. continue;
  60. foreach (KeyValuePair<byte, ChannelConfig> item in mini8.Value.ChannelConfig)
  61. {
  62. if (!channels.TryGetValueNotNull(item.Key, out ChannelData channel))
  63. continue;
  64. channels[item.Key] = item.Value.Adapt(channel);
  65. }
  66. }
  67. configFiles.CurrentConfigFile = file;
  68. this.UpdateConfigFile(file);
  69. return true;
  70. }
  71. public void UpdateConfigFile(TemperatureConfig config)
  72. {
  73. if (config.Mini8sConfig is null)
  74. return;
  75. foreach (KeyValuePair<byte, Mini8Config> item in config.Mini8sConfig)
  76. {
  77. if (!hardwares.Mini8Channels.TryGetValueNotNull(item.Key, out ConcurrentDictionary<byte, ChannelData> hardwareChannels))
  78. continue;
  79. if (item.Value.ChannelConfig is null)
  80. continue;
  81. foreach (KeyValuePair<byte, ChannelConfig> channelConfig in item.Value.ChannelConfig)
  82. {
  83. if (!hardwareChannels.TryGetValueNotNull(channelConfig.Key, out ChannelData hardwareChannel))
  84. continue;
  85. channelConfig.Value.Adapt(hardwareChannel);
  86. //MapData(channelConfig.Value, hardwareChannel);
  87. }
  88. }
  89. }
  90. public bool ReadConfigFromMini8(out TemperatureConfig? temperatureConfig)
  91. {
  92. temperatureConfig = null;
  93. TemperatureConfig cache = new()
  94. {
  95. ConfigName = "FromMini8",
  96. EditTime = DateTime.Now,
  97. Editor = "Mini8",
  98. Description = "This data is read from Mini8",
  99. Mini8sConfig = []
  100. };
  101. if (hardWareMonitor.GetCurrentStatus() is not IEnumerable<(byte channelIndex, Dictionary<byte, Mini8Output>? realtimeData)> statusGroups)
  102. goto final;
  103. Parallel.ForEach(statusGroups, (t) =>
  104. {
  105. byte mini8Index = t.channelIndex;
  106. Dictionary<byte, Mini8Output>? data = t.realtimeData;
  107. if (data is null)
  108. return;
  109. Mini8Config config = new()
  110. {
  111. Index = mini8Index,
  112. ChannelConfig = []
  113. };
  114. hardwares.Mini8Channels.TryGetValue(mini8Index, out var channels);
  115. foreach (KeyValuePair<byte, Mini8Output> channelRealTime in data)
  116. {
  117. ChannelConfig channelConfig = new();
  118. channelRealTime.Value.Adapt(channelConfig);
  119. channelConfig.Index = channelRealTime.Key;
  120. config.ChannelConfig[channelRealTime.Key] = channelConfig;
  121. if (channels is null)
  122. continue;
  123. if (!channels.TryGetValue(channelRealTime.Key, out var hardwareChannel) || hardwareChannel is null)
  124. continue;
  125. //Take ChannelMode from Hareware configruation
  126. channelConfig.ChannelMode = hardwareChannel.ChannelMode;
  127. }
  128. cache.Mini8sConfig[mini8Index] = config;
  129. });
  130. if (hardWareMonitor.GetCurrentLimit() is not IDictionary<byte, Dictionary<byte, Mini8Limit>?> limitGroups)
  131. goto final;
  132. Parallel.ForEach(limitGroups, (t) =>
  133. {
  134. byte mini8Index = t.Key;
  135. Dictionary<byte, Mini8Limit>? data = t.Value;
  136. if (data is null)
  137. return;
  138. if (!cache.Mini8sConfig.TryGetValueNotNull(mini8Index, out Mini8Config mini8Config))
  139. return;
  140. if (mini8Config.ChannelConfig is null)
  141. return;
  142. foreach (KeyValuePair<byte, Mini8Limit> limitPair in data)
  143. {
  144. if (!mini8Config.ChannelConfig.TryGetValueNotNull(limitPair.Key, out ChannelConfig channelConfig))
  145. continue;
  146. Mini8Limit limit = limitPair.Value;
  147. channelConfig.Caps = limit.Caps;
  148. channelConfig.Floor = limit.Floor;
  149. channelConfig.CapsWarning = limit.CapsWarning;
  150. channelConfig.FloorWarning = limit.FloorWarning;
  151. //if (channelConfig.SetPoint.InRange(limit.Floor, limit.Caps))
  152. //{
  153. // channelConfig.CapsWarning = (limit.Caps + channelConfig.SetPoint) / 2;
  154. // channelConfig.FloorWarning = (limit.Floor + channelConfig.SetPoint) / 2;
  155. //}
  156. //else
  157. //{
  158. // float spilt = (limit.Caps - limit.Floor) / 4;
  159. // channelConfig.CapsWarning = limit.Caps - spilt;
  160. // channelConfig.FloorWarning = limit.Floor + spilt;
  161. //}
  162. }
  163. }
  164. );
  165. final:
  166. temperatureConfig = cache;
  167. return true;
  168. }
  169. }