fraidy commit

This commit is contained in:
Stoyan Zlatev
2025-01-24 14:45:10 +01:00
parent 4f535b81cb
commit 9b76168d0d
6 changed files with 282 additions and 151 deletions
+2 -1
View File
@@ -75,6 +75,7 @@
<Compile Include="Controls\Eregister\EregisterFeaturesItemsControl.xaml.cs">
<DependentUpon>EregisterFeaturesItemsControl.xaml</DependentUpon>
</Compile>
<Compile Include="Models\Pages\Eregister\ManualProgrammingModel.cs" />
<Compile Include="Pages\Eregister\ManualProgrammingPage.xaml.cs">
<DependentUpon>ManualProgrammingPage.xaml</DependentUpon>
</Compile>
@@ -86,6 +87,7 @@
</Compile>
<Compile Include="ViewModels\Eregister\EregisterFeatureItemVM.cs" />
<Compile Include="ViewModels\Eregister\EregisterManualProgrammingVM.cs" />
<Compile Include="ViewModels\Eregister\FeatureDictionary.cs" />
<Compile Include="ViewModels\VMLocator.cs" />
<Page Include="Controls\Eregister\EregisterFeaturesItemsControl.xaml">
<SubType>Designer</SubType>
@@ -171,7 +173,6 @@
<ItemGroup>
<Folder Include="Models\Data\" />
<Folder Include="Models\Http\" />
<Folder Include="Models\Pages\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Hardware\WaterMeter\eRegister\Common.Hardware.WaterMeter.eRegister\Common.Hardware.WaterMeter.eRegister.csproj">
@@ -0,0 +1,6 @@
namespace Common.GUI.Models.Pages.Eregister
{
public class ManualProgrammingModel
{
}
}
@@ -6,6 +6,7 @@
<Controls:MainLayout>
<Grid DataContext="{Binding EregisterManualProgrammingVM, Source={StaticResource VMLocator}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
@@ -18,7 +19,8 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock FontWeight="Bold"
<TextBlock Background="WhiteSmoke"
FontWeight="Bold"
Grid.Row="0"
Padding="10 3"
Text="{Binding Header}" />
@@ -31,10 +33,9 @@
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Margin="20 0 0 0"
Padding="10 3"
Text="{Binding Header}" />
<ItemsControl Grid.Row="1" ItemsSource="{Binding ItemsSource}" Margin="40 0 0 0">
<ItemsControl Grid.Row="1" ItemsSource="{Binding ItemsSource}" Margin="20 0 0 0">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type VM:EregisterFeatureBooleanValueVM}">
<CheckBox Background="WhiteSmoke"
@@ -117,8 +118,7 @@
HorizontalAlignment="Right"
ItemsSource="{Binding Options}"
Padding="10 3"
SelectedValue="{Binding Value}"
SelectedValuePath="{Binding Content}"
SelectedItem="{Binding Option}"
Width="150" />
<TextBlock Foreground="DimGray"
HorizontalAlignment="Stretch"
@@ -1,23 +1,202 @@
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.Linq;
using System.Reflection;
public class EregisterFeaturesVM : ObservableCollection<EregisterFeatureItemVM>
{
private readonly EregisterFeatures features = new EregisterFeatures();
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)
{
foreach (var _kvp in kvp.Value)
{
var item = new EregisterFeatureItemVM
{
Header = $"{(kvp.Key ? "Single PAM" : "Multi PAM")} - Auth level: {_kvp.Key}"
};
foreach (var __kvp in _kvp.Value)
{
var _item = new EregisterFeaturesItemVM
{
Header = $"{__kvp.Key}",
FeatureInfo = __kvp.Value.FeatureInfo
};
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.ValueChanged += this.ValueChanged;
_item.Add(__item);
}
item.Add(_item);
}
this.Add(item);
}
}
}
private void ValueChanged()
{
foreach (var item in this)
{
item.BindFeatures(this.features);
}
}
}
public class EregisterFeatureItemVM : VM<EregisterFeatureItemVM>
{
public event Action ValueChanged;
public string Header { get; set; }
public ObservableCollection<EregisterFeatureItemVM> ItemsSource { get; }
= new ObservableCollection<EregisterFeatureItemVM>();
internal virtual void Add(EregisterFeatureItemVM item)
=> this.ItemsSource.Add(item);
protected void NotifyValueChanged()
=> this.ValueChanged?.Invoke();
internal virtual void BindFeatures(object features)
{
}
}
public class EregisterFeaturesItemVM : EregisterFeatureItemVM
{
public object Feature { get; set; }
public PropertyInfo FeatureInfo { get; set; }
internal override void BindFeatures(object features)
{
var valuesCount = this.ItemsSource.Count;
foreach (var item in this.ItemsSource)
{
if (item is EregisterFeatureParameterItemVM parameter)
{
if (parameter.Value is null)
{
valuesCount--;
}
}
}
}
}
public class EregisterFeatureParameterItemVM : EregisterFeatureItemVM
{
private readonly PropertyInfo propertyInfo;
protected readonly PropertyInfo propertyInfo;
public EregisterFeatureParameterItemVM(PropertyInfo propertyInfo, string header)
{
@@ -29,7 +208,17 @@
public object Value
{
get => this.value;
set => this.SetValue(() => this.value = value);
set => this.SetValue(value);
}
protected virtual void SetValue(object value)
{
this.SetValue(() => this.value = value, nameof(Value));
if (value != null)
{
this.NotifyValueChanged();
}
}
}
@@ -38,6 +227,13 @@
public EregisterFeatureBooleanValueVM(PropertyInfo propertyInfo, string header) : base(propertyInfo, header)
{
}
protected override void SetValue(object value)
{
value = bool.TryParse($"{value}", out var _value) ? _value : default(bool?);
base.SetValue(value);
}
}
public class EregisterFeatureByteValueVM : EregisterFeatureParameterItemVM
@@ -45,6 +241,13 @@
public EregisterFeatureByteValueVM(PropertyInfo propertyInfo, string header) : base(propertyInfo, header)
{
}
protected override void SetValue(object value)
{
value = byte.TryParse($"{value}", out var _value) ? _value : default(byte?);
base.SetValue(value);
}
}
public class EregisterFeatureInt32ValueVM : EregisterFeatureParameterItemVM
@@ -52,6 +255,13 @@
public EregisterFeatureInt32ValueVM(PropertyInfo propertyInfo, string header) : base(propertyInfo, header)
{
}
protected override void SetValue(object value)
{
value = int.TryParse($"{value}", out var _value) ? _value : default(int?);
base.SetValue(value);
}
}
public class EregisterFeatureUInt16ValueVM : EregisterFeatureParameterItemVM
@@ -59,6 +269,13 @@
public EregisterFeatureUInt16ValueVM(PropertyInfo propertyInfo, string header) : base(propertyInfo, header)
{
}
protected override void SetValue(object value)
{
value = ushort.TryParse($"{value}", out var _value) ? _value : default(ushort?);
base.SetValue(value);
}
}
public class EregisterFeatureUInt32ValueVM : EregisterFeatureParameterItemVM
@@ -66,6 +283,13 @@
public EregisterFeatureUInt32ValueVM(PropertyInfo propertyInfo, string header) : base(propertyInfo, header)
{
}
protected override void SetValue(object value)
{
value = uint.TryParse($"{value}", out var _value) ? _value : default(uint?);
base.SetValue(value);
}
}
public class EregisterFeatureStringValueVM : EregisterFeatureParameterItemVM
@@ -73,6 +297,16 @@
public EregisterFeatureStringValueVM(PropertyInfo propertyInfo, string header) : base(propertyInfo, header)
{
}
protected override void SetValue(object value)
{
if (string.IsNullOrWhiteSpace($"{value}"))
{
value = null;
}
base.SetValue(value);
}
}
public class EregisterFeatureOptionValueVM : EregisterFeatureParameterItemVM
@@ -81,6 +315,27 @@
: base(propertyInfo, header)
=> this.Options = Enum.GetNames(propertyInfo.PropertyType);
public string Option
{
get => $"{this.Value}";
set => this.SetValue(value);
}
public ICollection<string> Options { get; set; }
protected override void SetValue(object value)
{
var enumType = this.propertyInfo.PropertyType;
var parameters = new object[] { $"{value}", true, null };
var enumTryParseResult = typeof(Enum)
?.GetMethods(BindingFlags.Public | BindingFlags.Static)
?.Where(x => x.Name.StartsWith(nameof(Enum.TryParse)))
?.FirstOrDefault()
?.MakeGenericMethod(enumType)
?.Invoke(null, parameters);
value = enumTryParseResult is bool parsed && parsed ? parameters[2] : null;
base.SetValue(value);
}
}
}
@@ -1,155 +1,14 @@
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();
this.ItemsSource = new EregisterFeaturesVM();
}
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; }
public EregisterFeaturesVM ItemsSource { get; }
}
}
@@ -0,0 +1,10 @@
namespace Common.GUI.ViewModels.Eregister
{
using System.Collections.Generic;
using System.Reflection;
public class FeatureDictionary : SortedDictionary<string, PropertyInfo>
{
public PropertyInfo FeatureInfo { get; set; }
}
}