HardwareOperator.cs 6.3 KB

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