123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614 |
- using Mapster;
- using Prism.Dialogs;
- using ScottPlot;
- using ScottPlot.Colormaps;
- using ScottPlot.Plottables;
- using ScottPlot.WPF;
- using SqlSugar;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows;
- using System.Windows.Media;
- using Universal;
- using static Dm.FldrStatement;
- namespace ProximaAnalizer.ViewModels;
- internal partial class DBInfoTraceViewModel : ObservableObject
- {
- public DBInfoTraceViewModel(TraceData traceData,
- SqlSugarCustom sqlSugarCustom,
- IEventAggregator eventAggregator,
- IDialogService dialogService)
- {
- this.traceData = traceData;
- this.sqlSugarCustom = sqlSugarCustom;
- this.dialogService = dialogService;
- this.PlotControl = new();
- this.InitPlot();
- eventAggregator.GetEvent<RefreshRecipeData>().Subscribe(Search);
- }
- private void InitPlot()
- {
- this.PlotControl.Plot.Grid.XAxisStyle.MajorLineStyle.Width = 1f;
- this.PlotControl.Plot.Grid.YAxisStyle.MajorLineStyle.Width = 1f;
- this.PlotControl.Plot.Axes.Bottom.TickLabelStyle.Alignment = Alignment.MiddleLeft;
- this.PlotControl.Plot.RenderManager.RenderStarting += (s, e) =>
- {
- Tick[] ticks = this.PlotControl.Plot.Axes.Bottom.TickGenerator.Ticks;
- for (int i = 0; i < ticks.Length; i++)
- {
- DateTime dt = DateTime.FromOADate(ticks[i].Position);
- string label = $"{dt:MM-dd HH:mm:ss}";
- ticks[i] = new Tick(ticks[i].Position, label);
- }
- };
- this.PlotControl.Plot.FigureBackground.Color = ScottPlot.Colors.Transparent;
- this.PlotControl.Plot.Axes.Bottom.TickLabelStyle.Rotation = 90;
- this.PlotControl.Plot.Axes.Bottom.TickLabelStyle.Alignment = Alignment.MiddleLeft;
- //PixelPadding padding = new(40, 20, 65, 10);
- //this.PlotControl.Plot.Layout.Fixed(padding);
- this.PlotControl.Plot.FigureBackground.Color = ScottPlot.Colors.Transparent;
- //UpdateDetail();
- }
- [ObservableProperty]
- private WpfPlot _PlotControl;
- private readonly TraceData traceData;
- private readonly SqlSugarCustom sqlSugarCustom;
- private readonly IDialogService dialogService;
- [ObservableProperty]
- private ObservableDictionary<DateTime, SelectRecipeStep> _RecipeSteps = [];
- [ObservableProperty]
- private IDictionary<string, object>? _Hierachy;
- [ObservableProperty]
- private ObservableDictionary<string, object> _Left = [];
- [ObservableProperty]
- private ObservableDictionary<string, object> _Right = [];
- private readonly Stack<IDictionary<string, object>> Cache = new();
- private bool AutoGenerated = false;
- [RelayCommand]
- private void Selected(SelectRecipeStep step)
- {
- if (RecipeSteps is null)
- return;
- if (sqlSugarCustom.Client is null)
- return;
- if (AutoGenerated)
- {
- UnSelected(step);
- return;
- }
- if (RecipeSteps.Where(t => t.Value.IsSelected).Count() != 2)
- return;
- DateTime start = RecipeSteps.Where(t => t.Value.IsSelected).First().Key;
- DateTime end = RecipeSteps.Where(t => t.Value.IsSelected).Last().Key;
- RecipeSteps.Where(t => t.Key >= start && t.Key <= end).Foreach(t => t.Value.IsSelected = true);
- this.AutoGenerated = true;
- }
- [RelayCommand]
- private void UnSelected(SelectRecipeStep step)
- {
- RecipeSteps.Where(t => t.Value.IsSelected).Foreach(t => t.Value.IsSelected = false);
- this.AutoGenerated = false;
- }
- [RelayCommand]
- private void SelectHierachy(KeyValuePair<string, object> item)
- {
- if (this.Hierachy is null)
- return;
- if (item.Value is IDictionary<string, object> next)
- {
- this.Cache.Push(this.Hierachy);
- this.Hierachy = next;
- return;
- }
- //this.Left.TryAdd((string)item.Value, item.Value);
- //IDialogParameters parameters, Action< IDialogResult > callback
- IDialogParameters para = new DialogParameters() { { "Line", item.Value } };
- dialogService.Show("LinePicker", para, AddLine);
- }
- [RelayCommand]
- private void EditLine(KeyValuePair<string, object> item)
- {
- IDialogParameters para = new DialogParameters() { { "Line", item.Key } };
- dialogService.Show("LinePicker", para, AddLine);
- }
- private void AddLine(IDialogResult dialogResult)
- {
- dialogResult.Parameters.TryGetValue("Line", out LineType? lineType);
- dialogResult.Parameters.TryGetValue("Name", out string? line);
- dialogResult.Parameters.TryGetValue("Axis", out string? Axis);
- if (lineType is null || string.IsNullOrEmpty(line) || string.IsNullOrEmpty(Axis))
- return;
- ObservableDictionary<string, object>? lineCollection = Axis switch
- {
- "L" => Left,
- "R" => Right,
- _ => default
- };
- if (lineCollection is null)
- return;
- if (lineCollection.TryGetValue(line, out object? oldType) && oldType is LineType oldLine)
- oldLine.IsEnable = true;
- lineCollection[line] = lineType;
- }
- [RelayCommand]
- private void Return()
- {
- if (this.Cache.TryPop(out var output) && output is not null)
- this.Hierachy = output;
- }
- [RelayCommand]
- private void Search()
- {
- if (traceData.ProcessData is null)
- return;
- if (sqlSugarCustom.Client is null)
- return;
- this.Cache.Clear();
- this.RecipeSteps ??= [];
- this.RecipeSteps.Clear();
- this.Clear("");
- DateTime beginTime = traceData.ProcessData.Process_Begin_Time;
- DateTime endTime = traceData.ProcessData.Process_End_Time;
- long beginTicks = beginTime.Ticks;
- long endTicks = endTime.Ticks;
- List<RecipeStepData> recipeSteps;
- try
- {
- recipeSteps = sqlSugarCustom.Client.Queryable<RecipeStepData>().AS("recipe_step_data")
- .Where(t => t.Process_Data_Guid == traceData.ProcessData.Guid)
- .OrderBy(t => t.Step_Begin_Time).ToList();
- }
- catch
- {
- MessageBox.Show($"recipe_step_data 记录不存在", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- if (recipeSteps.Count == 0)
- {
- MessageBox.Show($"recipe_step 步骤数量为0", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- foreach (var item in recipeSteps)
- {
- SelectRecipeStep step = new();
- item.Adapt(step);
- this.RecipeSteps.TryAdd(item.Step_Begin_Time, step);
- }
- dynamic f;
- dynamic system;
- try
- {
- f = sqlSugarCustom.Client.Queryable<dynamic>().AS($"\"{beginTime:yyyyMMdd}.PM1\"").First();
- system = sqlSugarCustom.Client.Queryable<dynamic>().AS($"\"{beginTime:yyyyMMdd}.System\"").First();
- }
- catch
- {
- MessageBox.Show($"{beginTime:yyyyMMdd} 记录不存在", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- GeneralProcessData processData = new();
- if (f is not IDictionary<string, object> input)
- return;
- if (!processData.ToDictionary(input, out Dictionary<string, object>? output) || output is null)
- return;
- if (output["PM1"] is not IDictionary<string, object> pmData)
- return;
- if (!processData.ToDictionary(system, out Dictionary<string, object>? systemDic) || systemDic is null)
- return;
- Dictionary<string, object> temp = [];
- Dictionary<string, object> PM1 = [];
- Dictionary<string, object> System = [];
- temp.Add("PM1", PM1);
- temp.Add("System", System);
- this.CreateTablePM(pmData, PM1);
- this.CreateTableSystem(systemDic!, System);
- this.Hierachy = temp;
- this.Cache.Push(temp);
- }
- private bool CreateTablePM(IDictionary<string, object> data, IDictionary<string, object> cache)
- {
- Dictionary<string, object> ValueSensor = [];
- Dictionary<string, object> StatusSensor = [];
- Dictionary<string, object> leakCheck = [];
- Dictionary<string, object> recipe = [];
- Dictionary<string, object> aoValue = [];
- Dictionary<string, object> ffu = [];
- Dictionary<string, object> mfc = [];
- Dictionary<string, object> gaslineHeater = [];
- Dictionary<string, object> bufferFoup = [];
- Dictionary<string, object> avValue = [];
- foreach (var item in data)
- {
- if (item.Value is not IDictionary<string, object> values)
- {
- switch (item.Key)
- {
- case string s when s.EndsWith("Enable"):
- continue;
- case string s when s.StartsWith("LeakCheck"):
- leakCheck.Add(item.Key, item.Value);
- continue;
- default:
- recipe.Add(item.Key, item.Value);
- continue;
- }
- }
- switch (item.Key)
- {
- case "APC":
- case "APCVATGV":
- case "BoatElevatorServo":
- case "BoatRotationServo":
- case "BufferServo":
- case "Shutter":
- CreateTable(cache, item.Key, values);
- continue;
- case string s when s.StartsWith("Trig"):
- if (item.Value is IDictionary<string, object> value)
- aoValue.Add(item.Key, value["AOValue"]);
- continue;
- case string s when s.StartsWith("FS"):
- case string s1 when s1.StartsWith("PG"):
- case string s2 when s2.StartsWith("PS"):
- case string s3 when s3.StartsWith("VG"):
- if (item.Value is IDictionary<string, object> vss)
- ValueSensor.Add(item.Key, vss["Value"]);
- continue;
- case string s when s.StartsWith("Sensor"):
- if (item.Value is IDictionary<string, object> status)
- StatusSensor.Add(item.Key, status["Value"]);
- continue;
- case string s when s.StartsWith("MFC"):
- if (item.Value is IDictionary<string, object> mfcs)
- mfcs.Foreach(t => mfc.Add($"{item.Key}_{t.Key}", t.Value));
- continue;
- case string s when s.StartsWith("FFU"):
- if (item.Value is IDictionary<string, object> ffus)
- ffus.Foreach(t => ffu.Add($"{item.Key}_{t.Key}", t.Value));
- continue;
- case string s when s.StartsWith("GaselineHeater"):
- if (item.Value is IDictionary<string, object> gaslines)
- gaslines.Foreach(t => gaslineHeater.Add($"{item.Key}_{t.Key}", t.Value));
- continue;
- case string s when s.StartsWith("Valve"):
- if (item.Value is IDictionary<string, object> valves)
- valves.Foreach(t => avValue.Add($"{item.Key}_{t.Key}", t.Value));
- continue;
- default:
- continue;
- }
- }
- CreateTable(cache, "MFC", mfc);
- CreateTable(cache, "FFU", ffu);
- CreateTable(cache, "Valve", avValue);
- CreateTable(cache, "GaselineHeater", gaslineHeater);
- CreateTable(cache, "ValueSensor", ValueSensor);
- CreateTable(cache, "StatusSensor", StatusSensor);
- CreateTable(cache, "LeakCheck", leakCheck);
- CreateTable(cache, "AoValue", aoValue);
- CreateTable(cache, "Recipe", recipe);
- return true;
- }
- private void CreateTableSystem(IDictionary<string, object> data, IDictionary<string, object> cache)
- {
- Dictionary<string, object> systemCollection = [];
- Dictionary<string, object> alarmCollection = [];
- Dictionary<string, object> Heater = [];
- Dictionary<string, object> Stocker = [];
- Dictionary<string, object> LoadPort = [];
- Dictionary<string, object> FIMS = [];
- foreach (var item in data)
- {
- if (item.Value is not IDictionary<string, object> values)
- continue;
- switch (item.Key)
- {
- case "Boat":
- case "CarrierRobot":
- case "Scheduler":
- case "WaferRobot":
- CreateTable(cache, item.Key, values);
- continue;
- case string s when s.StartsWith("Stocker"):
- Stocker.Add(item.Key, values);
- continue;
- case string s when s.StartsWith("LP"):
- LoadPort.Add(item.Key, values);
- continue;
- //case string s when s.StartsWith("FIMS"):
- // FIMS.Add(item.Key, values);
- // continue;
- case "System":
- if (values is not IDictionary<string, object> systems)
- continue;
- foreach (var system in systems)
- {
- switch (system.Key)
- {
- case string s when s.StartsWith("Heater"):
- Heater.Add(system.Key, system.Value);
- continue;
- case string s when s.StartsWith("AlarmSignalHeater"):
- alarmCollection.Add(system.Key, ((IDictionary<string, object>)system.Value)["Value"] ??= false);
- continue;
- default:
- systemCollection.Add(system.Key, system.Value);
- break;
- }
- }
- continue;
- default:
- break;
- }
- //CreateTable(cache, "FIMS", FIMS);
- CreateTable(cache, "Heater", Heater);
- CreateTable(cache, "LoadPort", LoadPort);
- CreateTable(cache, "Stocker", Stocker);
- CreateTable(cache, "System", systemCollection);
- CreateTable(cache, "AlarmSignalHeater", alarmCollection);
- }
- }
- private bool CreateTable(IDictionary<string, object> cache, string key, object value)
- {
- return cache.TryAdd(key, value);
- }
- [RelayCommand]
- private void RemoveLeft(string key)
- {
- this.Left.TryRemove(key, out object? line);
- if (line is LineType lineType)
- lineType.IsEnable = true;
- }
- [RelayCommand]
- private void RemoveRight(string key)
- {
- this.Right.TryRemove(key, out object? line);
- if (line is LineType lineType)
- lineType.IsEnable = true;
- }
- [RelayCommand]
- private void Clear(string para)
- {
- switch (para)
- {
- case "L":
- this.Left.Foreach(t => ((LineType)t.Value).IsEnable = true);
- this.Left.Clear();
- break;
- case "R":
- this.Right.Foreach(t => ((LineType)t.Value).IsEnable = true);
- this.Right.Clear();
- break;
- default:
- this.Right.Foreach(t => ((LineType)t.Value).IsEnable = true);
- this.Right.Clear();
- this.Left.Foreach(t => ((LineType)t.Value).IsEnable = true);
- this.Left.Clear();
- break;
- }
- }
- [RelayCommand]
- private void GeneratePlot()
- {
- if (this.sqlSugarCustom.Client is null)
- return;
- if (!this.RecipeSteps.Any(t => t.Value.IsSelected))
- return;
- DateTime start = this.RecipeSteps.Where(t => t.Value.IsSelected).First().Key;
- DateTime end = this.RecipeSteps.Where(t => t.Value.IsSelected).Last().Key;
- var whereFunc = ObjectFuncModel.Create("Format", "time", ">", "{long}:" + $"{start.Ticks}", "&&", "time", "<", "{long}:" + $"{end.Ticks}");
- var f = sqlSugarCustom.Client.Queryable<dynamic>().AS($"\"{start:yyyyMMdd}.PM1\"").Where(whereFunc).ToList();
- var system = sqlSugarCustom.Client.Queryable<dynamic>().AS($"\"{start:yyyyMMdd}.System\"").Where(whereFunc).ToList();
- f.AddRange(system);
- Dictionary<string, List<float>> cacheLeft = [];
- Dictionary<string, List<float>> cacheRight = [];
- List<DateTime> time = [];
- foreach (var item in f)
- {
- if (item is not IDictionary<string, object> data)
- continue;
- time.Add(new DateTime((long)data["time"]));
- foreach (string key in Left.Keys)
- {
- if (data.TryGetValue(key, out object? value) && value is not null)
- {
- if (!cacheLeft.TryGetValue(key, out List<float>? collections) || collections is null)
- {
- collections = [];
- cacheLeft[key] = collections;
- }
- collections.Add((float)value);
- }
- }
- foreach (string key in Right.Keys)
- {
- if (data.TryGetValue(key, out object? value) && value is not null)
- {
- if (!cacheRight.TryGetValue(key, out List<float>? collections) || collections is null)
- {
- collections = [];
- cacheRight[key] = collections;
- }
- collections.Add((float)value);
- }
- }
- }
- this.PlotControl.Plot.Clear();
- foreach (var item in cacheLeft)
- this.SetLeftLine(time, item.Value, ((LineType)Left[item.Key])!.LinePattern, MarkerStyle.None, 2f, ((LineType)Left[item.Key])!.HexRGB);
- foreach (var item in cacheRight)
- this.SetRightLine(time, item.Value, ((LineType)Right[item.Key])!.LinePattern, MarkerStyle.None, 2f, ((LineType)Right[item.Key])!.HexRGB);
- this.PlotControl.Plot.Axes.Bottom.TickLabelStyle.OffsetY= 0;
- //this.PlotControl.Plot.
- this.PlotControl.Plot.Axes.AutoScale();
- PlotControl.Refresh();
- }
- private void SetLeftLine(List<DateTime> time, List<float> value, LinePattern linePattern, MarkerStyle markerStyle, float lineWidth, string color)
- {
- Scatter mainScatter = this.PlotControl.Plot.Add.Scatter(time, value);
- mainScatter.MarkerStyle = markerStyle;
- mainScatter.LineWidth = lineWidth;
- mainScatter.LinePattern = linePattern;
- mainScatter.Color = new ScottPlot.Color(color);
- mainScatter.Axes.YAxis = this.PlotControl.Plot.Axes.Left;
- }
- private void SetRightLine(List<DateTime> time, List<float> value, LinePattern linePattern, MarkerStyle markerStyle, float lineWidth, string color)
- {
- Scatter mainScatter = this.PlotControl.Plot.Add.Scatter(time, value);
- mainScatter.MarkerStyle = markerStyle;
- mainScatter.LineWidth = lineWidth;
- mainScatter.LinePattern = linePattern;
- mainScatter.Color = new ScottPlot.Color(color);
- mainScatter.Axes.YAxis = this.PlotControl.Plot.Axes.Right;
- }
- }
- public partial class SelectRecipeStep : ObservableObject
- {
- [ObservableProperty]
- private bool _IsSelected;
- public string? Guid { get; set; }
- public DateTime Step_Begin_Time { get; set; }
- public DateTime Step_End_Time { get; set; }
- public string? Step_Name { get; set; }
- public float Step_Time { get; set; }
- public int Step_Number { get; set; }
- public string? Sub_Recipe_Step_Time { get; set; }
- public string? Sub_Recipe_Step_Number { get; set; }
- public string? Sub_Recipe_Step_Name { get; set; }
- public string? Sub_Recipe_Loop_Info { get; set; }
- public string? Temp_correction { get; set; }
- public string? Temp_pid { get; set; }
- }
- public class DBProcessData<T_Hierarchy>(char spilter, int skip)
- where T_Hierarchy : IDictionary<string, object>, new()
- {
- public bool ToDictionary(IDictionary<string, object> input, out T_Hierarchy? output)
- {
- output = default;
- if (input is null)
- return false;
- T_Hierarchy cache = [];
- foreach (KeyValuePair<string, object> rawData in input)
- {
- Span<string> source = rawData.Key.Split(spilter).AsSpan()[skip..];
- ColumAnalizer(cache, source, rawData.Key);
- }
- output = cache;
- return true;
- }
- private static void ColumAnalizer(/*ref*/ T_Hierarchy cache, Span<string> seprated, object value)
- {
- if (seprated.Length <= 1)
- {
- cache[seprated[0]] = value;
- return;
- }
- if (!cache.TryGetValue(seprated[0], out object? output) || output is not T_Hierarchy hierarchy)
- {
- hierarchy = [];
- cache[seprated[0]] = hierarchy;
- }
- cache = hierarchy;
- ColumAnalizer(cache, seprated[1..], value);
- }
- }
- public class GeneralProcessData(int skip = 0) : DBProcessData<Dictionary<string, object>>('.', skip)
- {
- }
|