diff --git a/src/main/java/seng302/Networking/BoatLocationMessage.java b/src/main/java/seng302/Networking/BoatLocationMessage.java index 8f6fe6c3..dfa4291a 100644 --- a/src/main/java/seng302/Networking/BoatLocationMessage.java +++ b/src/main/java/seng302/Networking/BoatLocationMessage.java @@ -19,7 +19,7 @@ public class BoatLocationMessage private int sourceID; ///Sequence number of the message. - private long sequenceNumber; + private int sequenceNumber; ///Device type of the message (physical source of the message). private byte deviceType; @@ -107,7 +107,7 @@ public class BoatLocationMessage * @param currentSet * @param rudderAngle */ - public BoatLocationMessage(byte messageVersionNumber, long time, int sourceID, long sequenceNumber, byte deviceType, int latitude, int longitude, int altitude, int heading, short pitch, short roll, int boatSpeed, int boatCOG, int boatSOG, int apparentWindSpeed, short apparentWindAngle, int trueWindSpeed, short trueWindDirection, short trueWindAngle, int currentDrift, int currentSet, short rudderAngle) + public BoatLocationMessage(byte messageVersionNumber, long time, int sourceID, int sequenceNumber, byte deviceType, int latitude, int longitude, int altitude, int heading, short pitch, short roll, int boatSpeed, int boatCOG, int boatSOG, int apparentWindSpeed, short apparentWindAngle, int trueWindSpeed, short trueWindDirection, short trueWindAngle, int currentDrift, int currentSet, short rudderAngle) { this.messageVersionNumber = messageVersionNumber; this.time = time; @@ -167,12 +167,12 @@ public class BoatLocationMessage this.sourceID = sourceID; } - public long getSequenceNumber() + public int getSequenceNumber() { return sequenceNumber; } - public void setSequenceNumber(long sequenceNumber) + public void setSequenceNumber(int sequenceNumber) { this.sequenceNumber = sequenceNumber; } diff --git a/src/main/java/seng302/Networking/MessageDecoders/BoatLocationDecoder.java b/src/main/java/seng302/Networking/MessageDecoders/BoatLocationDecoder.java index 2ac3b8ab..f851dd6f 100644 --- a/src/main/java/seng302/Networking/MessageDecoders/BoatLocationDecoder.java +++ b/src/main/java/seng302/Networking/MessageDecoders/BoatLocationDecoder.java @@ -1,5 +1,9 @@ package seng302.Networking.MessageDecoders; +import seng302.Networking.BoatLocationMessage; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; import java.util.Arrays; /** @@ -29,6 +33,8 @@ public class BoatLocationDecoder { private byte[] currentSet; private byte[] rudderAngle; + private BoatLocationMessage message; + public BoatLocationDecoder(byte[] encodedBoatLocation) { messageVersionNumber = encodedBoatLocation[0]; time = Arrays.copyOfRange(encodedBoatLocation, 1, 7); @@ -52,5 +58,62 @@ public class BoatLocationDecoder { currentDrift = Arrays.copyOfRange(encodedBoatLocation,50,52); currentSet = Arrays.copyOfRange(encodedBoatLocation,52, 54); rudderAngle = Arrays.copyOfRange(encodedBoatLocation,54, 56); + + message = new BoatLocationMessage(messageVersionNumber, bytesToLong(time), + bytesToInt(sourceID), bytesToInt(seqNum), + deviceType, bytesToInt(latitude), + bytesToInt(longitude), bytesToInt(altitude), + twoByteToInt(heading), bytesToShort(pitch), + bytesToShort(roll), twoByteToInt(boatSpeed), + twoByteToInt(cog), twoByteToInt(sog), + twoByteToInt(apparentWindSpeed), bytesToShort(apparentWindAngle), + twoByteToInt(trueWindSpeed), bytesToShort(trueWindDirection), + bytesToShort(trueWindAngle), twoByteToInt(currentDrift), + twoByteToInt(currentSet), bytesToShort(rudderAngle) + ); + } + + private int twoByteToInt(byte[] bytesInt){ + ByteBuffer byteBuffer = ByteBuffer.allocate(4); + byteBuffer.order(ByteOrder.BIG_ENDIAN); + byteBuffer.put((byte)0); + byteBuffer.put((byte)0); + byteBuffer.put(bytesInt); + int num = byteBuffer.getInt(0); + + return num; + } + + private int bytesToInt(byte[] bytesInt){ + ByteBuffer wrapped = ByteBuffer.wrap(bytesInt); + int num = wrapped.getInt(); + return num; + } + + private short bytesToShort(byte[] bytesShort){ + ByteBuffer wrapped = ByteBuffer.wrap(bytesShort); + short num = wrapped.getShort(); + return num; + } + + + private long bytesToLong(byte[] bytesLong){ + ByteBuffer byteBuffer = ByteBuffer.allocate(8); + byteBuffer.order(ByteOrder.BIG_ENDIAN); + byteBuffer.put((byte)0); + byteBuffer.put((byte)0); + byteBuffer.put(bytesLong); +// byteBuffer.put(bytesLong[0]); +// byteBuffer.put(bytesLong[1]); +// byteBuffer.put(bytesLong[2]); +// byteBuffer.put(bytesLong[3]); +// byteBuffer.put(bytesLong[4]); +// byteBuffer.put(bytesLong[5]); + long longVal = byteBuffer.getLong(0); + return longVal; + } + + public BoatLocationMessage getMessage() { + return message; } } diff --git a/src/main/java/seng302/Networking/RaceVisionByteEncoder.java b/src/main/java/seng302/Networking/RaceVisionByteEncoder.java index 22ff92e9..d75111e4 100644 --- a/src/main/java/seng302/Networking/RaceVisionByteEncoder.java +++ b/src/main/java/seng302/Networking/RaceVisionByteEncoder.java @@ -162,7 +162,7 @@ public class RaceVisionByteEncoder { byte[] time = convert(boatLocationMessage.getTime(), 6); byte[] sourceID = convert(boatLocationMessage.getSourceID(), 4); byte[] seqNum = convert(boatLocationMessage.getSequenceNumber(), 4); - byte[] deviceType = convert(boatLocationMessage.getDeviceType(), 1); + byte deviceType = boatLocationMessage.getDeviceType(); byte[] latitude = convert(boatLocationMessage.getLatitude(), 4); byte[] longitude = convert(boatLocationMessage.getLongitude(), 4); byte[] altitude = convert(boatLocationMessage.getAltitude(), 4); @@ -336,7 +336,7 @@ public class RaceVisionByteEncoder { return result; } ByteBuffer byteBuffer = ByteBuffer.allocate(2); - byteBuffer.putLong(n); + byteBuffer.putShort(n); byte[] bytes = byteBuffer.array(); result = Arrays.copyOfRange(bytes, 2 - size, 2); return result; diff --git a/src/test/java/seng302/Networking/MessageDecoders/BoatLocationDecoderTest.java b/src/test/java/seng302/Networking/MessageDecoders/BoatLocationDecoderTest.java new file mode 100644 index 00000000..5ce56908 --- /dev/null +++ b/src/test/java/seng302/Networking/MessageDecoders/BoatLocationDecoderTest.java @@ -0,0 +1,49 @@ +package seng302.Networking.MessageDecoders; + +import org.junit.Assert; +import org.junit.Test; +import seng302.Networking.BoatLocationMessage; +import seng302.Networking.RaceVisionByteEncoder; + + +/** + * Created by hba56 on 23/04/17. + */ +public class BoatLocationDecoderTest { + @Test + public void getByteArrayTest(){ + long time = System.currentTimeMillis(); + BoatLocationMessage testMessage = new BoatLocationMessage((byte)1, time, (byte)2, + 3, (byte) 1, 180, -180, 4, (short)5, + (short)6, (short)7, 8, 9, 10, 11, + (short) 12, 13,(short) 14 ,(short) 15, + 16, 17, (short) 18); + RaceVisionByteEncoder raceVisionByteEncoder = new RaceVisionByteEncoder(); + byte [] testEncodedMessage = raceVisionByteEncoder.boatLocation(testMessage); + + BoatLocationDecoder testDecoder = new BoatLocationDecoder(testEncodedMessage); + BoatLocationMessage decodedTest = testDecoder.getMessage(); + + Assert.assertEquals(testMessage.getMessageVersionNumber(), decodedTest.getMessageVersionNumber()); + Assert.assertEquals(testMessage.getTime(), decodedTest.getTime()); + Assert.assertEquals(testMessage.getSequenceNumber(), decodedTest.getSequenceNumber()); + Assert.assertEquals(testMessage.getDeviceType(), decodedTest.getDeviceType()); + Assert.assertEquals(testMessage.getLatitude(), decodedTest.getLatitude()); + Assert.assertEquals(testMessage.getLongitude(), decodedTest.getLongitude()); + Assert.assertEquals(testMessage.getAltitude(), decodedTest.getAltitude()); + Assert.assertEquals(testMessage.getHeading(), decodedTest.getHeading()); + Assert.assertEquals(testMessage.getPitch(), decodedTest.getPitch()); + Assert.assertEquals(testMessage.getRoll(), decodedTest.getRoll()); + Assert.assertEquals(testMessage.getBoatSpeed(), decodedTest.getBoatSpeed()); + + Assert.assertEquals(testMessage.getBoatCOG(), decodedTest.getBoatCOG()); + Assert.assertEquals(testMessage.getBoatSOG(), decodedTest.getBoatSOG()); + Assert.assertEquals(testMessage.getApparentWindSpeed(), decodedTest.getApparentWindSpeed()); + Assert.assertEquals(testMessage.getTrueWindSpeed(), decodedTest.getTrueWindSpeed()); + Assert.assertEquals(testMessage.getTrueWindDirection(), decodedTest.getTrueWindDirection()); + Assert.assertEquals(testMessage.getTrueWindAngle(), decodedTest.getTrueWindAngle()); + Assert.assertEquals(testMessage.getCurrentDrift(), decodedTest.getCurrentDrift()); + Assert.assertEquals(testMessage.getCurrentSet(), decodedTest.getCurrentSet()); + Assert.assertEquals(testMessage.getRudderAngle(), decodedTest.getRudderAngle()); + } +}