Added Knots <-> MMperSec conversions to AC35UnitConverter.
Removed redundant/unused conversions. Documented all of the conversions, and renamed them to pack/unpackX, to match the API spec. Updated/added some tests in AC35UnitConverterTest. RaceStatus now contains a Bearing instead of a packed int bearing. RaceStatus now contains wind speed in knots, instead of MMperSec packed. This means that only RaceStatus decoder/encoder need to care about the bits-over-wire packed values. issue #35 #36 #story[1095]
This commit is contained in:
@@ -5,6 +5,8 @@ import network.Messages.BoatStatus;
|
||||
import network.Messages.Enums.RaceStatusEnum;
|
||||
import network.Messages.Enums.RaceTypeEnum;
|
||||
import network.Messages.RaceStatus;
|
||||
import network.Utils.AC35UnitConverter;
|
||||
import shared.model.Bearing;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -65,10 +67,12 @@ public class RaceStatusDecoder {
|
||||
long expectedStart = bytesToLong(expectedStartBytes);
|
||||
|
||||
byte[] windDirectionBytes = Arrays.copyOfRange(encodedMessage, 18, 20);
|
||||
int windDirection = bytesToInt(windDirectionBytes);
|
||||
int windDirectionInt = bytesToInt(windDirectionBytes);
|
||||
Bearing windDirection = Bearing.fromDegrees(AC35UnitConverter.unpackHeading(windDirectionInt));
|
||||
|
||||
byte[] windSpeedBytes = Arrays.copyOfRange(encodedMessage, 20, 22);
|
||||
short windSpeed = bytesToShort(windSpeedBytes);
|
||||
int windSpeedInt = bytesToInt(windSpeedBytes);
|
||||
double windSpeedKnots = AC35UnitConverter.unpackMMperSecToKnots(windSpeedInt);
|
||||
|
||||
byte[] numberOfBoatsBytes = Arrays.copyOfRange(encodedMessage, 22, 23);
|
||||
int numberOfBoats = bytesToInt(numberOfBoatsBytes);
|
||||
@@ -99,7 +103,7 @@ public class RaceStatusDecoder {
|
||||
raceStatus,
|
||||
expectedStart,
|
||||
windDirection,
|
||||
windSpeed,
|
||||
windSpeedKnots,
|
||||
raceType,
|
||||
boatStatuses );
|
||||
}
|
||||
|
||||
@@ -4,10 +4,13 @@ package network.MessageEncoders;
|
||||
import network.Messages.AC35Data;
|
||||
import network.Messages.BoatStatus;
|
||||
import network.Messages.RaceStatus;
|
||||
import network.Utils.AC35UnitConverter;
|
||||
import shared.model.Bearing;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
|
||||
import static network.Utils.ByteConverter.bytesToInt;
|
||||
import static network.Utils.ByteConverter.intToBytes;
|
||||
import static network.Utils.ByteConverter.longToBytes;
|
||||
|
||||
@@ -52,10 +55,12 @@ public class RaceStatusEncoder implements MessageEncoder {
|
||||
byte[] expectedStart = longToBytes(raceStatus.getExpectedStartTime(), 6);
|
||||
|
||||
//North = 0x0000 East = 0x4000 South = 0x8000.
|
||||
byte[] raceWind = intToBytes(raceStatus.getWindDirection(), 2);
|
||||
int windDirectionInt = AC35UnitConverter.packHeading(raceStatus.getWindDirection().degrees());
|
||||
byte[] raceWind = intToBytes(windDirectionInt, 2);
|
||||
|
||||
//mm/sec
|
||||
byte[] windSpeed = intToBytes(raceStatus.getWindSpeed(), 2);
|
||||
int windSpeedInt = AC35UnitConverter.packKnotsToMMperSec(raceStatus.getWindSpeed());
|
||||
byte[] windSpeed = intToBytes(windSpeedInt, 2);
|
||||
|
||||
|
||||
byte[] numBoats = intToBytes(boatStatuses.size(), 1);
|
||||
|
||||
@@ -5,8 +5,8 @@ import network.Messages.Enums.MessageType;
|
||||
import network.Utils.AC35UnitConverter;
|
||||
import shared.model.Constants;
|
||||
|
||||
import static network.Utils.AC35UnitConverter.convertGPS;
|
||||
import static network.Utils.AC35UnitConverter.convertGPSToInt;
|
||||
import static network.Utils.AC35UnitConverter.unpackGPS;
|
||||
import static network.Utils.AC35UnitConverter.packGPS;
|
||||
|
||||
/**
|
||||
* Represents the information in a boat location message (AC streaming spec: 4.9).
|
||||
@@ -159,8 +159,8 @@ public class BoatLocation extends AC35Data {
|
||||
this.sourceID = sourceID;
|
||||
this.sequenceNumber = sequenceNumber;
|
||||
this.deviceType = 1;
|
||||
this.latitude = convertGPSToInt(lat);
|
||||
this.longitude = convertGPSToInt(lon);
|
||||
this.latitude = packGPS(lat);
|
||||
this.longitude = packGPS(lon);
|
||||
this.altitude = 0;
|
||||
this.heading = convertHeadingDoubleToInt(heading);
|
||||
this.pitch = 0;
|
||||
@@ -340,11 +340,11 @@ public class BoatLocation extends AC35Data {
|
||||
}
|
||||
|
||||
public double getLatitudeDouble(){
|
||||
return convertGPS(this.latitude);
|
||||
return unpackGPS(this.latitude);
|
||||
}
|
||||
|
||||
public double getLongitudeDouble(){
|
||||
return convertGPS(this.longitude);
|
||||
return unpackGPS(this.longitude);
|
||||
}
|
||||
|
||||
public void setLongitude(int longitude) {
|
||||
@@ -474,11 +474,11 @@ public class BoatLocation extends AC35Data {
|
||||
}
|
||||
|
||||
public double getHeadingDegrees(){
|
||||
return AC35UnitConverter.convertHeading(getHeading());
|
||||
return AC35UnitConverter.unpackHeading(getHeading());
|
||||
}
|
||||
|
||||
public double getTrueWindAngleDegrees(){
|
||||
return AC35UnitConverter.convertTrueWindAngle(getTrueWindAngle());
|
||||
return AC35UnitConverter.unpackTrueWindAngle(getTrueWindAngle());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,6 +5,7 @@ import network.Messages.Enums.MessageType;
|
||||
import network.Messages.Enums.RaceStatusEnum;
|
||||
import network.Messages.Enums.RaceTypeEnum;
|
||||
import network.Utils.AC35UnitConverter;
|
||||
import shared.model.Bearing;
|
||||
import shared.model.Constants;
|
||||
|
||||
import java.util.List;
|
||||
@@ -51,12 +52,13 @@ public class RaceStatus extends AC35Data {
|
||||
/**
|
||||
* The wind direction of the course.
|
||||
*/
|
||||
private int windDirection;
|
||||
private Bearing windDirection;
|
||||
|
||||
/**
|
||||
* The wind speed of the course.
|
||||
* Knots.
|
||||
*/
|
||||
private int windSpeed;
|
||||
private double windSpeed;
|
||||
|
||||
/**
|
||||
* The type of race this is.
|
||||
@@ -78,11 +80,12 @@ public class RaceStatus extends AC35Data {
|
||||
* @param raceStatus The status of the race.
|
||||
* @param expectedStartTime The expected start time of the race.
|
||||
* @param windDirection The current wind direction in the race.
|
||||
* @param windSpeed The current wind speed in the race.
|
||||
* @param windSpeed The current wind speed in the race, in knots.
|
||||
* @param raceType The type of race this is.
|
||||
* @param boatStatuses A list of BoatStatuses. One for each boat.
|
||||
*/
|
||||
public RaceStatus(byte messageVersionNumber, long currentTime, int raceID, RaceStatusEnum raceStatus, long expectedStartTime, int windDirection, int windSpeed, RaceTypeEnum raceType, List<BoatStatus> boatStatuses) {
|
||||
public RaceStatus(byte messageVersionNumber, long currentTime, int raceID, RaceStatusEnum raceStatus, long expectedStartTime, Bearing windDirection, double windSpeed, RaceTypeEnum raceType, List<BoatStatus> boatStatuses) {
|
||||
|
||||
super(MessageType.RACESTATUS);
|
||||
this.messageVersionNumber = messageVersionNumber;
|
||||
this.currentTime = currentTime;
|
||||
@@ -96,6 +99,7 @@ public class RaceStatus extends AC35Data {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns the version number of this message.
|
||||
* @return The version number of the message.
|
||||
@@ -144,16 +148,16 @@ public class RaceStatus extends AC35Data {
|
||||
* Returns the current direction of the wind in the race.
|
||||
* @return Current wind direction.
|
||||
*/
|
||||
public int getWindDirection()
|
||||
public Bearing getWindDirection()
|
||||
{
|
||||
return windDirection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the wind speed for this race status, in millimeters per second.
|
||||
* @return Wind speed in millimeters per second.
|
||||
* Returns the wind speed for this race status, in knots.
|
||||
* @return Wind speed in knots.
|
||||
*/
|
||||
public int getWindSpeed()
|
||||
public double getWindSpeed()
|
||||
{
|
||||
return windSpeed;
|
||||
}
|
||||
@@ -221,16 +225,4 @@ public class RaceStatus extends AC35Data {
|
||||
return raceStatus == RaceStatusEnum.PRESTART;
|
||||
}
|
||||
|
||||
|
||||
public double getScaledWindDirection() {
|
||||
return AC35UnitConverter.convertHeading(windDirection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the wind speed for this race status, in knots.
|
||||
* @return Wind speed in knots.
|
||||
*/
|
||||
public double getWindSpeedKnots() {
|
||||
return (windSpeed / Constants.KnotsToMMPerSecond);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,43 +1,88 @@
|
||||
package network.Utils;
|
||||
|
||||
import shared.model.Constants;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 28/04/17.
|
||||
* Contains various unit conversion for encoding/decoding messages.
|
||||
* Our program uses the "unpacked" units, and the over-the-wire format uses "packed" units (e.g., degrees stored as ints).
|
||||
*/
|
||||
public class AC35UnitConverter {
|
||||
|
||||
public static double convertGPS(int value){
|
||||
//converts latitude or longitue to angle
|
||||
|
||||
/**
|
||||
* Converts a packed GPSCoordinate (latitude or longitude) into the unpacked unit.
|
||||
* @param value Packed lat/long value.
|
||||
* @return Unpacked lat/long angle, in degrees.
|
||||
*/
|
||||
public static double unpackGPS(int value) {
|
||||
return (double) value * 180.0 / 2147483648.0;//2^31 = 2147483648
|
||||
}
|
||||
|
||||
public static int convertGPSToInt(double value){
|
||||
//converts latitude or longitue to angle
|
||||
/**
|
||||
* Converts a latitude or longitude angle into a packed unit.
|
||||
* @param value The lat/long angle, in degrees, to convert.
|
||||
* @return The packed value.
|
||||
*/
|
||||
public static int packGPS(double value) {
|
||||
return (int) (value * 2147483648.0/180.0);//2^31 = 2147483648
|
||||
}
|
||||
|
||||
public static double convertHeading(long value){
|
||||
return (double) value * 360.0/65536.0;//2^15
|
||||
|
||||
/**
|
||||
* Unpacks a heading from an int to an angle in degrees (this is a bearing).
|
||||
* @param value The packed value to unpack.
|
||||
* @return The unpacked value in degrees.
|
||||
*/
|
||||
public static double unpackHeading(int value) {
|
||||
return (value * 360.0 / 65536.0);//2^15
|
||||
}
|
||||
|
||||
public static double convertHeading(int value){
|
||||
return (double) value * 360.0/65536.0;//2^15
|
||||
}
|
||||
|
||||
|
||||
public static double convertHeading(double value){
|
||||
return value * 360.0/65536.0;//2^15
|
||||
}
|
||||
|
||||
public static int encodeHeading(int value){
|
||||
/**
|
||||
* Packs a heading (this is a bearing), in degrees, to a packed int value.
|
||||
* @param value The heading in degrees.
|
||||
* @return The packed value.
|
||||
*/
|
||||
public static int packHeading(double value) {
|
||||
return (int) (value / 360.0 * 65536.0);//2^15
|
||||
}
|
||||
|
||||
public static int encodeHeading(double value){
|
||||
return (int) (value / 360.0 * 65536.0);//2^15
|
||||
|
||||
/**
|
||||
* Unpacks a true wind angle from a short to an angle in degrees (this is an azimuth).
|
||||
* @param value The packed value to unpack.
|
||||
* @return The unpacked value in degrees.
|
||||
*/
|
||||
public static double unpackTrueWindAngle(short value) {
|
||||
return (value * 180.0 / 32768.0);//-2^15 to 2^15
|
||||
}
|
||||
|
||||
public static double convertTrueWindAngle(long value){
|
||||
return (double) value * 180.0/32768.0;//-2^15 to 2^15
|
||||
/**
|
||||
* Packs a true wind angle (this is an azimuth) from an angle in degrees to a packed short value.
|
||||
* @param value The unpacked value in degrees.
|
||||
* @return The packed value.
|
||||
*/
|
||||
public static short packTrueWindAngle(double value) {
|
||||
return (short) (value / 180.0 * 32768.0);//-2^15 to 2^15
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Unpacks a speed, in millimeters per second, to a double, in knots.
|
||||
* @param millimetersPerSec Speed in millimeters per second.
|
||||
* @return Speed in knots.
|
||||
*/
|
||||
public static double unpackMMperSecToKnots(int millimetersPerSec) {
|
||||
return (millimetersPerSec / Constants.KnotsToMMPerSecond);
|
||||
}
|
||||
|
||||
/**
|
||||
* Packs a speed, in knots, into an int, in millimeters per second.
|
||||
* @param speedKnots Speed in knots.
|
||||
* @return Speed in millimeters per second.
|
||||
*/
|
||||
public static int packKnotsToMMperSec(double speedKnots) {
|
||||
return (int) (speedKnots * Constants.KnotsToMMPerSecond);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user