-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mark Lanning
authored and
Mark Lanning
committed
Dec 11, 2024
1 parent
eb208cb
commit f02865d
Showing
4 changed files
with
126 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// ================================================================================ | ||
// <copyright file="Telemetry.cs" company="Starlight Software Co"> | ||
// 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. | ||
// </copyright> | ||
// ================================================================================ | ||
|
||
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<string, string> Attributes { get; set; } = []; | ||
|
||
public TelemetryEvent() | ||
{ | ||
//nothing | ||
} | ||
|
||
public TelemetryEvent(string type, DateTimeOffset timestamp) | ||
{ | ||
Type = type; | ||
Timestamp = timestamp; | ||
} | ||
|
||
/// <summary> | ||
/// Generate the telemetry sentence | ||
/// </summary> | ||
/// <returns>Telemetry Sentence.. IE: $1724387849602|PA|r:1|s:143|p:PPE Mask|q:1|p:000*79</returns> | ||
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 --- | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="telemetrySentence">Telemetry Sentence.. IE: $1724387849602|PA|r:1|s:143|p:PPE Mask|q:1|p:000*79</param> | ||
/// <returns></returns> | ||
/// <exception cref="ArgumentException">Invalid sentence</exception> | ||
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 | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
Base/tests/ThingsLibrary.Base.Tests/DataType/Events/Telemetry.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// ================================================================================ | ||
// <copyright file="Telemetry.cs" company="Starlight Software Co"> | ||
// 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. | ||
// </copyright> | ||
// ================================================================================ | ||
|
||
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"]); | ||
} | ||
|
||
} | ||
} |
This file was deleted.
Oops, something went wrong.