ConfigUpdater.cs 6.8 KB

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