laatzen/Common/OmniPlus/InspectionUI/ViewModels/MainVM.cs
2024-02-20 12:17:07 +01:00

180 lines
5.4 KiB
C#

namespace InspectionUI.ViewModels
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
public class MainVM : VM
{
public MainVM() : base()
{
this.IsReloadEnabled = true;
this.Reload = new Command(this.ReloadCommand);
this.StartInspection = new Command(this.StartInspectionCommand);
this.CancelInspection = new Command(this.CancelInspectionCommand);
this.MetersPool = new MetersPoolVM(this.ConnectionType);
this.Sizes = this.MetersPool.LoadSizes();
this.Size = this.Sizes.FirstOrDefault();
this.Types = this.MetersPool.LoadTypes();
this.Type = this.Types.FirstOrDefault();
}
private string size;
public string Size
{
get => this.size;
set => this.Set(() => this.size = value);
}
private string type;
public string Type
{
get => this.type;
set => this.Set(() => this.type = value);
}
private string suchnr;
public string SuchNr
{
get => this.suchnr;
set => this.Set(() => this.suchnr = value);
}
private int connectionType;
public int ConnectionType
{
get => this.connectionType;
set => this.Set(() => this.connectionType = value);
}
private bool keepCorrection;
public bool KeepCorrection
{
get => this.keepCorrection;
set => this.Set(() => this.keepCorrection = value);
}
private bool reloadEnabled;
public bool IsReloadEnabled
{
get => this.reloadEnabled;
private set => this.Set(() => this.reloadEnabled = value);
}
public ICommand Reload { get; }
private bool startInspectionEnabled;
public bool IsStartInspectionEnabled
{
get => this.startInspectionEnabled;
private set => this.Set(() => this.startInspectionEnabled = value);
}
public ICommand StartInspection { get; }
private bool cancelInspectionEnabled;
public bool IsCancelInspectionEnabled
{
get => this.cancelInspectionEnabled;
private set => this.Set(() => this.cancelInspectionEnabled = value);
}
public ICommand CancelInspection { get; }
private string errors;
public string Errors
{
get => this.errors;
private set => this.Set(() => this.errors = value);
}
private MetersPoolVM metersPoolVM;
public MetersPoolVM MetersPool
{
get => this.metersPoolVM;
private set => this.Set(() => this.metersPoolVM = value);
}
public IList<string> Sizes { get; }
public IList<string> Types { get; }
private void ReloadCommand()
{
this.EnableButtons(false);
this.IsReloadEnabled = false;
this.Errors = default(string);
if (!string.IsNullOrWhiteSpace(this.SuchNr))
{
this.MetersPool.LoadAuftrag(this.SuchNr);
if (!this.MetersPool.HasPruefpunkte)
{
this.IsReloadEnabled = true;
this.Errors = $"Keine daten gefunden für FertigungauftragsNr: {this.suchnr}";
}
}
else if (!string.IsNullOrWhiteSpace(this.size) && !string.IsNullOrWhiteSpace(this.type))
{
this.MetersPool.LoadAuftrag(this.size, this.type);
if (!this.MetersPool.HasPruefpunkte)
{
this.IsReloadEnabled = true;
this.Errors = $"Keine daten gefunden für Größe: {this.size} und Typ: {this.type}";
}
}
else
{
this.IsReloadEnabled = true;
this.Errors = "FertigungauftragsNr oder Größe und Typ eingeben!";
}
if (string.IsNullOrWhiteSpace(this.Errors))
{
this.StartNewTask(() =>
{
this.Errors = "Connecting meters ...";
this.MetersPool.LoadMetersAsync();
})
.ContinueWith(_ =>
{
this.Errors = default(string);
this.EnableButtons(true);
});
}
}
private void StartInspectionCommand()
{
this.StartNewTask(() =>
{
this.EnableButtons(false);
this.MetersPool.RunInspection(this.KeepCorrection);
this.IsCancelInspectionEnabled = false;
this.IsReloadEnabled = true;
});
}
private void CancelInspectionCommand()
{
this.StartNewTask(() =>
{
this.EnableButtons(false);
this.MetersPool.CancelInspection();
this.IsCancelInspectionEnabled = false;
this.IsReloadEnabled = true;
});
}
private void EnableButtons(bool enabled)
{
this.IsReloadEnabled = enabled;
this.IsStartInspectionEnabled = enabled;
this.IsCancelInspectionEnabled = !enabled;
}
}
}