Skip to content

Commit

Permalink
updating telemetry data type
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Lanning authored and Mark Lanning committed Aug 24, 2024
1 parent 8391c57 commit c714b04
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
52 changes: 52 additions & 0 deletions Base/src/ThingsLibrary.Base/DataType/Telemetry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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<string, string> Attributes { get; set; } = [];

public TelemetryEntry()
{
//nothing
}

public TelemetryEntry(string type, DateTimeOffset timestamp)
{
this.Type = type;
this.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($"${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
}
}
2 changes: 1 addition & 1 deletion Base/src/ThingsLibrary.Base/ThingsLibrary.Base.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ThingsLibrary.Schema.Library" Version="0.2.6" />
<PackageReference Include="ThingsLibrary.Schema.Library" Version="0.2.7" />
</ItemGroup>
</Project>
26 changes: 26 additions & 0 deletions Base/tests/ThingsLibrary.Base.Tests/DataType/Telemetry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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('*'));
}

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.2" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="ThingsLibrary.Schema.Library" Version="0.2.6" />
<PackageReference Include="ThingsLibrary.Schema.Library" Version="0.2.7" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit c714b04

Please sign in to comment.