Skip to content

Commit

Permalink
Added UnitTestAISMessage for some of the more common unit test.
Browse files Browse the repository at this point in the history
Changed implementation based on SonarCloud recommendation.
  • Loading branch information
amsga committed Dec 14, 2024
1 parent 07a0fb8 commit b15b747
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 110 deletions.
4 changes: 2 additions & 2 deletions AIS/AIS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageId>TensionDev.Maritime.AIS</PackageId>
<Version>0.4.1</Version>
<Version>0.4.2</Version>
<Authors>TensionDev amsga</Authors>
<Company>TensionDev</Company>
<Product>TensionDev.Maritime.AIS</Product>
Expand All @@ -21,7 +21,7 @@
<PackageReleaseNotes>Initial project release</PackageReleaseNotes>
<NeutralLanguage>en-SG</NeutralLanguage>
<AssemblyVersion>0.4.0.0</AssemblyVersion>
<FileVersion>0.4.1.0</FileVersion>
<FileVersion>0.4.2.0</FileVersion>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
Expand Down
56 changes: 34 additions & 22 deletions AIS/AISMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace TensionDev.Maritime.AIS
{
public abstract class AISMessage
{
private protected static UInt16 s_groupId = 0;
protected UInt16 s_groupId = 0;

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

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

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

AISMessage aisMessage;
AISMessage aisMessage = CreateAISMessage(messageId);

if (sentenceIdentifier == "VDM")
{
aisMessage.SentenceFormatter = SentenceFormatterEnum.VDM;
}
else if (sentenceIdentifier == "VDO")
{
aisMessage.SentenceFormatter = SentenceFormatterEnum.VDO;
}

aisMessage.DecodePayloads(payloads);

return aisMessage;
}

/// <summary>
/// Creates an AIS Message Object based on encoded message ID
/// </summary>
/// <param name="messageId">Encoded Message ID</param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
private static AISMessage CreateAISMessage(string messageId)
{
AISMessage aisMessage;
switch (messageId)
{
case "1":
Expand Down Expand Up @@ -142,17 +165,6 @@ public static AISMessage DecodeSentences(IList<String> sentences)
throw new NotImplementedException("Message Identifier not recognised.");
}

if (sentenceIdentifier == "VDM")
{
aisMessage.SentenceFormatter = SentenceFormatterEnum.VDM;
}
else if (sentenceIdentifier == "VDO")
{
aisMessage.SentenceFormatter = SentenceFormatterEnum.VDO;
}

aisMessage.DecodePayloads(payloads);

return aisMessage;
}

Expand All @@ -162,7 +174,7 @@ public static AISMessage DecodeSentences(IList<String> sentences)
/// <param name="value">The Value to get the bitvector from</param>
/// <param name="bitcount">The end index of the bits required</param>
/// <returns>The bitvector containing the required number of bits</returns>
public UInt64 GetBitVector(Int64 value, Int32 bitcount)
public static UInt64 GetBitVector(Int64 value, Int32 bitcount)
{
UInt64 bv = 0;
Int64 mask;
Expand All @@ -184,7 +196,7 @@ public UInt64 GetBitVector(Int64 value, Int32 bitcount)
/// <param name="bitcount">The end index of the bits required</param>
/// <param name="startindex">The start index of the bits required</param>
/// <returns>The bitvector containing the required number of bits</returns>
public UInt64 GetBitVector(Int64 value, Int32 bitcount, Int32 startindex)
public static UInt64 GetBitVector(Int64 value, Int32 bitcount, Int32 startindex)
{
UInt64 bv = 0;
Int64 mask;
Expand All @@ -204,15 +216,15 @@ public UInt64 GetBitVector(Int64 value, Int32 bitcount, Int32 startindex)
/// <param name="value">The Value to get the bitvector from</param>
/// <param name="bitcount">The end index of the bits required</param>
/// <returns>The bitvector containing the required number of bits</returns>
public UInt64 GetBitVector(UInt64 value, Int32 bitcount)
public static UInt64 GetBitVector(UInt64 value, Int32 bitcount)
{
UInt64 bv = 0;
UInt64 mask;

for (Int32 i = 0; i < bitcount; i++)
{
mask = (UInt64)Math.Pow(2, i);
bv = (UInt64)(mask & value) | bv;
bv = (mask & value) | bv;
}

return bv;
Expand All @@ -226,15 +238,15 @@ public UInt64 GetBitVector(UInt64 value, Int32 bitcount)
/// <param name="bitcount">The end index of the bits required</param>
/// <param name="startindex">The start index of the bits required</param>
/// <returns>The bitvector containing the required number of bits</returns>
public UInt64 GetBitVector(UInt64 value, Int32 bitcount, Int32 startindex)
public static UInt64 GetBitVector(UInt64 value, Int32 bitcount, Int32 startindex)
{
UInt64 bv = 0;
UInt64 mask;

for (Int32 i = startindex; i < bitcount; i++)
{
mask = (UInt64)Math.Pow(2, i);
bv = (UInt64)(mask & value) | bv;
bv = (mask & value) | bv;
}

bv >>= startindex;
Expand Down Expand Up @@ -273,7 +285,7 @@ public String EncodePayload(UInt64 bitvector, Int32 bitsleft)
return EncodePayload(bitvector / 64, bitsleft - 6) + symbol;
}

public UInt64 DecodePayload(String payload, Int32 startIndex, Int32 length)
public static UInt64 DecodePayload(String payload, Int32 startIndex, Int32 length)
{
if (length > 10)
{
Expand Down Expand Up @@ -304,7 +316,7 @@ public UInt64 DecodePayload(String payload, Int32 startIndex, Int32 length)
/// </summary>
/// <param name="sentence">The NMEA 0183 sentence to be computed, inclusive of the start delimiter "!" and just before the checksum delimiter "*"</param>
/// <returns>The 8-bit XOR value.</returns>
protected Byte CalculateChecksum(String sentence)
protected static Byte CalculateChecksum(String sentence)
{
Byte checksum = 0b0;
Byte[] data = Encoding.ASCII.GetBytes(sentence.Substring(1));
Expand Down
12 changes: 6 additions & 6 deletions AIS/AISMessage01.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,15 @@ private void GetBitVector0_59()

if (rateOfTurn8 < 0)
{
UInt64 tempRateOfTurn = (UInt64)(rateOfTurn8 + (Int32)0xFF);
UInt64 tempRateOfTurn = (UInt64)(rateOfTurn8 + 0xFF);

_bitVector0_59 <<= 8;
_bitVector0_59 |= tempRateOfTurn;
}
else
{
_bitVector0_59 <<= 8;
_bitVector0_59 |= (UInt64)((UInt16)rateOfTurn8);
_bitVector0_59 |= (UInt16)rateOfTurn8;
}

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

if (longitude28 < 0)
{
UInt64 tempLongitude = (UInt64)(longitude28 + (Int32)0xFFFFFFF);
UInt64 tempLongitude = (UInt64)(longitude28 + 0xFFFFFFF);

_bitVector60_119 <<= 28;
_bitVector60_119 |= tempLongitude;
}
else
{
_bitVector60_119 <<= 28;
_bitVector60_119 |= (UInt64)((UInt32)longitude28);
_bitVector60_119 |= (UInt32)longitude28;
}

if (latitude27 < 0)
{
UInt64 tempLatitude = (UInt64)(latitude27 + (Int32)0x7FFFFFF);
UInt64 tempLatitude = (UInt64)(latitude27 + 0x7FFFFFF);

_bitVector60_119 <<= 27;
_bitVector60_119 |= tempLatitude;
}
else
{
_bitVector60_119 <<= 27;
_bitVector60_119 |= (UInt64)((UInt32)latitude27);
_bitVector60_119 |= (UInt32)latitude27;
}

_bitVector60_119 <<= 4;
Expand Down
12 changes: 6 additions & 6 deletions AIS/AISMessage02.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,15 @@ private void GetBitVector0_59()

if (rateOfTurn8 < 0)
{
UInt64 tempRateOfTurn = (UInt64)(rateOfTurn8 + (Int32)0xFF);
UInt64 tempRateOfTurn = (UInt64)(rateOfTurn8 + 0xFF);

_bitVector0_59 <<= 8;
_bitVector0_59 |= tempRateOfTurn;
}
else
{
_bitVector0_59 <<= 8;
_bitVector0_59 |= (UInt64)((UInt16)rateOfTurn8);
_bitVector0_59 |= (UInt16)rateOfTurn8;
}

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

if (longitude28 < 0)
{
UInt64 tempLongitude = (UInt64)(longitude28 + (Int32)0xFFFFFFF);
UInt64 tempLongitude = (UInt64)(longitude28 + 0xFFFFFFF);

_bitVector60_119 <<= 28;
_bitVector60_119 |= tempLongitude;
}
else
{
_bitVector60_119 <<= 28;
_bitVector60_119 |= (UInt64)((UInt32)longitude28);
_bitVector60_119 |= (UInt32)longitude28;
}

if (latitude27 < 0)
{
UInt64 tempLatitude = (UInt64)(latitude27 + (Int32)0x7FFFFFF);
UInt64 tempLatitude = (UInt64)(latitude27 + 0x7FFFFFF);

_bitVector60_119 <<= 27;
_bitVector60_119 |= tempLatitude;
}
else
{
_bitVector60_119 <<= 27;
_bitVector60_119 |= (UInt64)((UInt32)latitude27);
_bitVector60_119 |= (UInt32)latitude27;
}

_bitVector60_119 <<= 4;
Expand Down
12 changes: 6 additions & 6 deletions AIS/AISMessage03.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,15 @@ private void GetBitVector0_59()

if (rateOfTurn8 < 0)
{
UInt64 tempRateOfTurn = (UInt64)(rateOfTurn8 + (Int32)0xFF);
UInt64 tempRateOfTurn = (UInt64)(rateOfTurn8 + 0xFF);

_bitVector0_59 <<= 8;
_bitVector0_59 |= tempRateOfTurn;
}
else
{
_bitVector0_59 <<= 8;
_bitVector0_59 |= (UInt64)((UInt16)rateOfTurn8);
_bitVector0_59 |= (UInt16)rateOfTurn8;
}

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

if (longitude28 < 0)
{
UInt64 tempLongitude = (UInt64)(longitude28 + (Int32)0xFFFFFFF);
UInt64 tempLongitude = (UInt64)(longitude28 + 0xFFFFFFF);

_bitVector60_119 <<= 28;
_bitVector60_119 |= tempLongitude;
}
else
{
_bitVector60_119 <<= 28;
_bitVector60_119 |= (UInt64)((UInt32)longitude28);
_bitVector60_119 |= (UInt32)longitude28;
}

if (latitude27 < 0)
{
UInt64 tempLatitude = (UInt64)(latitude27 + (Int32)0x7FFFFFF);
UInt64 tempLatitude = (UInt64)(latitude27 + 0x7FFFFFF);

_bitVector60_119 <<= 27;
_bitVector60_119 |= tempLatitude;
}
else
{
_bitVector60_119 <<= 27;
_bitVector60_119 |= (UInt64)((UInt32)latitude27);
_bitVector60_119 |= (UInt32)latitude27;
}

_bitVector60_119 <<= 4;
Expand Down
12 changes: 6 additions & 6 deletions AIS/AISMessage04.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,23 +288,23 @@ private void GetBitVector60_119()

if (longitude28 < 0)
{
UInt64 tempLongitude = (UInt64)(longitude28 + (Int32)0xFFFFFFF);
UInt64 tempLongitude = (UInt64)(longitude28 + 0xFFFFFFF);

_bitVector60_119 <<= 28;
_bitVector60_119 |= tempLongitude;
}
else
{
_bitVector60_119 <<= 28;
_bitVector60_119 |= (UInt64)((UInt32)longitude28);
_bitVector60_119 |= (UInt32)longitude28;
}

if (latitude27 < 0)
{
UInt64 tempLatitude = (UInt64)(latitude27 + (Int32)0x7FFFFFF);
UInt64 tempLatitude = (UInt64)(latitude27 + 0x7FFFFFF);

_bitVector60_119 <<= 13;
_bitVector60_119 |= GetBitVector((UInt64)tempLatitude, 27, 14);
_bitVector60_119 |= GetBitVector(tempLatitude, 27, 14);
}
else
{
Expand All @@ -319,10 +319,10 @@ private void GetBitVector120_167()

if (latitude27 < 0)
{
UInt64 tempLatitude = (UInt64)(latitude27 + (Int32)0x7FFFFFF);
UInt64 tempLatitude = (UInt64)(latitude27 + 0x7FFFFFF);

_bitVector120_167 <<= 14;
_bitVector120_167 |= GetBitVector((UInt64)tempLatitude, 14);
_bitVector120_167 |= GetBitVector(tempLatitude, 14);
}
else
{
Expand Down
Loading

0 comments on commit b15b747

Please sign in to comment.