Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BinaryData.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
10
12{
16 public class BinaryData : MqttData
17 {
18 private byte[] value;
19 private bool firstReport = true;
20
24 public BinaryData()
25 : base()
26 {
27 }
28
34 public BinaryData(MqttTopic Topic, byte[] Value)
35 : base(Topic)
36 {
37 this.value = Value;
38 }
39
46 public override Task<DataProcessingResult> DataReported(MqttTopic Topic, MqttContent Content)
47 {
48 if (this.firstReport)
49 this.firstReport = false;
50 else
51 {
52 IMqttData Processor = Topic.FindDataType(Content);
53 if (!(Processor is BinaryData))
54 return Task.FromResult(DataProcessingResult.Incompatible);
55 }
56
57 this.value = Content.Data;
58 this.Timestamp = DateTime.UtcNow;
59 this.QoS = Content.Header.QualityOfService;
60 this.Retain = Content.Header.Retain;
61
62 return Task.FromResult(DataProcessingResult.Processed);
63 }
64
68 public override Task<string> GetTypeName(Language Language)
69 {
70 return Language.GetStringAsync(typeof(MqttTopicNode), 31, "Binary");
71 }
72
80 public override Task StartReadout(ThingReference ThingReference, ISensorReadout Request, string Prefix, bool Last)
81 {
82 List<Field> Data = new List<Field>()
83 {
84 new Int32Field(ThingReference, this.Timestamp, this.Append(Prefix, "#Bytes"),
85 this.value?.Length ?? 0, FieldType.Momentary, FieldQoS.AutomaticReadout)
86 };
87
88 if (!(this.value is null) && this.value.Length <= 256)
89 {
90 Data.Add(new StringField(ThingReference, this.Timestamp, "Raw",
91 Convert.ToBase64String(this.value), FieldType.Momentary, FieldQoS.AutomaticReadout));
92 }
93
94 Request.ReportFields(Last, Data);
95
96 return Task.CompletedTask;
97 }
98
102 public override bool IsControllable => true;
103
108 {
109 return new ControlParameter[]
110 {
111 new StringControlParameter("Value", "Publish", "Value:", "BASE-64 value of topic.", Base64Data.RegExString,
112 (n) => Task.FromResult<string>(Convert.ToBase64String(this.value)),
113 (n, v) =>
114 {
115 this.value = Convert.FromBase64String(v);
116 this.Topic.MqttClient.PUBLISH(this.Topic.FullTopic, this.QoS, this.Retain, this.value);
117 return Task.CompletedTask;
118 })
119 };
120 }
121
125 public override void SnifferOutput(ICommunicationLayer Output)
126 {
127 if (this.value is null)
128 this.Information(Output, "NULL");
129 else if (this.value.Length == 1)
130 this.Information(Output, "1 byte.");
131 else
132 this.Information(Output, this.value.Length.ToString() + " bytes.");
133 }
134
138 public override Grade DefaultSupport => Grade.Barely;
139
147 {
148 IMqttData Result = new BinaryData(Topic, default);
149 Result.DataReported(Topic, Content);
150 return Result;
151 }
152 }
153}
Information about content received from the MQTT server.
Definition: MqttContent.cs:9
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 base64-encoded binary data.
Definition: Base64Data.cs:19
const string RegExString
Regular expression for BASE64-encoded data.
Definition: Base64Data.cs:23
Represents an MQTT topic with binary data.
Definition: BinaryData.cs:17
override Task StartReadout(ThingReference ThingReference, ISensorReadout Request, string Prefix, bool Last)
Starts a readout of the data.
Definition: BinaryData.cs:80
override Task< string > GetTypeName(Language Language)
Type name representing data.
Definition: BinaryData.cs:68
override Grade DefaultSupport
Default support.
Definition: BinaryData.cs:138
BinaryData(MqttTopic Topic, byte[] Value)
Represents an MQTT topic with binary data.
Definition: BinaryData.cs:34
override void SnifferOutput(ICommunicationLayer Output)
Outputs the parsed data to the sniffer.
Definition: BinaryData.cs:125
override ControlParameter[] GetControlParameters()
TODO
Definition: BinaryData.cs:107
override Task< DataProcessingResult > DataReported(MqttTopic Topic, MqttContent Content)
Called when new data has been published.
Definition: BinaryData.cs:46
override IMqttData CreateNew(MqttTopic Topic, MqttContent Content)
Creates a new instance of the data.
Definition: BinaryData.cs:146
BinaryData()
Represents an MQTT topic with binary data.
Definition: BinaryData.cs:24
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
IMqttData FindDataType(MqttContent Content)
FInds best implementation to process binary data.
Definition: MqttTopic.cs:273
A Metering node representing an MQTT topic
Represents a 32-bit integer value.
Definition: Int32Field.cs:10
Represents a string value.
Definition: StringField.cs:10
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