213 lines
7.0 KiB
C#
213 lines
7.0 KiB
C#
namespace Common.UI.ViewModels
|
|
{
|
|
using Common.Hardware.SIRT;
|
|
using Common.Hardware.SIRT.Tasks;
|
|
using Common.Hardware.WaterMeter.eRegister;
|
|
using Common.Hardware.WaterMeter.eRegister.Features;
|
|
using Common.Hardware.WaterMeter.eRegister.Models;
|
|
using Common.UI.Infrastructure;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Input;
|
|
|
|
public class ManualProgrammingVM : VM<ManualProgrammingVM>
|
|
{
|
|
public int? Frequency { get; set; } = 433;
|
|
|
|
public uint? SerienNr { get; set; } = 22724224;
|
|
|
|
public uint? Address { get; set; } = 319017454;
|
|
|
|
private bool enabled = true;
|
|
public bool IsEnabled
|
|
{
|
|
get => this.enabled;
|
|
set => this.SetValue(x => x.enabled = value);
|
|
}
|
|
|
|
public ICommand SetFactoryState => new Command(this.SetFactoryStateCommand);
|
|
|
|
public ObservableCollection<string> ItemsSource { get; set; } = new ObservableCollection<string>();
|
|
|
|
private void SetFactoryStateCommand()
|
|
{
|
|
this.InvokeAsync(() => this.IsEnabled = false);
|
|
|
|
Task.Factory.StartNew(async () =>
|
|
{
|
|
void WriteToLog(string message) => this.InvokeAsync(() => this.ItemsSource.Add(message));
|
|
|
|
WriteToLog($"MHz| | {this.Frequency}");
|
|
WriteToLog($"Adr| |esse: {this.Address}");
|
|
WriteToLog($"Ser| |ienNr: {this.SerienNr}");
|
|
|
|
var uriFormat = Appsettings.GetSetting("EncryptionKeisUrl");
|
|
var httpClient = new HttpClient();
|
|
var encryptionKey = default(byte[]);
|
|
|
|
try
|
|
{
|
|
using (var httpResponeMessage = await httpClient.GetAsync(string.Format(uriFormat, this.SerienNr)))
|
|
{
|
|
var content = await httpResponeMessage.Content.ReadAsStringAsync();
|
|
|
|
if (httpResponeMessage?.IsSuccessStatusCode == true)
|
|
{
|
|
var _encryptionKey = content?.Trim('"');
|
|
encryptionKey = _encryptionKey?.GetEncryptionKey();
|
|
}
|
|
else
|
|
{
|
|
WriteToLog("Feh| |ler beim Laden des Schlüssels!");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
WriteToLog(e.Message);
|
|
|
|
return;
|
|
}
|
|
|
|
WriteToLog($"Enc| |ryption key: {BitConverter.ToString(encryptionKey)}");
|
|
|
|
var task = new EregisterTask(this.Frequency.Value)
|
|
{
|
|
Timeout = 10_000,
|
|
WakeUpCmd = true,
|
|
DelPamSemi = true,
|
|
WakeUpModul = true,
|
|
RequestAddress = this.Address.Value,
|
|
EncryptionKeyI = encryptionKey,
|
|
Data = new ChangeFactoryState
|
|
{
|
|
FactoryState = FactoryState.Production
|
|
}
|
|
.Append(new ProvidePin
|
|
{
|
|
AuthLevel = AuthLevel.III
|
|
})
|
|
};
|
|
|
|
void MessageReceived(byte[] bytes) => WriteToLog($"RX:| |{BitConverter.ToString(bytes)}");
|
|
|
|
task.MessageReceived += MessageReceived;
|
|
|
|
for (var i = 1; i <= 5 && task.State != SIRTTaskState.Completed && task.State != SIRTTaskState.Cancelled; i++)
|
|
{
|
|
if (AppHost.SIRTHub.Start(task))
|
|
{
|
|
WriteToLog($"---| |{i} -------------------------");
|
|
WriteToLog($"TX:| |{task}");
|
|
|
|
task.Wait();
|
|
}
|
|
}
|
|
|
|
task.MessageReceived -= MessageReceived;
|
|
|
|
WriteToLog($"---| |-----------------------------");
|
|
WriteToLog($"ST:| |{task.State}");
|
|
|
|
this.InvokeAsync(() => this.IsEnabled = true);
|
|
});
|
|
}
|
|
}
|
|
|
|
public class DynamicPropertyVM : VM<DynamicPropertyVM>
|
|
{
|
|
protected readonly Dictionary<string, DynamicPropertyVM> properties;
|
|
protected readonly PropertyInfo propertyInfo;
|
|
protected readonly object instance;
|
|
protected readonly object value;
|
|
|
|
public DynamicPropertyVM(object instance, PropertyInfo propertyInfo)
|
|
{
|
|
this.properties = new Dictionary<string, DynamicPropertyVM>();
|
|
this.propertyInfo = propertyInfo;
|
|
this.instance = instance;
|
|
|
|
if (this.propertyInfo != null)
|
|
{
|
|
this.Name = this.propertyInfo.Name;
|
|
this.value = Activator.CreateInstance(this.propertyInfo.PropertyType);
|
|
|
|
if (this.value is Pam pam)
|
|
{
|
|
this.Name = $"{pam.CmdHex} - {this.Name}";
|
|
}
|
|
}
|
|
}
|
|
|
|
public event Action PropertyValueChanged;
|
|
|
|
public string Name { get; }
|
|
|
|
private bool selected;
|
|
public bool Selected
|
|
{
|
|
get => this.selected;
|
|
set => this.SetValue(x =>
|
|
{
|
|
x.selected = value;
|
|
|
|
var propertyValue = default(object);
|
|
|
|
if (x.selected)
|
|
{
|
|
propertyValue = this.value;
|
|
}
|
|
|
|
this.propertyInfo.SetValue(this.instance, propertyValue);
|
|
this.PropertyValueChanged?.Invoke();
|
|
});
|
|
}
|
|
|
|
public virtual IEnumerable<DynamicPropertyVM> GetAllProperties()
|
|
=> this.properties
|
|
.Values
|
|
.SelectMany(x => x.GetAllProperties())
|
|
.Union(this.properties.Values)
|
|
.OrderBy(x => x.Name);
|
|
}
|
|
|
|
public class DynamicTypeVM : DynamicPropertyVM
|
|
{
|
|
private readonly Type type;
|
|
|
|
public DynamicTypeVM(object instance) : base(instance, null)
|
|
{
|
|
this.type = this.instance.GetType();
|
|
|
|
foreach (var propertyInfo in this.type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
|
|
{
|
|
var pamAttribute = propertyInfo.GetCustomAttribute<PamAttribute>();
|
|
|
|
if (pamAttribute is null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var dynamicProperty = new DynamicPropertyVM(this.instance, propertyInfo);
|
|
|
|
if (propertyInfo.PropertyType == typeof(EregisterDebugFeatures))
|
|
{
|
|
var propertyInstance = new EregisterDebugFeatures();
|
|
|
|
propertyInfo.SetValue(this.instance, propertyInstance);
|
|
|
|
dynamicProperty = new DynamicTypeVM(propertyInstance);
|
|
}
|
|
|
|
this.properties[propertyInfo.Name] = dynamicProperty;
|
|
}
|
|
}
|
|
}
|
|
}
|