ChannelMultiEditViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using System.Net.WebSockets;
  2. namespace MinicsUI.ViewModels.Dialogs;
  3. public partial class ChannelMultiEditViewModel(Hardwares hardwares, MessageBoxHelper messageBoxHelper, HubSender sender) : ObservableObject, IDialogAwareTitle
  4. {
  5. #region IDialogAwareTitle
  6. public string Title { get; set; } = "MultiEdit";
  7. DialogCloseListener IDialogAware.RequestClose { get; }
  8. bool IDialogAware.CanCloseDialog()
  9. {
  10. return true;
  11. }
  12. void IDialogAware.OnDialogClosed()
  13. {
  14. }
  15. void IDialogAware.OnDialogOpened(IDialogParameters parameters)
  16. {
  17. if (!parameters.TryGetValue("Mini8Info", out Mini8Info? mini8) || mini8 is null)
  18. return;
  19. if (!hardwares.Mini8Channels.TryGetValue(mini8.Index, out ObservableDictionary<byte, Channel>? channels) || channels is null)
  20. return;
  21. this.Channels = channels;
  22. this.Selecters ??= [];
  23. foreach (var item in channels.Values)
  24. {
  25. if (item.ChannelMode == ChannelMode.UnUsed)
  26. continue;
  27. switch (item.ChannelMode)
  28. {
  29. case ChannelMode.Control:
  30. case ChannelMode.Monitor:
  31. this.Selecters.Add(new(item));
  32. break;
  33. case ChannelMode.UnUsed:
  34. default:
  35. break;
  36. }
  37. }
  38. }
  39. #endregion
  40. [ObservableProperty]
  41. private string? _Hint;
  42. [ObservableProperty]
  43. private Visibility _HintVis = Visibility.Collapsed;
  44. [ObservableProperty]
  45. private ObservableCollection<Selector> _Selecters = [];
  46. [ObservableProperty]
  47. private ObservableDictionary<byte, Channel> _Channels = [];
  48. [RelayCommand]
  49. private void Select(string para)
  50. {
  51. _ = para switch
  52. {
  53. "All" => this.Selecters.Foreach(t => t.IsSelected = true),
  54. "None" => this.Selecters.Foreach(t => t.IsSelected = false),
  55. _ => 0
  56. };
  57. }
  58. [RelayCommand]
  59. private void AutoTune()
  60. {
  61. List<Channel> channels = [];
  62. foreach (Selector selector in this.Selecters.Where(t => t.IsSelected))
  63. {
  64. Channel channel = selector.Channel;
  65. if (channel is null)
  66. return;
  67. if (channel.Inhibit == Inhibit.Disable)
  68. {
  69. messageBoxHelper.Show($"Channel {channel.Name} Inhibit Status: {channel.Inhibit}", MessageBoxButton.OK, MessageBoxImage.Error);
  70. return;
  71. }
  72. if (channel.ChannelMode != ChannelMode.Control)
  73. {
  74. messageBoxHelper.Show($"Channel {channel.Name} Channel Mode: {channel.ChannelMode}", MessageBoxButton.OK, MessageBoxImage.Error);
  75. return;
  76. }
  77. switch (channel.AutoTuneStatus)
  78. {
  79. case AutoTuneStatus.Ready:
  80. case AutoTuneStatus.Complete:
  81. case AutoTuneStatus.Aborted:
  82. case AutoTuneStatus.Timeout:
  83. case AutoTuneStatus.Overflow:
  84. case AutoTuneStatus.Unavailable:
  85. default:
  86. break;
  87. case AutoTuneStatus.Triggered:
  88. case AutoTuneStatus.Tuning:
  89. messageBoxHelper.Show($"Channel {channel.Name} AutoTune Status: {channel.AutoTuneStatus}", MessageBoxButton.OK, MessageBoxImage.Error);
  90. return;
  91. }
  92. channels.Add(channel);
  93. }
  94. IDialogResult? result = messageBoxHelper.ShowAsync((string)App.Current.Resources["SendParameter"], MessageBoxButton.OKCancel, MessageBoxImage.Question).Result;
  95. if (result is null || result.Result != ButtonResult.OK)
  96. return;
  97. this.HintVis = Visibility.Visible;
  98. Task.Factory.StartNew(() =>
  99. {
  100. foreach (Channel channel in channels)
  101. {
  102. this.Hint = $"Enable Channel {channel.Name} Autotune...";
  103. if (!sender.EnableAT(channel.Mini8Index, channel.ChannelIndex, true))
  104. App.Current.Dispatcher.Invoke(() => { messageBoxHelper.ShowAsync($"{channel.Name} Failed", MessageBoxButton.OK, MessageBoxImage.Error).Wait(); });
  105. Thread.Sleep(200);
  106. }
  107. App.Current.Dispatcher.Invoke(() => { this.HintVis = Visibility.Collapsed; });
  108. });
  109. }
  110. [RelayCommand]
  111. private void AbortAutoTune()
  112. {
  113. this.HintVis = Visibility.Visible;
  114. Task.Factory.StartNew(() =>
  115. {
  116. foreach (Selector selector in this.Selecters.Where(t => t.IsSelected))
  117. {
  118. this.Hint = $"Abort Channel {selector.Channel.Name} Autotune...";
  119. Channel channel = selector.Channel;
  120. if (!sender.EnableAT(channel.Mini8Index, channel.ChannelIndex, false))
  121. App.Current.Dispatcher.Invoke(() => { messageBoxHelper.ShowAsync($" {selector.Channel.Name} Failed", MessageBoxButton.OK, MessageBoxImage.Error).Wait(); });
  122. Thread.Sleep(200);
  123. }
  124. App.Current.Dispatcher.Invoke(() => { this.HintVis = Visibility.Collapsed; });
  125. });
  126. }
  127. [RelayCommand]
  128. private void ControlMode(string para)
  129. {
  130. this.HintVis = Visibility.Visible;
  131. Task.Factory.StartNew(() =>
  132. {
  133. IEnumerable<Selector> selectors = this.Selecters.Where(t => t.IsSelected);
  134. foreach (var selector in selectors)
  135. {
  136. this.Hint = $"Set Channel {selector.Channel.Name} {para}...";
  137. bool isSuccess = para switch
  138. {
  139. "Enable" => sender.EnableChannel(selector.Channel.Mini8Index, selector.Channel.ChannelIndex, Inhibit.Enable),
  140. "Disable" => sender.EnableChannel(selector.Channel.Mini8Index, selector.Channel.ChannelIndex, Inhibit.Disable),
  141. _ => false
  142. };
  143. if (!isSuccess)
  144. App.Current.Dispatcher.Invoke(() => { messageBoxHelper.ShowAsync($"{selector.Channel.Name} Failed", MessageBoxButton.OK, MessageBoxImage.Error).Wait(); });
  145. Thread.Sleep(200);
  146. }
  147. App.Current.Dispatcher.Invoke(() => { this.HintVis = Visibility.Collapsed; });
  148. });
  149. }
  150. [RelayCommand]
  151. private void ChannelModeSwitch(string para)
  152. {
  153. IDialogResult? result = messageBoxHelper.ShowAsync((string)App.Current.Resources["SendParameter"], MessageBoxButton.OKCancel, MessageBoxImage.Question).Result;
  154. if (result is null || result.Result != ButtonResult.OK)
  155. return;
  156. IEnumerable<Selector> selectors = this.Selecters.Where(t => t.IsSelected);
  157. foreach (var selector in selectors)
  158. {
  159. bool isSuccess = para switch
  160. {
  161. "Control" => sender.SwitchChannelMode(selector.Channel.Mini8Index, selector.Channel.ChannelIndex, ChannelMode.Control),
  162. "Monitor" => sender.SwitchChannelMode(selector.Channel.Mini8Index, selector.Channel.ChannelIndex, ChannelMode.Monitor),
  163. _ => false
  164. };
  165. }
  166. }
  167. [RelayCommand]
  168. private void ApplyATPID()
  169. {
  170. IDialogResult? result = messageBoxHelper.ShowAsync((string)App.Current.Resources["SendParameter"], MessageBoxButton.OKCancel, MessageBoxImage.Question).Result;
  171. if (result is null || result.Result != ButtonResult.OK)
  172. return;
  173. this.HintVis = Visibility.Visible;
  174. IEnumerable<Selector> selectors = this.Selecters.Where(t => t.IsSelected);
  175. Task.Factory.StartNew(() =>
  176. {
  177. foreach (var selector in selectors)
  178. {
  179. Channel ChannelSet = new();
  180. selector.Channel.Adapt(ChannelSet);
  181. if (selector.Channel.Inhibit == Inhibit.Disable)
  182. {
  183. App.Current.Dispatcher.Invoke(() =>
  184. {
  185. messageBoxHelper.ShowAsync($"Channel {selector.Channel.Name} Inhibit Status: {selector.Channel.Inhibit}", MessageBoxButton.OK, MessageBoxImage.Error).Wait();
  186. });
  187. continue;
  188. }
  189. if (selector.Channel.ChannelMode != ChannelMode.Control)
  190. {
  191. App.Current.Dispatcher.Invoke(() =>
  192. {
  193. messageBoxHelper.ShowAsync($"Channel {selector.Channel.Name} Channel Mode: {selector.Channel.ChannelMode}", MessageBoxButton.OK, MessageBoxImage.Error).Wait();
  194. });
  195. continue;
  196. }
  197. switch (selector.Channel.AutoTuneStatus)
  198. {
  199. case AutoTuneStatus.Complete:
  200. break;
  201. case AutoTuneStatus.Ready:
  202. case AutoTuneStatus.Aborted:
  203. case AutoTuneStatus.Timeout:
  204. case AutoTuneStatus.Overflow:
  205. case AutoTuneStatus.Unavailable:
  206. case AutoTuneStatus.Triggered:
  207. case AutoTuneStatus.Tuning:
  208. default:
  209. App.Current.Dispatcher.Invoke(() =>
  210. {
  211. messageBoxHelper.ShowAsync($"Channel {selector.Channel.Name} AutoTune Status: {selector.Channel.AutoTuneStatus}", MessageBoxButton.OK, MessageBoxImage.Error).Wait();
  212. });
  213. continue;
  214. }
  215. this.Hint = $"Channel {selector.Channel.Name} Applying Autotune PID ...";
  216. ChannelSet.Running_P = selector.Channel.AutoTune_P;
  217. ChannelSet.Running_I = selector.Channel.AutoTune_I;
  218. ChannelSet.Running_D = selector.Channel.AutoTune_D;
  219. bool success = sender.SelectPID(selector.Channel.Mini8Index, selector.Channel.ChannelIndex, ActiveTuneSet.Running);
  220. success &= sender.SendSetValue(ChannelSet);
  221. if (!success)
  222. App.Current.Dispatcher.Invoke(() =>
  223. {
  224. messageBoxHelper.ShowAsync($"Channel {selector.Channel.Name} Failed", MessageBoxButton.OK, MessageBoxImage.Error).Wait();
  225. });
  226. Thread.Sleep(300);
  227. }
  228. App.Current.Dispatcher.Invoke(() => { this.HintVis = Visibility.Collapsed; });
  229. });
  230. }
  231. [RelayCommand]
  232. private void Exit()
  233. {
  234. (this as IDialogAware).RequestClose.Invoke();
  235. }
  236. }
  237. public partial class Selector : ObservableObject
  238. {
  239. public Selector(Channel channelName)
  240. {
  241. this.Channel = channelName;
  242. }
  243. [ObservableProperty]
  244. private Channel _Channel;
  245. [ObservableProperty]
  246. private bool _IsSelected;
  247. }