Skip to content

Commit 8cb592a

Browse files
committed
16 - Added further tests for the telegrams
1 parent 35ed38b commit 8cb592a

11 files changed

+521
-108
lines changed

RS485 Monitor/src/Telegrams/BaseTelegram.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public BaseTelegram(byte[] rawData)
201201

202202
// Check end telegram value
203203
int pos_end_tag = MIN_DATA_LEN - 1 + pduLen;
204-
if (rawData.Length < pos_end_tag || rawData[pos_end_tag] != END_TELEGRAM)
204+
if (rawData.Length < (pos_end_tag + 1) || rawData[pos_end_tag] != END_TELEGRAM)
205205
{
206206
log.Error("RawData does not hold Endtag");
207207
throw new ArgumentException("Raw data does not contain End tag");
@@ -250,7 +250,11 @@ public virtual string ToStringDetailed()
250250
/// <returns></returns>
251251
public bool Equals(BaseTelegram? other)
252252
{
253-
return Array.Equals(this.Raw, other?.Raw);
253+
if (other == null)
254+
{
255+
return false;
256+
}
257+
return this.Raw.SequenceEqual(other.Raw);
254258
}
255259

256260
}

RS485 Monitor/src/Telegrams/ControllerResponse.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@ public enum ParkStatus
6666
public ParkStatus Parking {
6767
get {
6868
ParkStatus status = ParkStatus.PARKING_UNKNOWN;
69-
try {
69+
if (Enum.IsDefined(typeof(ParkStatus), (int)PDU[POS_PARKING]))
70+
{
7071
status = (ParkStatus)PDU[POS_PARKING];
71-
} catch (Exception e) {
72-
log.Error(e, "Error parsing parking status");
7372
}
7473
return status;
7574
}
@@ -107,6 +106,7 @@ public ControllerResponse(BaseTelegram t)
107106
public override string ToString()
108107
{
109108
log.Trace(base.ToString());
110-
return $"Controller Response: Mode {Gear}, {Current}A, {Speed}km/h, {Temperature}°C, Parking: {Parking}";
109+
string current_invariant = string.Create(System.Globalization.CultureInfo.InvariantCulture, $"{Current}");
110+
return $"Controller Response: Mode {Gear}, {current_invariant}A, {Speed}km/h, {Temperature}°C, Parking: {Parking}";
111111
}
112112
}

tests/BaseTelegramTest.cs

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,112 @@ public class BaseTelegramTest
66
[Test]
77
public void ParseTelegramSuccessful()
88
{
9-
byte[] raw = new byte[] { 0xB6, 0x6B, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D };
9+
byte[] raw = [0xB6, 0x6B, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D];
1010

1111
BaseTelegram telegram = new(raw);
1212

1313
Assert.That(telegram.Type, Is.EqualTo(BaseTelegram.TelegramType.RESPONSE));
14-
Assert.That(telegram.Destination, Is.EqualTo(0xDA));
15-
Assert.That(telegram.Source, Is.EqualTo(0xAA));
14+
Assert.That(telegram.Destination, Is.EqualTo(0xAA));
15+
Assert.That(telegram.Source, Is.EqualTo(0xDA));
1616
Assert.That(telegram.Valid, Is.EqualTo(true));
1717
}
1818

1919
[Test]
2020
public void ParseTelegramRequestSuccessful()
2121
{
22-
byte[] raw = new byte[] { 0xC5, 0x5C, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D };
22+
byte[] raw = [0xC5, 0x5C, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D];
2323

2424
BaseTelegram telegram = new(raw);
2525

2626
Assert.That(telegram.Type, Is.EqualTo(BaseTelegram.TelegramType.REQUEST));
27-
Assert.That(telegram.Destination, Is.EqualTo(0xDA));
28-
Assert.That(telegram.Source, Is.EqualTo(0xAA));
27+
Assert.That(telegram.Destination, Is.EqualTo(0xAA));
28+
Assert.That(telegram.Source, Is.EqualTo(0xDA));
2929
Assert.That(telegram.Valid, Is.EqualTo(true));
3030
}
3131

3232
[Test]
3333
public void ParseTelegramWrongChecksum()
3434
{
35-
byte[] raw = new byte[] { 0xB6, 0x6B, 0xAA, 0xDA, 0x0A, 0x02, 0x10, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D };
35+
byte[] raw = [0xB6, 0x6B, 0xAA, 0xDA, 0x0A, 0x02, 0x10, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D];
3636

3737
BaseTelegram telegram = new(raw);
3838

3939
Assert.That(telegram.Type, Is.EqualTo(BaseTelegram.TelegramType.RESPONSE));
40-
Assert.That(telegram.Destination, Is.EqualTo(0xDA));
41-
Assert.That(telegram.Source, Is.EqualTo(0xAA));
40+
Assert.That(telegram.Destination, Is.EqualTo(0xAA));
41+
Assert.That(telegram.Source, Is.EqualTo(0xDA));
4242
Assert.That(telegram.Valid, Is.EqualTo(false));
4343
}
4444

45+
[Test]
46+
public void ParseTelegramTooShort()
47+
{
48+
byte[] raw = [0xB6, 0x6B, 0xAA];
49+
50+
var ex = Assert.Throws<ArgumentException>(() => new BaseTelegram(raw));
51+
Assert.That(ex.Message, Is.EqualTo("Raw data is too short"));
52+
}
53+
54+
[Test]
55+
public void ParseTelegramInvalidDataLength()
56+
{
57+
byte[] raw = [0xB6, 0x6B, 0xAA, 0xDA, 0x21, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D];
58+
59+
var ex = Assert.Throws<ArgumentException>(() => new BaseTelegram(raw));
60+
Assert.That(ex.Message, Is.EqualTo("Invalid data len 33. Max supported: 32"));
61+
}
62+
63+
[Test]
64+
public void ParseTelegramMissingEndTag()
65+
{
66+
byte[] raw = [0xB6, 0x6B, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C];
67+
68+
var ex = Assert.Throws<ArgumentException>(() => new BaseTelegram(raw));
69+
Assert.That(ex.Message, Is.EqualTo("Raw data does not contain End tag"));
70+
}
71+
72+
[Test]
73+
public void ToStringTest()
74+
{
75+
byte[] raw = [0xB6, 0x6B, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D];
76+
77+
BaseTelegram telegram = new(raw);
78+
79+
string expected = "B6 6B AA DA 0A 02 00 04 00 00 13 00 00 02 01 1C 0D ";
80+
Assert.That(telegram.ToString(), Is.EqualTo(expected));
81+
}
82+
83+
[Test]
84+
public void ToStringDetailedTest()
85+
{
86+
byte[] raw = [0xB6, 0x6B, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D];
87+
88+
BaseTelegram telegram = new(raw);
89+
90+
string expected = "B6 6B AA DA 0A 02 00 04 00 00 13 00 00 02 01 1C 0D ";
91+
Assert.That(telegram.ToStringDetailed(), Is.EqualTo(expected));
92+
}
93+
94+
[Test]
95+
public void EqualsTest()
96+
{
97+
byte[] raw1 = [0xB6, 0x6B, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D];
98+
byte[] raw2 = [0xB6, 0x6B, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D];
99+
100+
BaseTelegram telegram1 = new(raw1);
101+
BaseTelegram telegram2 = new(raw2);
102+
103+
Assert.That(telegram1.Equals(telegram2), Is.EqualTo(true));
104+
}
105+
106+
[Test]
107+
public void NotEqualsTest()
108+
{
109+
byte[] raw1 = [0xB6, 0x6B, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D];
110+
byte[] raw2 = [0xC5, 0x5C, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D];
111+
112+
BaseTelegram telegram1 = new(raw1);
113+
BaseTelegram telegram2 = new(raw2);
114+
115+
Assert.That(telegram1.Equals(telegram2), Is.EqualTo(false));
116+
}
45117
}

tests/BatteryRequestTest.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace RS485_Monitor.tests;
2+
using NUnit.Framework;
3+
4+
public class BatteryRequestTest
5+
{
6+
[Test]
7+
public void BatteryRequestSuccessful()
8+
{
9+
byte[] raw = [0xC5, 0x5C, 0x5A, 0xAA, 0x01, 0x00, 0x01, 0x0D];
10+
11+
BaseTelegram baseTelegram = new(raw);
12+
BatteryRequest batteryRequest = new(baseTelegram);
13+
14+
using (Assert.EnterMultipleScope())
15+
{
16+
Assert.That(batteryRequest.Type, Is.EqualTo(BaseTelegram.TelegramType.REQUEST));
17+
Assert.That(batteryRequest.Destination, Is.EqualTo((byte)Units.BATTERY));
18+
Assert.That(batteryRequest.Source, Is.EqualTo((byte)Units.ECU));
19+
Assert.That(batteryRequest.Valid, Is.EqualTo(true));
20+
}
21+
}
22+
23+
[Test]
24+
public void BatteryRequestInvalidPDUSize()
25+
{
26+
byte[] raw = [0xC5, 0x5C, 0x5A, 0xAA, 0x02, 0x00, 0x01, 0x02, 0x0D];
27+
28+
BaseTelegram baseTelegram = new(raw);
29+
var ex = Assert.Throws<ArgumentException>(() => new BatteryRequest(baseTelegram));
30+
Assert.That(ex.Message, Is.EqualTo("Unexpected size of 2"));
31+
}
32+
33+
[Test]
34+
public void BatteryRequestInvalidSourceOrDestination()
35+
{
36+
byte[] raw = [0xC5, 0x5C, 0xBB, 0xCC, 0x01, 0x00, 0x01, 0x0D];
37+
38+
BaseTelegram baseTelegram = new(raw);
39+
var ex = Assert.Throws<ArgumentException>(() => new BatteryRequest(baseTelegram));
40+
Assert.That(ex.Message, Is.EqualTo("Not a BatteryResponse telegram"));
41+
}
42+
43+
[Test]
44+
public void BatteryRequestToString()
45+
{
46+
byte[] raw = [0xC5, 0x5C, 0x5A, 0xAA, 0x01, 0x00, 0x01, 0x0D];
47+
48+
BaseTelegram baseTelegram = new(raw);
49+
BatteryRequest batteryRequest = new(baseTelegram);
50+
51+
string expected = "Battery Request";
52+
Assert.That(batteryRequest.ToString(), Is.EqualTo(expected));
53+
}
54+
}

tests/BatteryResponseTest.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
namespace RS485_Monitor.tests;
2+
using NUnit.Framework;
3+
4+
public class BatteryResponseTest
5+
{
6+
private BaseTelegram? batteryBase = null;
7+
8+
[SetUp]
9+
public void Setup()
10+
{
11+
byte[] raw = [0xB6, 0x6B, 0xAA, 0x5A, 0x0A, 0x4D, 0x48, 0x17, 0x00, 0x00, 0x23, 0x00, 0x0B, 0x00, 0x00, 0x30, 0x0D];
12+
batteryBase = new BaseTelegram(raw);
13+
}
14+
15+
[Test]
16+
public void ConvertToBatteryReponseSuccess()
17+
{
18+
BatteryResponse status = new(batteryBase!);
19+
Assert.That(status, Is.Not.Null);
20+
}
21+
22+
[Test]
23+
public void ConvertToBatteryResponseFail()
24+
{
25+
byte[] raw = [0xB6, 0x6B, 0xAA, 0x5A, 0x09, 0x4D, 0x48, 0x17, 0x00, 0x00, 0x23, 0x00, 0x0B, 0x00, 0x30, 0x0D];
26+
BaseTelegram invalidTelegram = new(raw);
27+
28+
Assert.Throws<ArgumentException>(() => new BatteryResponse(invalidTelegram));
29+
}
30+
31+
[Test]
32+
public void CheckContent()
33+
{
34+
BatteryResponse status = new(batteryBase!);
35+
using (Assert.EnterMultipleScope())
36+
{
37+
Assert.That(status.Voltage, Is.EqualTo(77));
38+
Assert.That(status.SoC, Is.EqualTo(72));
39+
Assert.That(status.Temperature, Is.EqualTo(23));
40+
Assert.That(status.Current, Is.EqualTo(0x0));
41+
Assert.That(status.Cycles, Is.EqualTo(35));
42+
Assert.That(status.DischargeCycles, Is.EqualTo(11));
43+
Assert.That(status.VBreaker, Is.EqualTo(BatteryResponse.VBreakerStatus.OK));
44+
Assert.That(status.Activity, Is.EqualTo(BatteryResponse.BatteryActivity.NO_ACTIVITY));
45+
Assert.That(status.Charging, Is.False);
46+
}
47+
}
48+
49+
[Test]
50+
public void ToStringTest()
51+
{
52+
BatteryResponse status = new(batteryBase!);
53+
string expected = "Battery Response: 77V, 72%, 23°C, 0 Amp, Charged: 35x, Discharged: 11x, VBreaker: OK, Activity: NO_ACTIVITY, Charging: False";
54+
Assert.That(status.ToString(), Is.EqualTo(expected));
55+
}
56+
57+
[Test]
58+
public void BatteryResponseInvalidSourceOrDestination()
59+
{
60+
byte[] raw = [0xB6, 0x6B, 0xBB, 0xCC, 0x0A, 0x4D, 0x48, 0x17, 0x00, 0x00, 0x23, 0x00, 0x0B, 0x00, 0x00, 0x30, 0x0D];
61+
BaseTelegram invalidTelegram = new(raw);
62+
63+
var ex = Assert.Throws<ArgumentException>(() => new BatteryResponse(invalidTelegram));
64+
Assert.That(ex.Message, Is.EqualTo("Not a BatteryResponse telegram"));
65+
}
66+
67+
[Test]
68+
public void BatteryResponseInvalidPDUSize()
69+
{
70+
byte[] raw = [0xB6, 0x6B, 0xAA, 0x5A, 0x0B, 0x4D, 0x48, 0x17, 0x00, 0x00, 0x23, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x30, 0x0D];
71+
BaseTelegram invalidTelegram = new(raw);
72+
73+
var ex = Assert.Throws<ArgumentException>(() => new BatteryResponse(invalidTelegram));
74+
Assert.That(ex.Message, Is.EqualTo("Unexpected size of 11"));
75+
}
76+
}

tests/BatteryStatusTest.cs

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)