DBInfoTraceViewModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. using Mapster;
  2. using Prism.Dialogs;
  3. using ScottPlot;
  4. using ScottPlot.Colormaps;
  5. using ScottPlot.Plottables;
  6. using ScottPlot.WPF;
  7. using SqlSugar;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using System.Windows;
  11. using System.Windows.Media;
  12. using Universal;
  13. using static Dm.FldrStatement;
  14. namespace ProximaAnalizer.ViewModels;
  15. internal partial class DBInfoTraceViewModel : ObservableObject
  16. {
  17. public DBInfoTraceViewModel(TraceData traceData,
  18. SqlSugarCustom sqlSugarCustom,
  19. IEventAggregator eventAggregator,
  20. IDialogService dialogService)
  21. {
  22. this.traceData = traceData;
  23. this.sqlSugarCustom = sqlSugarCustom;
  24. this.dialogService = dialogService;
  25. this.PlotControl = new();
  26. this.InitPlot();
  27. eventAggregator.GetEvent<RefreshRecipeData>().Subscribe(Search);
  28. }
  29. private void InitPlot()
  30. {
  31. this.PlotControl.Plot.Grid.XAxisStyle.MajorLineStyle.Width = 1f;
  32. this.PlotControl.Plot.Grid.YAxisStyle.MajorLineStyle.Width = 1f;
  33. this.PlotControl.Plot.Axes.Bottom.TickLabelStyle.Alignment = Alignment.MiddleLeft;
  34. this.PlotControl.Plot.RenderManager.RenderStarting += (s, e) =>
  35. {
  36. Tick[] ticks = this.PlotControl.Plot.Axes.Bottom.TickGenerator.Ticks;
  37. for (int i = 0; i < ticks.Length; i++)
  38. {
  39. DateTime dt = DateTime.FromOADate(ticks[i].Position);
  40. string label = $"{dt:MM-dd HH:mm:ss}";
  41. ticks[i] = new Tick(ticks[i].Position, label);
  42. }
  43. };
  44. this.PlotControl.Plot.FigureBackground.Color = ScottPlot.Colors.Transparent;
  45. this.PlotControl.Plot.Axes.Bottom.TickLabelStyle.Rotation = 90;
  46. this.PlotControl.Plot.Axes.Bottom.TickLabelStyle.Alignment = Alignment.MiddleLeft;
  47. //PixelPadding padding = new(40, 20, 65, 10);
  48. //this.PlotControl.Plot.Layout.Fixed(padding);
  49. this.PlotControl.Plot.FigureBackground.Color = ScottPlot.Colors.Transparent;
  50. //UpdateDetail();
  51. }
  52. [ObservableProperty]
  53. private WpfPlot _PlotControl;
  54. private readonly TraceData traceData;
  55. private readonly SqlSugarCustom sqlSugarCustom;
  56. private readonly IDialogService dialogService;
  57. [ObservableProperty]
  58. private ObservableDictionary<DateTime, SelectRecipeStep> _RecipeSteps = [];
  59. [ObservableProperty]
  60. private IDictionary<string, object>? _Hierachy;
  61. [ObservableProperty]
  62. private ObservableDictionary<string, object> _Left = [];
  63. [ObservableProperty]
  64. private ObservableDictionary<string, object> _Right = [];
  65. private readonly Stack<IDictionary<string, object>> Cache = new();
  66. private bool AutoGenerated = false;
  67. [RelayCommand]
  68. private void Selected(SelectRecipeStep step)
  69. {
  70. if (RecipeSteps is null)
  71. return;
  72. if (sqlSugarCustom.Client is null)
  73. return;
  74. if (AutoGenerated)
  75. {
  76. UnSelected(step);
  77. return;
  78. }
  79. if (RecipeSteps.Where(t => t.Value.IsSelected).Count() != 2)
  80. return;
  81. DateTime start = RecipeSteps.Where(t => t.Value.IsSelected).First().Key;
  82. DateTime end = RecipeSteps.Where(t => t.Value.IsSelected).Last().Key;
  83. RecipeSteps.Where(t => t.Key >= start && t.Key <= end).Foreach(t => t.Value.IsSelected = true);
  84. this.AutoGenerated = true;
  85. }
  86. [RelayCommand]
  87. private void UnSelected(SelectRecipeStep step)
  88. {
  89. RecipeSteps.Where(t => t.Value.IsSelected).Foreach(t => t.Value.IsSelected = false);
  90. this.AutoGenerated = false;
  91. }
  92. [RelayCommand]
  93. private void SelectHierachy(KeyValuePair<string, object> item)
  94. {
  95. if (this.Hierachy is null)
  96. return;
  97. if (item.Value is IDictionary<string, object> next)
  98. {
  99. this.Cache.Push(this.Hierachy);
  100. this.Hierachy = next;
  101. return;
  102. }
  103. //this.Left.TryAdd((string)item.Value, item.Value);
  104. //IDialogParameters parameters, Action< IDialogResult > callback
  105. IDialogParameters para = new DialogParameters() { { "Line", item.Value } };
  106. dialogService.Show("LinePicker", para, AddLine);
  107. }
  108. [RelayCommand]
  109. private void EditLine(KeyValuePair<string, object> item)
  110. {
  111. IDialogParameters para = new DialogParameters() { { "Line", item.Key } };
  112. dialogService.Show("LinePicker", para, AddLine);
  113. }
  114. private void AddLine(IDialogResult dialogResult)
  115. {
  116. dialogResult.Parameters.TryGetValue("Line", out LineType? lineType);
  117. dialogResult.Parameters.TryGetValue("Name", out string? line);
  118. dialogResult.Parameters.TryGetValue("Axis", out string? Axis);
  119. if (lineType is null || string.IsNullOrEmpty(line) || string.IsNullOrEmpty(Axis))
  120. return;
  121. ObservableDictionary<string, object>? lineCollection = Axis switch
  122. {
  123. "L" => Left,
  124. "R" => Right,
  125. _ => default
  126. };
  127. if (lineCollection is null)
  128. return;
  129. if (lineCollection.TryGetValue(line, out object? oldType) && oldType is LineType oldLine)
  130. oldLine.IsEnable = true;
  131. lineCollection[line] = lineType;
  132. }
  133. [RelayCommand]
  134. private void Return()
  135. {
  136. if (this.Cache.TryPop(out var output) && output is not null)
  137. this.Hierachy = output;
  138. }
  139. [RelayCommand]
  140. private void Search()
  141. {
  142. if (traceData.ProcessData is null)
  143. return;
  144. if (sqlSugarCustom.Client is null)
  145. return;
  146. this.Cache.Clear();
  147. this.RecipeSteps ??= [];
  148. this.RecipeSteps.Clear();
  149. this.Clear("");
  150. DateTime beginTime = traceData.ProcessData.Process_Begin_Time;
  151. DateTime endTime = traceData.ProcessData.Process_End_Time;
  152. long beginTicks = beginTime.Ticks;
  153. long endTicks = endTime.Ticks;
  154. List<RecipeStepData> recipeSteps;
  155. try
  156. {
  157. recipeSteps = sqlSugarCustom.Client.Queryable<RecipeStepData>().AS("recipe_step_data")
  158. .Where(t => t.Process_Data_Guid == traceData.ProcessData.Guid)
  159. .OrderBy(t => t.Step_Begin_Time).ToList();
  160. }
  161. catch
  162. {
  163. MessageBox.Show($"recipe_step_data 记录不存在", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  164. return;
  165. }
  166. if (recipeSteps.Count == 0)
  167. {
  168. MessageBox.Show($"recipe_step 步骤数量为0", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  169. return;
  170. }
  171. foreach (var item in recipeSteps)
  172. {
  173. SelectRecipeStep step = new();
  174. item.Adapt(step);
  175. this.RecipeSteps.TryAdd(item.Step_Begin_Time, step);
  176. }
  177. dynamic f;
  178. dynamic system;
  179. try
  180. {
  181. f = sqlSugarCustom.Client.Queryable<dynamic>().AS($"\"{beginTime:yyyyMMdd}.PM1\"").First();
  182. system = sqlSugarCustom.Client.Queryable<dynamic>().AS($"\"{beginTime:yyyyMMdd}.System\"").First();
  183. }
  184. catch
  185. {
  186. MessageBox.Show($"{beginTime:yyyyMMdd} 记录不存在", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  187. return;
  188. }
  189. GeneralProcessData processData = new();
  190. if (f is not IDictionary<string, object> input)
  191. return;
  192. if (!processData.ToDictionary(input, out Dictionary<string, object>? output) || output is null)
  193. return;
  194. if (output["PM1"] is not IDictionary<string, object> pmData)
  195. return;
  196. if (!processData.ToDictionary(system, out Dictionary<string, object>? systemDic) || systemDic is null)
  197. return;
  198. Dictionary<string, object> temp = [];
  199. Dictionary<string, object> PM1 = [];
  200. Dictionary<string, object> System = [];
  201. temp.Add("PM1", PM1);
  202. temp.Add("System", System);
  203. this.CreateTablePM(pmData, PM1);
  204. this.CreateTableSystem(systemDic!, System);
  205. this.Hierachy = temp;
  206. this.Cache.Push(temp);
  207. }
  208. private bool CreateTablePM(IDictionary<string, object> data, IDictionary<string, object> cache)
  209. {
  210. Dictionary<string, object> ValueSensor = [];
  211. Dictionary<string, object> StatusSensor = [];
  212. Dictionary<string, object> leakCheck = [];
  213. Dictionary<string, object> recipe = [];
  214. Dictionary<string, object> aoValue = [];
  215. Dictionary<string, object> ffu = [];
  216. Dictionary<string, object> mfc = [];
  217. Dictionary<string, object> gaslineHeater = [];
  218. Dictionary<string, object> bufferFoup = [];
  219. Dictionary<string, object> avValue = [];
  220. foreach (var item in data)
  221. {
  222. if (item.Value is not IDictionary<string, object> values)
  223. {
  224. switch (item.Key)
  225. {
  226. case string s when s.EndsWith("Enable"):
  227. continue;
  228. case string s when s.StartsWith("LeakCheck"):
  229. leakCheck.Add(item.Key, item.Value);
  230. continue;
  231. default:
  232. recipe.Add(item.Key, item.Value);
  233. continue;
  234. }
  235. }
  236. switch (item.Key)
  237. {
  238. case "APC":
  239. case "APCVATGV":
  240. case "BoatElevatorServo":
  241. case "BoatRotationServo":
  242. case "BufferServo":
  243. case "Shutter":
  244. CreateTable(cache, item.Key, values);
  245. continue;
  246. case string s when s.StartsWith("Trig"):
  247. if (item.Value is IDictionary<string, object> value)
  248. aoValue.Add(item.Key, value["AOValue"]);
  249. continue;
  250. case string s when s.StartsWith("FS"):
  251. case string s1 when s1.StartsWith("PG"):
  252. case string s2 when s2.StartsWith("PS"):
  253. case string s3 when s3.StartsWith("VG"):
  254. if (item.Value is IDictionary<string, object> vss)
  255. ValueSensor.Add(item.Key, vss["Value"]);
  256. continue;
  257. case string s when s.StartsWith("Sensor"):
  258. if (item.Value is IDictionary<string, object> status)
  259. StatusSensor.Add(item.Key, status["Value"]);
  260. continue;
  261. case string s when s.StartsWith("MFC"):
  262. if (item.Value is IDictionary<string, object> mfcs)
  263. mfcs.Foreach(t => mfc.Add($"{item.Key}_{t.Key}", t.Value));
  264. continue;
  265. case string s when s.StartsWith("FFU"):
  266. if (item.Value is IDictionary<string, object> ffus)
  267. ffus.Foreach(t => ffu.Add($"{item.Key}_{t.Key}", t.Value));
  268. continue;
  269. case string s when s.StartsWith("GaselineHeater"):
  270. if (item.Value is IDictionary<string, object> gaslines)
  271. gaslines.Foreach(t => gaslineHeater.Add($"{item.Key}_{t.Key}", t.Value));
  272. continue;
  273. case string s when s.StartsWith("Valve"):
  274. if (item.Value is IDictionary<string, object> valves)
  275. valves.Foreach(t => avValue.Add($"{item.Key}_{t.Key}", t.Value));
  276. continue;
  277. default:
  278. continue;
  279. }
  280. }
  281. CreateTable(cache, "MFC", mfc);
  282. CreateTable(cache, "FFU", ffu);
  283. CreateTable(cache, "Valve", avValue);
  284. CreateTable(cache, "GaselineHeater", gaslineHeater);
  285. CreateTable(cache, "ValueSensor", ValueSensor);
  286. CreateTable(cache, "StatusSensor", StatusSensor);
  287. CreateTable(cache, "LeakCheck", leakCheck);
  288. CreateTable(cache, "AoValue", aoValue);
  289. CreateTable(cache, "Recipe", recipe);
  290. return true;
  291. }
  292. private void CreateTableSystem(IDictionary<string, object> data, IDictionary<string, object> cache)
  293. {
  294. Dictionary<string, object> systemCollection = [];
  295. Dictionary<string, object> alarmCollection = [];
  296. Dictionary<string, object> Heater = [];
  297. Dictionary<string, object> Stocker = [];
  298. Dictionary<string, object> LoadPort = [];
  299. Dictionary<string, object> FIMS = [];
  300. foreach (var item in data)
  301. {
  302. if (item.Value is not IDictionary<string, object> values)
  303. continue;
  304. switch (item.Key)
  305. {
  306. case "Boat":
  307. case "CarrierRobot":
  308. case "Scheduler":
  309. case "WaferRobot":
  310. CreateTable(cache, item.Key, values);
  311. continue;
  312. case string s when s.StartsWith("Stocker"):
  313. Stocker.Add(item.Key, values);
  314. continue;
  315. case string s when s.StartsWith("LP"):
  316. LoadPort.Add(item.Key, values);
  317. continue;
  318. //case string s when s.StartsWith("FIMS"):
  319. // FIMS.Add(item.Key, values);
  320. // continue;
  321. case "System":
  322. if (values is not IDictionary<string, object> systems)
  323. continue;
  324. foreach (var system in systems)
  325. {
  326. switch (system.Key)
  327. {
  328. case string s when s.StartsWith("Heater"):
  329. Heater.Add(system.Key, system.Value);
  330. continue;
  331. case string s when s.StartsWith("AlarmSignalHeater"):
  332. alarmCollection.Add(system.Key, ((IDictionary<string, object>)system.Value)["Value"] ??= false);
  333. continue;
  334. default:
  335. systemCollection.Add(system.Key, system.Value);
  336. break;
  337. }
  338. }
  339. continue;
  340. default:
  341. break;
  342. }
  343. //CreateTable(cache, "FIMS", FIMS);
  344. CreateTable(cache, "Heater", Heater);
  345. CreateTable(cache, "LoadPort", LoadPort);
  346. CreateTable(cache, "Stocker", Stocker);
  347. CreateTable(cache, "System", systemCollection);
  348. CreateTable(cache, "AlarmSignalHeater", alarmCollection);
  349. }
  350. }
  351. private bool CreateTable(IDictionary<string, object> cache, string key, object value)
  352. {
  353. return cache.TryAdd(key, value);
  354. }
  355. [RelayCommand]
  356. private void RemoveLeft(string key)
  357. {
  358. this.Left.TryRemove(key, out object? line);
  359. if (line is LineType lineType)
  360. lineType.IsEnable = true;
  361. }
  362. [RelayCommand]
  363. private void RemoveRight(string key)
  364. {
  365. this.Right.TryRemove(key, out object? line);
  366. if (line is LineType lineType)
  367. lineType.IsEnable = true;
  368. }
  369. [RelayCommand]
  370. private void Clear(string para)
  371. {
  372. switch (para)
  373. {
  374. case "L":
  375. this.Left.Foreach(t => ((LineType)t.Value).IsEnable = true);
  376. this.Left.Clear();
  377. break;
  378. case "R":
  379. this.Right.Foreach(t => ((LineType)t.Value).IsEnable = true);
  380. this.Right.Clear();
  381. break;
  382. default:
  383. this.Right.Foreach(t => ((LineType)t.Value).IsEnable = true);
  384. this.Right.Clear();
  385. this.Left.Foreach(t => ((LineType)t.Value).IsEnable = true);
  386. this.Left.Clear();
  387. break;
  388. }
  389. }
  390. [RelayCommand]
  391. private void GeneratePlot()
  392. {
  393. if (this.sqlSugarCustom.Client is null)
  394. return;
  395. if (!this.RecipeSteps.Any(t => t.Value.IsSelected))
  396. return;
  397. DateTime start = this.RecipeSteps.Where(t => t.Value.IsSelected).First().Key;
  398. DateTime end = this.RecipeSteps.Where(t => t.Value.IsSelected).Last().Key;
  399. var whereFunc = ObjectFuncModel.Create("Format", "time", ">", "{long}:" + $"{start.Ticks}", "&&", "time", "<", "{long}:" + $"{end.Ticks}");
  400. var f = sqlSugarCustom.Client.Queryable<dynamic>().AS($"\"{start:yyyyMMdd}.PM1\"").Where(whereFunc).ToList();
  401. var system = sqlSugarCustom.Client.Queryable<dynamic>().AS($"\"{start:yyyyMMdd}.System\"").Where(whereFunc).ToList();
  402. f.AddRange(system);
  403. Dictionary<string, List<float>> cacheLeft = [];
  404. Dictionary<string, List<float>> cacheRight = [];
  405. List<DateTime> time = [];
  406. foreach (var item in f)
  407. {
  408. if (item is not IDictionary<string, object> data)
  409. continue;
  410. time.Add(new DateTime((long)data["time"]));
  411. foreach (string key in Left.Keys)
  412. {
  413. if (data.TryGetValue(key, out object? value) && value is not null)
  414. {
  415. if (!cacheLeft.TryGetValue(key, out List<float>? collections) || collections is null)
  416. {
  417. collections = [];
  418. cacheLeft[key] = collections;
  419. }
  420. collections.Add((float)value);
  421. }
  422. }
  423. foreach (string key in Right.Keys)
  424. {
  425. if (data.TryGetValue(key, out object? value) && value is not null)
  426. {
  427. if (!cacheRight.TryGetValue(key, out List<float>? collections) || collections is null)
  428. {
  429. collections = [];
  430. cacheRight[key] = collections;
  431. }
  432. collections.Add((float)value);
  433. }
  434. }
  435. }
  436. this.PlotControl.Plot.Clear();
  437. foreach (var item in cacheLeft)
  438. this.SetLeftLine(time, item.Value, ((LineType)Left[item.Key])!.LinePattern, MarkerStyle.None, 2f, ((LineType)Left[item.Key])!.HexRGB);
  439. foreach (var item in cacheRight)
  440. this.SetRightLine(time, item.Value, ((LineType)Right[item.Key])!.LinePattern, MarkerStyle.None, 2f, ((LineType)Right[item.Key])!.HexRGB);
  441. this.PlotControl.Plot.Axes.Bottom.TickLabelStyle.OffsetY= 0;
  442. //this.PlotControl.Plot.
  443. this.PlotControl.Plot.Axes.AutoScale();
  444. PlotControl.Refresh();
  445. }
  446. private void SetLeftLine(List<DateTime> time, List<float> value, LinePattern linePattern, MarkerStyle markerStyle, float lineWidth, string color)
  447. {
  448. Scatter mainScatter = this.PlotControl.Plot.Add.Scatter(time, value);
  449. mainScatter.MarkerStyle = markerStyle;
  450. mainScatter.LineWidth = lineWidth;
  451. mainScatter.LinePattern = linePattern;
  452. mainScatter.Color = new ScottPlot.Color(color);
  453. mainScatter.Axes.YAxis = this.PlotControl.Plot.Axes.Left;
  454. }
  455. private void SetRightLine(List<DateTime> time, List<float> value, LinePattern linePattern, MarkerStyle markerStyle, float lineWidth, string color)
  456. {
  457. Scatter mainScatter = this.PlotControl.Plot.Add.Scatter(time, value);
  458. mainScatter.MarkerStyle = markerStyle;
  459. mainScatter.LineWidth = lineWidth;
  460. mainScatter.LinePattern = linePattern;
  461. mainScatter.Color = new ScottPlot.Color(color);
  462. mainScatter.Axes.YAxis = this.PlotControl.Plot.Axes.Right;
  463. }
  464. }
  465. public partial class SelectRecipeStep : ObservableObject
  466. {
  467. [ObservableProperty]
  468. private bool _IsSelected;
  469. public string? Guid { get; set; }
  470. public DateTime Step_Begin_Time { get; set; }
  471. public DateTime Step_End_Time { get; set; }
  472. public string? Step_Name { get; set; }
  473. public float Step_Time { get; set; }
  474. public int Step_Number { get; set; }
  475. public string? Sub_Recipe_Step_Time { get; set; }
  476. public string? Sub_Recipe_Step_Number { get; set; }
  477. public string? Sub_Recipe_Step_Name { get; set; }
  478. public string? Sub_Recipe_Loop_Info { get; set; }
  479. public string? Temp_correction { get; set; }
  480. public string? Temp_pid { get; set; }
  481. }
  482. public class DBProcessData<T_Hierarchy>(char spilter, int skip)
  483. where T_Hierarchy : IDictionary<string, object>, new()
  484. {
  485. public bool ToDictionary(IDictionary<string, object> input, out T_Hierarchy? output)
  486. {
  487. output = default;
  488. if (input is null)
  489. return false;
  490. T_Hierarchy cache = [];
  491. foreach (KeyValuePair<string, object> rawData in input)
  492. {
  493. Span<string> source = rawData.Key.Split(spilter).AsSpan()[skip..];
  494. ColumAnalizer(cache, source, rawData.Key);
  495. }
  496. output = cache;
  497. return true;
  498. }
  499. private static void ColumAnalizer(/*ref*/ T_Hierarchy cache, Span<string> seprated, object value)
  500. {
  501. if (seprated.Length <= 1)
  502. {
  503. cache[seprated[0]] = value;
  504. return;
  505. }
  506. if (!cache.TryGetValue(seprated[0], out object? output) || output is not T_Hierarchy hierarchy)
  507. {
  508. hierarchy = [];
  509. cache[seprated[0]] = hierarchy;
  510. }
  511. cache = hierarchy;
  512. ColumAnalizer(cache, seprated[1..], value);
  513. }
  514. }
  515. public class GeneralProcessData(int skip = 0) : DBProcessData<Dictionary<string, object>>('.', skip)
  516. {
  517. }