DBInfoTraceViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using Mapster;
  2. using ProximaAnalizer.Helpers;
  3. using ScottPlot;
  4. using ScottPlot.WPF;
  5. using SqlSugar;
  6. using System.Collections.ObjectModel;
  7. using System.Windows;
  8. using Universal;
  9. namespace ProximaAnalizer.ViewModels;
  10. internal partial class DBInfoTraceViewModel : ObservableObject
  11. {
  12. public DBInfoTraceViewModel(
  13. TraceData traceData,
  14. DBDataHelper dBDataHelper,
  15. IEventAggregator eventAggregator,
  16. IDialogService dialogService)
  17. {
  18. this._DBDataHelper = dBDataHelper;
  19. this._traceData = traceData;
  20. this._dialogService = dialogService;
  21. this.Left = [];
  22. this.Right = [];
  23. this.PlotControl = new();
  24. this._DisplayDataHelper = new(Left, Right);
  25. this._PlotHelper = new(this.PlotControl);
  26. this._PlotHelper.InitPlot();
  27. eventAggregator.GetEvent<RefreshRecipeData>().Subscribe(InitDisplayData);
  28. }
  29. private readonly TraceData _traceData;
  30. private readonly IDialogService _dialogService;
  31. private readonly PlotHepler _PlotHelper;
  32. private readonly DBDataHelper _DBDataHelper;
  33. private readonly DisplayDataHelper _DisplayDataHelper;
  34. [ObservableProperty]
  35. private WpfPlot _PlotControl;
  36. [ObservableProperty]
  37. private bool _DisplayAlarm = false;
  38. [ObservableProperty]
  39. private bool _DisplayWarning = false;
  40. [ObservableProperty]
  41. private double _AlarmInverval = 60d;
  42. private void InitDisplayData()
  43. {
  44. if (_traceData.ProcessData is null)
  45. return;
  46. this._HierachyCache.Clear();
  47. this.RecipeSteps.Clear();
  48. this.Clear("Both");
  49. if (!this._DBDataHelper.GetRecipeSteps(_traceData.ProcessData.Guid, out string reason, out List<RecipeStepData>? recipeSteps) || recipeSteps is null)
  50. {
  51. MessageBox.Show(reason, "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  52. return;
  53. }
  54. recipeSteps.ForEach(t => this.RecipeSteps.TryAdd(t.Step_Begin_Time, t.Adapt<SelectRecipeStep>()));
  55. if (!this._DBDataHelper.GetFirstData(_traceData.ProcessData.Process_Begin_Time, out string error, out Dictionary<string, object>? firstData) || firstData is null)
  56. {
  57. MessageBox.Show(error, "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  58. return;
  59. }
  60. this.Hierachy = firstData;
  61. this._HierachyCache.Push(firstData);
  62. this.Alarms.Clear();
  63. }
  64. #region Step Operations
  65. [ObservableProperty]
  66. private ObservableDictionary<DateTime, SelectRecipeStep> _RecipeSteps = [];
  67. private bool AutoGenerated = false;
  68. [RelayCommand]
  69. private void Selected(SelectRecipeStep step)
  70. {
  71. if (RecipeSteps is null)
  72. return;
  73. if (AutoGenerated)
  74. {
  75. UnSelected(step);
  76. return;
  77. }
  78. if (RecipeSteps.Where(t => t.Value.IsSelected).Count() != 2)
  79. return;
  80. DateTime start = RecipeSteps.Where(t => t.Value.IsSelected).First().Key;
  81. DateTime end = RecipeSteps.Where(t => t.Value.IsSelected).Last().Key;
  82. RecipeSteps.Where(t => t.Key >= start && t.Key <= end).Foreach(t => t.Value.IsSelected = true);
  83. this.AutoGenerated = true;
  84. }
  85. [RelayCommand]
  86. private void UnSelected(SelectRecipeStep step)
  87. {
  88. RecipeSteps.Where(t => t.Value.IsSelected).Foreach(t => t.Value.IsSelected = false);
  89. this.AutoGenerated = false;
  90. }
  91. #endregion
  92. #region Data Hierachy Operations
  93. [ObservableProperty]
  94. private IDictionary<string, object>? _Hierachy;
  95. private readonly Stack<IDictionary<string, object>> _HierachyCache = new();
  96. [RelayCommand]
  97. private void SelectHierachy(KeyValuePair<string, object> item)
  98. {
  99. if (this.Hierachy is null)
  100. return;
  101. if (item.Value is IDictionary<string, object> next)
  102. {
  103. this._HierachyCache.Push(this.Hierachy);
  104. this.Hierachy = next;
  105. return;
  106. }
  107. IDialogParameters para = new DialogParameters() { { "Line", item.Value } };
  108. _dialogService.Show("LinePicker", para, AddLineCallback);
  109. }
  110. [RelayCommand]
  111. private void Return()
  112. {
  113. if (this._HierachyCache.TryPop(out var output) && output is not null)
  114. this.Hierachy = output;
  115. }
  116. #endregion
  117. #region Line Operations
  118. [ObservableProperty]
  119. private ObservableDictionary<string, object> _Left = [];
  120. [ObservableProperty]
  121. private ObservableDictionary<string, object> _Right = [];
  122. [RelayCommand]
  123. private void EditLine(KeyValuePair<string, object> item)
  124. {
  125. IDialogParameters para = new DialogParameters() { { "Line", item.Key } };
  126. _dialogService.Show("LinePicker", para, AddLineCallback);
  127. }
  128. private void AddLineCallback(IDialogResult dialogResult)
  129. {
  130. dialogResult.Parameters.TryGetValue("Line", out LineType? lineType);
  131. dialogResult.Parameters.TryGetValue("Name", out string? line);
  132. dialogResult.Parameters.TryGetValue("Axis", out string? Axis);
  133. if (lineType is null || string.IsNullOrEmpty(line) || string.IsNullOrEmpty(Axis))
  134. return;
  135. ObservableDictionary<string, object>? lineCollection = Axis switch
  136. {
  137. "L" => Left,
  138. "R" => Right,
  139. _ => default
  140. };
  141. if (lineCollection is null)
  142. return;
  143. if (lineCollection.TryGetValue(line, out object? oldType) && oldType is LineType oldLine)
  144. oldLine.IsEnable = true;
  145. lineCollection[line] = lineType;
  146. }
  147. [RelayCommand]
  148. private void RemoveLeft(string key)
  149. {
  150. this.Left.TryRemove(key, out object? line);
  151. if (line is LineType lineType)
  152. lineType.IsEnable = true;
  153. }
  154. [RelayCommand]
  155. private void RemoveRight(string key)
  156. {
  157. this.Right.TryRemove(key, out object? line);
  158. if (line is LineType lineType)
  159. lineType.IsEnable = true;
  160. }
  161. [RelayCommand]
  162. private void Clear(string para)
  163. {
  164. switch (para)
  165. {
  166. case "L":
  167. this.Left.Foreach(t => ((LineType)t.Value).IsEnable = true);
  168. this.Left.Clear();
  169. break;
  170. case "R":
  171. this.Right.Foreach(t => ((LineType)t.Value).IsEnable = true);
  172. this.Right.Clear();
  173. break;
  174. default:
  175. this.Right.Foreach(t => ((LineType)t.Value).IsEnable = true);
  176. this.Right.Clear();
  177. this.Left.Foreach(t => ((LineType)t.Value).IsEnable = true);
  178. this.Left.Clear();
  179. break;
  180. }
  181. }
  182. #endregion
  183. #region Plot Operations
  184. [ObservableProperty]
  185. private ObservableCollection<EventData> _Alarms = [];
  186. [RelayCommand]
  187. private void GeneratePlot()
  188. {
  189. if (!this.RecipeSteps.Any(t => t.Value.IsSelected))
  190. {
  191. MessageBox.Show("Recipe Step 未选择", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  192. return;
  193. }
  194. if (this.Left.Count == 0 && Right.Count == 0)
  195. {
  196. MessageBox.Show("分析数据 未选择", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  197. return;
  198. }
  199. DateTime startTime = this.RecipeSteps.Where(t => t.Value.IsSelected).First().Key;
  200. DateTime endTime = this.RecipeSteps.Where(t => t.Value.IsSelected).Last().Key;
  201. this._DBDataHelper.SetTimeRange(startTime, endTime);
  202. this.GetDBFields(out HashSet<string> pmFields, out HashSet<string> systemFields);
  203. if (!this._DBDataHelper.GetPMData(pmFields, out List<dynamic>? mainData) || mainData is null)
  204. return;
  205. this._DBDataHelper.GetSystemData(systemFields, out List<dynamic>? subData);
  206. this._DisplayDataHelper.ClearData();
  207. this._DisplayDataHelper.CreateData(mainData, subData);
  208. this.PlotControl.Plot.Clear();
  209. foreach (var item in this._DisplayDataHelper.DataLeft)
  210. this._PlotHelper.AddLeftLine(this._DisplayDataHelper.Time, item.Value, ((LineType)Left[item.Key])!.LinePattern, MarkerStyle.None, 1.5f, ((LineType)Left[item.Key])!.HexRGB);
  211. foreach (var item in this._DisplayDataHelper.DataRight)
  212. this._PlotHelper.AddRightLine(this._DisplayDataHelper.Time, item.Value, ((LineType)Right[item.Key])!.LinePattern, MarkerStyle.None, 1.5f, ((LineType)Right[item.Key])!.HexRGB);
  213. if (!this._DBDataHelper.GetAlarmData(out List<EventData>? alarm) || alarm is null)
  214. return;
  215. this.GenerateAlarm(alarm);
  216. this.PlotControl.Plot.Axes.AutoScale();
  217. this.PlotControl.Refresh();
  218. }
  219. public void GenerateAlarm(List<EventData> alarm)
  220. {
  221. this.Alarms.Clear();
  222. this.Alarms.AddRange(alarm);
  223. Dictionary<string, DateTime> intervalFilter = [];
  224. HashSet<DateTime> dup = [];
  225. foreach (EventData item in alarm)
  226. {
  227. if (string.IsNullOrEmpty(item.Description))
  228. continue;
  229. if (intervalFilter.TryGetValue(item.Description, out DateTime time) &&
  230. (item.Occur_Time - time).TotalSeconds < AlarmInverval)
  231. continue;
  232. intervalFilter[item.Description] = item.Occur_Time;
  233. switch (item.Level)
  234. {
  235. case "Alarm":
  236. if (!this.DisplayAlarm)
  237. continue;
  238. if (!dup.Add(item.Occur_Time))
  239. continue;
  240. this._PlotHelper.AddAlarmLine(item.Occur_Time, $"{item.Source}");
  241. break;
  242. case "Warning":
  243. if (!this.DisplayWarning)
  244. continue;
  245. if (!dup.Add(item.Occur_Time))
  246. continue;
  247. this._PlotHelper.AddWarningLine(item.Occur_Time, $"{item.Source}");
  248. break;
  249. default:
  250. break;
  251. }
  252. }
  253. }
  254. private void GetDBFields(out HashSet<string> pmFields, out HashSet<string> systemFields)
  255. {
  256. pmFields = ["time"];
  257. systemFields = [];
  258. foreach (string field in this.Left.Keys)
  259. {
  260. _ = field switch
  261. {
  262. string s when s.Contains("PM1") => pmFields.Add(field),
  263. string s when s.Contains("System") => systemFields.Add(field),
  264. _ => systemFields.Add(field)
  265. };
  266. }
  267. foreach (string field in this.Right.Keys)
  268. {
  269. _ = field switch
  270. {
  271. string s when s.Contains("PM1") => pmFields.Add(field),
  272. string s when s.Contains("System") => systemFields.Add(field),
  273. _ => systemFields.Add(field)
  274. };
  275. }
  276. }
  277. [RelayCommand]
  278. private void ViewTraceData()
  279. {
  280. IDialogParameters para = new DialogParameters
  281. {
  282. { "Data", this._DisplayDataHelper},
  283. };
  284. this._dialogService.Show("TraceData", para, null);
  285. }
  286. [RelayCommand]
  287. private void ReScale(string para)
  288. {
  289. switch (para)
  290. {
  291. case "+":
  292. this.PlotControl.Plot.Axes.Zoom(1.25, 1);
  293. break;
  294. case "-":
  295. this.PlotControl.Plot.Axes.Zoom(0.8, 1);
  296. break;
  297. case "add":
  298. this.PlotControl.Plot.Axes.Zoom(1, 1.25);
  299. break;
  300. case "minus":
  301. this.PlotControl.Plot.Axes.Zoom(1, 0.8);
  302. break;
  303. default:
  304. this.PlotControl.Plot.Axes.AutoScale();
  305. this.PlotControl.Plot.Axes.Zoom(1.085, 1);
  306. break;
  307. }
  308. this.PlotControl.Refresh();
  309. }
  310. #endregion
  311. }
  312. public partial class SelectRecipeStep : ObservableObject
  313. {
  314. [ObservableProperty]
  315. private bool _IsSelected;
  316. public string? Guid { get; set; }
  317. public DateTime Step_Begin_Time { get; set; }
  318. public DateTime Step_End_Time { get; set; }
  319. public string? Step_Name { get; set; }
  320. public float Step_Time { get; set; }
  321. public int Step_Number { get; set; }
  322. public string? Sub_Recipe_Step_Time { get; set; }
  323. public string? Sub_Recipe_Step_Number { get; set; }
  324. public string? Sub_Recipe_Step_Name { get; set; }
  325. public string? Sub_Recipe_Loop_Info { get; set; }
  326. public string? Temp_correction { get; set; }
  327. public string? Temp_pid { get; set; }
  328. }