Skip to content

Commit 8e3533e

Browse files
authored
Merge Introduction of TimeStamp in BaseTelegram
2 parents f14b4db + 242a805 commit 8e3533e

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

RS485 Monitor/src/Telegrams/BaseTelegram.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ public enum TelegramType
137137
/// </summary>
138138
public TelegramType Type { get => (TelegramType)RawType; }
139139

140+
/// <summary>
141+
/// Timestamp when the telegram was received / created
142+
/// </summary>
143+
public DateTime TimeStamp { get; }
144+
140145
#endregion
141146

142147
/// <summary>
@@ -165,11 +170,15 @@ protected BaseTelegram(BaseTelegram c)
165170
/// Create a new base telegram based on the given raw data
166171
/// </summary>
167172
/// <param name="rawData">raw data of one telegram</param>
173+
/// <param name="timestamp">
174+
/// Optional timestamp when the telegram was received. If it stays null,
175+
/// the current timestamp is used.
176+
/// </param>
168177
/// <exception cref="ArgumentNullException">Raw data is null.</exception>
169178
/// <exception cref="ArgumentException">Raw data is too short.</exception>
170179
/// <exception cref="ArgumentException">Invalid data length in the raw data.</exception>
171180
/// <exception cref="ArgumentException">Raw data does not contain End tag.</exception>
172-
public BaseTelegram(byte[] rawData)
181+
public BaseTelegram(byte[] rawData, DateTime? timestamp = null)
173182
{
174183
// Basic validation
175184
ArgumentNullException.ThrowIfNull(rawData);
@@ -206,6 +215,9 @@ public BaseTelegram(byte[] rawData)
206215
log.Error("RawData does not hold Endtag");
207216
throw new ArgumentException("Raw data does not contain End tag");
208217
}
218+
219+
// Set timestamp
220+
TimeStamp = timestamp ?? DateTime.Now;
209221
}
210222

211223
/// <summary>

tests/BaseTelegramTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,26 @@ public void NotEqualsTest()
114114

115115
Assert.That(telegram1.Equals(telegram2), Is.EqualTo(false));
116116
}
117+
118+
[Test]
119+
public void AutomaticTimeStampCreated()
120+
{
121+
byte[] raw = [0xB6, 0x6B, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D];
122+
123+
BaseTelegram telegram = new(raw);
124+
var now = DateTime.Now;
125+
126+
Assert.That(telegram.TimeStamp.Ticks, Is.EqualTo(now.Ticks).Within(300));
127+
}
128+
129+
[Test]
130+
public void ManualTimeStampCreated()
131+
{
132+
byte[] raw = [0xB6, 0x6B, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D];
133+
DateTime timestamp = new(2021, 12, 24, 12, 0, 0);
134+
135+
BaseTelegram telegram = new(raw, timestamp);
136+
137+
Assert.That(telegram.TimeStamp, Is.EqualTo(timestamp));
138+
}
117139
}

0 commit comments

Comments
 (0)