Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
QuantityData.cs
1using System;
2using System.Text;
3using System.Text.RegularExpressions;
4using System.Threading.Tasks;
5using Waher.Content;
12
14{
18 public class QuantityData : MqttData
19 {
23 public static readonly Regex RegEx = new Regex(@"^(?'Magnitude'[+-]?\d*([.]\d*)?([eE][+-]?\d+)?)\s+(?'Unit'[^\s]+)$", RegexOptions.Compiled | RegexOptions.Singleline);
24
25 private string unit;
26 private double value;
27 private byte nrDecimals;
28
32 public QuantityData()
33 : base()
34 {
35 }
36
44 public QuantityData(MqttTopic Topic, double Value, byte NrDecimals, string Unit)
45 : base(Topic)
46 {
47 this.value = Value;
48 this.nrDecimals = NrDecimals;
49 this.unit = Unit;
50 }
51
58 public override Task<DataProcessingResult> DataReported(MqttTopic Topic, MqttContent Content)
59 {
60 string s = Content.DataString;
61 Match M = RegEx.Match(s);
62 if (M.Success && CommonTypes.TryParse(M.Groups["Magnitude"].Value, out this.value, out this.nrDecimals))
63 {
64 this.unit = M.Groups["Unit"].Value;
65 this.Timestamp = DateTime.UtcNow;
66 this.QoS = Content.Header.QualityOfService;
67 this.Retain = Content.Header.Retain;
68
69 return Task.FromResult(DataProcessingResult.ProcessedNewMomentaryValues);
70 }
71 else
72 return Task.FromResult(DataProcessingResult.Incompatible);
73 }
74
78 public override Task<string> GetTypeName(Language Language)
79 {
80 return Language.GetStringAsync(typeof(MqttTopicNode), 39, "Quantity");
81 }
82
90 public override Task StartReadout(ThingReference ThingReference, ISensorReadout Request, string Prefix, bool Last)
91 {
92 Request.ReportFields(Last, new QuantityField(ThingReference, this.Timestamp, this.Append(Prefix, "Value"),
93 this.value, this.nrDecimals, this.unit, FieldType.Momentary, FieldQoS.AutomaticReadout));
94 return Task.CompletedTask;
95 }
96
100 public override bool IsControllable => true;
101
106 {
107 return new ControlParameter[]
108 {
109 new DoubleControlParameter("Value", "Publish", "Value:", "Units: " + this.unit,
110 (n) => Task.FromResult<double?>(this.value),
111 (n, v) =>
112 {
113 this.value = v;
114 this.Topic.MqttClient.PUBLISH(this.Topic.FullTopic, this.QoS, this.Retain, Encoding.UTF8.GetBytes(CommonTypes.Encode(v, this.nrDecimals) + " " + this.unit));
115 return Task.CompletedTask;
116 })
117 };
118 }
119
123 public override void SnifferOutput(ICommunicationLayer Output)
124 {
125 this.Information(Output, this.value.ToString("F" + this.nrDecimals.ToString()) + " " + this.unit);
126 }
127
131 public override Grade DefaultSupport => Grade.Perfect;
132
140 {
141 IMqttData Result = new QuantityData(Topic, default, default, default);
142 Result.DataReported(Topic, Content);
143 return Result;
144 }
145 }
146}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:46
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.
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
Represents an MQTT topic with Physical Quantity data.
Definition: QuantityData.cs:19
override Task< string > GetTypeName(Language Language)
Type name representing data.
Definition: QuantityData.cs:78
QuantityData(MqttTopic Topic, double Value, byte NrDecimals, string Unit)
Represents an MQTT topic with Physical Quantity data.
Definition: QuantityData.cs:44
override IMqttData CreateNew(MqttTopic Topic, MqttContent Content)
Creates a new instance of the data.
QuantityData()
Represents an MQTT topic with Physical Quantity data.
Definition: QuantityData.cs:32
override void SnifferOutput(ICommunicationLayer Output)
Outputs the parsed data to the sniffer.
override ControlParameter[] GetControlParameters()
TODO
override Task StartReadout(ThingReference ThingReference, ISensorReadout Request, string Prefix, bool Last)
Starts a readout of the data.
Definition: QuantityData.cs:90
override Task< DataProcessingResult > DataReported(MqttTopic Topic, MqttContent Content)
Called when new data has been published.
Definition: QuantityData.cs:58
override Grade DefaultSupport
Default support.
MQTT Topic information.
Definition: MqttTopic.cs:19
A Metering node representing an MQTT topic
Represents a physical quantity 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