Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
When.cs
1using System;
2using System.Threading.Tasks;
3using System.Xml;
5using Waher.Events;
8
10{
14 public class When : StateMachineNode
15 {
16 private ScriptableDateTimeAttribute value;
17 private ScriptableStringAttribute timeCoordinates;
18
22 public When()
23 : base()
24 {
25 }
26
30 [DefaultValueNull]
31 public string ValueDefinition
32 {
33 get => this.value?.Definition;
34 set => this.value = new ScriptableDateTimeAttribute(value, this);
35 }
36
40 [DefaultValueNull]
42 {
43 get => this.timeCoordinates?.Definition;
44 set => this.timeCoordinates = new ScriptableStringAttribute(value, this);
45 }
46
50 public override string LocalName => nameof(When);
51
56 public override IStateMachineNode Create()
57 {
58 return new When();
59 }
60
65 public override Task Parse(XmlElement Xml)
66 {
67 this.value = new ScriptableDateTimeAttribute(Xml.InnerText, this);
68 this.timeCoordinates = new ScriptableStringAttribute(XML.Attribute(Xml, "timeCoordinates"), this);
69
70 return base.Parse(Xml);
71 }
72
78 public async Task<DateTime> CalcWhenUtc(EvaluationArguments Arguments)
79 {
80 if (this.value is null)
81 return DateTime.UtcNow;
82 else
83 {
84 DateTime Value = await this.value.Evaluate(Arguments.Variables);
85 string TimeCoordinates = await this.timeCoordinates.Evaluate(Arguments.Variables);
86
87 if (!string.IsNullOrEmpty(TimeCoordinates))
88 {
89 if (string.Equals(TimeCoordinates, "Local", StringComparison.OrdinalIgnoreCase))
90 {
91 if (Value.Kind == DateTimeKind.Unspecified)
92 Value = new DateTime(Value.Ticks, DateTimeKind.Local);
93 }
94 else if (string.Equals(TimeCoordinates, "UTC", StringComparison.OrdinalIgnoreCase))
95 {
96 if (Value.Kind == DateTimeKind.Unspecified)
97 Value = new DateTime(Value.Ticks, DateTimeKind.Utc);
98 }
99 else if (TimeCoordinates.StartsWith('-') &&
100 TimeSpan.TryParse(TimeCoordinates[1..], out TimeSpan TS))
101 {
102 if (Value.Kind == DateTimeKind.Unspecified)
103 {
104 Value = new DateTime(Value.Ticks, DateTimeKind.Utc);
105 Value = Value.Add(TS);
106 }
107 }
108 else if ((TimeCoordinates.StartsWith('+') &&
109 TimeSpan.TryParse(TimeCoordinates[1..], out TS)) ||
110 TimeSpan.TryParse(TimeCoordinates, out TS))
111 {
112 if (Value.Kind == DateTimeKind.Unspecified)
113 {
114 Value = new DateTime(Value.Ticks, DateTimeKind.Utc);
115 Value = Value.Subtract(TS);
116 }
117 }
118 else
119 {
120 Log.Error("Invalid Time Coordinates in State Machine When definition: " + TimeCoordinates,
121 Arguments.CurrentState.StateMachineId.Value);
122 }
123 }
124
125 return Value.ToUniversalTime();
126 }
127 }
128
129 }
130}
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:1062
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Error(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an error event.
Definition: Log.cs:692
CaseInsensitiveString StateMachineId
ID of State-Machine.
Definition: CurrentState.cs:41
Defines when an action is to be executed.
Definition: When.cs:15
async Task< DateTime > CalcWhenUtc(EvaluationArguments Arguments)
Calculates the point in time when an action is to be executed.
Definition: When.cs:78
override Task Parse(XmlElement Xml)
Parses the State-machine node.
Definition: When.cs:65
When()
Defines when an action is to be executed.
Definition: When.cs:22
string TimeCoordinatesDefinition
Time Coordinates definition.
Definition: When.cs:42
override IStateMachineNode Create()
Creates a new node of the corresponding type.
Definition: When.cs:56
async Task< T > Evaluate(Variables Variables)
Evaluates the attribute
Contains information required for evaluating script in a state-machine.
Abstract base class for State-Machine nodes.