Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
OnTime.cs
1using System;
2using System.Threading.Tasks;
3using System.Xml;
4using Waher.Content;
6using Waher.Events;
9
11{
15 public class OnTime : TimedEventNode
16 {
17 private ScriptableTimeSpanAttribute value;
18 private ScriptableStringAttribute timeCoordinates;
19
23 public OnTime()
24 : base()
25 {
26 }
27
31 [DefaultValueNull]
32 public string ValueDefinition
33 {
34 get => this.value?.Definition;
35 set => this.value = new ScriptableTimeSpanAttribute(value, this);
36 }
37
41 [DefaultValueNull]
43 {
44 get => this.timeCoordinates?.Definition;
45 set => this.timeCoordinates = new ScriptableStringAttribute(value, this);
46 }
47
51 public override string LocalName => nameof(OnTime);
52
57 public override IStateMachineNode Create()
58 {
59 return new OnTime();
60 }
61
66 public override Task Parse(XmlElement Xml)
67 {
68 this.value = new ScriptableTimeSpanAttribute(Xml.InnerText, this);
69 this.timeCoordinates = new ScriptableStringAttribute(XML.Attribute(Xml, "timeCoordinates"), this);
70
71 return base.Parse(Xml);
72 }
73
79 public override async Task<EventTimepointUtc> GetEventTimepointUtc(EvaluationArguments Arguments)
80 {
81 if (this.value is null)
82 return new EventTimepointUtc();
83 else
84 {
85 TimeSpan TS = await this.value.Evaluate(Arguments.Variables);
86 string TimeCoordinates = await this.timeCoordinates.Evaluate(Arguments.Variables);
87 DateTime UtcNow = DateTime.UtcNow;
88 DateTime Value;
89
90 if (string.IsNullOrEmpty(TimeCoordinates) ||
91 string.Equals(TimeCoordinates, "Local", StringComparison.OrdinalIgnoreCase))
92 {
93 Value = DateTime.Today.Add(TS);
94 }
95 else if (string.Equals(TimeCoordinates, "UTC", StringComparison.OrdinalIgnoreCase))
96 {
97 Value = UtcNow.Date.Add(TS);
98 }
99 else if (TimeCoordinates.StartsWith('-') &&
100 TimeSpan.TryParse(TimeCoordinates[1..], out TimeSpan TS2))
101 {
102 Value = UtcNow.Date.Add(TS).Add(TS2);
103 }
104 else if ((TimeCoordinates.StartsWith('+') &&
105 TimeSpan.TryParse(TimeCoordinates[1..], out TS2)) ||
106 TimeSpan.TryParse(TimeCoordinates, out TS2))
107 {
108 Value = UtcNow.Date.Add(TS).Subtract(TS2);
109 }
110 else
111 {
112 Value = DateTime.Today.Add(TS);
113
114 Log.Error("Invalid Time Coordinates in State Machine OnTime event definition: " + TimeCoordinates,
115 Arguments.CurrentState.StateMachineId.Value);
116 }
117
118 Value = Value.ToUniversalTime();
119
120 if (Value < UtcNow)
121 Value = Value.AddDays(Math.Ceiling(UtcNow.Subtract(Value).TotalDays));
122
123 return new EventTimepointUtc(UtcNow, Value, everyDay);
124 }
125 }
126
127 private static readonly Duration everyDay = Duration.FromDays(1);
128
132 public override string Label => this.value.Definition;
133 }
134}
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.
override string Label
UML Label for event.
Definition: OnTime.cs:132
override async Task< EventTimepointUtc > GetEventTimepointUtc(EvaluationArguments Arguments)
Gets the timepoint for when the event elapses.
Definition: OnTime.cs:79
string TimeCoordinatesDefinition
Time Coordinates definition.
Definition: OnTime.cs:43
override IStateMachineNode Create()
Creates a new node of the corresponding type.
Definition: OnTime.cs:57
override Task Parse(XmlElement Xml)
Parses the State-machine node.
Definition: OnTime.cs:66
OnTime()
Event raised at a certain time.
Definition: OnTime.cs:23
Contains information about when a timed event elapses, in UTC time coordinates.
Abstract base class for timed State-Machine event nodes.
Represents a duration value, as defined by the xsd:duration data type: http://www....
Definition: Duration.cs:14
static Duration FromDays(int Days)
Creates a Duration object from a given number of days.
Definition: Duration.cs:604