Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BooleanData.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
4using Waher.Content;
11
13{
17 public class BooleanData : MqttData
18 {
19 private bool value;
20
24 public BooleanData()
25 : base()
26 {
27 }
28
34 public BooleanData(MqttTopic Topic, bool Value)
35 : base(Topic)
36 {
37 this.value = Value;
38 }
39
46 public override Task<DataProcessingResult> DataReported(MqttTopic Topic, MqttContent Content)
47 {
48 if (CommonTypes.TryParse(Content.DataString, out bool b))
49 {
50 this.value = b;
51 this.Timestamp = DateTime.UtcNow;
52 this.QoS = Content.Header.QualityOfService;
53 this.Retain = Content.Header.Retain;
54 return Task.FromResult(DataProcessingResult.ProcessedNewMomentaryValues);
55 }
56 else
57 return Task.FromResult(DataProcessingResult.Incompatible);
58 }
59
63 public override Task<string> GetTypeName(Language Language)
64 {
65 return Language.GetStringAsync(typeof(MqttTopicNode), 32, "Boolean");
66 }
67
75 public override Task StartReadout(ThingReference ThingReference, ISensorReadout Request, string Prefix, bool Last)
76 {
77 Request.ReportFields(Last, new BooleanField(ThingReference, this.Timestamp, this.Append(Prefix, "Value"),
78 this.value, FieldType.Momentary, FieldQoS.AutomaticReadout));
79
80 return Task.CompletedTask;
81 }
82
86 public override bool IsControllable => true;
87
92 {
93 return new ControlParameter[]
94 {
95 new BooleanControlParameter("Value", "Publish", "Value.", "Boolean value of topic.",
96 (n) => Task.FromResult<bool?>(this.value),
97 (n, v) =>
98 {
99 this.value = v;
100 this.Topic.MqttClient.PUBLISH(this.Topic.FullTopic, this.QoS, this.Retain, Encoding.UTF8.GetBytes(CommonTypes.Encode(v)));
101 return Task.CompletedTask;
102 })
103 };
104 }
105
109 public override void SnifferOutput(ICommunicationLayer Output)
110 {
111 this.Information(Output, this.value.ToString());
112 }
113
117 public override Grade DefaultSupport => Grade.Perfect;
118
126 {
127 IMqttData Result = new BooleanData(Topic, default);
128 Result.DataReported(Topic, Content);
129 return Result;
130 }
131 }
132}
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 boolean data.
Definition: BooleanData.cs:18
BooleanData()
Represents an MQTT topic with boolean data.
Definition: BooleanData.cs:24
override Task StartReadout(ThingReference ThingReference, ISensorReadout Request, string Prefix, bool Last)
Starts a readout of the data.
Definition: BooleanData.cs:75
override Task< string > GetTypeName(Language Language)
Type name representing data.
Definition: BooleanData.cs:63
override IMqttData CreateNew(MqttTopic Topic, MqttContent Content)
Creates a new instance of the data.
Definition: BooleanData.cs:125
override void SnifferOutput(ICommunicationLayer Output)
Outputs the parsed data to the sniffer.
Definition: BooleanData.cs:109
override ControlParameter[] GetControlParameters()
TODO
Definition: BooleanData.cs:91
override Grade DefaultSupport
Default support.
Definition: BooleanData.cs:117
override Task< DataProcessingResult > DataReported(MqttTopic Topic, MqttContent Content)
Called when new data has been published.
Definition: BooleanData.cs:46
BooleanData(MqttTopic Topic, bool Value)
Represents an MQTT topic with boolean data.
Definition: BooleanData.cs:34
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 boolean value that can be either true or false.
Definition: BooleanField.cs:11
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