Skip to content

Commit

Permalink
updated TelemetryEvent class
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Lanning authored and Mark Lanning committed Dec 11, 2024
1 parent eb208cb commit f02865d
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 85 deletions.
86 changes: 86 additions & 0 deletions Base/src/ThingsLibrary.Base/DataType/Events/Telemetry.cs
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
}
}
59 changes: 0 additions & 59 deletions Base/src/ThingsLibrary.Base/DataType/Telemetry.cs

This file was deleted.

40 changes: 40 additions & 0 deletions Base/tests/ThingsLibrary.Base.Tests/DataType/Events/Telemetry.cs
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"]);
}

}
}
26 changes: 0 additions & 26 deletions Base/tests/ThingsLibrary.Base.Tests/DataType/Telemetry.cs

This file was deleted.

0 comments on commit f02865d

Please sign in to comment.