From a7e0ffae42096cd6d4af18e0d99d8b0712335e23 Mon Sep 17 00:00:00 2001 From: fjc40 Date: Fri, 21 Apr 2017 12:25:57 +1200 Subject: [PATCH 1/2] Added class to encapsulate a BoatLocationMessage. #story[778] --- .../BoatLocationMessage.java | 345 ++++++++++++++++++ 1 file changed, 345 insertions(+) create mode 100644 mock/src/main/java/seng302/RaceEventMessages/BoatLocationMessage.java diff --git a/mock/src/main/java/seng302/RaceEventMessages/BoatLocationMessage.java b/mock/src/main/java/seng302/RaceEventMessages/BoatLocationMessage.java new file mode 100644 index 00000000..1171fd6b --- /dev/null +++ b/mock/src/main/java/seng302/RaceEventMessages/BoatLocationMessage.java @@ -0,0 +1,345 @@ +package seng302.RaceEventMessages; + +/** + * Created by f123 on 21-Apr-17. + */ + +/** + * Represents the information in a boat location message (AC streaming spec: 4.9). + */ +public class BoatLocationMessage +{ + ///Version number of the message - is always 1. + private byte messageVersionNumber = 1; + + ///Time of the event - milliseconds since jan 1 1970. Proper type is 6 byte int. + private long time; + + ///Source ID of the boat. + private int sourceID; + + ///Sequence number of the message. + private long sequenceNumber; + + ///Device type of the message (physical source of the message). + private byte deviceType; + + ///Latitude of the boat. + private int latitude; + + ///Longitude of the boat. + private int longitude; + + ///Altitude of the boat. + private int altitude; + + ///Heading of the boat. Clockwise, 0 = north. Proper type is unsigned 2 byte int. + private int heading; + + ///Pitch of the boat. + private short pitch; + + ///Roll of the boat. + private short roll; + + ///Speed of the boat. Proper type is unsigned 2 byte int. millimeters per second. + private int boatSpeed; + + ///Course over ground (COG) of the boat. Proper type is unsigned 2 byte int. + private int boatCOG; + + ///Speed over ground (SOG) of the boat. Proper type is unsigned 2 byte int. millimeters per second. + private int boatSOG; + + ///Apparent wind speed at time of event. Proper type is unsigned 2 byte int. millimeters per second. + private int apparentWindSpeed; + + ///Apparent wind angle at time of the event. Wind over starboard = positive. + private short apparentWindAngle; + + ///True wind speed. Proper type is unsigned 2 byte int. millimeters per second. + private int trueWindSpeed; + + ///True wind angle. Clockwise compass direction, 0 = north. + private short trueWindAngle; + + ///Current drift. Proper type is unsigned 2 byte int. millimeters per second. + private int currentDrift; + + ///Current set. Proper type is unsigned 2 byte int. Clockwise compass direction, 0 = north. + private int currentSet; + + ///Rudder angle. Positive is rudder set to turn yacht to port. + private short rudderAngle; + + + /** + * Ctor. + */ + public BoatLocationMessage() + { + } + + /** + * Ctor, with all parameters. + * @param messageVersionNumber + * @param time + * @param sourceID + * @param sequenceNumber + * @param deviceType + * @param latitude + * @param longitude + * @param altitude + * @param heading + * @param pitch + * @param roll + * @param boatSpeed + * @param boatCOG + * @param boatSOG + * @param apparentWindSpeed + * @param apparentWindAngle + * @param trueWindSpeed + * @param trueWindAngle + * @param currentDrift + * @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 trueWindAngle, int currentDrift, int currentSet, short rudderAngle) + { + this.messageVersionNumber = messageVersionNumber; + this.time = time; + this.sourceID = sourceID; + this.sequenceNumber = sequenceNumber; + this.deviceType = deviceType; + this.latitude = latitude; + this.longitude = longitude; + this.altitude = altitude; + this.heading = heading; + this.pitch = pitch; + this.roll = roll; + this.boatSpeed = boatSpeed; + this.boatCOG = boatCOG; + this.boatSOG = boatSOG; + this.apparentWindSpeed = apparentWindSpeed; + this.apparentWindAngle = apparentWindAngle; + this.trueWindSpeed = trueWindSpeed; + this.trueWindAngle = trueWindAngle; + this.currentDrift = currentDrift; + this.currentSet = currentSet; + this.rudderAngle = rudderAngle; + } + + + //Getters and setters for message properties. + + + public byte getMessageVersionNumber() + { + return messageVersionNumber; + } + + public void setMessageVersionNumber(byte messageVersionNumber) + { + this.messageVersionNumber = messageVersionNumber; + } + + public long getTime() + { + return time; + } + + public void setTime(long time) + { + this.time = time; + } + + public int getSourceID() + { + return sourceID; + } + + public void setSourceID(int sourceID) + { + this.sourceID = sourceID; + } + + public long getSequenceNumber() + { + return sequenceNumber; + } + + public void setSequenceNumber(long sequenceNumber) + { + this.sequenceNumber = sequenceNumber; + } + + public byte getDeviceType() + { + return deviceType; + } + + public void setDeviceType(byte deviceType) + { + this.deviceType = deviceType; + } + + public int getLatitude() + { + return latitude; + } + + public void setLatitude(int latitude) + { + this.latitude = latitude; + } + + public int getLongitude() + { + return longitude; + } + + public void setLongitude(int longitude) + { + this.longitude = longitude; + } + + public int getAltitude() + { + return altitude; + } + + public void setAltitude(int altitude) + { + this.altitude = altitude; + } + + public int getHeading() + { + return heading; + } + + public void setHeading(int heading) + { + this.heading = heading; + } + + public short getPitch() + { + return pitch; + } + + public void setPitch(short pitch) + { + this.pitch = pitch; + } + + public short getRoll() + { + return roll; + } + + public void setRoll(short roll) + { + this.roll = roll; + } + + public int getBoatSpeed() + { + return boatSpeed; + } + + public void setBoatSpeed(int boatSpeed) + { + this.boatSpeed = boatSpeed; + } + + public int getBoatCOG() + { + return boatCOG; + } + + public void setBoatCOG(int boatCOG) + { + this.boatCOG = boatCOG; + } + + public int getBoatSOG() + { + return boatSOG; + } + + public void setBoatSOG(int boatSOG) + { + this.boatSOG = boatSOG; + } + + public int getApparentWindSpeed() + { + return apparentWindSpeed; + } + + public void setApparentWindSpeed(int apparentWindSpeed) + { + this.apparentWindSpeed = apparentWindSpeed; + } + + public short getApparentWindAngle() + { + return apparentWindAngle; + } + + public void setApparentWindAngle(short apparentWindAngle) + { + this.apparentWindAngle = apparentWindAngle; + } + + public int getTrueWindSpeed() + { + return trueWindSpeed; + } + + public void setTrueWindSpeed(int trueWindSpeed) + { + this.trueWindSpeed = trueWindSpeed; + } + + public short getTrueWindAngle() + { + return trueWindAngle; + } + + public void setTrueWindAngle(short trueWindAngle) + { + this.trueWindAngle = trueWindAngle; + } + + public int getCurrentDrift() + { + return currentDrift; + } + + public void setCurrentDrift(int currentDrift) + { + this.currentDrift = currentDrift; + } + + public int getCurrentSet() + { + return currentSet; + } + + public void setCurrentSet(int currentSet) + { + this.currentSet = currentSet; + } + + public short getRudderAngle() + { + return rudderAngle; + } + + public void setRudderAngle(short rudderAngle) + { + this.rudderAngle = rudderAngle; + } +} From c63c6a3c7720eb58f3f50913c9d6447a77ee1e9f Mon Sep 17 00:00:00 2001 From: Fan-Wu Yang Date: Fri, 21 Apr 2017 12:47:02 +1200 Subject: [PATCH 2/2] Added Encoding to RaceVisionByteEncoder - Added chatterText --- .../Networking/RaceVisionByteEncoder.java | 54 ++++++++++++------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/src/main/java/seng302/Networking/RaceVisionByteEncoder.java b/src/main/java/seng302/Networking/RaceVisionByteEncoder.java index 383edb79..5e5ac150 100644 --- a/src/main/java/seng302/Networking/RaceVisionByteEncoder.java +++ b/src/main/java/seng302/Networking/RaceVisionByteEncoder.java @@ -3,7 +3,9 @@ package seng302.Networking; import seng302.Model.BoatInRace; import java.nio.ByteBuffer; +import java.nio.charset.Charset; import java.util.ArrayList; +import java.util.Arrays; /** * Created by fwy13 on 19/04/17. @@ -139,39 +141,53 @@ public class RaceVisionByteEncoder { result.put(event); return result.array(); } -/* + public byte[] chatterText(int messageType, String message){ int messageVersion = 0b1; byte[] type = convert(messageType, 1); byte[] length = convert(message.length(), 1); - byte[] messageBytes = message.getBytes(Charset.forName("UTF-8")); + byte[] text = convert(message, length[0]); - //byte[] text = - return; - }*/ + ByteBuffer result = ByteBuffer.allocate(3 + text.length); + result.put(convert(messageVersion, 1)); + result.put(type); + result.put(length); + result.put(text); + + return result.array(); + } /* + public byte[] boatLocation(){ + + }*/ + public byte[] convert(String s, int size){ byte[] m = s.getBytes(Charset.forName("UTF-8")); int length = m.length; + byte[] result; if (length > 255){ length = 255; + } else if (size < 1){ + result = new byte[0]; + return result; } - byte[] result = Arrays.copyOfRange(m, 0, length + 1); + result = Arrays.copyOfRange(m, 0, length + 1); + return result; } -*/ + public byte[] convert(int n, int size){ byte[] result; if (size > 4){ result = new byte[4]; return result; + } else if (size < 1){ + result = new byte[0]; + return result; } ByteBuffer byteBuffer = ByteBuffer.allocate(4); byteBuffer.putInt(n); byte[] bytes = byteBuffer.array(); - result = new byte[size]; - for (int i = 4 - size ; i < 4; i++){ - result[i-size] = bytes[i]; - } + result = Arrays.copyOfRange(bytes, 4 - size, 4); return result; } @@ -180,14 +196,14 @@ public class RaceVisionByteEncoder { if (size > 8){ result = new byte[8]; return result; + } else if (size < 1){ + result = new byte[0]; + return result; } ByteBuffer byteBuffer = ByteBuffer.allocate(8); byteBuffer.putLong(n); byte[] bytes = byteBuffer.array(); - result = new byte[size]; - for (int i = 8 - size ; i < 8; i++){ - result[i-size] = bytes[i]; - } + result = Arrays.copyOfRange(bytes, 8 - size, 8); return result; } @@ -197,14 +213,14 @@ public class RaceVisionByteEncoder { if (size > 2){ result = new byte[2]; return result; + } else if (size < 1){ + result = new byte[0]; + return result; } ByteBuffer byteBuffer = ByteBuffer.allocate(2); byteBuffer.putLong(n); byte[] bytes = byteBuffer.array(); - result = new byte[size]; - for (int i = 2 - size ; i < 2; i++){ - result[i-size] = bytes[i]; - } + result = Arrays.copyOfRange(bytes, 2 - size, 2); return result; }