Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion RS485 Monitor/src/Telegrams/BaseTelegram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public enum TelegramType
/// </summary>
public TelegramType Type { get => (TelegramType)RawType; }

/// <summary>
/// Timestamp when the telegram was received / created
/// </summary>
public DateTime TimeStamp { get; }

#endregion

/// <summary>
Expand Down Expand Up @@ -165,11 +170,15 @@ protected BaseTelegram(BaseTelegram c)
/// Create a new base telegram based on the given raw data
/// </summary>
/// <param name="rawData">raw data of one telegram</param>
/// <param name="timestamp">
/// Optional timestamp when the telegram was received. If it stays null,
/// the current timestamp is used.
/// </param>
/// <exception cref="ArgumentNullException">Raw data is null.</exception>
/// <exception cref="ArgumentException">Raw data is too short.</exception>
/// <exception cref="ArgumentException">Invalid data length in the raw data.</exception>
/// <exception cref="ArgumentException">Raw data does not contain End tag.</exception>
public BaseTelegram(byte[] rawData)
public BaseTelegram(byte[] rawData, DateTime? timestamp = null)
{
// Basic validation
ArgumentNullException.ThrowIfNull(rawData);
Expand Down Expand Up @@ -206,6 +215,9 @@ public BaseTelegram(byte[] rawData)
log.Error("RawData does not hold Endtag");
throw new ArgumentException("Raw data does not contain End tag");
}

// Set timestamp
TimeStamp = timestamp ?? DateTime.Now;
}

/// <summary>
Expand Down
22 changes: 22 additions & 0 deletions tests/BaseTelegramTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,26 @@ public void NotEqualsTest()

Assert.That(telegram1.Equals(telegram2), Is.EqualTo(false));
}

[Test]
public void AutomaticTimeStampCreated()
{
byte[] raw = [0xB6, 0x6B, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D];

BaseTelegram telegram = new(raw);
var now = DateTime.Now;

Assert.That(telegram.TimeStamp.Ticks, Is.EqualTo(now.Ticks).Within(300));
}

[Test]
public void ManualTimeStampCreated()
{
byte[] raw = [0xB6, 0x6B, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D];
DateTime timestamp = new(2021, 12, 24, 12, 0, 0);

BaseTelegram telegram = new(raw, timestamp);

Assert.That(telegram.TimeStamp, Is.EqualTo(timestamp));
}
}