eregister init and finit
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
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 System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
internal class EregisterManuelProgrammingVM : BaseViewModel<EregisterManuelProgrammingVM>
|
||||
{
|
||||
private readonly System.Timers.Timer timer;
|
||||
private readonly SIRTBroadcaster broadcaster;
|
||||
private readonly EregisterBatch eregisterFactory;
|
||||
|
||||
private CancellationTokenSource cancellationTokenSource;
|
||||
private CancellationToken cancellationToken;
|
||||
private Task programmingTask;
|
||||
private DateTime timestamp;
|
||||
|
||||
public EregisterManuelProgrammingVM()
|
||||
{
|
||||
this.timer = new System.Timers.Timer
|
||||
{
|
||||
AutoReset = true,
|
||||
Enabled = false,
|
||||
Interval = 500,
|
||||
};
|
||||
this.timer.Elapsed += this.Timer_Elapsed;
|
||||
this.broadcaster = AppHost.SIRTBroadcaster;
|
||||
this.eregisterFactory = new EregisterBatch(this.broadcaster);
|
||||
this.ProgrammingTsks = new ObservableCollection<EregisterTasksVM>();
|
||||
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; }
|
||||
|
||||
public ObservableCollection<EregisterTasksVM> ProgrammingTsks { get; set; }
|
||||
|
||||
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);
|
||||
|
||||
var pendingAction = AuftragDbContext.P2000Shared.LoadPendingActions(1);
|
||||
var sharedId = pendingAction?.Id ?? 0;
|
||||
var actions = AuftragDbContext.Eregister_Shared.LoadActions(sharedId);
|
||||
parametersList = AuftragDbContext.LoadEregisterProgrammingParametersList(sharedId);
|
||||
|
||||
var uriFormat = Appsettings.Get("EncryptionKeisUrl");
|
||||
var httpClient = new HttpClient();
|
||||
|
||||
foreach (var epp in parametersList)
|
||||
{
|
||||
epp.Adresse = actions.FirstOrDefault(x => x.SerialNr == epp.SerienNr).FinalAddress;
|
||||
epp.TempAdresse = actions.FirstOrDefault(x => x.SerialNr == epp.SerienNr).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('"');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var programmingTasksEnumerable = this.eregisterFactory.LoadInspectionInitialization(parametersList);
|
||||
|
||||
this.InvokeAsync(() =>
|
||||
{
|
||||
foreach (var pt in this.ProgrammingTsks)
|
||||
{
|
||||
pt.ProgrammingTsks.Clear();
|
||||
}
|
||||
|
||||
this.ProgrammingTsks.Clear();
|
||||
|
||||
|
||||
foreach (var programmingTasks in programmingTasksEnumerable)
|
||||
{
|
||||
this.ProgrammingTsks.Add(new EregisterTasksVM(programmingTasks));
|
||||
}
|
||||
});
|
||||
|
||||
await Task.Delay(1000);
|
||||
|
||||
this.TimeElapsed = null;
|
||||
this.timestamp = DateTime.Now;
|
||||
this.timer.Start();
|
||||
this.programmingTask = this.eregisterFactory
|
||||
.StartInspectionInitialization(this.cancellationToken)
|
||||
.ContinueWith(_ =>
|
||||
{
|
||||
this.timer.Stop();
|
||||
|
||||
this.StartEnabled = true;
|
||||
this.StopEnabled = false;
|
||||
});
|
||||
|
||||
}, 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
namespace Common.UI.ViewModels
|
||||
{
|
||||
using Common.Hardware.SIRT;
|
||||
using Common.Hardware.WaterMeter.eRegister;
|
||||
using Common.UI.Infrastructure;
|
||||
|
||||
internal class EregisterTaskVM : BaseViewModel<EregisterTaskVM>
|
||||
{
|
||||
private readonly EregisterTask programmingTask;
|
||||
|
||||
public EregisterTaskVM(EregisterTask programmingTask)
|
||||
{
|
||||
this.programmingTask = programmingTask;
|
||||
this.programmingTask.Changed += this.UpdateState;
|
||||
|
||||
this.UpdateState(this.programmingTask);
|
||||
}
|
||||
|
||||
private int slotNr;
|
||||
public int SlotNr
|
||||
{
|
||||
get => this.slotNr;
|
||||
set => this.Set(vm => vm.slotNr = value);
|
||||
}
|
||||
|
||||
private int poNr;
|
||||
public int PoNr
|
||||
{
|
||||
get => this.poNr;
|
||||
set => this.Set(vm => vm.poNr = value);
|
||||
}
|
||||
|
||||
private int serialNr;
|
||||
public int SerialNr
|
||||
{
|
||||
get => this.serialNr;
|
||||
set => this.Set(vm => vm.serialNr = value);
|
||||
}
|
||||
|
||||
private string name;
|
||||
public string Name
|
||||
{
|
||||
get => this.name;
|
||||
set => this.Set(vm => vm.name = value);
|
||||
}
|
||||
|
||||
private string label;
|
||||
public string Label
|
||||
{
|
||||
get => this.label;
|
||||
set => this.Set(vm => vm.label = value);
|
||||
}
|
||||
|
||||
private uint address;
|
||||
public uint Address
|
||||
{
|
||||
get => this.address;
|
||||
set => this.Set(vm => vm.address = value);
|
||||
}
|
||||
|
||||
private int frequency;
|
||||
public int Frequency
|
||||
{
|
||||
get => this.frequency;
|
||||
set => this.Set(vm => vm.frequency = value);
|
||||
}
|
||||
|
||||
private string rssi;
|
||||
|
||||
public string RSSI
|
||||
{
|
||||
get => this.rssi;
|
||||
set => this.Set(vm => vm.rssi = value);
|
||||
}
|
||||
|
||||
private SIRTTaskState state;
|
||||
public SIRTTaskState State
|
||||
{
|
||||
get => this.state;
|
||||
set => this.Set(vm => vm.state = value);
|
||||
}
|
||||
|
||||
private void UpdateState(EregisterTask task)
|
||||
=> this.InvokeAsync(() =>
|
||||
{
|
||||
this.State = task.State;
|
||||
this.SlotNr = task.SlotNr;
|
||||
this.PoNr = task.PoNr;
|
||||
this.SerialNr = task.SerialNr;
|
||||
this.Name = task.Name;
|
||||
this.Label = task.Label;
|
||||
this.Address = task.Address;
|
||||
this.Frequency = task.Frequency;
|
||||
this.RSSI = task.RSSI;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace Common.UI.ViewModels
|
||||
{
|
||||
using Common.Hardware.WaterMeter.eRegister;
|
||||
using Common.UI.Infrastructure;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
internal class EregisterTasksVM : BaseViewModel<EregisterTaskVM>
|
||||
{
|
||||
public EregisterTasksVM(IEnumerable<EregisterTask> programmingTasks)
|
||||
{
|
||||
this.ProgrammingTsks = new ObservableCollection<EregisterTaskVM>();
|
||||
|
||||
foreach (var programmingTask in programmingTasks)
|
||||
{
|
||||
this.ProgrammingTsks.Add(new EregisterTaskVM(programmingTask));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public ObservableCollection<EregisterTaskVM> ProgrammingTsks { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
namespace Common.UI.ViewModels
|
||||
{
|
||||
using Common.UI.Infrastructure;
|
||||
|
||||
using System;
|
||||
using System.Windows.Input;
|
||||
|
||||
internal class MainWindowVM : BaseViewModel<MainWindowVM>
|
||||
{
|
||||
public MainWindowVM()
|
||||
{
|
||||
this.SetSourceCommand = new Command<string>(this.SetSourceCommandHandler);
|
||||
|
||||
this.SetSourceCommandHandler("EregisterManuelProgramming.xaml");
|
||||
}
|
||||
|
||||
private Uri source;
|
||||
public Uri Source
|
||||
{
|
||||
get => this.source;
|
||||
set => this.Set(vm => vm.source = value);
|
||||
}
|
||||
|
||||
public ICommand SetSourceCommand { get; }
|
||||
|
||||
private void SetSourceCommandHandler(string url)
|
||||
=> this.Source = new Uri(url, UriKind.Relative);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
namespace Common.UI.ViewModels
|
||||
{
|
||||
using Common.Hardware.Ports;
|
||||
using Common.Hardware.SIRT;
|
||||
using Common.UI.Infrastructure;
|
||||
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Management;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
internal class SIRTBroadcasterVM : BaseViewModel<SIRTBroadcasterVM>
|
||||
{
|
||||
private readonly SIRTBroadcaster broadcaster;
|
||||
private readonly ManagementEventWatcher comWatcher;
|
||||
|
||||
public SIRTBroadcasterVM()
|
||||
{
|
||||
this.broadcaster = AppHost.SIRTBroadcaster;
|
||||
this.Connections = new ObservableCollection<SIRTConnectionVM>();
|
||||
this.PortNames = new ObservableCollection<string>();
|
||||
this.AddSIRTCommand = new Command(this.AddSIRTConnectionCommandHandler);
|
||||
this.ClearAllCommand = new Command(this.ClearAllCommandHandler);
|
||||
this.comWatcher = new ManagementEventWatcher("SELECT * FROM Win32_DeviceChangeEvent");
|
||||
this.comWatcher.EventArrived += (_, args) => this.UpdatePortNames();
|
||||
this.comWatcher.Start();
|
||||
|
||||
this.AddSIRTs();
|
||||
this.UpdatePortNames();
|
||||
}
|
||||
|
||||
private bool comPortsEnabled;
|
||||
public bool ComPortsEnabled
|
||||
{
|
||||
get => this.comPortsEnabled;
|
||||
set => this.Set(vm => vm.comPortsEnabled = value);
|
||||
}
|
||||
|
||||
private bool addEnabled;
|
||||
public bool AddEnabled
|
||||
{
|
||||
get => this.addEnabled;
|
||||
set => this.Set(vm => vm.addEnabled = value);
|
||||
}
|
||||
|
||||
private bool cleanEnabled;
|
||||
public bool ClearEnabled
|
||||
{
|
||||
get => this.cleanEnabled;
|
||||
set => this.Set(vm => vm.cleanEnabled = value);
|
||||
}
|
||||
|
||||
private string portName;
|
||||
public string PortName
|
||||
{
|
||||
get => this.portName;
|
||||
set
|
||||
{
|
||||
this.Set(vm => vm.portName = value);
|
||||
|
||||
this.AddEnabled = !string.IsNullOrWhiteSpace(value);
|
||||
}
|
||||
}
|
||||
|
||||
public Action InvokeWhenReady { get; set; }
|
||||
|
||||
public ICommand AddSIRTCommand { get; }
|
||||
|
||||
public ICommand ClearAllCommand { get; }
|
||||
|
||||
public ObservableCollection<string> PortNames { get; }
|
||||
|
||||
public ObservableCollection<SIRTConnectionVM> Connections { get; }
|
||||
|
||||
private void AddSIRTConnectionCommandHandler()
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(this.PortName))
|
||||
{
|
||||
var connection = this.broadcaster.CreateConnection(this.PortName);
|
||||
var connectionVM = new SIRTConnectionVM(connection);
|
||||
connectionVM.Removed += this.RemoveSIRTConnectionVM;
|
||||
this.Connections.Add(connectionVM);
|
||||
|
||||
this.ClearEnabled = true;
|
||||
this.PortName = default(string);
|
||||
this.UpdatePortNames();
|
||||
}
|
||||
}
|
||||
|
||||
private void ClearAllCommandHandler()
|
||||
{
|
||||
this.ComPortsEnabled = false;
|
||||
this.ClearEnabled = false;
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
this.broadcaster.RemoveConnections();
|
||||
this.InvokeAsync(this.Connections.Clear);
|
||||
this.UpdatePortNames();
|
||||
});
|
||||
}
|
||||
|
||||
private void RemoveSIRTConnectionVM(SIRTConnectionVM connectionVM)
|
||||
{
|
||||
this.ComPortsEnabled = false;
|
||||
this.ClearEnabled = false;
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
this.broadcaster.RemoveConnection(connectionVM.PortName);
|
||||
})
|
||||
.ContinueWith(_ =>
|
||||
{
|
||||
this.InvokeAsync(() =>
|
||||
{
|
||||
this.Connections.Remove(connectionVM);
|
||||
});
|
||||
|
||||
this.UpdatePortNames();
|
||||
});
|
||||
}
|
||||
|
||||
private void AddSIRTs()
|
||||
{
|
||||
var portNames = Appsettings
|
||||
.Get("SIRTComPortNames")
|
||||
?.Split(new[] { ';', ',', ' ' })
|
||||
?.Select(x => x?.Trim())
|
||||
?.ToArray() ?? new string[0];
|
||||
|
||||
portNames = SerialPort
|
||||
.GetPortNames()
|
||||
.Intersect(portNames)
|
||||
.ToArray();
|
||||
|
||||
foreach (var portName in portNames)
|
||||
{
|
||||
var connection = this.broadcaster.CreateConnection(portName);
|
||||
var sirtVM = new SIRTConnectionVM(connection);
|
||||
this.Connections.Add(sirtVM);
|
||||
this.ClearEnabled = true;
|
||||
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdatePortNames()
|
||||
=> this.InvokeAsync(() =>
|
||||
{
|
||||
this.PortNames.Clear();
|
||||
|
||||
var portNames = SerialPort
|
||||
.GetPortNames()
|
||||
.Except(this.broadcaster.GetPortNames())
|
||||
.OrderBy(x => x);
|
||||
|
||||
foreach (var portName in portNames)
|
||||
{
|
||||
this.PortNames.Add(portName);
|
||||
}
|
||||
|
||||
this.ComPortsEnabled = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
namespace Common.UI.ViewModels
|
||||
{
|
||||
using Common.Hardware.SIRT;
|
||||
using Common.UI.Infrastructure;
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
internal class SIRTConnectionVM : BaseViewModel<SIRTConnectionVM>
|
||||
{
|
||||
private readonly SIRTConnection connection;
|
||||
|
||||
public SIRTConnectionVM(SIRTConnection connection)
|
||||
{
|
||||
this.connection = connection;
|
||||
this.connection.Activated += this.SIRTBroadcaster_Activated;
|
||||
this.connection.Connected += this.SIRTBroadcaster_Connected;
|
||||
this.connection.Disconnected += this.SIRTBroadcaster_Disconnected;
|
||||
this.RemoveCommand = new Command(this.RemoveCommand_Handler);
|
||||
this.DisconnectCommand = new Command(this.DisconnectCommand_Handler);
|
||||
this.ReconnectCommand = new Command(this.ReconnectCommand_Handler);
|
||||
|
||||
this.UpdateState(this.connection);
|
||||
}
|
||||
|
||||
public event Action<SIRTConnectionVM> Removed;
|
||||
|
||||
private string portName;
|
||||
public string PortName
|
||||
{
|
||||
get => this.portName;
|
||||
set => this.Set(vm => vm.portName = value);
|
||||
}
|
||||
|
||||
private string hexId;
|
||||
public string HexId
|
||||
{
|
||||
get => this.hexId;
|
||||
set => this.Set(vm => vm.hexId = value);
|
||||
}
|
||||
|
||||
private string frequency;
|
||||
public string Frequency
|
||||
{
|
||||
get => this.frequency;
|
||||
set => this.Set(vm => vm.frequency = value);
|
||||
}
|
||||
|
||||
private string running;
|
||||
public string IsRunning
|
||||
{
|
||||
get => this.running;
|
||||
set => this.Set(vm => vm.running = value);
|
||||
}
|
||||
|
||||
private bool disconnectEnabled;
|
||||
public bool DisconnectEnabled
|
||||
{
|
||||
get => this.disconnectEnabled;
|
||||
set => this.Set(vm => vm.disconnectEnabled = value);
|
||||
}
|
||||
|
||||
private bool reconnectEnabled;
|
||||
public bool ReconnectEnabled
|
||||
{
|
||||
get => this.reconnectEnabled;
|
||||
set => this.Set(vm => vm.reconnectEnabled = value);
|
||||
}
|
||||
|
||||
private bool removeEnabled;
|
||||
public bool RemoveEnabled
|
||||
{
|
||||
get => this.removeEnabled;
|
||||
set => this.Set(vm => vm.removeEnabled = value);
|
||||
}
|
||||
|
||||
public ICommand DisconnectCommand { get; }
|
||||
|
||||
public ICommand ReconnectCommand { get; }
|
||||
|
||||
public ICommand RemoveCommand { get; }
|
||||
|
||||
private void RemoveCommand_Handler()
|
||||
=> this.Removed?.Invoke(this);
|
||||
|
||||
private void DisconnectCommand_Handler()
|
||||
{
|
||||
this.DisconnectEnabled = false;
|
||||
this.ReconnectEnabled = false;
|
||||
this.RemoveEnabled = false;
|
||||
this.IsRunning = null;
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
this.connection.Close();
|
||||
this.UpdateState(this.connection);
|
||||
});
|
||||
}
|
||||
|
||||
private void ReconnectCommand_Handler()
|
||||
{
|
||||
this.DisconnectEnabled = false;
|
||||
this.ReconnectEnabled = false;
|
||||
this.RemoveEnabled = false;
|
||||
this.IsRunning = null;
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
this.connection.Close();
|
||||
this.UpdateState(this.connection);
|
||||
this.connection.Open();
|
||||
});
|
||||
}
|
||||
|
||||
private void SIRTBroadcaster_Activated()
|
||||
{
|
||||
this.DisconnectEnabled = true;
|
||||
this.ReconnectEnabled = false;
|
||||
this.RemoveEnabled = true;
|
||||
this.IsRunning = "Activated";
|
||||
this.UpdateState(this.connection);
|
||||
}
|
||||
|
||||
private void SIRTBroadcaster_Connected()
|
||||
{
|
||||
this.DisconnectEnabled = true;
|
||||
this.ReconnectEnabled = false;
|
||||
this.RemoveEnabled = true;
|
||||
this.IsRunning = "Connected";
|
||||
this.UpdateState(this.connection);
|
||||
}
|
||||
|
||||
private void SIRTBroadcaster_Disconnected()
|
||||
{
|
||||
this.DisconnectEnabled = false;
|
||||
this.ReconnectEnabled = true;
|
||||
this.RemoveEnabled = true;
|
||||
this.IsRunning = "Disconnected";
|
||||
this.UpdateState(this.connection);
|
||||
}
|
||||
|
||||
private void UpdateState(SIRTConnection connection)
|
||||
{
|
||||
this.PortName = connection.COMPort;
|
||||
this.HexId = connection.HexId;
|
||||
this.Frequency = $"{connection.Frequency}MHz";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user