From 080778bf162b124b23a777eae5de7ef499a91c55 Mon Sep 17 00:00:00 2001 From: Stoyan Zlatev Date: Thu, 8 Feb 2024 15:48:12 +0100 Subject: [PATCH] Tasks with states --- .../InspectionUI/ViewModels/MainVM.cs | 2 +- .../InspectionUI/ViewModels/MeterVM.cs | 158 +++++++++++------- .../InspectionUI/ViewModels/MetersPoolVM.cs | 9 +- Common/OmniPlus/InspectionUI/ViewModels/VM.cs | 14 ++ Common/OmniPlus/OmniPlus/OmniContext.cs | 27 ++- 5 files changed, 133 insertions(+), 77 deletions(-) diff --git a/Common/OmniPlus/InspectionUI/ViewModels/MainVM.cs b/Common/OmniPlus/InspectionUI/ViewModels/MainVM.cs index 6cdfb3f0..18d28e45 100644 --- a/Common/OmniPlus/InspectionUI/ViewModels/MainVM.cs +++ b/Common/OmniPlus/InspectionUI/ViewModels/MainVM.cs @@ -108,7 +108,7 @@ this.StartNewTask(() => { this.Errors = "Connecting meters ..."; - this.MetersPool.LoadMeters(); + this.MetersPool.LoadMetersAsync(); }) .ContinueWith(_ => { diff --git a/Common/OmniPlus/InspectionUI/ViewModels/MeterVM.cs b/Common/OmniPlus/InspectionUI/ViewModels/MeterVM.cs index 1b1d3b69..d69a80e5 100644 --- a/Common/OmniPlus/InspectionUI/ViewModels/MeterVM.cs +++ b/Common/OmniPlus/InspectionUI/ViewModels/MeterVM.cs @@ -1,7 +1,5 @@ namespace InspectionUI.ViewModels { - using InspectionUI.Models; - using OmniPlus; using System; @@ -74,7 +72,20 @@ public Task LoadTestRunAsync(CancellationToken cancellationToken) { - return this.StartNewTask(this.meter.LoadTestRun, cancellationToken); + void LoadTestRun(object state) + { + if (state is MeterVM meterVM) + { + if (!meterVM.meter.IsConnected) + { + return; + } + + meterVM.meter.LoadTestRun(); + } + } + + return this.StartNewTask(LoadTestRun, this, cancellationToken); } public Task InitializeInspectionAsync(bool keepCorrection, CancellationToken cancellationToken) @@ -84,103 +95,120 @@ cancellationToken.Register(this.meter.FinishInspection); } - this.uinr = 0; - - void InitializeInspection() + void InitializeInspection(object state) { - if (!this.meter.IsConnected) + if (state is MeterVM meterVM) { - return; + meterVM.uinr = 0; + + var meter = meterVM.meter; + + if (!meter.IsConnected) + { + return; + } + + meter.InitializeForInspection(keepCorrection); + meterVM.Set(meter.TestRun.CrrfBefore, "{0:0.0}", 3); } - - this.meter.InitializeForInspection(keepCorrection); - - this.Set(this.meter.TestRun.CrrfBefore, "{0:0.0}", 3); } - return this.StartNewTask(InitializeInspection, cancellationToken); + return this.StartNewTask(InitializeInspection, this, cancellationToken); } public Task StartCalibrationAsync(OmniPruefpunkt pp, CancellationToken cancellationToken) { - this.uinr++; - - void StartCalibration() + void StartCalibration(object state) { - if (!this.meter.IsConnected) + if (state is MeterVM meterVM) { - return; + meterVM.uinr++; + + var _pp = pp; + var meter = meterVM.meter; + + if (!meter.IsConnected) + { + return; + } + + meter.StartCalibration((ushort)_pp.Rotations, (ushort)_pp.Duration); + meterVM.ShowRestTime(_pp.Duration, meterVM.uinr * 3 + 1); } - - this.meter.StartCalibration((ushort)pp.Rotations, (ushort)pp.Duration); - - this.ShowRestTime(pp.Duration, this.uinr * 3 + 1); } - return this.StartNewTask(StartCalibration, cancellationToken); + return this.StartNewTask(StartCalibration, this, cancellationToken); } public Task StopCalibrationAsync(OmniPruefpunkt pp, double refm3s, CancellationToken cancellationToken) { - void StopCalibration() + void StopCalibration(object state) { - if (!this.meter.IsConnected) + if (state is MeterVM meterVM) { - return; + var _pp = pp; + var meter = meterVM.meter; + + if (!meter.IsConnected) + { + return; + } + + var response = meter.StopCalibration(_pp, refm3s); + var index = meterVM.uinr * 3 + 1; + + meterVM.Set(response.Item2, "{0}", index); + meterVM.Set(response.Item1, "{0:0.0000000}", index + 1); + meterVM.Set(response.Item3, "{0:0.00}", index + 2); } - - var response = this.meter.StopCalibration(pp, refm3s); - - var index = this.uinr * 3 + 1; - - this.Set(response.Item2, "{0}", index); - this.Set(response.Item1, "{0:0.0000000}", index + 1); - this.Set(response.Item3, "{0:0.00}", index + 2); } - return this.StartNewTask(StopCalibration, cancellationToken); + return this.StartNewTask(StopCalibration, this, cancellationToken); } public Task FinishInspectionAsync(CancellationToken cancellationToken) { - void FinishInspection() + void FinishInspection(object state) { - if (!this.meter.IsConnected) + if (state is MeterVM meterVM) { - return; - } + var meter = meterVM.meter; - this.meter.FinishInspection(); - - var crrf = this.meter.TestRun.CrrfAfter; - var foreground = this.meter.TestRun.Succeeded == true - ? Brushes.Green - : Brushes.Red; - - this.Set(x => x.Foreground = foreground, this.LastIndex); - - this.Set(crrf, "{0:0.0}", this.LastIndex); - - this.uinr = 1; - - foreach (var testedPP in this.meter.TestRun.TestedPoints) - { - var index = this.uinr * 3 + 1 + 2; - - this.Set(testedPP.Acc, "{0:0.00}", index); - this.Set(x => + if (!meter.IsConnected) { - foreground = testedPP.Succeeded == true - ? Brushes.Green - : Brushes.Red; - x.Foreground = foreground; - }, index); + return; + } - this.uinr++; + meter.FinishInspection(); + + var crrf = meter.TestRun.CrrfAfter; + var foreground = meter.TestRun.Succeeded == true + ? Brushes.Green + : Brushes.Red; + + meterVM.Set(x => x.Foreground = foreground, meterVM.LastIndex); + meterVM.Set(crrf, "{0:0.0}", meterVM.LastIndex); + meterVM.uinr = 1; + + foreach (var testedPP in meter.TestRun.TestedPoints) + { + var index = meterVM.uinr * 3 + 1 + 2; + + meterVM.Set(testedPP.Acc, "{0:0.00}", index); + meterVM.Set(x => + { + foreground = testedPP.Succeeded == true + ? Brushes.Green + : Brushes.Red; + x.Foreground = foreground; + }, index); + + meterVM.uinr++; + } } } - return this.StartNewTask(FinishInspection, cancellationToken); + return this.StartNewTask(FinishInspection, this, cancellationToken); } private void ShowRestTime(int duration, int index) diff --git a/Common/OmniPlus/InspectionUI/ViewModels/MetersPoolVM.cs b/Common/OmniPlus/InspectionUI/ViewModels/MetersPoolVM.cs index 98b0dc41..272da8b6 100644 --- a/Common/OmniPlus/InspectionUI/ViewModels/MetersPoolVM.cs +++ b/Common/OmniPlus/InspectionUI/ViewModels/MetersPoolVM.cs @@ -16,9 +16,9 @@ private CancellationTokenSource cancellationTokenSource; private ReferenceMeterVM referenceMeter; - public MetersPoolVM(OmniContext omniContext) : base() + public MetersPoolVM() : base() { - this.omniContext = omniContext; + this.omniContext = OmniContext.Current; } public void CancelInspection() @@ -34,11 +34,11 @@ } - public void LoadMeters() + public void LoadMetersAsync() { this.referenceMeter = new ReferenceMeterVM(OmniContext.Current); - this.Add(this.referenceMeter); + this.Add(this.referenceMeter); this.omniContext.LoadMeters(Appsettings.COMPorts, Appsettings.PSNR); foreach (var meter in this.omniContext.Meters) @@ -52,7 +52,6 @@ public void RunInspection(bool keepCorrection = false) { // show reference meter seconds - // load meterVM in each task as state this.cancellationTokenSource = new CancellationTokenSource(); var cancellationToken = this.cancellationTokenSource.Token; var tasks = new List(); diff --git a/Common/OmniPlus/InspectionUI/ViewModels/VM.cs b/Common/OmniPlus/InspectionUI/ViewModels/VM.cs index bcc48637..41715d68 100644 --- a/Common/OmniPlus/InspectionUI/ViewModels/VM.cs +++ b/Common/OmniPlus/InspectionUI/ViewModels/VM.cs @@ -99,6 +99,20 @@ }, cancellationToken); } + protected Task StartNewTask(Action action, object state, CancellationToken cancellationToken = default(CancellationToken)) + { + return Task + .Factory + .StartNew(action, state, cancellationToken) + .ContinueWith(task => + { + if (task.Exception != null) + { + AppLogger.Log(task.Exception); + } + }, cancellationToken); + } + protected class Command : ICommand { private readonly Action executable; diff --git a/Common/OmniPlus/OmniPlus/OmniContext.cs b/Common/OmniPlus/OmniPlus/OmniContext.cs index 5c4a1ca6..9004036d 100644 --- a/Common/OmniPlus/OmniPlus/OmniContext.cs +++ b/Common/OmniPlus/OmniPlus/OmniContext.cs @@ -4,6 +4,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; + using System.Threading; using System.Threading.Tasks; public class OmniContext : IDisposable @@ -12,6 +13,8 @@ private readonly OmniDatabase database; private readonly string outputDirectory; + private int slotNr; + public static void Create(String omniDB, String outputDirectory) { Current = new OmniContext(omniDB, outputDirectory); @@ -24,8 +27,6 @@ this.outputDirectory = outputDirectory; this.database = new OmniDatabase(connectionString, outputDirectory); this.meters = new ConcurrentDictionary(); - - //Current = this; } public OmniAuftrag Auftrag { get; set; } @@ -86,18 +87,32 @@ { this.Dispose(); - var slotNr = 1; + this.slotNr = 1; + + var tasks = new List(); foreach (var portName in portNames.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)) { - var meter = new OmniMeter(psnr, slotNr, portName, this.outputDirectory); + tasks + .Add(Task.Factory + .StartNew(state => + { + if (state is OmniContext context) + { + var meter = new OmniMeter(psnr, context.slotNr, portName, context.outputDirectory); - meter.ConnectIrda(); + meter.ConnectIrda(); - this.meters.AddOrUpdate(slotNr, meter, (_, _1) => meter); + context.meters.AddOrUpdate(slotNr, meter, (_, _1) => meter); + } + }, this, CancellationToken.None)); slotNr++; } + + Task.Factory + .ContinueWhenAll(tasks.ToArray(), _ => { }) + .Wait(); } internal int UpdateTestRun(IDictionary parameters, int id, byte slotNr)