|
| 1 | +// ================================================================================ |
| 2 | +// <copyright file="Telemetry.cs" company="Starlight Software Co"> |
| 3 | +// Copyright (c) Starlight Software Co. All rights reserved. |
| 4 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 5 | +// </copyright> |
| 6 | +// ================================================================================ |
| 7 | + |
| 8 | +using ThingsLibrary.Schema.Library.Extensions; |
| 9 | + |
| 10 | +namespace ThingsLibrary.DataType.Events |
| 11 | +{ |
| 12 | + public class TelemetryEvent |
| 13 | + { |
| 14 | + [JsonPropertyName("date")] |
| 15 | + public DateTimeOffset Timestamp { get; set; } = DateTime.UtcNow; |
| 16 | + |
| 17 | + [JsonPropertyName("type")] |
| 18 | + public string Type { get; set; } = string.Empty; |
| 19 | + |
| 20 | + [JsonPropertyName("tags")] |
| 21 | + public Dictionary<string, string> Attributes { get; set; } = []; |
| 22 | + |
| 23 | + public TelemetryEvent() |
| 24 | + { |
| 25 | + //nothing |
| 26 | + } |
| 27 | + |
| 28 | + public TelemetryEvent(string type, DateTimeOffset timestamp) |
| 29 | + { |
| 30 | + Type = type; |
| 31 | + Timestamp = timestamp; |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Generate the telemetry sentence |
| 36 | + /// </summary> |
| 37 | + /// <returns>Telemetry Sentence.. IE: $1724387849602|PA|r:1|s:143|p:PPE Mask|q:1|p:000*79</returns> |
| 38 | + public override string ToString() |
| 39 | + { |
| 40 | + var sentence = new StringBuilder($"${Timestamp.ToUnixTimeMilliseconds()}|{Type}"); |
| 41 | + sentence.Append(string.Join(string.Empty, Attributes.Select(x => $"|{x.Key}:{x.Value}"))); |
| 42 | + |
| 43 | + sentence.AppendChecksum(); |
| 44 | + |
| 45 | + return sentence.ToString(); |
| 46 | + } |
| 47 | + |
| 48 | + #region --- Static --- |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// |
| 52 | + /// </summary> |
| 53 | + /// <param name="telemetrySentence">Telemetry Sentence.. IE: $1724387849602|PA|r:1|s:143|p:PPE Mask|q:1|p:000*79</param> |
| 54 | + /// <returns></returns> |
| 55 | + /// <exception cref="ArgumentException">Invalid sentence</exception> |
| 56 | + public static TelemetryEvent Parse(string telemetrySentence) |
| 57 | + { |
| 58 | + ArgumentNullException.ThrowIfNullOrWhiteSpace(telemetrySentence); |
| 59 | + |
| 60 | + if (!telemetrySentence.ValidateChecksum()) |
| 61 | + { |
| 62 | + throw new ArgumentException("Invalid checksum"); |
| 63 | + } |
| 64 | + |
| 65 | + int num = telemetrySentence.LastIndexOf('*'); //it has to be in the sentence since the checksum validated |
| 66 | + |
| 67 | + string[] array = telemetrySentence.Substring(1, num - 1).Split('|'); |
| 68 | + |
| 69 | + var date = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(array[0])); |
| 70 | + |
| 71 | + var telemetryEvent = new TelemetryEvent(array[1], date); |
| 72 | + |
| 73 | + for (int i = 2; i < array.Length; i++) |
| 74 | + { |
| 75 | + num = array[i].IndexOf(':'); |
| 76 | + if (num < 0) { continue; } |
| 77 | + |
| 78 | + telemetryEvent.Attributes[array[i][..num]] = array[i][(num + 1)..]; |
| 79 | + } |
| 80 | + |
| 81 | + return telemetryEvent; |
| 82 | + } |
| 83 | + |
| 84 | + #endregion |
| 85 | + } |
| 86 | +} |
0 commit comments