StatusViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using GeneralData;
  2. using HandyControl.Tools.Extension;
  3. using NPOI.SS.UserModel;
  4. using Org.BouncyCastle.Tls.Crypto;
  5. using ScottPlot.Colormaps;
  6. using ScottPlot.Plottables;
  7. using System.Windows.Media.TextFormatting;
  8. using Universal;
  9. using static SkiaSharp.HarfBuzz.SKShaper;
  10. namespace HistoryUI.ViewModels;
  11. public partial class StatusViewModel : BaseViewModel<DBFormat>
  12. {
  13. public StatusViewModel(Hardwares hardwares, IORM orm) : base(hardwares, orm)
  14. {
  15. this.DataGirdVis = Visibility.Collapsed;
  16. this.Loading = Visibility.Collapsed;
  17. this.PlotControl = new();
  18. this.InitPlot();
  19. }
  20. private void InitPlot()
  21. {
  22. this.PlotControl.Plot.Grid.XAxisStyle.MajorLineStyle.Width = 1f;
  23. this.PlotControl.Plot.Grid.YAxisStyle.MajorLineStyle.Width = 1f;
  24. this.PlotControl.Plot.FigureBackground.Color = ScottPlot.Colors.Transparent;
  25. this.PlotControl.Plot.RenderManager.RenderStarting += (s, e) =>
  26. {
  27. Tick[] ticks = this.PlotControl.Plot.Axes.Bottom.TickGenerator.Ticks;
  28. for (int i = 0; i < ticks.Length; i++)
  29. {
  30. DateTime dt = DateTime.FromOADate(ticks[i].Position);
  31. string label = $"{dt:MM/dd HH:mm:ss}";
  32. ticks[i] = new Tick(ticks[i].Position, label);
  33. }
  34. };
  35. PixelPadding padding = new(40, 20, 65, 10);
  36. this.PlotControl.Plot.Layout.Fixed(padding);
  37. this.PlotControl.Plot.FigureBackground.Color = ScottPlot.Colors.Transparent;
  38. this.Query();
  39. }
  40. [ObservableProperty]
  41. private WpfPlot _PlotControl;
  42. [ObservableProperty]
  43. private string? _Hint;
  44. [ObservableProperty]
  45. private Visibility _DataGirdVis;
  46. [ObservableProperty]
  47. private Visibility _Loading;
  48. [RelayCommand]
  49. private void Show(string para)
  50. {
  51. switch (para)
  52. {
  53. case "Show":
  54. this.DataGirdVis = Visibility.Visible;
  55. break;
  56. case "Hide":
  57. this.DataGirdVis = Visibility.Collapsed;
  58. break;
  59. default:
  60. break;
  61. }
  62. }
  63. [RelayCommand]
  64. protected override void Query()
  65. {
  66. if (!base.QueryBase(out byte mini8, out byte Channel))
  67. return;
  68. this.Loading = Visibility.Visible;
  69. _orm.Query<DBFormat>($"Mini8-{mini8}-{Channel}",
  70. t =>
  71. t.DateTime >= this.StartTime &&
  72. t.DateTime <= this.EndTime
  73. , QueryResult);
  74. }
  75. private class Area(DateTime start, DateTime? end, bool success)
  76. {
  77. public bool Success { get; set; } = success;
  78. public DateTime Start { get; } = start;
  79. public DateTime? End { get; set; } = end;
  80. }
  81. private class DataRange
  82. {
  83. public readonly List<float> target = [];
  84. public readonly List<float> temp = [];
  85. public readonly List<float> caps = [];
  86. public readonly List<float> floor = [];
  87. public readonly List<float> capsWarning = [];
  88. public readonly List<float> floorWarning = [];
  89. public readonly List<DateTime> tcBrokenLine = [];
  90. public readonly List<DateTime> time = [];
  91. public readonly List<Area> autoTuneArea = [];
  92. public readonly List<Area> tcBrockenArea = [];
  93. }
  94. private readonly List<DataRange> _DataRangeCache = [];
  95. private readonly Dictionary<int, DBFormat> _DataCache = [];
  96. private void QueryResult(List<DBFormat> results)
  97. {
  98. if (results.Count < 1)
  99. {
  100. App.Current.Dispatcher.Invoke(() => this.Loading = Visibility.Collapsed);
  101. return;
  102. }
  103. results = [.. results.OrderBy(t => t.DateTime)];
  104. _DataCache.Clear();
  105. _DataRangeCache.Clear();
  106. for (int i = 1; i <= results.Count; i++)
  107. {
  108. _DataCache.Add(i, results[i - 1]);
  109. if (_DataCache[i].ActiveTuneSet != ActiveTuneSet.AutoTune)
  110. continue;
  111. _DataCache[i].Running_P = _DataCache[i].AutoTune_P;
  112. _DataCache[i].Running_I = _DataCache[i].AutoTune_I;
  113. _DataCache[i].Running_D = _DataCache[i].AutoTune_D;
  114. }
  115. //Create Data Range
  116. DateTime? lastTime = null;
  117. AutoTuneStatus? autoTune = null;
  118. TcBorken tcBorken = TcBorken.Normal;
  119. float lastPV = 0;
  120. foreach (DBFormat dbData in results)
  121. {
  122. if (dbData.SensorBreakAlarm == TcBorken.Error)
  123. dbData.PV = lastPV;
  124. lastPV = dbData.PV;
  125. TimeSpan? span = dbData.DateTime - lastTime;
  126. if (lastTime is null || !span.HasValue || span.Value.TotalSeconds > 10)
  127. {
  128. autoTune = null;
  129. tcBorken = TcBorken.Normal;
  130. _DataRangeCache.Add(new());
  131. }
  132. lastTime = dbData.DateTime;
  133. DataRange dataRang = _DataRangeCache.Last();
  134. dataRang.time.Add(dbData.DateTime);
  135. dataRang.temp.Add(dbData.PV);
  136. dataRang.target.Add(dbData.SetPoint);
  137. dataRang.caps.Add(dbData.Caps);
  138. dataRang.floor.Add(dbData.Floor);
  139. dataRang.capsWarning.Add(dbData.CapsWarning);
  140. dataRang.floorWarning.Add(dbData.FloorWarning);
  141. if (tcBorken == dbData.SensorBreakAlarm)
  142. goto Step_AutoTuneStatusCheck;
  143. switch (dbData.SensorBreakAlarm)
  144. {
  145. case TcBorken.Normal:
  146. if (dataRang.tcBrockenArea.Count == 0)
  147. break;
  148. Area s = dataRang.tcBrockenArea.Last();
  149. s.Success = true;
  150. s.End = dbData.DateTime;
  151. break;
  152. case TcBorken.Error:
  153. dataRang.tcBrockenArea.Add(new(dbData.DateTime, null, false));
  154. break;
  155. default:
  156. break;
  157. }
  158. dataRang.tcBrokenLine.Add(dbData.DateTime);
  159. tcBorken = dbData.SensorBreakAlarm;
  160. Step_AutoTuneStatusCheck:
  161. //autoTune ??= dbData.AutoTuneStatus;
  162. if (autoTune.HasValue && autoTune == dbData.AutoTuneStatus)
  163. continue;
  164. switch (dbData.AutoTuneStatus)
  165. {
  166. case AutoTuneStatus.Triggered:
  167. case AutoTuneStatus.Tuning:
  168. autoTune = AutoTuneStatus.Tuning;
  169. dataRang.autoTuneArea.Add(new(dbData.DateTime, null, true));
  170. break;
  171. case AutoTuneStatus.Complete:
  172. autoTune = AutoTuneStatus.Complete;
  173. if (dataRang.autoTuneArea.Count == 0)
  174. {
  175. dataRang.autoTuneArea.Add(new(dataRang.time[0], dbData.DateTime, true));
  176. break;
  177. }
  178. Area area1 = dataRang.autoTuneArea.Last();
  179. area1.Success = true;
  180. area1.End = dbData.DateTime;
  181. break;
  182. case AutoTuneStatus.Aborted:
  183. case AutoTuneStatus.Timeout:
  184. case AutoTuneStatus.Overflow:
  185. autoTune = AutoTuneStatus.Aborted;
  186. if (dataRang.autoTuneArea.Count == 0)
  187. {
  188. dataRang.autoTuneArea.Add(new(dataRang.time[0], dbData.DateTime, false));
  189. break;
  190. }
  191. Area area2 = dataRang.autoTuneArea.Last();
  192. area2.Success = false;
  193. area2.End = dbData.DateTime;
  194. break;
  195. default:
  196. break;
  197. }
  198. }
  199. App.Current.Dispatcher.Invoke(() =>
  200. {
  201. this.PlotControl.Plot.Clear();
  202. this._DataRangeCache.ForEach(range => SetDataRangeInPlot(range));
  203. this.RefreshPlot();
  204. this.RefreshDataTable(results);
  205. this.Loading = Visibility.Collapsed;
  206. });
  207. }
  208. private void SetLine(List<DateTime> time, List<float> value, string color, LinePattern linePattern, MarkerStyle markerStyle, float lineWidth)
  209. {
  210. Scatter mainScatter = this.PlotControl.Plot.Add.Scatter(time, value, ScottPlot.Color.FromHex(color));
  211. mainScatter.MarkerStyle = markerStyle;
  212. mainScatter.LineWidth = lineWidth;
  213. mainScatter.LinePattern = linePattern;
  214. }
  215. private void SetDataRangeInPlot(DataRange range)
  216. {
  217. this.SetLine(range.time, range.temp, "0000cd", LinePattern.Solid, MarkerStyle.None, 2f);
  218. this.SetLine(range.time, range.caps, "FF0000", LinePattern.Dotted, MarkerStyle.None, 1.5f);
  219. this.SetLine(range.time, range.floor, "FF0000", LinePattern.Dotted, MarkerStyle.None, 1.5f);
  220. this.SetLine(range.time, range.capsWarning, "FFA500", LinePattern.Dotted, MarkerStyle.None, 1.5f);
  221. this.SetLine(range.time, range.floorWarning, "FFA500", LinePattern.Dotted, MarkerStyle.None, 1.5f);
  222. this.SetLine(range.time, range.target, "00FF00", LinePattern.Dotted, MarkerStyle.None, 1.5f);
  223. foreach (var item in range.autoTuneArea)
  224. {
  225. item.End ??= range.time.Last();
  226. _ = item.Success switch
  227. {
  228. true => this.PlotControl.Plot.Add.HorizontalSpan(item.Start.ToOADate(), item.End.Value.ToOADate(), ScottPlot.Color.FromHex("00ff00").WithAlpha(0.2)),
  229. false => this.PlotControl.Plot.Add.HorizontalSpan(item.Start.ToOADate(), item.End.Value.ToOADate(), ScottPlot.Color.FromHex("828282").WithAlpha(0.2))
  230. };
  231. }
  232. foreach (var item in range.tcBrockenArea)
  233. {
  234. item.End ??= range.time.Last();
  235. this.PlotControl.Plot.Add.HorizontalSpan(item.Start.ToOADate(), item.End.Value.ToOADate(), ScottPlot.Color.FromHex("f98083").WithAlpha(0.2));
  236. }
  237. }
  238. private void RefreshDataTable(List<DBFormat> results)
  239. {
  240. this.Results ??= [];
  241. this.Results.Clear();
  242. for (int i = 0; i < results.Count; i++)
  243. this.Results.Add(i, results[i]);
  244. }
  245. private void RefreshPlot()
  246. {
  247. this.PlotControl.Plot.HideLegend();
  248. this.PlotControl.Plot.Axes.DateTimeTicksBottom();
  249. this.PlotControl.Plot.Axes.Bottom.TickLabelStyle.Rotation = -45;
  250. this.PlotControl.Plot.Axes.Bottom.TickLabelStyle.Alignment = Alignment.MiddleRight;
  251. this.PlotControl.Plot.Axes.AutoScale();
  252. this.PlotControl.Refresh();
  253. this.PlotControl.Plot.Axes.Zoom(1.085, 1);
  254. }
  255. [RelayCommand]
  256. private void ReScale(string para)
  257. {
  258. switch (para)
  259. {
  260. case "+":
  261. this.PlotControl.Plot.Axes.Zoom(1.25, 1);
  262. break;
  263. case "-":
  264. this.PlotControl.Plot.Axes.Zoom(0.8, 1);
  265. break;
  266. case "add":
  267. this.PlotControl.Plot.Axes.Zoom(1, 1.25);
  268. break;
  269. case "minus":
  270. this.PlotControl.Plot.Axes.Zoom(1, 0.8);
  271. break;
  272. default:
  273. this.PlotControl.Plot.Axes.AutoScale();
  274. this.PlotControl.Plot.Axes.Zoom(1.085, 1);
  275. break;
  276. }
  277. this.PlotControl.Refresh();
  278. }
  279. }