laatzen/Common/Ui/Common.GUI/ViewModels/Eregister/EregisterManualProgrammingVM.cs
2025-01-24 12:10:40 +01:00

156 lines
5.9 KiB
C#

namespace Common.GUI.ViewModels.Eregister
{
using Common.GUI.ViewModels.Base;
using Common.Hardware.WaterMeter.eRegister.Features;
using Common.Hardware.WaterMeter.eRegister.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
public class EregisterManualProgrammingVM : VM<EregisterManualProgrammingVM>
{
public EregisterManualProgrammingVM()
{
this.ItemsSource = new ObservableCollection<EregisterFeatureItemVM>();
this.InitializeEregisterManualProgrammingVM();
}
public ObservableCollection<EregisterFeatureItemVM> ItemsSource { get; }
private void InitializeEregisterManualProgrammingVM()
{
var features = new SortedDictionary<bool, SortedDictionary<string, SortedDictionary<string, FeatureDictionary>>>();
void ParseType(Type type)
{
foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
var propertyType = property.PropertyType;
if (!typeof(Pam).IsAssignableFrom(propertyType))
{
continue;
}
var pamAttribute = property.GetCustomAttribute<PamAttribute>();
if (pamAttribute is null)
{
continue;
}
var isSingleCmd = pamAttribute.IsSingleCmd;
if (!features.ContainsKey(isSingleCmd))
{
features[isSingleCmd] = new SortedDictionary<string, SortedDictionary<string, FeatureDictionary>>();
}
var _features = features[isSingleCmd];
var authLevel = $"{pamAttribute.AuthLevel}";
if (!_features.ContainsKey(authLevel))
{
_features[authLevel] = new SortedDictionary<string, FeatureDictionary>();
}
var __features = _features[authLevel];
var pam = Activator.CreateInstance(property.PropertyType) as Pam;
var cmdHex = $"{property.Name}: {pam.CmdHex}";
if (!__features.ContainsKey(cmdHex))
{
__features[cmdHex] = new FeatureDictionary();
}
var ___features = __features[cmdHex];
___features.FeatureInfo = property;
foreach (var _property in propertyType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (_property.CanWrite && _property.PropertyType != typeof(byte[]))
{
___features[_property.Name] = _property;
}
}
}
}
ParseType(typeof(EregisterFeatures));
ParseType(typeof(EregisterDebugFeatures));
foreach (var kvp in features)
{
var type = kvp.Key ? "Single PAM" : "Multi PAM";
foreach (var _kvp in kvp.Value)
{
var _item = new EregisterFeatureItemVM
{
Header = $"{type} - Auth level {_kvp.Key}"
};
foreach (var __kvp in _kvp.Value)
{
var __item = new EregisterFeatureItemVM
{
Header = $"{__kvp.Key}"
};
foreach (var ___kvp in __kvp.Value)
{
var header = ___kvp.Key;
var propertyInfo = ___kvp.Value;
var propertyType = propertyInfo.PropertyType;
var ___item = default(EregisterFeatureParameterItemVM);
if (propertyType == typeof(bool))
{
___item = new EregisterFeatureBooleanValueVM(propertyInfo, header);
}
else if (propertyType == typeof(byte))
{
___item = new EregisterFeatureByteValueVM(propertyInfo, header);
}
else if (propertyType == typeof(int))
{
___item = new EregisterFeatureInt32ValueVM(propertyInfo, header);
}
else if (propertyType == typeof(ushort))
{
___item = new EregisterFeatureUInt16ValueVM(propertyInfo, header);
}
else if (propertyType == typeof(uint))
{
___item = new EregisterFeatureUInt32ValueVM(propertyInfo, header);
}
else if (propertyType.IsEnum)
{
___item = new EregisterFeatureOptionValueVM(propertyInfo, header);
}
else
{
___item = new EregisterFeatureStringValueVM(propertyInfo, header);
}
__item.ItemsSource.Add(___item);
}
_item.ItemsSource.Add(__item);
}
this.ItemsSource.Add(_item);
}
}
}
}
public class FeatureDictionary : SortedDictionary<string, PropertyInfo>
{
public PropertyInfo FeatureInfo { get; set; }
}
}