DBInfoTraceViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. 
  2. using ScottPlot;
  3. using ScottPlot.WPF;
  4. namespace ProximaAnalizer.ViewModels;
  5. internal partial class DBInfoTraceViewModel : ObservableObject
  6. {
  7. public DBInfoTraceViewModel(
  8. TraceData traceData,
  9. DBDataHelper dBDataHelper,
  10. IEventAggregator eventAggregator,
  11. IDialogService dialogService)
  12. {
  13. this._DBDataHelper = dBDataHelper;
  14. this._traceData = traceData;
  15. this._dialogService = dialogService;
  16. this._eventAggregator = eventAggregator;
  17. this.Left = [];
  18. this.Right = [];
  19. this.PlotControl = new();
  20. this._DisplayDataHelper = new(Left, Right);
  21. this._PlotHelper = new(this.PlotControl);
  22. this._PlotHelper.InitPlot();
  23. eventAggregator.GetEvent<RefreshRecipeData>().Subscribe(InitDisplayData);
  24. }
  25. private readonly TraceData _traceData;
  26. private readonly IDialogService _dialogService;
  27. private readonly IEventAggregator _eventAggregator;
  28. private readonly PlotHepler _PlotHelper;
  29. private readonly DBDataHelper _DBDataHelper;
  30. private readonly DisplayDataHelper _DisplayDataHelper;
  31. [ObservableProperty]
  32. private WpfPlot _PlotControl;
  33. [ObservableProperty]
  34. private bool _DisplayAlarm = false;
  35. [ObservableProperty]
  36. private bool _DisplayWarning = false;
  37. [ObservableProperty]
  38. private double _AlarmInverval = 60d;
  39. private void InitDisplayData()
  40. {
  41. if (_traceData.ProcessData is null)
  42. return;
  43. this._HierachyCache.Clear();
  44. this.RecipeSteps.Clear();
  45. this.Clear("Both");
  46. if (!this._DBDataHelper.GetRecipeSteps(_traceData.ProcessData.Guid, out string reason, out List<RecipeStepData>? recipeSteps) || recipeSteps is null)
  47. {
  48. MessageBox.Show(reason, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
  49. return;
  50. }
  51. recipeSteps.ForEach(t => this.RecipeSteps.TryAdd(t.Step_Begin_Time, t.Adapt<SelectRecipeStep>()));
  52. if (!this._DBDataHelper.GetFirstData(_traceData.ProcessData.Process_Begin_Time, out string error, out Dictionary<string, object>? firstData) || firstData is null)
  53. {
  54. MessageBox.Show(error, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
  55. return;
  56. }
  57. this.Hierachy = firstData;
  58. this._HierachyCache.Push(firstData);
  59. this.Alarms.Clear();
  60. }
  61. #region Step Operations
  62. [ObservableProperty]
  63. private ObservableDictionary<DateTime, SelectRecipeStep> _RecipeSteps = [];
  64. private bool AutoGenerated = false;
  65. [RelayCommand]
  66. private void Selected(SelectRecipeStep step)
  67. {
  68. if (RecipeSteps is null)
  69. return;
  70. if (AutoGenerated)
  71. {
  72. UnSelected(step);
  73. return;
  74. }
  75. if (RecipeSteps.Where(t => t.Value.IsSelected).Count() != 2)
  76. return;
  77. DateTime start = RecipeSteps.Where(t => t.Value.IsSelected).First().Key;
  78. DateTime end = RecipeSteps.Where(t => t.Value.IsSelected).Last().Key;
  79. RecipeSteps.Where(t => t.Key >= start && t.Key <= end).Foreach(t => t.Value.IsSelected = true);
  80. this.AutoGenerated = true;
  81. }
  82. [RelayCommand]
  83. private void UnSelected(SelectRecipeStep step)
  84. {
  85. RecipeSteps.Where(t => t.Value.IsSelected).Foreach(t => t.Value.IsSelected = false);
  86. this.AutoGenerated = false;
  87. }
  88. #endregion
  89. #region Data Hierachy Operations
  90. [ObservableProperty]
  91. private IDictionary<string, object>? _Hierachy;
  92. private readonly Stack<IDictionary<string, object>> _HierachyCache = new();
  93. [RelayCommand]
  94. private void SelectHierachy(KeyValuePair<string, object> item)
  95. {
  96. if (this.Hierachy is null)
  97. return;
  98. if (item.Value is IDictionary<string, object> next)
  99. {
  100. this._HierachyCache.Push(this.Hierachy);
  101. this.Hierachy = next;
  102. return;
  103. }
  104. IDialogParameters para = new DialogParameters() { { "Line", item.Value } };
  105. _dialogService.Show("LinePicker", para, AddLineCallback);
  106. }
  107. [RelayCommand]
  108. private void Return()
  109. {
  110. if (this._HierachyCache.TryPop(out var output) && output is not null)
  111. this.Hierachy = output;
  112. }
  113. #endregion
  114. #region Line Operations
  115. [ObservableProperty]
  116. private ObservableDictionary<string, LineType> _Left = [];
  117. [ObservableProperty]
  118. private ObservableDictionary<string, LineType> _Right = [];
  119. [RelayCommand]
  120. private void EditLine(KeyValuePair<string, LineType> item)
  121. {
  122. IDialogParameters para = new DialogParameters() { { "Line", item.Key } };
  123. _dialogService.Show("LinePicker", para, AddLineCallback);
  124. }
  125. private void AddLineCallback(IDialogResult dialogResult)
  126. {
  127. dialogResult.Parameters.TryGetValue("Line", out LineType? lineType);
  128. dialogResult.Parameters.TryGetValue("Name", out string? line);
  129. dialogResult.Parameters.TryGetValue("Axis", out string? Axis);
  130. if (lineType is null || string.IsNullOrEmpty(line) || string.IsNullOrEmpty(Axis))
  131. return;
  132. lineType.IsEnable = false;
  133. ObservableDictionary<string, LineType>? lineCollection = Axis switch
  134. {
  135. "L" => Left,
  136. "R" => Right,
  137. _ => default
  138. };
  139. if (lineCollection is null)
  140. return;
  141. if (lineCollection.TryGetValue(line, out LineType? oldType) && oldType is LineType oldLine)
  142. oldLine.IsEnable = true;
  143. lineCollection[line] = lineType;
  144. switch (Axis)
  145. {
  146. case "L":
  147. this.RemoveRight(line);
  148. break;
  149. case "R":
  150. this.RemoveLeft(line);
  151. break;
  152. default:
  153. break;
  154. }
  155. }
  156. [RelayCommand]
  157. private void RemoveLeft(string key)
  158. {
  159. this.Left.TryRemove(key, out LineType? line);
  160. if (line is LineType lineType)
  161. lineType.IsEnable = true;
  162. }
  163. [RelayCommand]
  164. private void RemoveRight(string key)
  165. {
  166. this.Right.TryRemove(key, out LineType? line);
  167. if (line is LineType lineType)
  168. lineType.IsEnable = true;
  169. }
  170. [RelayCommand]
  171. private void SaveLoad(string para)
  172. {
  173. switch (para)
  174. {
  175. case "Save":
  176. {
  177. SaveFileDialog saveFileDialog = new()
  178. {
  179. Filter = "Line Picker |*.lpr;",
  180. FileName = "Default.lpr",
  181. InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
  182. };
  183. if (saveFileDialog.ShowDialog() != true)
  184. return;
  185. List<LineSave> left = [];
  186. foreach (var item in this.Left)
  187. {
  188. LineSave save = new()
  189. {
  190. Direction = "L",
  191. Name = item.Key,
  192. Dash = item.Value.DashArray,
  193. Color = item.Value.HexRGB
  194. };
  195. left.Add(save);
  196. }
  197. foreach (var item in this.Right)
  198. {
  199. LineSave save = new()
  200. {
  201. Direction = "R",
  202. Name = item.Key,
  203. Dash = item.Value.DashArray,
  204. Color = item.Value.HexRGB
  205. };
  206. left.Add(save);
  207. }
  208. try
  209. {
  210. using StreamWriter sw = new(saveFileDialog.FileName);
  211. sw.WriteLine(JsonSerializer.Serialize(left));
  212. MessageBox.Show("文件保存成功", "Save Line Picker File", MessageBoxButton.OK, MessageBoxImage.Information);
  213. }
  214. catch
  215. {
  216. MessageBox.Show("文件保存失败", "Save Line Picker File", MessageBoxButton.OK, MessageBoxImage.Error);
  217. }
  218. return;
  219. }
  220. case "Load":
  221. {
  222. OpenFileDialog open = new()
  223. {
  224. Filter = "Line Picker |*.lpr;",
  225. FileName = "Default.lpr",
  226. InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
  227. };
  228. if (open.ShowDialog() != true)
  229. return;
  230. try
  231. {
  232. using StreamReader sr = new(open.FileName);
  233. string? left = sr.ReadToEnd();
  234. if (string.IsNullOrEmpty(left))
  235. {
  236. MessageBox.Show("文件读取失败", "Save Line Picker File", MessageBoxButton.OK, MessageBoxImage.Error);
  237. return;
  238. }
  239. if (JsonSerializer.Deserialize<List<LineSave>>(left) is not List<LineSave> lines)
  240. {
  241. MessageBox.Show("文件读取失败", "Save Line Picker File", MessageBoxButton.OK, MessageBoxImage.Error);
  242. return;
  243. }
  244. this.Left.Clear();
  245. this.Right.Clear();
  246. foreach (var line in lines)
  247. {
  248. if (string.IsNullOrEmpty(line.Name) || string.IsNullOrEmpty(line.Color) || line.Dash is null)
  249. continue;
  250. DialogResult result = new();
  251. result.Parameters.Add("Line", new LineType(line.Color, line.Dash));
  252. result.Parameters.Add("Name", line.Name);
  253. result.Parameters.Add("Axis", line.Direction!);
  254. this.AddLineCallback(result);
  255. }
  256. }
  257. catch
  258. {
  259. MessageBox.Show("文件读取失败", "Save Line Picker File", MessageBoxButton.OK, MessageBoxImage.Error);
  260. }
  261. return;
  262. }
  263. default:
  264. break;
  265. }
  266. }
  267. [RelayCommand]
  268. private void Clear(string para)
  269. {
  270. switch (para)
  271. {
  272. case "L":
  273. this.Left.Foreach(t => ((LineType)t.Value).IsEnable = true);
  274. this.Left.Clear();
  275. break;
  276. case "R":
  277. this.Right.Foreach(t => ((LineType)t.Value).IsEnable = true);
  278. this.Right.Clear();
  279. break;
  280. default:
  281. this.Right.Foreach(t => ((LineType)t.Value).IsEnable = true);
  282. this.Right.Clear();
  283. this.Left.Foreach(t => ((LineType)t.Value).IsEnable = true);
  284. this.Left.Clear();
  285. break;
  286. }
  287. }
  288. #endregion
  289. #region Plot Operations
  290. [ObservableProperty]
  291. private ObservableCollection<EventData> _Alarms = [];
  292. [RelayCommand]
  293. private void GeneratePlot()
  294. {
  295. if (!this.RecipeSteps.Any(t => t.Value.IsSelected))
  296. {
  297. MessageBox.Show("Recipe Step 未选择", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
  298. return;
  299. }
  300. if (this.Left.Count == 0 && Right.Count == 0)
  301. {
  302. MessageBox.Show("分析数据 未选择", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
  303. return;
  304. }
  305. DateTime startTime = this.RecipeSteps.Where(t => t.Value.IsSelected).First().Key;
  306. DateTime endTime = this.RecipeSteps.Where(t => t.Value.IsSelected).Last().Key;
  307. this._DBDataHelper.SetTimeRange(startTime, endTime);
  308. this.GetDBFields(out HashSet<string> pmFields, out HashSet<string> systemFields);
  309. if (!this._DBDataHelper.GetPMData(pmFields, out List<dynamic>? mainData) || mainData is null)
  310. return;
  311. this._DBDataHelper.GetSystemData(systemFields, out List<dynamic>? subData);
  312. this._DisplayDataHelper.ClearData();
  313. this._DisplayDataHelper.CreateData(mainData, subData);
  314. this.PlotControl.Plot.Clear();
  315. foreach (var item in this._DisplayDataHelper.DataLeft)
  316. this._PlotHelper.AddLeftLine(this._DisplayDataHelper.Time, item.Value, ((LineType)Left[item.Key])!.LinePattern, MarkerStyle.None, 1.5f, ((LineType)Left[item.Key])!.HexRGB!);
  317. foreach (var item in this._DisplayDataHelper.DataRight)
  318. this._PlotHelper.AddRightLine(this._DisplayDataHelper.Time, item.Value, ((LineType)Right[item.Key])!.LinePattern, MarkerStyle.None, 1.5f, ((LineType)Right[item.Key])!.HexRGB!);
  319. if (!this._DBDataHelper.GetAlarmData(out List<EventData>? alarm) || alarm is null)
  320. return;
  321. this.GenerateAlarm(alarm);
  322. this.PlotControl.Plot.Axes.AutoScale();
  323. this.PlotControl.Refresh();
  324. }
  325. private void GenerateAlarm(List<EventData> alarm)
  326. {
  327. this.Alarms.Clear();
  328. this.Alarms.AddRange(alarm);
  329. Dictionary<string, DateTime> intervalFilter = [];
  330. HashSet<DateTime> dup = [];
  331. foreach (EventData item in alarm)
  332. {
  333. if (string.IsNullOrEmpty(item.Event_Enum))
  334. continue;
  335. if (intervalFilter.TryGetValue(item.Event_Enum, out DateTime time) &&
  336. (item.Occur_Time - time).TotalSeconds < AlarmInverval)
  337. continue;
  338. intervalFilter[item.Event_Enum] = item.Occur_Time;
  339. switch (item.Level)
  340. {
  341. case "Alarm":
  342. if (!this.DisplayAlarm)
  343. continue;
  344. if (!dup.Add(item.Occur_Time))
  345. continue;
  346. this._PlotHelper.AddAlarmLine(item.Occur_Time, $"{item.Source}");
  347. break;
  348. case "Warning":
  349. if (!this.DisplayWarning)
  350. continue;
  351. if (!dup.Add(item.Occur_Time))
  352. continue;
  353. this._PlotHelper.AddWarningLine(item.Occur_Time, $"{item.Source}");
  354. break;
  355. default:
  356. break;
  357. }
  358. }
  359. }
  360. private void GetDBFields(out HashSet<string> pmFields, out HashSet<string> systemFields)
  361. {
  362. pmFields = ["time"];
  363. systemFields = [];
  364. foreach (string field in this.Left.Keys)
  365. {
  366. _ = field switch
  367. {
  368. string s when s.Contains("PM1") => pmFields.Add(field),
  369. string s when s.Contains("System") => systemFields.Add(field),
  370. _ => systemFields.Add(field)
  371. };
  372. }
  373. foreach (string field in this.Right.Keys)
  374. {
  375. _ = field switch
  376. {
  377. string s when s.Contains("PM1") => pmFields.Add(field),
  378. string s when s.Contains("System") => systemFields.Add(field),
  379. _ => systemFields.Add(field)
  380. };
  381. }
  382. }
  383. [RelayCommand]
  384. private void ViewTraceData()
  385. {
  386. IDialogParameters para = new DialogParameters
  387. {
  388. { "Data", this._DisplayDataHelper},
  389. {"Alarm", this.Alarms }
  390. };
  391. this._dialogService.Show("TraceData", para, null);
  392. }
  393. [RelayCommand]
  394. private void ReScale(string para)
  395. {
  396. switch (para)
  397. {
  398. case "+":
  399. this.PlotControl.Plot.Axes.Zoom(1.25, 1);
  400. break;
  401. case "-":
  402. this.PlotControl.Plot.Axes.Zoom(0.8, 1);
  403. break;
  404. case "add":
  405. this.PlotControl.Plot.Axes.Zoom(1, 1.25);
  406. break;
  407. case "minus":
  408. this.PlotControl.Plot.Axes.Zoom(1, 0.8);
  409. break;
  410. default:
  411. this.PlotControl.Plot.Axes.AutoScale();
  412. this.PlotControl.Plot.Axes.Zoom(1.085, 1);
  413. break;
  414. }
  415. this.PlotControl.Refresh();
  416. }
  417. #endregion
  418. [RelayCommand]
  419. private void AlarmDetail(object para)
  420. {
  421. if (para is not EventData eventData)
  422. return;
  423. _eventAggregator.GetEvent<WindowSwitch>().Page = Pages.HistoryData;
  424. _eventAggregator.GetEvent<WindowSwitch>().Publish();
  425. RefreshAlarmData alarmData = _eventAggregator.GetEvent<RefreshAlarmData>();
  426. alarmData.Selected = eventData;
  427. alarmData.FromPage = Pages.MainWindow;
  428. alarmData.Publish();
  429. }
  430. [RelayCommand]
  431. private void ReturnPage()
  432. {
  433. _eventAggregator.GetEvent<WindowSwitch>().Page = Pages.RecipeStepNavi;
  434. _eventAggregator.GetEvent<WindowSwitch>().Publish();
  435. }
  436. }
  437. public class LineSave
  438. {
  439. public string? Direction { get; set; }
  440. public string? Name { get; set; }
  441. public string? Color { get; set; }
  442. public DoubleCollection? Dash { get; set; }
  443. }
  444. public partial class SelectRecipeStep : ObservableObject
  445. {
  446. [ObservableProperty]
  447. private bool _IsSelected;
  448. public string? Guid { get; set; }
  449. public DateTime Step_Begin_Time { get; set; }
  450. public DateTime Step_End_Time { get; set; }
  451. public string? Step_Name { get; set; }
  452. public float Step_Time { get; set; }
  453. public int Step_Number { get; set; }
  454. public string? Sub_Recipe_Step_Time { get; set; }
  455. public string? Sub_Recipe_Step_Number { get; set; }
  456. public string? Sub_Recipe_Step_Name { get; set; }
  457. public string? Sub_Recipe_Loop_Info { get; set; }
  458. public string? Temp_correction { get; set; }
  459. public string? Temp_pid { get; set; }
  460. }