2using System.Collections.Generic;
4using System.Threading.Tasks;
45 private readonly Dictionary<TedsAccessCode, KeyValuePair<DateTime, Teds>> teds =
new Dictionary<TedsAccessCode, KeyValuePair<DateTime, Teds>>();
46 private readonly
byte[] ncapId;
47 private readonly
byte[] timId;
48 private readonly ushort channelId;
49 private readonly
string communicationTopic;
74 this.communicationTopic = this.EvaluateCommunicationTopic();
99 public byte[]
TimId => this.timId;
116 private string EvaluateCommunicationTopic()
119 if (
string.IsNullOrEmpty(s))
126 if (this.channelId > 0)
128 i = s.LastIndexOf(
'/');
132 s = s.Substring(0, i);
135 i = s.LastIndexOf(
'/');
139 s = s.Substring(0, i);
142 i = s.LastIndexOf(
'/');
146 return s.Substring(0, i);
175 if (!this.dataMode.HasValue)
187 switch (this.dataMode.Value)
197 Data = Convert.FromBase64String(Content.
DataString);
252 int TimeoutMilliseconds,
int StaleLimitSeconds)
256 StringBuilder ToSniffer = BrokerNode.HasSniffers ?
new StringBuilder() :
null;
259 this.timId, this.channelId,
SamplingMode, TimeoutMilliseconds * 1e-3, ToSniffer);
262 TimeoutMilliseconds, StaleLimitSeconds, this.ncapId, this.timId, this.channelId);
264 if (!Result.IsCompleted)
266 if (!(ToSniffer is
null))
267 await BrokerNode.
Information(ToSniffer.ToString());
287 int TimeoutMilliseconds,
int StaleLimitSeconds)
291 StringBuilder ToSniffer = BrokerNode.HasSniffers ?
new StringBuilder() :
null;
294 this.timId, this.channelId,
TedsAccessCode, 0, TimeoutMilliseconds * 1e-3, ToSniffer);
297 TimeoutMilliseconds, StaleLimitSeconds, this.ncapId, this.timId, this.channelId);
299 if (!Result.IsCompleted)
301 if (!(ToSniffer is
null))
302 await BrokerNode.
Information(ToSniffer.ToString());
312 DateTime TP = DateTime.UtcNow;
314 this.GetTimeouts(out
int TimeoutMilliseconds, out
int _, out
int RefreshTedsHours, out _);
318 if (this.teds.TryGetValue(Code, out KeyValuePair<DateTime, Teds> P) && TP.Subtract(P.Key).TotalHours < RefreshTedsHours)
319 return (P.Value, P.Key);
333 this.teds[Code] =
new KeyValuePair<DateTime, Teds>(TP,
Teds);
342 return (
null, DateTime.MinValue);
345 private void GetTimeouts(out
int TimeoutMilliseconds, out
int StaleSeconds, out
int RefreshTedsHours, out
Unit PreferredUnit)
347 if (this.
Topic.
Node is MqttNcapTopicNode NcapNode)
349 TimeoutMilliseconds = NcapNode.TimeoutMilliseconds;
350 StaleSeconds = NcapNode.StaleSeconds;
351 RefreshTedsHours = NcapNode.RefreshTedsHours;
353 if (!(NcapNode is MqttChannelTopicNode ChannelNode) ||
354 string.IsNullOrEmpty(ChannelNode.PreferredUnit) ||
355 !
Unit.
TryParse(ChannelNode.PreferredUnit, out PreferredUnit))
357 PreferredUnit =
null;
362 TimeoutMilliseconds = 10000;
364 RefreshTedsHours = 24;
365 PreferredUnit =
null;
380 this.GetTimeouts(out
int TimeoutMilliseconds, out
int StaleSeconds, out
int RefreshTedsHours, out
Unit PreferredUnit);
382 if (this.channelId > 0)
393 Field[] Fields3 = RemoveDuplicates(Fields2, Fields);
395 if (!(Fields3 is
null))
424 private static Field[] RemoveDuplicates(
Field[] Fields,
Field[] AlreadyReported)
426 Dictionary<string, Field> ByName =
new Dictionary<string, Field>();
428 foreach (
Field F
in AlreadyReported)
431 List<Field> Result =
null;
433 foreach (
Field F
in Fields)
435 if (ByName.TryGetValue(F.Name, out
Field F2) && F.ObjectValue.Equals(F2.ObjectValue))
439 Result =
new List<Field>();
444 return Result?.ToArray();
Information about content received from the MQTT server.
ICommunicationLayer CommunicationLayer
Communication layer on which the message was received.
string DataString
String representation of UTF-8 encoded binary data.
Contains information about a language.
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 ...
static bool TryParse(string UnitString, out Unit Unit)
Tries to parse a string into a unit.
Contains methods for simple hash calculations.
static byte[] StringToBinary(string s)
Parses a hex string.
IEEE 1451.0 Binary object
IEEE 1451.0 TEDS Access Message
static byte[] SerializeRequest(byte[] NcapId, byte[] TimId, ushort ChannelId, TedsAccessCode TedsAccessCode, uint TedsOffset, double TimeoutSeconds, StringBuilder SnifferOutput)
Serializes a request for TEDS.
Task<(ushort ErrorCode, Teds Teds)> TryParseTeds()
Tries to parse a TEDS from the message.
Field[] GetFields(ThingReference Thing, DateTime Timestamp)
Gets the information in the record, as an array of fields.
IEEE 1451.0 Transducer Access Message
bool TryParseTransducerData(ThingReference Thing, Teds ChannelTeds, Unit PreferredUnit, out ushort ErrorCode, out TransducerData Data)
Tries to parse Transducer Data from the message.
static byte[] SerializeRequest(byte[] NcapId, byte[] TimId, ushort ChannelId, SamplingMode SamplingMode, double TimeoutSeconds, StringBuilder SnifferOutput)
Serializes a request for transducer data.
Field[] Fields
Transducer Data
Encapsulates messages from an IEEE1451.1.6 device.
async Task< TransducerAccessMessage > RequestTransducerData(SamplingMode SamplingMode, int TimeoutMilliseconds, int StaleLimitSeconds)
Requests transducer data from an NCAP.
override Task< string > GetTypeName(Language Language)
Type name representing data.
override Grade DefaultSupport
Default support.
async Task< TedsAccessMessage > RequestTEDS(TedsAccessCode TedsAccessCode, int TimeoutMilliseconds, int StaleLimitSeconds)
Requests transducer data from an NCAP.
byte[] TimId
TIM ID (can be null, if addressing the NCAP)
override async Task< DataProcessingResult > DataReported(MqttTopic Topic, MqttContent Content)
Called when new data has been published.
MessageData(MqttTopic Topic, Message Message, byte[] NcapId, byte[] TimId, ushort ChannelId)
Encapsulates messages from an IEEE1451.1.6 device.
bool DataReported(Message Message)
Called when new data has been received.
int ChannelId
Channel ID (can be 0, if addressing the TIM, or NCAP)
MessageData()
Encapsulates messages from an IEEE1451.1.6 device.
override async Task StartReadout(ThingReference ThingReference, ISensorReadout Request, string Prefix, bool Last)
Starts a readout of the data.
override IMqttData CreateNew(MqttTopic Topic, MqttContent Content)
Creates a new instance of the data.
MessageData(MqttTopic Topic, byte[] NcapId, byte[] TimId, ushort ChannelId)
Encapsulates messages from an IEEE1451.1.6 device.
override void SnifferOutput(ICommunicationLayer Output)
Outputs the parsed data to the sniffer.
Helps connect IEEE 1451.1.6 requests and responses across MQTT topics.
static bool DataReported(Message Message, byte[] NcapId, byte[] TimId, ushort ChannelId)
Called when new data has been received.
static bool IsZero(byte[] A)
Checks if an ID is "zero", i.e. contains only zero bytes.
Abstract base class for IEEE 1451.1.6 NCAPs.
static async Task< DataProcessingResult > MessageReceived(MqttData This, MqttTopic Topic, Message Message)
Processes an IEEE 1451.0 message.
IEEE 1451.1.6 root topic node
Static class for IEEE 1451-related parsing tasks.
static Task< Message > TryParseMessage(byte[] Data)
Tries to parse an IEEE 1451.0-encoded data.
Represents an MQTT topic with base64-encoded binary data.
static readonly Regex RegEx
Parsed regular expression for BASE64-encoded data.
Represents an MQTT topic with binary data encoded as decimal strings.
static readonly Regex RegEx
Parsed regular expression for hexadecimal string data.
Abstract base class for MQTT data encapsulations.
void Information(ICommunicationLayer Output, string Info)
Outputs information to sniffer.
MqttTopic Topic
Topic used
MQTT Broker connection object.
Task Publish(string Topic, MqttQualityOfService QoS, bool Retain, byte[] Data)
Publishes binary data to a topic.
MqttBrokerNode Node
Reference to broker node.
MqttBroker Broker
MQTT Broker
string FullTopic
Full topic name
IMqttTopicNode Node
Reference to the MQTT Topic Node
Node representing a connection to an MQTT broker.
async Task Information(string Comment)
Called to inform the viewer of something.
Base class for all sensor data fields.
Contains information about an error on a thing
Contains a reference to a thing
override string ToString()
Interface for observable classes implementing communication protocols.
Interface for classes managing sensor data readouts.
bool IsIncluded(string FieldName)
Checks if a field with the given parameters is included in the readout.
Task ReportErrors(bool Done, params ThingError[] Errors)
Report error states to the client.
Task ReportFields(bool Done, params Field[] Fields)
Report read fields to the client.
Interface for MQTT Data encapsulations
MqttQualityOfService
MQTT Quality of Service level.
Prefix
SI prefixes. http://physics.nist.gov/cuu/Units/prefixes.html
TedsAccessCode
TEDS Access Code
SamplingMode
Sampling Mode
DataMode
Data mode of topic
DataProcessingResult
Results from processing an incoming message.
FieldType
Field Type flags