328 lines
9.8 KiB
C#
328 lines
9.8 KiB
C#
namespace InspectionUI.ViewModels
|
|
{
|
|
using OmniPlus;
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Timers;
|
|
using System.Windows.Media;
|
|
|
|
public class MeterVM : ItemsGridRowVM
|
|
{
|
|
const int OFFSET = 2;
|
|
|
|
private readonly OmniMeter meter;
|
|
|
|
private int column;
|
|
private bool active;
|
|
|
|
public MeterVM(OmniMeter meter) : base()
|
|
{
|
|
this.meter = meter;
|
|
this.column = OFFSET;
|
|
|
|
this.Add(new ItemsGridHeaderVM { Value = $"{this.meter.SlotNr, 2}." });
|
|
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.MeterNr = this.meter.MeterNr;
|
|
}
|
|
|
|
protected int? SlotNr
|
|
=> this.meter?.SlotNr;
|
|
|
|
private string meterId;
|
|
protected string MeterNr
|
|
{
|
|
get => this.meterId;
|
|
set => this.Set(() => this.meterId = value, value, "{0}", 1);
|
|
}
|
|
|
|
protected string DisplayValue => this.meter.ReadDisplay();
|
|
|
|
protected string MeterUnits => this.meter.MeterUnits;
|
|
|
|
protected string message;
|
|
public string Message
|
|
{
|
|
get => this.message;
|
|
set => this.Set(() => this.message = value);
|
|
}
|
|
|
|
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)
|
|
{
|
|
var meter = vm.meter;
|
|
|
|
meter.InitializeForInspection(keepCorrection);
|
|
vm.Set(meter.TestRun.CrrfBefore, "{0:0.0}", OFFSET);
|
|
}
|
|
}
|
|
|
|
return this.StartTask(InitializeInspection, cancellationToken);
|
|
}
|
|
|
|
public Task StartCalibrationAsync(OmniPruefpunkt pp, CancellationToken cancellationToken)
|
|
{
|
|
void StartCalibration(object state)
|
|
{
|
|
if (state is MeterVM vm && vm.active)
|
|
{
|
|
this.column++;
|
|
|
|
var _pp = pp;
|
|
var meter = vm.meter;
|
|
|
|
meter.StartCalibration((ushort)_pp.Rotations, (ushort)_pp.Duration);
|
|
vm.ShowRestTime(_pp.Duration, this.column);
|
|
}
|
|
}
|
|
|
|
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);
|
|
|
|
vm.Set(response.Item3, "{0:0.00}", this.column);
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
void WaitUntil(object _)
|
|
{
|
|
vm.Set(x => x.Foreground = Brushes.LightGray, vm.LastIndex);
|
|
vm.Set("Wird programmiert...", null, vm.LastIndex);
|
|
}
|
|
|
|
var timer = new System.Threading.Timer(WaitUntil, null, 0, 1000);
|
|
|
|
meter.FinishInspection();
|
|
timer.Dispose();
|
|
|
|
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);
|
|
|
|
var ppnr = OFFSET;
|
|
|
|
foreach (var testedPP in meter.TestRun.TestedPoints)
|
|
{
|
|
ppnr++;
|
|
vm.Set(testedPP.Acc, "{0:0.00}", ppnr);
|
|
vm.Set(x =>
|
|
{
|
|
foreground = testedPP.Succeeded == true
|
|
? Brushes.Green
|
|
: Brushes.Red;
|
|
x.Foreground = foreground;
|
|
}, ppnr);
|
|
}
|
|
}
|
|
}
|
|
|
|
return this.StartTask(FinishInspection, cancellationToken);
|
|
}
|
|
|
|
private string waageFile;
|
|
|
|
internal void InitDisplayReading()
|
|
{
|
|
this.Clear();
|
|
|
|
var timestamp = DateTime.Now;
|
|
var displayValue = this.DisplayValue;
|
|
var currentValueCell = new ItemsGridCellVM
|
|
{
|
|
Value = $"{displayValue}"
|
|
};
|
|
|
|
var dir = Directory.CreateDirectory("./waage").FullName;
|
|
this.waageFile = $"{dir}/{this.MeterNr}.txt";
|
|
|
|
this.Add(new ItemsGridHeaderVM { Value = $"{this.SlotNr,2}." });
|
|
this.Add(new ItemsGridCellVM { Value = this.MeterNr });
|
|
this.Add(new ItemsGridCellVM { Value = this.MeterUnits });
|
|
this.Add(new ItemsGridCellVM { Value = displayValue });
|
|
this.Add(currentValueCell);
|
|
|
|
File.AppendAllText(this.waageFile, $"{timestamp:yyyy-MM-dd HH:mm:ss};{displayValue}{Environment.NewLine}");
|
|
|
|
this.NotifyPropertyChanged();
|
|
}
|
|
|
|
internal Task StartDisplayReadingAsync(CancellationToken cancellationToken)
|
|
{
|
|
void StartDisplayReading(object state)
|
|
{
|
|
if (state is MeterVM vm)
|
|
{
|
|
var awaiter = new ManualResetEventSlim();
|
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
{
|
|
var displayValue = vm.DisplayValue;
|
|
var timestamp = DateTime.Now;
|
|
vm[4].Value = displayValue;
|
|
File.AppendAllText(this.waageFile, $"{timestamp:yyyy-MM-dd HH:mm:ss};{displayValue}{Environment.NewLine}");
|
|
|
|
awaiter.Reset();
|
|
awaiter.Wait(1000);
|
|
}
|
|
}
|
|
}
|
|
|
|
return Task.Factory.StartNew(StartDisplayReading, this, 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);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
} |