Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Utf8StringData.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
10
12{
16 public class Utf8StringData : MqttData
17 {
18 private string value;
19
24 : base()
25 {
26 }
27
33 public Utf8StringData(MqttTopic Topic, string Value)
34 : base(Topic)
35 {
36 this.value = Value;
37 }
38
45 public override Task<DataProcessingResult> DataReported(MqttTopic Topic, MqttContent Content)
46 {
47 string s = Content.DataString;
48 byte[] Bin = Encoding.UTF8.GetBytes(s);
49 byte[] Data = Content.Data;
50 int i, c = Bin.Length;
51
52 if (c != Data.Length)
53 return Task.FromResult(DataProcessingResult.Incompatible);
54
55 for (i = 0; i < c; i++)
56 {
57 if (Bin[i] != Data[i])
58 return Task.FromResult(DataProcessingResult.Incompatible);
59 }
60
61 this.value = s;
62 this.Timestamp = DateTime.UtcNow;
63 this.QoS = Content.Header.QualityOfService;
64 this.Retain = Content.Header.Retain;
65
66 return Task.FromResult(DataProcessingResult.ProcessedNewMomentaryValues);
67 }
68
72 public override Task<string> GetTypeName(Language Language)
73 {
74 return Language.GetStringAsync(typeof(MqttTopicNode), 40, "String");
75 }
76
84 public override Task StartReadout(ThingReference ThingReference, ISensorReadout Request, string Prefix, bool Last)
85 {
86 Request.ReportFields(Last, new StringField(ThingReference, this.Timestamp, this.Append(Prefix, "Value"),
87 this.value, FieldType.Momentary, FieldQoS.AutomaticReadout));
88 return Task.CompletedTask;
89 }
90
94 public override bool IsControllable => true;
95
100 {
101 return new ControlParameter[]
102 {
103 new StringControlParameter("Value", "Publish", "Value:", "String value of topic.",
104 (n) => Task.FromResult<string>(this.value),
105 (n, v) =>
106 {
107 this.value = v;
108 this.Topic.MqttClient.PUBLISH(this.Topic.FullTopic, this.QoS, this.Retain, Encoding.UTF8.GetBytes(v));
109 return Task.CompletedTask;
110 })
111 };
112 }
113
117 public override void SnifferOutput(ICommunicationLayer Output)
118 {
119 this.Information(Output, this.value.ToString());
120 }
121
125 public override Grade DefaultSupport => Grade.Ok;
126
134 {
135 IMqttData Result = new Utf8StringData(Topic, default);
136 Result.DataReported(Topic, Content);
137 return Result;
138 }
139 }
140}
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 string data.
override Task StartReadout(ThingReference ThingReference, ISensorReadout Request, string Prefix, bool Last)
Starts a readout of the data.
override Task< string > GetTypeName(Language Language)
Type name representing data.
override Task< DataProcessingResult > DataReported(MqttTopic Topic, MqttContent Content)
Called when new data has been published.
Utf8StringData()
Represents an MQTT topic with string data.
Utf8StringData(MqttTopic Topic, string Value)
Represents an MQTT topic with string data.
override IMqttData CreateNew(MqttTopic Topic, MqttContent Content)
Creates a new instance of the data.
override ControlParameter[] GetControlParameters()
TODO
override void SnifferOutput(ICommunicationLayer Output)
Outputs the parsed data to the sniffer.
MQTT Topic information.
Definition: MqttTopic.cs:19
A Metering node representing an MQTT topic
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