ChannedDetailViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. namespace HistoryView.ViewModels.Dialogs;
  2. internal partial class ChannedDetailViewModel(Data.Hardwares hardwares, MessageBoxHelper messageBoxHelper, HubSender sender, HistoryViewer historyViewer, UserInformation user) : ObservableObject, IDialogAwareTitle
  3. {
  4. public DialogCloseListener RequestClose { get; set; }
  5. public string Title { get; set; } = "Channel Setting";
  6. [ObservableProperty]
  7. private Mini8Info? _Mini8Info;
  8. [ObservableProperty]
  9. private Channel? _Channel;
  10. [ObservableProperty]
  11. private Channel? _ChannelSet;
  12. [ObservableProperty]
  13. private UserInformation? _UserInfo;
  14. [ObservableProperty]
  15. private ObservableCollection<string>? _BackWords;
  16. [ObservableProperty]
  17. private RealtimeDataPlotHelper? _PlotHelper;
  18. public bool CanCloseDialog() => true;
  19. public void OnDialogClosed()
  20. {
  21. this.PlotHelper?.Dispose();
  22. this.Mini8Info = null;
  23. this.Channel = null;
  24. this.ChannelSet = null;
  25. this.UserInfo = null;
  26. this.BackWords = null;
  27. this.PlotHelper = null;
  28. }
  29. public void OnDialogOpened(IDialogParameters parameters)
  30. {
  31. this.UserInfo = user;
  32. this.PlotHelper = new();
  33. if (parameters.TryGetValue("Channel", out Channel? channel) && channel is not null)
  34. {
  35. this.Channel = channel;
  36. this.PlotHelper.InitPlot(channel);
  37. this.BackWords ??= [];
  38. HashSet<string> hs = [];
  39. if (hardwares.Mini8s.TryGetValue(channel.Mini8Index, out Mini8Info? mini8Info) && mini8Info is not null)
  40. this.Mini8Info = mini8Info;
  41. foreach (Channel item in hardwares.Mini8Channels[channel.Mini8Index].Values)
  42. {
  43. if (item is null || string.IsNullOrEmpty(item.Name))
  44. continue;
  45. hs.Add(item.Name);
  46. }
  47. this.BackWords.AddRange(hs);
  48. }
  49. this.ChannelSet = new();
  50. channel.Adapt(this.ChannelSet);
  51. }
  52. [RelayCommand]
  53. private async Task Operate(string para)
  54. {
  55. bool succcess = false;
  56. if (this.ChannelSet is null)
  57. goto Hint;
  58. if (this.Channel is not null &&
  59. this.Channel.Inhibit == Inhibit.Disable &&
  60. this.ChannelSet.Inhibit == Inhibit.Disable)
  61. {
  62. await messageBoxHelper.ShowAsync($"Inhibit Status: {this.Channel.Inhibit}", MessageBoxButton.OK, MessageBoxImage.Error);
  63. return;
  64. }
  65. if (ChannelSet.Caps < ChannelSet.CapsWarning ||
  66. ChannelSet.CapsWarning < ChannelSet.FloorWarning ||
  67. ChannelSet.FloorWarning < ChannelSet.Floor)
  68. {
  69. await messageBoxHelper.ShowAsync($"Please follow the rule:{Environment.NewLine} Caps > CapsWarning > FloorWarning > Floor", MessageBoxButton.OK, MessageBoxImage.Error);
  70. return;
  71. }
  72. if (this.Channel is not null && this.Channel.ChannelMode != ChannelMode.Control)
  73. {
  74. await messageBoxHelper.ShowAsync($"Channel Mode: {this.Channel.ChannelMode}", MessageBoxButton.OK, MessageBoxImage.Error);
  75. return;
  76. }
  77. IDialogResult result = await messageBoxHelper.ShowAsync(Compare(), MessageBoxButton.OKCancel, MessageBoxImage.Question);
  78. if (result.Result != ButtonResult.OK)
  79. return;
  80. succcess = sender.SendSetValue(this.ChannelSet);
  81. Hint:
  82. if (!succcess)
  83. await messageBoxHelper.ShowAsync("Failed", MessageBoxButton.OK, MessageBoxImage.Error);
  84. }
  85. [RelayCommand]
  86. private async Task EnableAT()
  87. {
  88. if (this.Channel is null)
  89. return;
  90. if (this.Channel.Inhibit == Inhibit.Disable)
  91. {
  92. await messageBoxHelper.ShowAsync($"Inhibit Status: {this.Channel.Inhibit}", MessageBoxButton.OK, MessageBoxImage.Error);
  93. return;
  94. }
  95. if (this.Channel.ChannelMode != ChannelMode.Control)
  96. {
  97. await messageBoxHelper.ShowAsync($"Channel Mode: {this.Channel.ChannelMode}", MessageBoxButton.OK, MessageBoxImage.Error);
  98. return;
  99. }
  100. switch (this.Channel.AutoTuneStatus)
  101. {
  102. case AutoTuneStatus.Ready:
  103. case AutoTuneStatus.Complete:
  104. case AutoTuneStatus.Aborted:
  105. case AutoTuneStatus.Timeout:
  106. case AutoTuneStatus.Overflow:
  107. case AutoTuneStatus.Unavailable:
  108. break;
  109. case AutoTuneStatus.Triggered:
  110. case AutoTuneStatus.Tuning:
  111. await messageBoxHelper.ShowAsync($"AutoTune Status: {this.Channel.AutoTuneStatus}", MessageBoxButton.OK, MessageBoxImage.Error);
  112. return;
  113. default:
  114. break;
  115. }
  116. IDialogResult? result = await messageBoxHelper.ShowAsync((string)App.Current.Resources["SendParameter"], MessageBoxButton.OKCancel, MessageBoxImage.Question);
  117. if (result is null || result.Result != ButtonResult.OK)
  118. return;
  119. if (!sender.EnableAT(this.Channel.Mini8Index, this.Channel.ChannelIndex, true))
  120. await messageBoxHelper.ShowAsync("Failed", MessageBoxButton.OK, MessageBoxImage.Error);
  121. }
  122. [RelayCommand]
  123. private async Task AbortAT()
  124. {
  125. if (this.Channel is null)
  126. return;
  127. if (this.Channel.Inhibit == Inhibit.Disable)
  128. {
  129. await messageBoxHelper.ShowAsync($"Inhibit Status: {this.Channel.Inhibit}", MessageBoxButton.OK, MessageBoxImage.Error);
  130. return;
  131. }
  132. if (this.Channel.ChannelMode != ChannelMode.Control)
  133. {
  134. await messageBoxHelper.ShowAsync($"Channel Mode: {this.Channel.ChannelMode}", MessageBoxButton.OK, MessageBoxImage.Error);
  135. return;
  136. }
  137. IDialogResult? result = await messageBoxHelper.ShowAsync((string)App.Current.Resources["SendParameter"], MessageBoxButton.OKCancel, MessageBoxImage.Question);
  138. if (result is null || result.Result != ButtonResult.OK)
  139. return;
  140. if (!sender.EnableAT(this.Channel.Mini8Index, this.Channel.ChannelIndex, false))
  141. await messageBoxHelper.ShowAsync("Failed", MessageBoxButton.OK, MessageBoxImage.Error);
  142. }
  143. [RelayCommand]
  144. private async Task EnableChannel(string para)
  145. {
  146. if (this.Channel is null)
  147. return;
  148. if (string.IsNullOrEmpty(para))
  149. return;
  150. IDialogResult? result = await messageBoxHelper.ShowAsync((string)App.Current.Resources["SendParameter"], MessageBoxButton.OKCancel, MessageBoxImage.Question);
  151. if (result is null || result.Result != ButtonResult.OK)
  152. return;
  153. bool isSuccess = para switch
  154. {
  155. "Enable" => sender.EnableChannel(this.Channel.Mini8Index, this.Channel.ChannelIndex, Inhibit.Enable),
  156. "Disable" => sender.EnableChannel(this.Channel.Mini8Index, this.Channel.ChannelIndex, Inhibit.Disable),
  157. "Control" => sender.SwitchChannelMode(this.Channel.Mini8Index, this.Channel.ChannelIndex, ChannelMode.Control),
  158. "Monitor" => sender.SwitchChannelMode(this.Channel.Mini8Index, this.Channel.ChannelIndex, ChannelMode.Monitor),
  159. _ => false
  160. };
  161. if (!isSuccess)
  162. await messageBoxHelper.ShowAsync("Failed", MessageBoxButton.OK, MessageBoxImage.Error);
  163. }
  164. [RelayCommand]
  165. private async Task ApplyPID()
  166. {
  167. if (this.ChannelSet is null || this.Channel is null)
  168. return;
  169. if (this.Channel.Inhibit == Inhibit.Disable)
  170. {
  171. await messageBoxHelper.ShowAsync($"Inhibit Status: {this.Channel.Inhibit}", MessageBoxButton.OK, MessageBoxImage.Error);
  172. return;
  173. }
  174. if (this.Channel.ChannelMode != ChannelMode.Control)
  175. {
  176. await messageBoxHelper.ShowAsync($"Channel Mode: {this.Channel.ChannelMode}", MessageBoxButton.OK, MessageBoxImage.Error);
  177. return;
  178. }
  179. switch (this.Channel.AutoTuneStatus)
  180. {
  181. case AutoTuneStatus.Complete:
  182. break;
  183. case AutoTuneStatus.Ready:
  184. case AutoTuneStatus.Aborted:
  185. case AutoTuneStatus.Timeout:
  186. case AutoTuneStatus.Overflow:
  187. case AutoTuneStatus.Unavailable:
  188. case AutoTuneStatus.Triggered:
  189. case AutoTuneStatus.Tuning:
  190. default:
  191. await messageBoxHelper.ShowAsync($"AutoTune Status: {this.Channel.AutoTuneStatus}", MessageBoxButton.OK, MessageBoxImage.Error);
  192. return;
  193. }
  194. this.ChannelSet.Running_P = this.Channel.AutoTune_P;
  195. this.ChannelSet.Running_I = this.Channel.AutoTune_I;
  196. this.ChannelSet.Running_D = this.Channel.AutoTune_D;
  197. IDialogResult? result = await messageBoxHelper.ShowAsync(Compare(), MessageBoxButton.OKCancel, MessageBoxImage.Question);
  198. if (result is null || result.Result != ButtonResult.OK)
  199. return;
  200. bool success = sender.SelectPID(this.Channel.Mini8Index, this.Channel.ChannelIndex, ActiveTuneSet.Running);
  201. success &= sender.SendSetValue(this.ChannelSet);
  202. if (!success)
  203. await messageBoxHelper.ShowAsync("Failed", MessageBoxButton.OK, MessageBoxImage.Error);
  204. }
  205. [RelayCommand]
  206. private async Task SelectPID(string para)
  207. {
  208. if (this.Channel is null)
  209. return;
  210. if (this.Channel.Inhibit == Inhibit.Disable)
  211. {
  212. await messageBoxHelper.ShowAsync($"Inhibit Status: {this.Channel.Inhibit}", MessageBoxButton.OK, MessageBoxImage.Error);
  213. return;
  214. }
  215. if (this.Channel.ChannelMode != ChannelMode.Control)
  216. {
  217. await messageBoxHelper.ShowAsync($"Channel Mode: {this.Channel.ChannelMode}", MessageBoxButton.OK, MessageBoxImage.Error);
  218. return;
  219. }
  220. switch (this.Channel.AutoTuneStatus)
  221. {
  222. case AutoTuneStatus.Ready:
  223. case AutoTuneStatus.Complete:
  224. case AutoTuneStatus.Aborted:
  225. case AutoTuneStatus.Timeout:
  226. case AutoTuneStatus.Overflow:
  227. case AutoTuneStatus.Unavailable:
  228. break;
  229. case AutoTuneStatus.Triggered:
  230. case AutoTuneStatus.Tuning:
  231. await messageBoxHelper.ShowAsync($"AutoTune Status: {this.Channel.AutoTuneStatus}", MessageBoxButton.OK, MessageBoxImage.Error);
  232. return;
  233. default:
  234. break;
  235. }
  236. if (string.IsNullOrEmpty(para))
  237. return;
  238. IDialogResult? result = await messageBoxHelper.ShowAsync((string)App.Current.Resources["SendParameter"], MessageBoxButton.OKCancel, MessageBoxImage.Question);
  239. if (result is null || result.Result != ButtonResult.OK)
  240. return;
  241. bool isSuccess = para switch
  242. {
  243. "Running" => sender.SelectPID(this.Channel.Mini8Index, this.Channel.ChannelIndex, ActiveTuneSet.Running),
  244. "Autotune" => sender.SelectPID(this.Channel.Mini8Index, this.Channel.ChannelIndex, ActiveTuneSet.AutoTune),
  245. _ => false
  246. };
  247. if (!isSuccess)
  248. await messageBoxHelper.ShowAsync("Failed", MessageBoxButton.OK, MessageBoxImage.Error);
  249. }
  250. [RelayCommand]
  251. private void ViewHistory()
  252. {
  253. if (this.Channel is null)
  254. return;
  255. if (!historyViewer.StartChannelHistory(Channel.Mini8Index, Channel.ChannelIndex))
  256. return;
  257. }
  258. [RelayCommand]
  259. private void Exit() => this.RequestClose.Invoke();
  260. private string Compare()
  261. {
  262. if (this.ChannelSet is null || Channel is null)
  263. return string.Empty;
  264. StringBuilder sb = new();
  265. sb.AppendLine((string)App.Current.Resources["SendParameter"]);
  266. SBHelper("SetValue", Channel.SetPoint, ChannelSet.SetPoint, sb);
  267. SBHelper("Caps", Channel.Caps, ChannelSet.Caps, sb);
  268. SBHelper("Floor", Channel.Floor, ChannelSet.Floor, sb);
  269. SBHelper("CapsWarning", Channel.CapsWarning, ChannelSet.CapsWarning, sb);
  270. SBHelper("FloorWarning", Channel.FloorWarning, ChannelSet.FloorWarning, sb);
  271. SBHelper("Running_P", Channel.Running_P, ChannelSet.Running_P, sb);
  272. SBHelper("Running_I", Channel.Running_I, ChannelSet.Running_I, sb);
  273. SBHelper("Running_D", Channel.Running_D, ChannelSet.Running_D, sb);
  274. SBHelper("SetpointUpRate", Channel.SetpointUpRate, ChannelSet.SetpointUpRate, sb);
  275. SBHelper("SetpointDownRate", Channel.SetpointDownRate, ChannelSet.SetpointDownRate, sb);
  276. return sb.ToString();
  277. }
  278. private static void SBHelper<T>(string title, T oldValue, T newValue, StringBuilder sb) where T : IComparable
  279. {
  280. if (oldValue.CompareTo(newValue) != 0)
  281. sb.AppendLine($"{title.PadRight(20)} {oldValue.ToString()!.PadRight(6)} --> {newValue}");
  282. }
  283. }