Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DateTimeOffsetData.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
4using Waher.Content;
12
14{
19 {
20 private DateTimeOffset value;
21
26 : base()
27 {
28 }
29
35 public DateTimeOffsetData(MqttTopic Topic, DateTimeOffset Value)
36 : base(Topic)
37 {
38 this.value = Value;
39 }
40
47 public override Task<DataProcessingResult> DataReported(MqttTopic Topic, MqttContent Content)
48 {
49 if (DateTimeOffset.TryParse(Content.DataString, out DateTimeOffset Value) ||
50 CommonTypes.TryParseRfc822(Content.DataString, out Value) ||
51 XML.TryParse(Content.DataString, out Value))
52 {
53 this.value = Value;
54 this.Timestamp = DateTime.UtcNow;
55 this.QoS = Content.Header.QualityOfService;
56 this.Retain = Content.Header.Retain;
57
58 return Task.FromResult(DataProcessingResult.ProcessedNewMomentaryValues);
59 }
60 else
61 return Task.FromResult(DataProcessingResult.Incompatible);
62 }
63
67 public override Task<string> GetTypeName(Language Language)
68 {
69 return Language.GetStringAsync(typeof(MqttTopicNode), 33, "Date and Time");
70 }
71
79 public override Task StartReadout(ThingReference ThingReference, ISensorReadout Request, string Prefix, bool Last)
80 {
81 Request.ReportFields(Last, new DateTimeField(ThingReference, this.Timestamp, this.Append(Prefix, "Value"),
82 this.value.DateTime, FieldType.Momentary, FieldQoS.AutomaticReadout));
83
84 return Task.CompletedTask;
85 }
86
90 public override bool IsControllable => true;
91
96 {
97 return new ControlParameter[]
98 {
99 new DateTimeControlParameter("Timestamp", "Publish", "Timestamp:", "Date & time portion of topic.", null, null,
100 (n) => Task.FromResult<DateTime?>(this.value.DateTime),
101 (n, v) =>
102 {
103 this.value = new DateTimeOffset(v, this.value.Offset);
104 this.Topic.MqttClient.PUBLISH(this.Topic.FullTopic, this.QoS, this.Retain, Encoding.UTF8.GetBytes(CommonTypes.EncodeRfc822(this.value)));
105 return Task.CompletedTask;
106 }),
107 new TimeControlParameter("Offset", "Publish", "Time zone:", "Time zone portion of topic.", null, null,
108 (n) => Task.FromResult<TimeSpan?>(this.value.Offset),
109 (n, v) =>
110 {
111 this.value = new DateTimeOffset(this.value.DateTime, v);
112 this.Topic.MqttClient.PUBLISH(this.Topic.FullTopic, this.QoS, this.Retain, Encoding.UTF8.GetBytes(CommonTypes.EncodeRfc822(this.value)));
113 return Task.CompletedTask;
114 })
115 };
116 }
117
121 public override void SnifferOutput(ICommunicationLayer Output)
122 {
123 this.Information(Output, this.value.ToString());
124 }
125
129 public override Grade DefaultSupport => Grade.Perfect;
130
138 {
139 IMqttData Result = new DateTimeOffsetData(Topic, default);
140 Result.DataReported(Topic, Content);
141 return Result;
142 }
143 }
144}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static bool TryParseRfc822(string s, out DateTimeOffset Value)
Parses a date and time value encoded according to RFC 822, §5.
Definition: CommonTypes.cs:170
Helps with common XML-related tasks.
Definition: XML.cs:19
static bool TryParse(string s, out DateTime Value)
Tries to decode a string encoded DateTime.
Definition: XML.cs:744
Information about content received from the MQTT server.
Definition: MqttContent.cs:9
string DataString
String representation of UTF-8 encoded binary data.
Definition: MqttContent.cs:56
MqttHeader Header
MQTT Header
Definition: MqttContent.cs:35
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
Abstract base class for control parameters.
Represents an MQTT topic with Date & Time & Offset data.
override Task< DataProcessingResult > DataReported(MqttTopic Topic, MqttContent Content)
Called when new data has been published.
DateTimeOffsetData()
Represents an MQTT topic with Date & Time & Offset data.
override Task StartReadout(ThingReference ThingReference, ISensorReadout Request, string Prefix, bool Last)
Starts a readout of the data.
override void SnifferOutput(ICommunicationLayer Output)
Outputs the parsed data to the sniffer.
override Task< string > GetTypeName(Language Language)
Type name representing data.
override IMqttData CreateNew(MqttTopic Topic, MqttContent Content)
Creates a new instance of the data.
DateTimeOffsetData(MqttTopic Topic, DateTimeOffset Value)
Represents an MQTT topic with Date & Time & Offset data.
Abstract base class for MQTT data encapsulations.
Definition: MqttData.cs:18
void Information(ICommunicationLayer Output, string Info)
Outputs information to sniffer.
Definition: MqttData.cs:91
DateTime Timestamp
Timestamp of data reception.
Definition: MqttData.cs:39
string Append(string Prefix, string Name)
Appends a name to a topic name.
Definition: MqttData.cs:100
MQTT Topic information.
Definition: MqttTopic.cs:19
A Metering node representing an MQTT topic
Represents a date and optional time value.
Contains a reference to a thing
Interface for observable classes implementing communication protocols.
Interface for classes managing sensor data readouts.
Task ReportFields(bool Done, params Field[] Fields)
Report read fields to the client.
Interface for MQTT Data encapsulations
Definition: IMqttData.cs:38
Task< DataProcessingResult > DataReported(MqttTopic Topic, MqttContent Content)
Called when new data has been published.
Grade
Grade enumeration
Definition: Grade.cs:7
DataProcessingResult
Results from processing an incoming message.
Definition: IMqttData.cs:17
FieldQoS
Field Quality of Service flags
Definition: FieldQoS.cs:10
FieldType
Field Type flags
Definition: FieldType.cs:10
MqttQualityOfService QualityOfService
Quality of Service level.
Definition: MqttHeader.cs:16