278 lines
8.2 KiB
C#
278 lines
8.2 KiB
C#
namespace InspectionUI.ViewModels
|
|
{
|
|
using OmniPlus;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Timers;
|
|
using System.Windows.Media;
|
|
|
|
public class MeterVM : ItemsGridRowVM
|
|
{
|
|
private readonly Queue<string> states;
|
|
private readonly OmniMeter meter;
|
|
|
|
private int uinr;
|
|
private bool active;
|
|
|
|
public MeterVM(OmniMeter meter) : base()
|
|
{
|
|
this.meter = meter;
|
|
this.meter.StateChanged += this.Meter_StateChanged;
|
|
this.states = new Queue<string>();
|
|
|
|
this.Add(new ItemsGridHeaderVM { Value = $"{this.meter.SlotNr, 2}." });
|
|
this.Add(new ItemsGridCellVM());
|
|
this.Add(new ItemsGridCellVM());
|
|
this.Add(new ItemsGridCellVM());
|
|
|
|
var pruefpunkte = OmniContext
|
|
.Current
|
|
.Auftrag
|
|
.Pruefpunkte
|
|
.Count;
|
|
|
|
for (int i = 0; i < pruefpunkte; i++)
|
|
{
|
|
this.Add(new ItemsGridCellVM());
|
|
this.Add(new ItemsGridCellVM());
|
|
this.Add(new ItemsGridCellVM());
|
|
}
|
|
|
|
this.Add(new ItemsGridCellVM());
|
|
|
|
this.MeterNr = this.meter.MeterNr;
|
|
this.SerialNr = this.meter.SerialNr;
|
|
}
|
|
|
|
private string meterId;
|
|
protected string MeterNr
|
|
{
|
|
get => this.meterId;
|
|
set => this.Set(() => this.meterId = value, value, "{0}", 1);
|
|
}
|
|
|
|
private int? serialNr;
|
|
protected int? SerialNr
|
|
{
|
|
get => this.serialNr;
|
|
set => this.Set(() => this.serialNr = value, value, "{0}", 2);
|
|
}
|
|
|
|
internal Task ConnectAsync(int connectionType, CancellationToken cancellationToken)
|
|
{
|
|
void Connect(object state)
|
|
{
|
|
if (state is MeterVM vm)
|
|
{
|
|
vm.meter.Connect(connectionType);
|
|
}
|
|
}
|
|
|
|
return this.StartTask(Connect, cancellationToken);
|
|
}
|
|
|
|
public Task IdentifySelfAsync(CancellationToken cancellationToken)
|
|
{
|
|
void IdentifySelf(object state)
|
|
{
|
|
if (state is MeterVM vm)
|
|
{
|
|
vm.meter.IdentifySelf();
|
|
|
|
vm.MeterNr = vm.meter.MeterNr;
|
|
vm.active = vm.meter.IsActive;
|
|
}
|
|
}
|
|
|
|
return this.StartTask(IdentifySelf, cancellationToken);
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
try
|
|
{
|
|
this.meter.Dispose();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
this.meter.NotifyStateChanged(e);
|
|
}
|
|
|
|
base.Dispose();
|
|
}
|
|
|
|
public Task LoadTestRunAsync(CancellationToken cancellationToken)
|
|
{
|
|
void LoadTestRun(object state)
|
|
{
|
|
if (state is MeterVM vm && vm.active)
|
|
{
|
|
vm.meter.LoadTestRun();
|
|
}
|
|
}
|
|
|
|
return this.StartTask(LoadTestRun, cancellationToken);
|
|
}
|
|
|
|
public Task InitializeInspectionAsync(bool keepCorrection, CancellationToken cancellationToken)
|
|
{
|
|
if (keepCorrection)
|
|
{
|
|
cancellationToken.Register(this.meter.FinishInspection);
|
|
}
|
|
|
|
void InitializeInspection(object state)
|
|
{
|
|
if (state is MeterVM vm && vm.active)
|
|
{
|
|
vm.uinr = 0;
|
|
|
|
var meter = vm.meter;
|
|
|
|
meter.InitializeForInspection(keepCorrection);
|
|
vm.Set(meter.TestRun.CrrfBefore, "{0:0.0}", 3);
|
|
}
|
|
}
|
|
|
|
return this.StartTask(InitializeInspection, cancellationToken);
|
|
}
|
|
|
|
public Task StartCalibrationAsync(OmniPruefpunkt pp, CancellationToken cancellationToken)
|
|
{
|
|
void StartCalibration(object state)
|
|
{
|
|
if (state is MeterVM vm && vm.active)
|
|
{
|
|
vm.uinr++;
|
|
|
|
var _pp = pp;
|
|
var meter = vm.meter;
|
|
|
|
meter.StartCalibration((ushort)_pp.Rotations, (ushort)_pp.Duration);
|
|
vm.ShowRestTime(_pp.Duration, vm.uinr * 3 + 1);
|
|
}
|
|
}
|
|
|
|
return this.StartTask(StartCalibration, cancellationToken);
|
|
}
|
|
|
|
public Task StopCalibrationAsync(OmniPruefpunkt pp, double refm3s, CancellationToken cancellationToken)
|
|
{
|
|
void StopCalibration(object state)
|
|
{
|
|
if (state is MeterVM vm && vm.active)
|
|
{
|
|
var _pp = pp;
|
|
var meter = vm.meter;
|
|
|
|
var response = meter.StopCalibration(_pp, refm3s);
|
|
var index = vm.uinr * 3 + 1;
|
|
|
|
vm.Set(response.Item2, "{0}", index);
|
|
vm.Set(response.Item1, "{0:0.0000000}", index + 1);
|
|
vm.Set(response.Item3, "{0:0.00}", index + 2);
|
|
}
|
|
}
|
|
|
|
return this.StartTask(StopCalibration, cancellationToken);
|
|
}
|
|
|
|
public Task FinishInspectionAsync(CancellationToken cancellationToken)
|
|
{
|
|
void FinishInspection(object state)
|
|
{
|
|
if (state is MeterVM vm && vm.active)
|
|
{
|
|
var meter = vm.meter;
|
|
|
|
meter.FinishInspection();
|
|
|
|
var crrf = meter.TestRun.CrrfAfter;
|
|
var foreground = meter.TestRun.Succeeded == true
|
|
? Brushes.Green
|
|
: Brushes.Red;
|
|
|
|
vm.Set(x => x.Foreground = foreground, vm.LastIndex);
|
|
vm.Set(crrf, "{0:0.0}", vm.LastIndex);
|
|
vm.uinr = 1;
|
|
|
|
foreach (var testedPP in meter.TestRun.TestedPoints)
|
|
{
|
|
var index = vm.uinr * 3 + 1 + 2;
|
|
|
|
vm.Set(testedPP.Acc, "{0:0.00}", index);
|
|
vm.Set(x =>
|
|
{
|
|
foreground = testedPP.Succeeded == true
|
|
? Brushes.Green
|
|
: Brushes.Red;
|
|
x.Foreground = foreground;
|
|
}, index);
|
|
|
|
vm.uinr++;
|
|
}
|
|
}
|
|
}
|
|
|
|
return this.StartTask(FinishInspection, cancellationToken);
|
|
}
|
|
|
|
private void ShowRestTime(int duration, int index)
|
|
{
|
|
var interval = 1000;
|
|
var timeout = duration * interval;
|
|
var waitHandle = new ManualResetEventSlim();
|
|
var timer = new System.Timers.Timer
|
|
{
|
|
AutoReset = true,
|
|
Enabled = true,
|
|
Interval = interval
|
|
};
|
|
|
|
void TimerElapsed(object _, ElapsedEventArgs _1)
|
|
{
|
|
duration--;
|
|
|
|
if (duration > 0)
|
|
{
|
|
this.Set(duration, "{0}", index);
|
|
}
|
|
}
|
|
|
|
timer.Elapsed += TimerElapsed;
|
|
|
|
timer.Start();
|
|
waitHandle.Wait(timeout);
|
|
|
|
timer.Stop();
|
|
waitHandle.Reset();
|
|
waitHandle.Wait(1000);
|
|
|
|
timer.Elapsed -= TimerElapsed;
|
|
|
|
this.Set(default(string), "{0}", index);
|
|
}
|
|
|
|
private Task StartTask(Action<object> executable, CancellationToken cancellationToken)
|
|
{
|
|
return this
|
|
.StartNewTask(executable, this, cancellationToken)
|
|
.ContinueWith(t =>
|
|
{
|
|
if (t.Exception != null && t.AsyncState is MeterVM vm)
|
|
{
|
|
vm.meter.NotifyStateChanged(t.Exception);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void Meter_StateChanged(object state)
|
|
{
|
|
this.states.Enqueue($"[{DateTime.Now:HH:mm:ss.fff}] {state}{Environment.NewLine}");
|
|
}
|
|
}
|
|
} |