namespace Common.UI.VFM.ViewModels { using Common.UI.VFM.Models; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Threading; using System.Threading.Tasks; using System.Windows.Input; public class ShipmnetViewModel : AppViewModel { private readonly OmniShipmentModel model; public ShipmnetViewModel() : base() { this.resetted = true; this.model = new OmniShipmentModel(this.PortName, this.SqlConnectionString, this.LabelServer); this.ResetProcessState = new Command(this.ResetProcessStateHandler); this.StartProcessing = new Command(this.StartProcessingHandler); this.ReprintBoxLable = new Command(this.ReprintBoxLableHandler); this.ReprintTestLable = new Command(this.ReprintTestLableHandler); this.Processes = new ObservableCollection(this.LoadProcesses()); } public String CustId { get => this.model.CustId; set => this.Set(() => { this.model.CustId = value?.Trim(); this.ReprintEnabled = !this.Proccessing && this.model.WasPrinted(); }); } public String MeterPcb { get => this.model.MeterPcb; set => this.Set(() => { this.model.MeterPcb = value?.Trim(); this.ReprintEnabled = !this.Proccessing && this.model.WasPrinted(); }); } private Boolean reprintEnabled; public Boolean ReprintEnabled { get => this.reprintEnabled; set => this.Set(() => this.reprintEnabled = value); } private String message; public String Message { get => this.message; set => this.Set(() => this.message = value); } private Boolean resetted; private Boolean processing; public Boolean Proccessing { get => this.processing; set => this.Set(() => this.processing = value); } public ICommand ResetProcessState { get; } public ICommand StartProcessing { get; } public ICommand ReprintBoxLable { get; } public ICommand ReprintTestLable { get; } public ObservableCollection Processes { get; } private IEnumerable LoadProcesses() { foreach (var kvp in this.model.GetProcesses()) { yield return kvp; } } private void ReprintBoxLableHandler() { if (!this.Proccessing) { Task.Factory .StartNew(state => { if (state is ShipmnetViewModel viewModel) { var model = viewModel.model; model.LoadProductionOrderData(); model.LoadProductionTestData(); model.LoadDefaultValues(); // model.PrintBoxLabel(); } }, this, CancellationToken.None) .ContinueWith(task => { if (task.AsyncState is ShipmnetViewModel viewModel) { if (task.Exception != null) { viewModel.Message = task.Exception.Message; } viewModel.Proccessing = false; } }); } } private void ReprintTestLableHandler() { if (!this.Proccessing) { Task.Factory .StartNew(state => { if (state is ShipmnetViewModel viewModel) { var model = viewModel.model; model.LoadProductionOrderData(); model.LoadProductionTestData(); model.LoadDefaultValues(); // model.PrintTestLabel(); } }, this, CancellationToken.None) .ContinueWith(task => { if (task.AsyncState is ShipmnetViewModel viewModel) { if (task.Exception != null) { viewModel.Message = task.Exception.Message; } viewModel.Proccessing = false; } }); } } private void ResetProcessStateHandler() { if (!this.resetted) { this.resetted = true; this.CustId = default(String); this.MeterPcb = default(String); this.Message = default(String); foreach (var process in this.Processes) { process.State = ProcessState.Undefined; process.Message = default(String); } } } private void StartProcessingHandler() { if (!this.Proccessing && this.resetted) { Task.Factory .StartNew(state => { if (state is ShipmnetViewModel viewModel) { viewModel.resetted = false; viewModel.Proccessing = true; viewModel.ReprintEnabled = false; viewModel.Message = "Programmierung lauft ... Bitte warten!"; viewModel.model.Initialize(); var proccessed = true; foreach (var process in viewModel.Processes) { var result = process.StartProcessing(); if (result.State == ProcessState.Error) { viewModel.Message = "Programmierung wurde abgebrochen!"; proccessed = false; break; } } if (proccessed) { viewModel.model.Finish(); viewModel.Message = "Programmierung erfolgreich abgeschlossen! Etiketten werden gedruckt."; } viewModel.Proccessing = false; viewModel.ReprintEnabled = true; } }, this, CancellationToken.None) .ContinueWith(task => { if (task.AsyncState is ShipmnetViewModel viewModel) { if (task.Exception != null) { viewModel.Message = task.Exception.Message; } } }); } } } }