Merging main features into story 6. App now sends and receives key strokes. #story[1007, 1089]

This commit is contained in:
Joseph Gardner
2017-07-27 12:23:44 +12:00
parent 0352e3310d
commit 0ea7fb8c84
7 changed files with 150 additions and 9 deletions
@@ -193,6 +193,10 @@ public class BinaryMessageDecoder {
AverageWindDecoder awDecoder = new AverageWindDecoder(messageBody);
return awDecoder.getAverageWind();
case BOATACTION:
BoatActionDecoder baDecoder = new BoatActionDecoder(messageBody);
return new BoatAction(baDecoder.getBoatAction());
default:
//System.out.println("Broken Message!");
//throw new InvalidMessageException("Broken message! Did not recognise message type: " + headerMessageType + ".");
@@ -0,0 +1,20 @@
package network.MessageDecoders;
import network.Messages.Enums.BoatActionEnum;
import java.util.Arrays;
public class BoatActionDecoder {
byte byteBoatAction;
BoatActionEnum boatAction;
public BoatActionDecoder(byte[] encodedBoatAction) {
byteBoatAction = encodedBoatAction[0];
boatAction = BoatActionEnum.fromByte(byteBoatAction);
}
public BoatActionEnum getBoatAction() {
return boatAction;
}
}
@@ -339,4 +339,11 @@ public class RaceVisionByteEncoder {
return result.array();
}
public static byte[] boatActionMessage(BoatAction boatAction){
ByteBuffer boatActionMessage = ByteBuffer.allocate(1);
boatActionMessage.put(intToBytes(boatAction.getBoatAction(), 1));
byte [] result = boatActionMessage.array();
return result;
}
}
@@ -0,0 +1,22 @@
package network.Messages;
import network.Messages.Enums.BoatActionEnum;
import network.Messages.Enums.MessageType;
/**
* Created by David on 10/07/2017.
*/
public class BoatAction extends AC35Data {
private byte boatAction;
public BoatAction(BoatActionEnum boatAction){
super(MessageType.BOATACTION);
this.boatAction = boatAction.getValue();
}
public byte getBoatAction() {
return boatAction;
}
}
@@ -0,0 +1,71 @@
package network.Messages.Enums;
import java.util.HashMap;
import java.util.Map;
/**
* Boat actions
*/
public enum BoatActionEnum {
NOT_A_STATUS(-1),
AUTO_PILOT(1),
SAILS_IN(2),
SAILS_OUT(3),
TACK_GYBE(4),
UPWIND(5),
DOWNWIND(6);
private byte value;
/**
* Ctor. Creates a BoatActionEnum from a given primitive integer value, cast to a byte.
* @param value Integer, which is cast to byte, to construct from.
*/
private BoatActionEnum(int value) {
this.value = (byte) value;
}
/**
* Returns the primitive value of the enum.
* @return Primitive value of the enum.
*/
public byte getValue() {
return value;
}
/**
* Stores a mapping between Byte values and BoatActionEnum values.
*/
private static final Map<Byte, BoatActionEnum> byteToStatusMap = new HashMap<>();
/*
Static initialization block. Initializes the byteToStatusMap.
*/
static {
for (BoatActionEnum type : BoatActionEnum.values()) {
BoatActionEnum.byteToStatusMap.put(type.value, type);
}
}
/**
* Returns the enumeration value which corresponds to a given byte value.
* @param boatActionEnum Byte value to convert to a BoatActionEnum value.
* @return The BoatActionEnum value which corresponds to the given byte value.
*/
public static BoatActionEnum fromByte(byte boatActionEnum) {
//Gets the corresponding MessageType from the map.
BoatActionEnum type = BoatActionEnum.byteToStatusMap.get(boatActionEnum);
if (type == null) {
//If the byte value wasn't found, return the NOT_A_STATUS boatActionEnum.
return BoatActionEnum.NOT_A_STATUS;
} else {
//Otherwise, return the boatActionEnum.
return type;
}
}
}