LogTraceViewModel.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using ConfigOperator;
  2. using HardwareData;
  3. using HistoryUI.DataType;
  4. using ScottPlot.Plottables;
  5. namespace HistoryUI.ViewModels;
  6. public partial class LogTraceViewModel : BaseViewModel<DBFormat>
  7. {
  8. public LogTraceViewModel(Hardwares hardwares, IORM orm, BasicInfo basicInfo) : base(hardwares, orm)
  9. {
  10. this.Loading = Visibility.Collapsed;
  11. this.ChannelDetails = [];
  12. this.PlotControl = new();
  13. this.basicInfo = basicInfo;
  14. this.InitPlot();
  15. }
  16. private BasicInfo basicInfo;
  17. [ObservableProperty]
  18. private WpfPlot _PlotControl;
  19. [ObservableProperty]
  20. private string? _Hint;
  21. [ObservableProperty]
  22. private Visibility _Loading;
  23. private void InitPlot()
  24. {
  25. this.PlotControl.Plot.Grid.XAxisStyle.MajorLineStyle.Width = 1f;
  26. this.PlotControl.Plot.Grid.YAxisStyle.MajorLineStyle.Width = 1f;
  27. this.PlotControl.Plot.FigureBackground.Color = ScottPlot.Colors.Transparent;
  28. this.PlotControl.Plot.RenderManager.RenderStarting += (s, e) =>
  29. {
  30. Tick[] ticks = this.PlotControl.Plot.Axes.Bottom.TickGenerator.Ticks;
  31. for (int i = 0; i < ticks.Length; i++)
  32. {
  33. DateTime dt = DateTime.FromOADate(ticks[i].Position);
  34. string label = $"{dt:MM/dd HH:mm:ss}";
  35. ticks[i] = new Tick(ticks[i].Position, label);
  36. }
  37. };
  38. PixelPadding padding = new(40, 20, 65, 10);
  39. this.PlotControl.Plot.Layout.Fixed(padding);
  40. this.PlotControl.Plot.FigureBackground.Color = ScottPlot.Colors.Transparent;
  41. UpdateDetail();
  42. }
  43. private void UpdateDetail()
  44. {
  45. if (string.IsNullOrEmpty(basicInfo.DBConnectionString))
  46. return;
  47. if (base.Channels is null)
  48. return;
  49. if (!base.QueryBase(out byte mini8, out _))
  50. return;
  51. if (Mini8 != mini8)
  52. {
  53. this.ChannelDetails ??= [];
  54. this.ChannelDetails.Clear();
  55. foreach (var item in Channels)
  56. {
  57. IORM newOrm = new SqlSugarCustom();
  58. if (!newOrm.Initialize() || !newOrm.Open(basicInfo.DBConnectionString, ORM.DbType.PostgreSQL))
  59. continue;
  60. if (string.IsNullOrEmpty(item.Value.Name) || item.Value.Name.StartsWith("Spare"))
  61. {
  62. continue;
  63. }
  64. //Brush? brush;
  65. //try
  66. //{
  67. // brush = (Brush)App.Current.Resources[$"PlotLine{item.Key}"];
  68. //}
  69. //catch
  70. //{
  71. // brush = ChannelDetail.Default;
  72. // throw;
  73. //}
  74. ChannelDetail detail = new()
  75. {
  76. ChannelData = item.Value,
  77. Orm = newOrm,
  78. //ChannelColor = brush
  79. };
  80. this.ChannelDetails[item.Key] = detail;
  81. }
  82. Mini8 = mini8;
  83. }
  84. }
  85. [ObservableProperty]
  86. private ObservableDictionary<byte, ChannelDetail> _ChannelDetails;
  87. private byte Mini8;
  88. [RelayCommand]
  89. protected override void Query()
  90. {
  91. UpdateDetail();
  92. if (!base.QueryBase(out byte mini8, out _))
  93. return;
  94. this.Loading = Visibility.Visible;
  95. this.PlotControl.Plot.Clear();
  96. foreach (var channel in ChannelDetails)
  97. {
  98. if (channel.Value.Orm is null)
  99. continue;
  100. if (!channel.Value.IsSelected)
  101. continue;
  102. channel.Value.Orm.Query<DBFormat>($"Mini8-{mini8}-{channel.Key}",
  103. t =>
  104. t.DateTime >= this.StartTime &&
  105. t.DateTime <= this.EndTime
  106. , QueryResult);
  107. }
  108. }
  109. private void QueryResult(List<DBFormat> results)
  110. {
  111. if (results.Count < 1)
  112. {
  113. App.Current.Dispatcher.Invoke(() => this.Loading = Visibility.Collapsed);
  114. return;
  115. }
  116. List<DateTime> time = [];
  117. List<float> temp = [];
  118. results = [.. results.OrderBy(t => t.DateTime)];
  119. foreach (var item in results)
  120. {
  121. temp.Add(item.PV);
  122. time.Add(item.DateTime);
  123. }
  124. App.Current.Dispatcher.Invoke(() =>
  125. {
  126. this.SetLine(time, temp, LinePattern.Solid, MarkerStyle.None, 1f, out Brush brush);
  127. this.ChannelDetails[results.First().ChannelIndex].ChannelColor = brush;
  128. this.PlotControl.Plot.HideLegend();
  129. this.PlotControl.Plot.Axes.DateTimeTicksBottom();
  130. this.PlotControl.Plot.Axes.Bottom.TickLabelStyle.Rotation = -45;
  131. this.PlotControl.Plot.Axes.Bottom.TickLabelStyle.Alignment = Alignment.MiddleRight;
  132. this.PlotControl.Plot.Axes.AutoScale();
  133. this.PlotControl.Refresh();
  134. this.PlotControl.Plot.Axes.Zoom(1.085, 1);
  135. this.Loading = Visibility.Collapsed;
  136. });
  137. }
  138. private void SetLine(List<DateTime> time, List<float> value, LinePattern linePattern, MarkerStyle markerStyle, float lineWidth, out Brush brush)
  139. {
  140. Scatter mainScatter = this.PlotControl.Plot.Add.Scatter(time, value);
  141. mainScatter.MarkerStyle = markerStyle;
  142. mainScatter.LineWidth = lineWidth;
  143. mainScatter.LinePattern = linePattern;
  144. brush = new SolidColorBrush((System.Windows.Media.Color)ColorConverter.ConvertFromString(mainScatter.Color.ToStringRGB()));
  145. }
  146. [RelayCommand]
  147. private void ReScale(string para)
  148. {
  149. switch (para)
  150. {
  151. case "+":
  152. this.PlotControl.Plot.Axes.Zoom(1.25, 1);
  153. break;
  154. case "-":
  155. this.PlotControl.Plot.Axes.Zoom(0.8, 1);
  156. break;
  157. case "add":
  158. this.PlotControl.Plot.Axes.Zoom(1, 1.25);
  159. break;
  160. case "minus":
  161. this.PlotControl.Plot.Axes.Zoom(1, 0.8);
  162. break;
  163. default:
  164. this.PlotControl.Plot.Axes.AutoScale();
  165. this.PlotControl.Plot.Axes.Zoom(1.085, 1);
  166. break;
  167. }
  168. this.PlotControl.Refresh();
  169. }
  170. }
  171. public partial class ChannelDetail : ObservableObject
  172. {
  173. public IORM? Orm;
  174. [ObservableProperty]
  175. private Brush _ChannelColor = Default;
  176. [ObservableProperty]
  177. private bool _IsSelected = false;
  178. private readonly static Brush Default = new SolidColorBrush((System.Windows.Media.Color)ColorConverter.ConvertFromString("#7f7f7f"));
  179. partial void OnIsSelectedChanged(bool value)
  180. {
  181. if (!IsSelected)
  182. this.ChannelColor = Default;
  183. }
  184. [ObservableProperty]
  185. private ChannelData? _ChannelData;
  186. }