Added YachtEvent message with encoder and decoder

- Updated Encoder and Decoder factories for the new message type
- Added an enum for handling yacht event types

#story[1100]
This commit is contained in:
Connor Taylor-Brown
2017-08-17 01:10:55 +12:00
parent f7d0e9d466
commit f8150ef449
7 changed files with 214 additions and 2 deletions
@@ -39,7 +39,7 @@ public class DecoderFactory {
case RACESTARTSTATUS: return new RaceStartStatusDecoder();
//case YACHTEVENTCODE: return new YachtEventCodeDecoder();//TODO
case YACHTEVENTCODE: return new YachtEventCodeDecoder();
//case YACHTACTIONCODE: return new YachtActionCodeDecoder();//TODO
@@ -0,0 +1,47 @@
package network.MessageDecoders;
import network.Exceptions.InvalidMessageException;
import network.Messages.AC35Data;
import network.Messages.Enums.YachtEventEnum;
import network.Messages.YachtEvent;
import java.util.Arrays;
import static network.Utils.ByteConverter.bytesToInt;
import static network.Utils.ByteConverter.bytesToLong;
import static network.Utils.ByteConverter.bytesToShort;
/**
* Decodes {@link YachtEvent} messages.
*/
public class YachtEventCodeDecoder implements MessageDecoder {
private YachtEvent message;
@Override
public AC35Data decode(byte[] encodedMessage) throws InvalidMessageException {
// Deserialise message
byte[] timestamp = Arrays.copyOfRange(encodedMessage, 1, 7);
byte[] ackNum = Arrays.copyOfRange(encodedMessage, 7, 9);
byte[] raceID = Arrays.copyOfRange(encodedMessage, 9, 13);
byte[] sourceID = Arrays.copyOfRange(encodedMessage, 13, 17);
byte[] incidentID = Arrays.copyOfRange(encodedMessage, 17, 21);
byte eventID = encodedMessage[21];
// Unpack bytes into YachtEvent
this.message = new YachtEvent(
bytesToLong(timestamp),
bytesToShort(ackNum),
bytesToInt(raceID),
bytesToInt(sourceID),
bytesToInt(incidentID),
YachtEventEnum.fromByte(eventID)
);
// Return YachtEvent
return message;
}
public YachtEvent getMessage() {
return message;
}
}