239 lines
7.8 KiB
C#
239 lines
7.8 KiB
C#
namespace Common.UI.ViewModels
|
|
{
|
|
using Common.Hardware.SIRT;
|
|
using Common.Hardware.WaterMeter.eRegister;
|
|
using Common.Hardware.WaterMeter.eRegister.Models;
|
|
using Common.UI.Data;
|
|
using Common.UI.Infrastructure;
|
|
|
|
using NLog;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Input;
|
|
|
|
internal class EregisterInitializeVM : ViewModel<EregisterInitializeVM>
|
|
{
|
|
private readonly ILogger logger;
|
|
protected readonly System.Timers.Timer timer;
|
|
protected readonly SIRTFactory sirtFactory;
|
|
protected readonly EregisterBatch eregisterFactory;
|
|
|
|
private CancellationTokenSource cancellationTokenSource;
|
|
protected CancellationToken cancellationToken;
|
|
protected Task programmingTask;
|
|
protected DateTime timestamp;
|
|
private int sharedId;
|
|
|
|
public EregisterInitializeVM()
|
|
{
|
|
this.timer = new System.Timers.Timer
|
|
{
|
|
AutoReset = true,
|
|
Enabled = false,
|
|
Interval = 500,
|
|
};
|
|
this.timer.Elapsed += this.Timer_Elapsed;
|
|
this.logger = AppHost.GetService<ILogger>();
|
|
this.sirtFactory = AppHost.GetService<SIRTFactory>();
|
|
this.sirtFactory.Logging += x => this.logger.Debug(x);
|
|
this.eregisterFactory = new EregisterBatch(this.sirtFactory);
|
|
this.ProgrammingTasks = new ObservableCollection<EregisterTaskVM>();
|
|
this.StartProgrammingCommand = new Command(this.LoadProgrammingTasks);
|
|
this.StopProgrammingCommand = new Command(this.StopProgrammingCommandHandler);
|
|
this.StartEnabled = true;
|
|
}
|
|
|
|
private bool startEnabled;
|
|
public bool StartEnabled
|
|
{
|
|
get => this.startEnabled;
|
|
set => this.Set(vm => vm.startEnabled = value);
|
|
}
|
|
|
|
private bool stopEnabled;
|
|
public bool StopEnabled
|
|
{
|
|
get => this.stopEnabled;
|
|
set => this.Set(vm => vm.stopEnabled = value);
|
|
}
|
|
|
|
private string timeElapsed;
|
|
public string TimeElapsed
|
|
{
|
|
get => this.timeElapsed;
|
|
set => this.Set(vm => vm.timeElapsed = value);
|
|
}
|
|
|
|
public ICommand StartProgrammingCommand { get; }
|
|
|
|
public ICommand StopProgrammingCommand { get; }
|
|
|
|
private IEnumerable<GridItem> programmingTasks;
|
|
public IEnumerable<GridItem> ProgrammingTasks
|
|
{
|
|
get => this.programmingTasks;
|
|
set => this.Set(vm => vm.programmingTasks = value);
|
|
}
|
|
|
|
internal void StartProgramming(int sharedId)
|
|
{
|
|
this.sharedId = sharedId;
|
|
|
|
this.LoadProgrammingTasks();
|
|
}
|
|
|
|
protected void LoadUIComponenets(IEnumerable<IEnumerable<EregisterTask>> programmingTasksEnumerable)
|
|
{
|
|
var col = 0;
|
|
var row = 0;
|
|
var tasks = new List<GridItem>();
|
|
|
|
#region Headers
|
|
col = 0;
|
|
row = 0;
|
|
|
|
foreach (var programmingTasks in programmingTasksEnumerable)
|
|
{
|
|
var firstOrDefault = programmingTasks.FirstOrDefault();
|
|
|
|
tasks.Add(new EregisterHeaderVM
|
|
{
|
|
Column = col++,
|
|
Frequency = $"{firstOrDefault?.Frequency ?? 0}MHz",
|
|
PoNr = firstOrDefault?.PoNr ?? 0,
|
|
Row = row,
|
|
SerialNr = firstOrDefault?.SerialNr ?? 0,
|
|
SlotNr = firstOrDefault?.SlotNr ?? 0
|
|
});
|
|
}
|
|
#endregion Headers
|
|
|
|
#region Tasks
|
|
col = 0;
|
|
row = 1;
|
|
|
|
foreach (var programmingTasks in programmingTasksEnumerable)
|
|
{
|
|
foreach (var programmingTask in programmingTasks)
|
|
{
|
|
tasks.Add(new EregisterTaskVM(programmingTask, col, row++));
|
|
}
|
|
|
|
col += 1;
|
|
row = 1;
|
|
}
|
|
#endregion Tasks
|
|
|
|
this.ProgrammingTasks = tasks;
|
|
}
|
|
|
|
protected virtual async Task StartProgrammingAsync(EregisterProgrammingParametersList parametersList)
|
|
{
|
|
var programmingTasksEnumerable = this.eregisterFactory.LoadInspectionInitializationTasks(parametersList);
|
|
|
|
this.LoadUIComponenets(programmingTasksEnumerable);
|
|
|
|
await Task.Delay(3000);
|
|
|
|
this.TimeElapsed = null;
|
|
this.timestamp = DateTime.Now;
|
|
this.timer.Start();
|
|
this.programmingTask = this.eregisterFactory
|
|
.StartProgrammingAsync(this.cancellationToken)
|
|
.ContinueWith(_ =>
|
|
{
|
|
this.timer.Stop();
|
|
|
|
this.StartEnabled = true;
|
|
this.StopEnabled = false;
|
|
})
|
|
.ContinueWith(async _ =>
|
|
{
|
|
await Task.Delay(1_000);
|
|
|
|
this.sirtFactory.CloseConnections();
|
|
// this.InvokeAsync(App.Current.Shutdown);
|
|
});
|
|
}
|
|
|
|
private void LoadProgrammingTasks()
|
|
{
|
|
this.StartEnabled = false;
|
|
this.StopEnabled = true;
|
|
|
|
this.cancellationTokenSource = new CancellationTokenSource();
|
|
this.cancellationToken = this.cancellationTokenSource.Token;
|
|
|
|
Task.Run(async () =>
|
|
{
|
|
var parametersList = default(EregisterProgrammingParametersList);
|
|
|
|
if (this.sharedId <= 0 && int.TryParse($"{Appsettings.Get("PSNR")}", out var psnr))
|
|
{
|
|
var pendingAction = AuftragDbContext.P2000Shared.LoadPendingActions(psnr);
|
|
this.sharedId = pendingAction?.Id ?? 0;
|
|
}
|
|
|
|
var actions = AuftragDbContext.Eregister_Shared.LoadActions(this.sharedId);
|
|
parametersList = AuftragDbContext.LoadEregisterProgrammingParametersListForSharedId(this.sharedId);
|
|
|
|
var uriFormat = Appsettings.Get("EncryptionKeisUrl");
|
|
var httpClient = new HttpClient();
|
|
|
|
foreach (var epp in parametersList)
|
|
{
|
|
var finalAddress = actions.FirstOrDefault(x => x.SerialNr == epp.SerienNr).FinalAddress;
|
|
var tempAddress = actions.FirstOrDefault(x => x.SerialNr == epp.SerienNr).TempAddress;
|
|
|
|
epp.Adresse = finalAddress;
|
|
epp.TempAdresse = tempAddress;
|
|
|
|
using (var httpResponeMessage = await httpClient.GetAsync(string.Format(uriFormat, epp.SerienNr)))
|
|
{
|
|
if (httpResponeMessage.IsSuccessStatusCode)
|
|
{
|
|
var content = await httpResponeMessage.Content?.ReadAsStringAsync();
|
|
epp.EncryptionKey = content?.Trim('"');
|
|
}
|
|
}
|
|
}
|
|
|
|
await this.StartProgrammingAsync(parametersList);
|
|
|
|
}, this.cancellationToken);
|
|
}
|
|
|
|
private void StopProgrammingCommandHandler()
|
|
{
|
|
this.timer.Stop();
|
|
|
|
this.StartEnabled = true;
|
|
this.StopEnabled = false;
|
|
|
|
try
|
|
{
|
|
this.cancellationTokenSource?.Cancel();
|
|
}
|
|
catch // TODO: (Exception e)
|
|
{
|
|
|
|
}
|
|
|
|
this.eregisterFactory.StopProgramming();
|
|
}
|
|
|
|
private void Timer_Elapsed(object _, System.Timers.ElapsedEventArgs args)
|
|
{
|
|
var stamp = args.SignalTime - timestamp;
|
|
|
|
this.TimeElapsed = stamp.ToString("mm\\:ss");
|
|
}
|
|
}
|
|
}
|