Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MachineVariablesViewModel.cs
1using CommunityToolkit.Mvvm.ComponentModel;
3using System.Collections.ObjectModel;
4using System.Diagnostics.CodeAnalysis;
5using Waher.Script;
6
8{
13 {
19 : base()
20 {
21 this.Variables = [];
22
23 if (Args is not null)
24 {
25 this.Running = Args.Running;
26 this.Ended = Args.Ended;
27 this.CurrentState = Args.CurrentState;
28
29 if (Args.Variables is not null)
30 {
31 foreach (Variable Variable in Args.Variables)
32 this.Variables.Add(new VariableModel(Variable.Name, Variable.ValueObject));
33 }
34 }
35 }
36
38 protected override async Task OnInitialize()
39 {
40 await base.OnInitialize();
41
42 ServiceRef.XmppService.NeuroFeatureVariablesUpdated += this.Wallet_VariablesUpdated;
43 ServiceRef.XmppService.NeuroFeatureStateUpdated += this.Wallet_StateUpdated;
44 }
45
47 protected override Task OnDispose()
48 {
49 ServiceRef.XmppService.NeuroFeatureVariablesUpdated -= this.Wallet_VariablesUpdated;
50 ServiceRef.XmppService.NeuroFeatureStateUpdated -= this.Wallet_StateUpdated;
51
52 return base.OnDispose();
53 }
54
55 private Task Wallet_StateUpdated(object? Sender, NeuroFeatures.NewStateEventArgs e)
56 {
57 MainThread.BeginInvokeOnMainThread(() =>
58 {
59 this.CurrentState = e.NewState;
60 this.Ended = string.IsNullOrEmpty(e.NewState);
61 this.Running = !this.Ended;
62 });
63
64 return Task.CompletedTask;
65 }
66
67 private Task Wallet_VariablesUpdated(object? Sender, NeuroFeatures.VariablesUpdatedEventArgs e)
68 {
69 MainThread.BeginInvokeOnMainThread(() =>
70 {
71 foreach (Variable Variable in e.Variables)
72 {
73 if (this.TryGetVariableMode(Variable.Name, out VariableModel? Model))
74 Model.UpdateValue(Variable.ValueObject);
75 else
76 this.Variables.Add(new VariableModel(Variable.Name, Variable.ValueObject));
77 }
78 });
79
80 return Task.CompletedTask;
81 }
82
83 private bool TryGetVariableMode(string Name, [NotNullWhen(true)] out VariableModel? Result)
84 {
85 foreach (VariableModel Model in this.Variables)
86 {
87 if (Model.Name == Name)
88 {
89 Result = Model;
90 return true;
91 }
92 }
93
94 Result = null;
95 return false;
96 }
97
98 #region Properties
99
103 public ObservableCollection<VariableModel> Variables { get; }
104
108 [ObservableProperty]
109 private bool running;
110
114 [ObservableProperty]
115 private bool ended;
116
120 [ObservableProperty]
121 private string? currentState;
122
123 #endregion
124
125 }
126}
A base class for all view models, inheriting from the BindableObject. NOTE: using this class requir...
Holds navigation parameters specific to the current state of a state-machine.
The view model to bind to for when displaying information about the current state of a state-machine.
MachineVariablesViewModel(MachineVariablesNavigationArgs? Args)
The view model to bind to for when displaying information about the current state of a state-machine.
override Task OnDispose()
Method called when the view is disposed, and will not be used more. Use this method to unregister eve...
override async Task OnInitialize()
Method called when view is initialized for the first time. Use this method to implement registration ...
Contains information about a variable.
Definition: Variable.cs:10
object ValueObject
Object Value of variable.
Definition: Variable.cs:88
string Name
Name of variable.
Definition: Variable.cs:78
Collection of variables.
Definition: Variables.cs:25
virtual Variable Add(string Name, object Value)
Adds a variable to the collection.
Definition: Variables.cs:122