DBInfoTraceViewModel.cs 18 KB

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