HardwareOperator.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using MinicsConsole.Helper.RawDataFilter;
  2. namespace MinicsConsole.Business;
  3. public class HardwareOperator(
  4. Hardwares hardwares,
  5. HardWareMonitor hardWareMonitor,
  6. //HardwareFileLoader hardwareFileLoader,
  7. HardwareDBLoader hardwareDBLoader,
  8. UISender uIConnector,ILog log)
  9. {
  10. public Task QueryHardwares()
  11. {
  12. uIConnector.UpdateMini8All();
  13. uIConnector.UpdateChannelAll();
  14. uIConnector.UpdateAddressAll();
  15. foreach (KeyValuePair<byte, Mini8Provider> providers in hardWareMonitor.Mini8Providers)
  16. uIConnector.Mini8ConnectNotify(providers.Key, providers.Value.IsConnect);
  17. return Task.CompletedTask;
  18. }
  19. public void FullyReset()
  20. {
  21. hardWareMonitor.FullyReset();
  22. }
  23. public bool SendSetValue(ChannelData channelData)
  24. {
  25. if (!hardWareMonitor.Mini8Communicators.TryGetValue(channelData.Mini8Index, out IMini8Communicator? mini8Sender) || mini8Sender is null)
  26. {
  27. log.Error($"Hardware - SendSetValue Failed {channelData.Mini8Index} Not Exist in Mini8 Communicator");
  28. return false;
  29. }
  30. if (!hardwares.Mini8Channels.TryGetValue(channelData.Mini8Index, out ConcurrentDictionary<byte, ChannelData>? channleDataValues) || channelData is null)
  31. {
  32. log.Error($"Hardware - SendSetValue Failed {channelData!.Mini8Index} Not Exist in Mini8 Channels");
  33. return false;
  34. }
  35. if (!channleDataValues.TryGetValue(channelData.ChannelIndex, out ChannelData? channel) || channel is null)
  36. {
  37. log.Error($"Hardware - SendSetValue Failed {channelData!.Mini8Index} Not Exist in Mini8 Channel Data SetValues");
  38. return false;
  39. }
  40. Mini8Input input = new();
  41. channelData.Adapt(input);
  42. bool success = true;
  43. if (!mini8Sender.SendMini8Data(channelData.ChannelIndex, input))
  44. {
  45. log.Error($"Hardware - SendSetValue Failed {channelData!.Mini8Index} Send to Mini8 Failed ");
  46. success = false;
  47. }
  48. channelData.Adapt(channel);
  49. uIConnector.UpdateSingleChannel(channelData.Mini8Index, channelData.ChannelIndex, channel);
  50. log.Info($"HardwareHub - SendSetValue {channelData.Mini8Index} {channelData.Name}");
  51. log.Info($"{JsonSerializer.Serialize(channelData).Trim('{').Trim('}').Replace("\"", string.Empty).Replace(",", Environment.NewLine)}");
  52. return success;
  53. }
  54. public bool ReconnectMini8(byte mini8Index)
  55. {
  56. return hardWareMonitor.CreateConnection(mini8Index, true);
  57. }
  58. public bool EnableChannel(byte mini8Index, byte channelIndex, Inhibit inhibit)
  59. {
  60. if (!hardWareMonitor.Mini8Communicators.TryGetValue(mini8Index, out IMini8Communicator? mini8Sender) || mini8Sender is null)
  61. {
  62. log.Error($"Hardware - EnableChannel Failed {mini8Index} Not Exist in Mini8 Communicator");
  63. return false;
  64. }
  65. if (!hardwares.Mini8Channels.TryGetValue(mini8Index, out var channels) || channels is null)
  66. return false;
  67. if (!channels.TryGetValue(channelIndex, out ChannelData? channel) || channel is null)
  68. return false;
  69. if (!mini8Sender.EnableChannel(channelIndex, inhibit))
  70. return false;
  71. log.Info($"HardwareHub - EnableChannel Mini8-{mini8Index} Channel-{channelIndex} Inhibit-{inhibit}");
  72. return true;
  73. }
  74. public bool SwitchChannelMode(byte mini8Index, byte channelIndex, ChannelMode channelMode)
  75. {
  76. if (!hardWareMonitor.Mini8Communicators.TryGetValue(mini8Index, out IMini8Communicator? mini8Sender) || mini8Sender is null)
  77. {
  78. log.Error($"Hardware - SwitchChannelMode Failed {mini8Index} Not Exist in Mini8 Communicator");
  79. return false;
  80. }
  81. if (!hardwares.Mini8Channels.TryGetValue(mini8Index, out var channels) || channels is null)
  82. return false;
  83. if (!channels.TryGetValue(channelIndex, out ChannelData? channel) || channel is null)
  84. return false;
  85. channel.ChannelMode = channelMode;
  86. if (channelMode == ChannelMode.UnUsed)
  87. EnableChannel(mini8Index, channelIndex, Inhibit.Disable);
  88. uIConnector.UpdateSingleChannel(mini8Index, channelIndex, channel);
  89. hardwareDBLoader.SaveSingleMini8(mini8Index);
  90. log.Info($"HardwareHub - SwitchChannelMode Mini8-{mini8Index} Channel-{channelIndex} ChannelMode-{channelMode}");
  91. return true;
  92. }
  93. public bool SelectPID(byte mini8Index, byte channelIndex, ActiveTuneSet activeTuneSet)
  94. {
  95. if (!hardWareMonitor.Mini8Communicators.TryGetValue(mini8Index, out IMini8Communicator? mini8Sender) || mini8Sender is null)
  96. {
  97. log.Error($"Hardware - SelectPID Failed {mini8Index} Not Exist in Mini8 Communicator");
  98. return false;
  99. }
  100. log.Info($"HardwareHub - SelectPID Mini8-{mini8Index} Channel-{channelIndex} ActiveTuneSet-{activeTuneSet}");
  101. return mini8Sender.SelectPID(channelIndex, activeTuneSet);
  102. }
  103. public bool EnableAT(byte mini8Index, byte channelIndex, bool isEnable)
  104. {
  105. if (!hardWareMonitor.Mini8Communicators.TryGetValue(mini8Index, out IMini8Communicator? mini8Sender) || mini8Sender is null)
  106. {
  107. log.Error($"Hardware - EnableAT Failed {mini8Index} Not Exist in Mini8 Communicator");
  108. return false;
  109. }
  110. log.Info($"HardwareHub - EnableAT Mini8-{mini8Index} Channel-{channelIndex} Enable-{isEnable}");
  111. return mini8Sender.EnableChannelAutoTune(channelIndex, isEnable);
  112. }
  113. public Task EnableDisableDevice(byte mini8Index, bool isEnable)
  114. {
  115. if (hardWareMonitor.Mini8Communicators.TryGetValue(mini8Index, out IMini8Communicator? communicator) && communicator is not null)
  116. {
  117. _ = isEnable switch
  118. {
  119. true => ReconnectMini8(mini8Index),
  120. false => communicator.Close(),
  121. };
  122. }
  123. if (hardwares.Mini8s.TryGetValue(mini8Index, out Mini8Data? mini8) && mini8 is not null)
  124. {
  125. mini8.Enable = isEnable;
  126. uIConnector.UpdateMini8(mini8Index, mini8);
  127. }
  128. hardwareDBLoader.SaveSingleMini8(mini8Index);
  129. log.Info($"HardwareHub - EnableDisableDevice Mini8-{mini8Index} isEnable-{isEnable}");
  130. return Task.CompletedTask;
  131. }
  132. }