Tasks with states

This commit is contained in:
Stoyan Zlatev
2024-02-08 15:48:12 +01:00
parent 6d6690d465
commit 080778bf16
5 changed files with 133 additions and 77 deletions
@@ -108,7 +108,7 @@
this.StartNewTask(() =>
{
this.Errors = "Connecting meters ...";
this.MetersPool.LoadMeters();
this.MetersPool.LoadMetersAsync();
})
.ContinueWith(_ =>
{
@@ -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)
@@ -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<Task>();
@@ -99,6 +99,20 @@
}, cancellationToken);
}
protected Task StartNewTask(Action<object> 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;
+21 -6
View File
@@ -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<int, OmniMeter>();
//Current = this;
}
public OmniAuftrag Auftrag { get; set; }
@@ -86,18 +87,32 @@
{
this.Dispose();
var slotNr = 1;
this.slotNr = 1;
var tasks = new List<Task>();
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<string, object> parameters, int id, byte slotNr)