Push Alpha Version
This commit is contained in:
125
uniper_hmi_old/UniperHMI/SettingsPage/SettingsPageVM.cs
Normal file
125
uniper_hmi_old/UniperHMI/SettingsPage/SettingsPageVM.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using MahApps.Metro.Controls;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Windows;
|
||||
using TwinCAT.Ads.TypeSystem;
|
||||
using TwinCAT.TypeSystem;
|
||||
using Heisig.HMI.AdsManager;
|
||||
|
||||
namespace InfineonHMI
|
||||
{
|
||||
public partial class SettingsEntry : ObservableObject
|
||||
{
|
||||
[ObservableProperty]
|
||||
private string name = "";
|
||||
|
||||
[ObservableProperty]
|
||||
private string instancePath = "";
|
||||
|
||||
[ObservableProperty]
|
||||
private string? dataType;
|
||||
|
||||
[ObservableProperty]
|
||||
private Object? value;
|
||||
|
||||
public ObservableCollection<SettingsEntry> SubEntries { get; set; } = [];
|
||||
|
||||
[ObservableProperty]
|
||||
private Visibility visibility = Visibility.Collapsed;
|
||||
|
||||
private readonly IAdsManager _adsManager;
|
||||
|
||||
public SettingsEntry(IAdsManager adsManager)
|
||||
{
|
||||
_adsManager = adsManager;
|
||||
}
|
||||
|
||||
partial void OnValueChanging(object? oldValue, object? newValue)
|
||||
{
|
||||
if (newValue == null)
|
||||
{
|
||||
newValue = oldValue;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
_adsManager.WriteValue(InstancePath, newValue);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
newValue = oldValue;
|
||||
Debug.WriteLine("Value {0} could not be written to {1}", newValue, InstancePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public partial class SettingsPageVM : ObservableObject, IDisposable
|
||||
{
|
||||
public ObservableCollection<SettingsEntry> RootItem { get; private set; } = [];
|
||||
|
||||
private readonly IAdsManager _adsManager;
|
||||
|
||||
private readonly string _variableName;
|
||||
|
||||
public SettingsPageVM(IAdsManager adsManager, string variableName)
|
||||
{
|
||||
_adsManager = adsManager;
|
||||
_variableName = variableName;
|
||||
|
||||
_adsManager.Register(_variableName, ConfigChangedEvent);
|
||||
}
|
||||
|
||||
private void ConfigChangedEvent(object? sender, ValueChangedEventArgs e)
|
||||
{
|
||||
App.Current.Invoke(CreateSettingsTree);
|
||||
}
|
||||
|
||||
private void CreateSettingsTree()
|
||||
{
|
||||
ISymbolLoader? symbolLoader = _adsManager.GetSymbolLoader();
|
||||
|
||||
if (symbolLoader == null)
|
||||
return;
|
||||
|
||||
Symbol configSymbol = (Symbol)symbolLoader.Symbols[_variableName];
|
||||
SettingsEntry entry = GetSymbol(configSymbol);
|
||||
RootItem.Add(entry);
|
||||
}
|
||||
|
||||
private SettingsEntry GetSymbol(Symbol symbol)
|
||||
{
|
||||
// Create Symbol
|
||||
SettingsEntry entry = new(_adsManager)
|
||||
{
|
||||
Name = symbol.InstanceName,
|
||||
InstancePath = symbol.InstancePath,
|
||||
DataType = symbol.DataType?.Name
|
||||
};
|
||||
|
||||
// Check if symbol has sub symbols
|
||||
if (!symbol.IsPrimitiveType)
|
||||
{
|
||||
foreach (Symbol subSymbol in symbol.SubSymbols.Cast<Symbol>())
|
||||
{
|
||||
entry.SubEntries.Add(GetSymbol(subSymbol));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
entry.Visibility = Visibility.Visible;
|
||||
|
||||
entry.Value = symbol.ReadValue();
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_adsManager?.Deregister(_variableName, ConfigChangedEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user