Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
OnDateTime.cs
1using System;
2using System.Threading.Tasks;
3using System.Xml;
5using Waher.Events;
8
10{
15 {
16 private ScriptableDateTimeAttribute value;
17 private ScriptableStringAttribute timeCoordinates;
18
22 public OnDateTime()
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(OnDateTime);
51
56 public override IStateMachineNode Create()
57 {
58 return new OnDateTime();
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 override async Task<EventTimepointUtc> GetEventTimepointUtc(EvaluationArguments Arguments)
79 {
80 if (this.value is null)
81 return new EventTimepointUtc();
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 OnDateTime event definition: " + TimeCoordinates,
121 Arguments.CurrentState.StateMachineId.Value);
122 }
123 }
124
125 Value = Value.ToUniversalTime();
126
127 return new EventTimepointUtc(Value);
128 }
129 }
130
134 public override string Label => this.value.Definition;
135 }
136}
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
async Task< T > Evaluate(Variables Variables)
Evaluates the attribute
Contains information required for evaluating script in a state-machine.
Event raised after a certain date and time.
Definition: OnDateTime.cs:15
string TimeCoordinatesDefinition
Time Coordinates definition.
Definition: OnDateTime.cs:42
override async Task< EventTimepointUtc > GetEventTimepointUtc(EvaluationArguments Arguments)
Gets the timepoint for when the event elapses.
Definition: OnDateTime.cs:78
override Task Parse(XmlElement Xml)
Parses the State-machine node.
Definition: OnDateTime.cs:65
override IStateMachineNode Create()
Creates a new node of the corresponding type.
Definition: OnDateTime.cs:56
OnDateTime()
Event raised after a certain date and time.
Definition: OnDateTime.cs:22
Contains information about when a timed event elapses, in UTC time coordinates.
Abstract base class for timed State-Machine event nodes.