Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
HexStringData.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Text.RegularExpressions;
5using System.Threading.Tasks;
10using Waher.Security;
13
15{
19 public class HexStringData : MqttData
20 {
24 public const string RegExString = @"^\s*([A-Fa-f0-9]{2}){1,}\s*$";
25
29 public static readonly Regex RegEx = new Regex(RegExString, RegexOptions.Compiled | RegexOptions.Singleline);
30
31 private byte[] value;
32 private bool firstReport = true;
33
38 : base()
39 {
40 }
41
47 public HexStringData(MqttTopic Topic, byte[] Value)
48 : base(Topic)
49 {
50 this.value = Value;
51 }
52
59 public override Task<DataProcessingResult> DataReported(MqttTopic Topic, MqttContent Content)
60 {
61 string s = Content.DataString;
62
63 if (RegEx.IsMatch(s))
64 {
65 byte[] Data = Hashes.StringToBinary(s);
66 if (Data is null)
67 return Task.FromResult(DataProcessingResult.Incompatible);
68
69 if (this.firstReport)
70 this.firstReport = false;
71 else
72 {
73 IMqttData Processor = Topic.FindDataType(Content);
74 if (!(Processor is HexStringData))
75 return Task.FromResult(DataProcessingResult.Incompatible);
76 }
77
78 this.value = Data;
79 this.Timestamp = DateTime.UtcNow;
80 this.QoS = Content.Header.QualityOfService;
81 this.Retain = Content.Header.Retain;
82
83 return Task.FromResult(DataProcessingResult.Processed);
84 }
85 else
86 return Task.FromResult(DataProcessingResult.Incompatible);
87 }
88
92 public override Task<string> GetTypeName(Language Language)
93 {
94 return Language.GetStringAsync(typeof(MqttTopicNode), 43, "HEX");
95 }
96
104 public override Task StartReadout(ThingReference ThingReference, ISensorReadout Request, string Prefix, bool Last)
105 {
106 List<Field> Data = new List<Field>()
107 {
108 new Int32Field(ThingReference, this.Timestamp, this.Append(Prefix, "#Bytes"),
109 this.value?.Length ?? 0, FieldType.Momentary, FieldQoS.AutomaticReadout)
110 };
111
112 if (!(this.value is null) && this.value.Length <= 256)
113 {
114 Data.Add(new StringField(ThingReference, this.Timestamp, "Raw",
115 Convert.ToBase64String(this.value), FieldType.Momentary, FieldQoS.AutomaticReadout));
116 }
117
118 Request.ReportFields(Last, Data);
119
120 return Task.CompletedTask;
121 }
122
126 public override bool IsControllable => true;
127
132 {
133 return new ControlParameter[]
134 {
135 new StringControlParameter("Value", "Publish", "Value:", "HEX value of topic.", RegExString,
136 (n) => Task.FromResult(Hashes.BinaryToString(this.value)),
137 (n, v) =>
138 {
139 this.value = Hashes.StringToBinary(v);
140 this.Topic.MqttClient.PUBLISH(this.Topic.FullTopic, this.QoS, this.Retain, Encoding.UTF8.GetBytes(v));
141 return Task.CompletedTask;
142 })
143 };
144 }
145
149 public override void SnifferOutput(ICommunicationLayer Output)
150 {
151 if (this.value is null)
152 this.Information(Output, "NULL");
153 else if (this.value.Length == 1)
154 this.Information(Output, "1 byte.");
155 else
156 this.Information(Output, this.value.Length.ToString() + " bytes.");
157 }
158
162 public override Grade DefaultSupport => Grade.Excellent;
163
171 {
172 IMqttData Result = new HexStringData(Topic, default);
173 Result.DataReported(Topic, Content);
174 return Result;
175 }
176 }
177}
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
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static byte[] StringToBinary(string s)
Parses a hex string.
Definition: Hashes.cs:102
static string BinaryToString(byte[] Data)
Converts an array of bytes to a string with their hexadecimal representations (in lower case).
Definition: Hashes.cs:65
Abstract base class for control parameters.
Represents an MQTT topic with binary data encoded as decimal strings.
const string RegExString
Regular expression for hexadecimal string data.
HexStringData(MqttTopic Topic, byte[] Value)
Represents an MQTT topic with binary data encoded as decimal strings.
static readonly Regex RegEx
Parsed regular expression for hexadecimal string data.
override IMqttData CreateNew(MqttTopic Topic, MqttContent Content)
Creates a new instance of the data.
HexStringData()
Represents an MQTT topic with binary data encoded as decimal strings.
override Task< DataProcessingResult > DataReported(MqttTopic Topic, MqttContent Content)
Called when new data has been published.
override void SnifferOutput(ICommunicationLayer Output)
Outputs the parsed data to the sniffer.
override Task< string > GetTypeName(Language Language)
Type name representing data.
override ControlParameter[] GetControlParameters()
TODO
override Task StartReadout(ThingReference ThingReference, ISensorReadout Request, string Prefix, bool Last)
Starts a readout of the data.
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