diff --git a/Base/src/ThingsLibrary.Base/DataType/Events/Telemetry.cs b/Base/src/ThingsLibrary.Base/DataType/Events/Telemetry.cs new file mode 100644 index 0000000..a29eab8 --- /dev/null +++ b/Base/src/ThingsLibrary.Base/DataType/Events/Telemetry.cs @@ -0,0 +1,86 @@ +// ================================================================================ +// +// Copyright (c) Starlight Software Co. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. +// +// ================================================================================ + +using ThingsLibrary.Schema.Library.Extensions; + +namespace ThingsLibrary.DataType.Events +{ + public class TelemetryEvent + { + [JsonPropertyName("date")] + public DateTimeOffset Timestamp { get; set; } = DateTime.UtcNow; + + [JsonPropertyName("type")] + public string Type { get; set; } = string.Empty; + + [JsonPropertyName("tags")] + public Dictionary Attributes { get; set; } = []; + + public TelemetryEvent() + { + //nothing + } + + public TelemetryEvent(string type, DateTimeOffset timestamp) + { + Type = type; + Timestamp = timestamp; + } + + /// + /// Generate the telemetry sentence + /// + /// Telemetry Sentence.. IE: $1724387849602|PA|r:1|s:143|p:PPE Mask|q:1|p:000*79 + public override string ToString() + { + var sentence = new StringBuilder($"${Timestamp.ToUnixTimeMilliseconds()}|{Type}"); + sentence.Append(string.Join(string.Empty, Attributes.Select(x => $"|{x.Key}:{x.Value}"))); + + sentence.AppendChecksum(); + + return sentence.ToString(); + } + + #region --- Static --- + + /// + /// + /// + /// Telemetry Sentence.. IE: $1724387849602|PA|r:1|s:143|p:PPE Mask|q:1|p:000*79 + /// + /// Invalid sentence + public static TelemetryEvent Parse(string telemetrySentence) + { + ArgumentNullException.ThrowIfNullOrWhiteSpace(telemetrySentence); + + if (!telemetrySentence.ValidateChecksum()) + { + throw new ArgumentException("Invalid checksum"); + } + + int num = telemetrySentence.LastIndexOf('*'); //it has to be in the sentence since the checksum validated + + string[] array = telemetrySentence.Substring(1, num - 1).Split('|'); + + var date = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(array[0])); + + var telemetryEvent = new TelemetryEvent(array[1], date); + + for (int i = 2; i < array.Length; i++) + { + num = array[i].IndexOf(':'); + if (num < 0) { continue; } + + telemetryEvent.Attributes[array[i][..num]] = array[i][(num + 1)..]; + } + + return telemetryEvent; + } + + #endregion + } +} diff --git a/Base/src/ThingsLibrary.Base/DataType/Telemetry.cs b/Base/src/ThingsLibrary.Base/DataType/Telemetry.cs deleted file mode 100644 index d68087b..0000000 --- a/Base/src/ThingsLibrary.Base/DataType/Telemetry.cs +++ /dev/null @@ -1,59 +0,0 @@ -// ================================================================================ -// -// Copyright (c) Starlight Software Co. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. -// -// ================================================================================ - -using ThingsLibrary.Schema.Library.Extensions; - -namespace ThingsLibrary.DataType -{ - public class TelemetryEntry - { - [JsonPropertyName("date")] - public DateTimeOffset Timestamp { get; set; } = DateTime.UtcNow; - - [JsonPropertyName("type")] - public string Type { get; set; } = string.Empty; - - [JsonPropertyName("tags")] - public Dictionary Attributes { get; set; } = []; - - public TelemetryEntry() - { - //nothing - } - - public TelemetryEntry(string type, DateTimeOffset timestamp) - { - this.Type = type; - this.Timestamp = timestamp; - } - - - - /// - /// Generate the telemetry sentence - /// - /// Telemetry Sentence.. IE: $1724387849602|PA|r:1|s:143|p:PPE Mask|q:1|p:000*79 - public override string ToString() - { - var sentence = new StringBuilder($"${this.Timestamp.ToUnixTimeMilliseconds()}|{this.Type}"); - sentence.Append(string.Join(string.Empty, this.Attributes.Select(x => $"|{x.Key}:{x.Value}"))); - - sentence.AppendChecksum(); - - return sentence.ToString(); - } - - #region --- Static --- - - public static TelemetryEntry Parse(string telemetrySentence) - { - throw new NotImplementedException(); - } - - #endregion - } -} diff --git a/Base/tests/ThingsLibrary.Base.Tests/DataType/Events/Telemetry.cs b/Base/tests/ThingsLibrary.Base.Tests/DataType/Events/Telemetry.cs new file mode 100644 index 0000000..cc95c49 --- /dev/null +++ b/Base/tests/ThingsLibrary.Base.Tests/DataType/Events/Telemetry.cs @@ -0,0 +1,40 @@ +// ================================================================================ +// +// Copyright (c) Starlight Software Co. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. +// +// ================================================================================ + +using ThingsLibrary.DataType.Events; + +namespace ThingsLibrary.Base.Tests.DataType.Events +{ + [TestClass, ExcludeFromCodeCoverage] + public class TelemetryTests + { + [TestMethod] + public void Basic() + { + var telem = new TelemetryEvent("sens", DateTime.UtcNow); + + telem.Attributes.Add("gn", "Mark"); + telem.Attributes.Add("cp", "Starlight"); + telem.Attributes.Add("r", "1"); + + var sentence = telem.ToString(); + + var expectedPrefix = $"${telem.Timestamp.ToUnixTimeMilliseconds()}|{telem.Type}|"; + + Assert.IsTrue(sentence.StartsWith(expectedPrefix)); + Assert.IsTrue(sentence.Contains('*')); + + var telem2 = TelemetryEvent.Parse(sentence); + + Assert.IsTrue(telem2.Attributes.Count == 3); + Assert.AreEqual(telem2.Attributes["gn"], telem.Attributes["gn"]); + Assert.AreEqual(telem2.Attributes["cp"], telem.Attributes["cp"]); + Assert.AreEqual(telem2.Attributes["r"], telem.Attributes["r"]); + } + + } +} diff --git a/Base/tests/ThingsLibrary.Base.Tests/DataType/Telemetry.cs b/Base/tests/ThingsLibrary.Base.Tests/DataType/Telemetry.cs deleted file mode 100644 index 6238f11..0000000 --- a/Base/tests/ThingsLibrary.Base.Tests/DataType/Telemetry.cs +++ /dev/null @@ -1,26 +0,0 @@ -using ThingsLibrary.DataType; - -namespace ThingsLibrary.Tests.DataType -{ - [TestClass, ExcludeFromCodeCoverage] - public class TelemetryTests - { - [TestMethod] - public void Basic() - { - var telem = new TelemetryEntry("sens", DateTime.UtcNow); - - telem.Attributes.Add("gn", "Mark"); - telem.Attributes.Add("cp", "Starlight"); - telem.Attributes.Add("r", "1"); - - var sentence = telem.ToString(); - - var expectedPrefix = $"${telem.Timestamp.ToUnixTimeMilliseconds()}|{telem.Type}|"; - - Assert.IsTrue(sentence.StartsWith(expectedPrefix)); - Assert.IsTrue(sentence.Contains('*')); - } - - } -}