Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FloatingPointData.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
4using Waher.Content;
11
13{
18 {
19 private double value;
20 private byte nrDecimals;
21
26 : base()
27 {
28 }
29
36 public FloatingPointData(MqttTopic Topic, double Value, byte NrDecimals)
37 : base(Topic)
38 {
39 this.value = Value;
40 this.nrDecimals = NrDecimals;
41 }
42
49 public override Task<DataProcessingResult> DataReported(MqttTopic Topic, MqttContent Content)
50 {
51 if (CommonTypes.TryParse(Content.DataString, out double x, out byte NrDecimals))
52 {
53 this.value = x;
54 this.nrDecimals = NrDecimals;
55 this.Timestamp = DateTime.UtcNow;
56 this.QoS = Content.Header.QualityOfService;
57 this.Retain = Content.Header.Retain;
58
59 return Task.FromResult(DataProcessingResult.ProcessedNewMomentaryValues);
60 }
61 else
62 return Task.FromResult(DataProcessingResult.Incompatible);
63 }
64
68 public override Task<string> GetTypeName(Language Language)
69 {
70 return Language.GetStringAsync(typeof(MqttTopicNode), 35, "Floating-point");
71 }
72
80 public override Task StartReadout(ThingReference ThingReference, ISensorReadout Request, string Prefix, bool Last)
81 {
82 Request.ReportFields(Last, new QuantityField(ThingReference, this.Timestamp, this.Append(Prefix, "Value"),
83 this.value, this.nrDecimals, string.Empty, FieldType.Momentary, FieldQoS.AutomaticReadout));
84
85 return Task.CompletedTask;
86 }
87
91 public override bool IsControllable => true;
92
97 {
98 return new ControlParameter[]
99 {
100 new DoubleControlParameter("Value", "Publish", "Value:", "Floating-point value of topic.",
101 (n) => Task.FromResult<double?>(this.value),
102 (n, v) =>
103 {
104 this.value = v;
105 this.Topic.MqttClient.PUBLISH(this.Topic.FullTopic, this.QoS, this.Retain, Encoding.UTF8.GetBytes(CommonTypes.Encode(v, this.nrDecimals)));
106 return Task.CompletedTask;
107 })
108 };
109 }
110
114 public override void SnifferOutput(ICommunicationLayer Output)
115 {
116 this.Information(Output, this.value.ToString());
117 }
118
122 public override Grade DefaultSupport => Grade.Excellent;
123
131 {
132 IMqttData Result = new FloatingPointData(Topic, default, default);
133 Result.DataReported(Topic, Content);
134 return Result;
135 }
136 }
137}
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.
Represents an MQTT topic with Floating-point data.
override Task StartReadout(ThingReference ThingReference, ISensorReadout Request, string Prefix, bool Last)
Starts a readout of the data.
FloatingPointData(MqttTopic Topic, double Value, byte NrDecimals)
Represents an MQTT topic with Floating-point data.
override Task< DataProcessingResult > DataReported(MqttTopic Topic, MqttContent Content)
Called when new data has been published.
override IMqttData CreateNew(MqttTopic Topic, MqttContent Content)
Creates a new instance of the data.
override Task< string > GetTypeName(Language Language)
Type name representing data.
FloatingPointData()
Represents an MQTT topic with Floating-point data.
override void SnifferOutput(ICommunicationLayer Output)
Outputs the parsed data to the sniffer.
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 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