134 lines
3.8 KiB
C#
134 lines
3.8 KiB
C#
namespace OMNIPruefung.ViewModels.Content
|
|
{
|
|
using OMNIPruefung.Models;
|
|
using OMNIPruefung.ViewModels.Base;
|
|
using OMNIPruefung.ViewModels.Options;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.IO.Ports;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Input;
|
|
|
|
public class EOLContentVM : ContentVM
|
|
{
|
|
private readonly EOLSettings appsettings;
|
|
// private readonly OmniPlusDatabase
|
|
|
|
public EOLContentVM() : base()
|
|
{
|
|
this.Header = "EOL";
|
|
this.Option = new EOLOptionVM(this);
|
|
this.Option.OptionSelected += this.NotifyOptionSelected;
|
|
this.Coms = new ObservableCollection<string>();
|
|
this.appsettings = new EOLSettings();
|
|
|
|
this.LoadComPorts = new Command(this.InitializeComPorts);
|
|
this.CancelInspection = new Command(this.CancelInspectionHandler);
|
|
this.StartInspection = new Command(this.StartInspectionHandler);
|
|
|
|
this.InitializeComPorts();
|
|
}
|
|
|
|
private string error;
|
|
public string Error
|
|
{
|
|
get => this.error;
|
|
set
|
|
{
|
|
this.error = value;
|
|
|
|
this.NotifyPropertyChanged(nameof(this.Error));
|
|
}
|
|
}
|
|
|
|
public ICommand LoadComPorts { get; }
|
|
|
|
public ICommand CancelInspection { get; }
|
|
|
|
public ICommand StartInspection { get; }
|
|
|
|
public ObservableCollection<string> Coms { get; }
|
|
|
|
private void InitializeComPorts()
|
|
{
|
|
this.Coms.Clear();
|
|
this.Nodes.Clear();
|
|
this.Option.Clear();
|
|
|
|
try
|
|
{
|
|
var existingComPorts = SerialPort.GetPortNames();
|
|
var placeNr = 1;
|
|
|
|
foreach (var com in this.appsettings.COMPorts)
|
|
{
|
|
var comVM = new COMContentVM();
|
|
comVM.ContentSelected += this.NotifyCotnentSelected;
|
|
comVM.PortName = com;
|
|
comVM.Nr = placeNr++;
|
|
comVM.Exists = existingComPorts.Any(x => x.Equals(com, StringComparison.OrdinalIgnoreCase));
|
|
comVM.Header = comVM.Exists ? $"({com})" : $"({com} - nicht vorhanden)";
|
|
comVM.State = $"{comVM.Nr}. {comVM.Header}";
|
|
|
|
this.Coms.Add(com);
|
|
this.Nodes.Add(comVM);
|
|
this.Option.Add(comVM.Option);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
this.Error = e.Message;
|
|
}
|
|
|
|
this.Option.SetIndentation(this.Option.Indent);
|
|
}
|
|
|
|
private void CancelInspectionHandler()
|
|
{
|
|
foreach (var comVM in this.Nodes.Cast<COMContentVM>())
|
|
{
|
|
comVM.CancelExecution();
|
|
}
|
|
}
|
|
|
|
private void StartInspectionHandler()
|
|
{
|
|
var coms = this.Nodes.Cast<COMContentVM>();
|
|
var tasks = new List<Task>();
|
|
|
|
foreach (var comVM in coms)
|
|
{
|
|
tasks.Add(comVM.PrepareConnection());
|
|
}
|
|
|
|
this.ContinueWhenAll(tasks);
|
|
|
|
foreach (var comVM in coms)
|
|
{
|
|
tasks.Add(comVM.ReadSystemParameters());
|
|
}
|
|
|
|
this.ContinueWhenAll(tasks);
|
|
}
|
|
|
|
private void ContinueWhenAll(List<Task> tasks)
|
|
=> Task.Factory
|
|
.ContinueWhenAll(tasks.ToArray(), _ =>
|
|
{
|
|
tasks.Clear();
|
|
})
|
|
.Wait();
|
|
|
|
public override void Dispose()
|
|
{
|
|
foreach (var comVM in this.Nodes.Cast<COMContentVM>())
|
|
{
|
|
comVM.Dispose();
|
|
}
|
|
}
|
|
}
|
|
}
|