Skip to content

Commit b15b747

Browse files
committed
Added UnitTestAISMessage for some of the more common unit test.
Changed implementation based on SonarCloud recommendation.
1 parent 07a0fb8 commit b15b747

14 files changed

+184
-110
lines changed

AIS/AIS.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
88
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
99
<PackageId>TensionDev.Maritime.AIS</PackageId>
10-
<Version>0.4.1</Version>
10+
<Version>0.4.2</Version>
1111
<Authors>TensionDev amsga</Authors>
1212
<Company>TensionDev</Company>
1313
<Product>TensionDev.Maritime.AIS</Product>
@@ -21,7 +21,7 @@
2121
<PackageReleaseNotes>Initial project release</PackageReleaseNotes>
2222
<NeutralLanguage>en-SG</NeutralLanguage>
2323
<AssemblyVersion>0.4.0.0</AssemblyVersion>
24-
<FileVersion>0.4.1.0</FileVersion>
24+
<FileVersion>0.4.2.0</FileVersion>
2525
<IncludeSymbols>true</IncludeSymbols>
2626
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2727
</PropertyGroup>

AIS/AISMessage.cs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace TensionDev.Maritime.AIS
66
{
77
public abstract class AISMessage
88
{
9-
private protected static UInt16 s_groupId = 0;
9+
protected UInt16 s_groupId = 0;
1010

1111
protected UInt16 messageId6;
1212
protected UInt16 repeatIndicator2;
@@ -55,7 +55,7 @@ public static AISMessage DecodeSentences(IList<String> sentences)
5555
throw new NotImplementedException("Sentence Identifier not recognised.");
5656
}
5757

58-
String[] vs = sentence.Split(new char[] { ',', '*' });
58+
String[] vs = sentence.Split(',', '*' );
5959

6060
// Ensure sentences count is equal to sentence fragment count.
6161
if (vs[1] != sentences.Count.ToString())
@@ -82,8 +82,31 @@ public static AISMessage DecodeSentences(IList<String> sentences)
8282
payloads.Add(vs[5]);
8383
}
8484

85-
AISMessage aisMessage;
85+
AISMessage aisMessage = CreateAISMessage(messageId);
86+
87+
if (sentenceIdentifier == "VDM")
88+
{
89+
aisMessage.SentenceFormatter = SentenceFormatterEnum.VDM;
90+
}
91+
else if (sentenceIdentifier == "VDO")
92+
{
93+
aisMessage.SentenceFormatter = SentenceFormatterEnum.VDO;
94+
}
95+
96+
aisMessage.DecodePayloads(payloads);
8697

98+
return aisMessage;
99+
}
100+
101+
/// <summary>
102+
/// Creates an AIS Message Object based on encoded message ID
103+
/// </summary>
104+
/// <param name="messageId">Encoded Message ID</param>
105+
/// <returns></returns>
106+
/// <exception cref="NotImplementedException"></exception>
107+
private static AISMessage CreateAISMessage(string messageId)
108+
{
109+
AISMessage aisMessage;
87110
switch (messageId)
88111
{
89112
case "1":
@@ -142,17 +165,6 @@ public static AISMessage DecodeSentences(IList<String> sentences)
142165
throw new NotImplementedException("Message Identifier not recognised.");
143166
}
144167

145-
if (sentenceIdentifier == "VDM")
146-
{
147-
aisMessage.SentenceFormatter = SentenceFormatterEnum.VDM;
148-
}
149-
else if (sentenceIdentifier == "VDO")
150-
{
151-
aisMessage.SentenceFormatter = SentenceFormatterEnum.VDO;
152-
}
153-
154-
aisMessage.DecodePayloads(payloads);
155-
156168
return aisMessage;
157169
}
158170

@@ -162,7 +174,7 @@ public static AISMessage DecodeSentences(IList<String> sentences)
162174
/// <param name="value">The Value to get the bitvector from</param>
163175
/// <param name="bitcount">The end index of the bits required</param>
164176
/// <returns>The bitvector containing the required number of bits</returns>
165-
public UInt64 GetBitVector(Int64 value, Int32 bitcount)
177+
public static UInt64 GetBitVector(Int64 value, Int32 bitcount)
166178
{
167179
UInt64 bv = 0;
168180
Int64 mask;
@@ -184,7 +196,7 @@ public UInt64 GetBitVector(Int64 value, Int32 bitcount)
184196
/// <param name="bitcount">The end index of the bits required</param>
185197
/// <param name="startindex">The start index of the bits required</param>
186198
/// <returns>The bitvector containing the required number of bits</returns>
187-
public UInt64 GetBitVector(Int64 value, Int32 bitcount, Int32 startindex)
199+
public static UInt64 GetBitVector(Int64 value, Int32 bitcount, Int32 startindex)
188200
{
189201
UInt64 bv = 0;
190202
Int64 mask;
@@ -204,15 +216,15 @@ public UInt64 GetBitVector(Int64 value, Int32 bitcount, Int32 startindex)
204216
/// <param name="value">The Value to get the bitvector from</param>
205217
/// <param name="bitcount">The end index of the bits required</param>
206218
/// <returns>The bitvector containing the required number of bits</returns>
207-
public UInt64 GetBitVector(UInt64 value, Int32 bitcount)
219+
public static UInt64 GetBitVector(UInt64 value, Int32 bitcount)
208220
{
209221
UInt64 bv = 0;
210222
UInt64 mask;
211223

212224
for (Int32 i = 0; i < bitcount; i++)
213225
{
214226
mask = (UInt64)Math.Pow(2, i);
215-
bv = (UInt64)(mask & value) | bv;
227+
bv = (mask & value) | bv;
216228
}
217229

218230
return bv;
@@ -226,15 +238,15 @@ public UInt64 GetBitVector(UInt64 value, Int32 bitcount)
226238
/// <param name="bitcount">The end index of the bits required</param>
227239
/// <param name="startindex">The start index of the bits required</param>
228240
/// <returns>The bitvector containing the required number of bits</returns>
229-
public UInt64 GetBitVector(UInt64 value, Int32 bitcount, Int32 startindex)
241+
public static UInt64 GetBitVector(UInt64 value, Int32 bitcount, Int32 startindex)
230242
{
231243
UInt64 bv = 0;
232244
UInt64 mask;
233245

234246
for (Int32 i = startindex; i < bitcount; i++)
235247
{
236248
mask = (UInt64)Math.Pow(2, i);
237-
bv = (UInt64)(mask & value) | bv;
249+
bv = (mask & value) | bv;
238250
}
239251

240252
bv >>= startindex;
@@ -273,7 +285,7 @@ public String EncodePayload(UInt64 bitvector, Int32 bitsleft)
273285
return EncodePayload(bitvector / 64, bitsleft - 6) + symbol;
274286
}
275287

276-
public UInt64 DecodePayload(String payload, Int32 startIndex, Int32 length)
288+
public static UInt64 DecodePayload(String payload, Int32 startIndex, Int32 length)
277289
{
278290
if (length > 10)
279291
{
@@ -304,7 +316,7 @@ public UInt64 DecodePayload(String payload, Int32 startIndex, Int32 length)
304316
/// </summary>
305317
/// <param name="sentence">The NMEA 0183 sentence to be computed, inclusive of the start delimiter "!" and just before the checksum delimiter "*"</param>
306318
/// <returns>The 8-bit XOR value.</returns>
307-
protected Byte CalculateChecksum(String sentence)
319+
protected static Byte CalculateChecksum(String sentence)
308320
{
309321
Byte checksum = 0b0;
310322
Byte[] data = Encoding.ASCII.GetBytes(sentence.Substring(1));

AIS/AISMessage01.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,15 +413,15 @@ private void GetBitVector0_59()
413413

414414
if (rateOfTurn8 < 0)
415415
{
416-
UInt64 tempRateOfTurn = (UInt64)(rateOfTurn8 + (Int32)0xFF);
416+
UInt64 tempRateOfTurn = (UInt64)(rateOfTurn8 + 0xFF);
417417

418418
_bitVector0_59 <<= 8;
419419
_bitVector0_59 |= tempRateOfTurn;
420420
}
421421
else
422422
{
423423
_bitVector0_59 <<= 8;
424-
_bitVector0_59 |= (UInt64)((UInt16)rateOfTurn8);
424+
_bitVector0_59 |= (UInt16)rateOfTurn8;
425425
}
426426

427427
_bitVector0_59 <<= 10;
@@ -439,28 +439,28 @@ private void GetBitVector60_119()
439439

440440
if (longitude28 < 0)
441441
{
442-
UInt64 tempLongitude = (UInt64)(longitude28 + (Int32)0xFFFFFFF);
442+
UInt64 tempLongitude = (UInt64)(longitude28 + 0xFFFFFFF);
443443

444444
_bitVector60_119 <<= 28;
445445
_bitVector60_119 |= tempLongitude;
446446
}
447447
else
448448
{
449449
_bitVector60_119 <<= 28;
450-
_bitVector60_119 |= (UInt64)((UInt32)longitude28);
450+
_bitVector60_119 |= (UInt32)longitude28;
451451
}
452452

453453
if (latitude27 < 0)
454454
{
455-
UInt64 tempLatitude = (UInt64)(latitude27 + (Int32)0x7FFFFFF);
455+
UInt64 tempLatitude = (UInt64)(latitude27 + 0x7FFFFFF);
456456

457457
_bitVector60_119 <<= 27;
458458
_bitVector60_119 |= tempLatitude;
459459
}
460460
else
461461
{
462462
_bitVector60_119 <<= 27;
463-
_bitVector60_119 |= (UInt64)((UInt32)latitude27);
463+
_bitVector60_119 |= (UInt32)latitude27;
464464
}
465465

466466
_bitVector60_119 <<= 4;

AIS/AISMessage02.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,15 +413,15 @@ private void GetBitVector0_59()
413413

414414
if (rateOfTurn8 < 0)
415415
{
416-
UInt64 tempRateOfTurn = (UInt64)(rateOfTurn8 + (Int32)0xFF);
416+
UInt64 tempRateOfTurn = (UInt64)(rateOfTurn8 + 0xFF);
417417

418418
_bitVector0_59 <<= 8;
419419
_bitVector0_59 |= tempRateOfTurn;
420420
}
421421
else
422422
{
423423
_bitVector0_59 <<= 8;
424-
_bitVector0_59 |= (UInt64)((UInt16)rateOfTurn8);
424+
_bitVector0_59 |= (UInt16)rateOfTurn8;
425425
}
426426

427427
_bitVector0_59 <<= 10;
@@ -439,28 +439,28 @@ private void GetBitVector60_119()
439439

440440
if (longitude28 < 0)
441441
{
442-
UInt64 tempLongitude = (UInt64)(longitude28 + (Int32)0xFFFFFFF);
442+
UInt64 tempLongitude = (UInt64)(longitude28 + 0xFFFFFFF);
443443

444444
_bitVector60_119 <<= 28;
445445
_bitVector60_119 |= tempLongitude;
446446
}
447447
else
448448
{
449449
_bitVector60_119 <<= 28;
450-
_bitVector60_119 |= (UInt64)((UInt32)longitude28);
450+
_bitVector60_119 |= (UInt32)longitude28;
451451
}
452452

453453
if (latitude27 < 0)
454454
{
455-
UInt64 tempLatitude = (UInt64)(latitude27 + (Int32)0x7FFFFFF);
455+
UInt64 tempLatitude = (UInt64)(latitude27 + 0x7FFFFFF);
456456

457457
_bitVector60_119 <<= 27;
458458
_bitVector60_119 |= tempLatitude;
459459
}
460460
else
461461
{
462462
_bitVector60_119 <<= 27;
463-
_bitVector60_119 |= (UInt64)((UInt32)latitude27);
463+
_bitVector60_119 |= (UInt32)latitude27;
464464
}
465465

466466
_bitVector60_119 <<= 4;

AIS/AISMessage03.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,15 +413,15 @@ private void GetBitVector0_59()
413413

414414
if (rateOfTurn8 < 0)
415415
{
416-
UInt64 tempRateOfTurn = (UInt64)(rateOfTurn8 + (Int32)0xFF);
416+
UInt64 tempRateOfTurn = (UInt64)(rateOfTurn8 + 0xFF);
417417

418418
_bitVector0_59 <<= 8;
419419
_bitVector0_59 |= tempRateOfTurn;
420420
}
421421
else
422422
{
423423
_bitVector0_59 <<= 8;
424-
_bitVector0_59 |= (UInt64)((UInt16)rateOfTurn8);
424+
_bitVector0_59 |= (UInt16)rateOfTurn8;
425425
}
426426

427427
_bitVector0_59 <<= 10;
@@ -439,28 +439,28 @@ private void GetBitVector60_119()
439439

440440
if (longitude28 < 0)
441441
{
442-
UInt64 tempLongitude = (UInt64)(longitude28 + (Int32)0xFFFFFFF);
442+
UInt64 tempLongitude = (UInt64)(longitude28 + 0xFFFFFFF);
443443

444444
_bitVector60_119 <<= 28;
445445
_bitVector60_119 |= tempLongitude;
446446
}
447447
else
448448
{
449449
_bitVector60_119 <<= 28;
450-
_bitVector60_119 |= (UInt64)((UInt32)longitude28);
450+
_bitVector60_119 |= (UInt32)longitude28;
451451
}
452452

453453
if (latitude27 < 0)
454454
{
455-
UInt64 tempLatitude = (UInt64)(latitude27 + (Int32)0x7FFFFFF);
455+
UInt64 tempLatitude = (UInt64)(latitude27 + 0x7FFFFFF);
456456

457457
_bitVector60_119 <<= 27;
458458
_bitVector60_119 |= tempLatitude;
459459
}
460460
else
461461
{
462462
_bitVector60_119 <<= 27;
463-
_bitVector60_119 |= (UInt64)((UInt32)latitude27);
463+
_bitVector60_119 |= (UInt32)latitude27;
464464
}
465465

466466
_bitVector60_119 <<= 4;

AIS/AISMessage04.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,23 +288,23 @@ private void GetBitVector60_119()
288288

289289
if (longitude28 < 0)
290290
{
291-
UInt64 tempLongitude = (UInt64)(longitude28 + (Int32)0xFFFFFFF);
291+
UInt64 tempLongitude = (UInt64)(longitude28 + 0xFFFFFFF);
292292

293293
_bitVector60_119 <<= 28;
294294
_bitVector60_119 |= tempLongitude;
295295
}
296296
else
297297
{
298298
_bitVector60_119 <<= 28;
299-
_bitVector60_119 |= (UInt64)((UInt32)longitude28);
299+
_bitVector60_119 |= (UInt32)longitude28;
300300
}
301301

302302
if (latitude27 < 0)
303303
{
304-
UInt64 tempLatitude = (UInt64)(latitude27 + (Int32)0x7FFFFFF);
304+
UInt64 tempLatitude = (UInt64)(latitude27 + 0x7FFFFFF);
305305

306306
_bitVector60_119 <<= 13;
307-
_bitVector60_119 |= GetBitVector((UInt64)tempLatitude, 27, 14);
307+
_bitVector60_119 |= GetBitVector(tempLatitude, 27, 14);
308308
}
309309
else
310310
{
@@ -319,10 +319,10 @@ private void GetBitVector120_167()
319319

320320
if (latitude27 < 0)
321321
{
322-
UInt64 tempLatitude = (UInt64)(latitude27 + (Int32)0x7FFFFFF);
322+
UInt64 tempLatitude = (UInt64)(latitude27 + 0x7FFFFFF);
323323

324324
_bitVector120_167 <<= 14;
325-
_bitVector120_167 |= GetBitVector((UInt64)tempLatitude, 14);
325+
_bitVector120_167 |= GetBitVector(tempLatitude, 14);
326326
}
327327
else
328328
{

0 commit comments

Comments
 (0)