- Created Heart Beat encoding - Created race Status encoding - Create display Text encoding - Created race Start status encoding #story[778]main
parent
b5bd1c9d6d
commit
6046ba4cf1
@ -0,0 +1,23 @@
|
||||
package seng302.Model;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 19/04/17.
|
||||
*/
|
||||
public class RaceMessage {
|
||||
|
||||
private int lineNumber;
|
||||
private String messageText;
|
||||
|
||||
public RaceMessage(int lineNumber, String messageText){
|
||||
this.lineNumber = lineNumber;
|
||||
this.messageText = messageText;
|
||||
}
|
||||
|
||||
public int getLineNumber() {
|
||||
return lineNumber;
|
||||
}
|
||||
|
||||
public String getMessageText() {
|
||||
return messageText;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,169 @@
|
||||
package seng302.Model;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 19/04/17.
|
||||
*/
|
||||
public class RaceVisionByteEncoder {
|
||||
|
||||
public byte[] heartBeat(int seq){
|
||||
ByteBuffer heartBeat = ByteBuffer.allocate(4);
|
||||
heartBeat.putInt(seq);
|
||||
byte [] result = heartBeat.array();
|
||||
return result;
|
||||
}
|
||||
|
||||
public byte[] raceStatus(long time, int race, int raceState, long startTime, short raceWindDir, short raceWindSpeed, int raceType, BoatInRace[] boats){
|
||||
ByteBuffer raceStatusMessage = ByteBuffer.allocate(24 + 20*boats.length);
|
||||
//Version Number 1 bytes
|
||||
int versionNum = 0b10; //this changes with the pdf. (2)
|
||||
byte[] timeBytes = convert(time, 6);//time (6 bytes)
|
||||
byte[] raceID = ByteBuffer.allocate(4).putInt(race).array();//race identifier incase multiple races are going at once.
|
||||
byte[] raceStatus = convert(raceState, 1);//race status 0 - 10
|
||||
byte[] expectedStart = convert(startTime, 6);//number of milliseconds from Jan 1, 1970 for when the data is valid
|
||||
byte[] raceWind = ByteBuffer.allocate(2).putShort(raceWindDir).array();//North = 0x0000 East = 0x4000 South = 0x8000
|
||||
byte[] windSpeed = ByteBuffer.allocate(2).putShort(raceWindSpeed).array();//mm/sec
|
||||
byte[] numBoats = convert(boats.length, 1);
|
||||
byte[] bytesRaceType = convert(raceType, 1);//1 match race, 2 fleet race
|
||||
|
||||
raceStatusMessage.putInt(versionNum);
|
||||
raceStatusMessage.put(timeBytes);
|
||||
raceStatusMessage.put(raceID);
|
||||
raceStatusMessage.put(raceStatus);
|
||||
raceStatusMessage.put(expectedStart);
|
||||
raceStatusMessage.put(raceWind);
|
||||
raceStatusMessage.put(windSpeed);
|
||||
raceStatusMessage.put(numBoats);
|
||||
raceStatusMessage.put(bytesRaceType);
|
||||
|
||||
for (int i = 0; i < boats.length; i++){
|
||||
int sourceID = 0; //TODO use boats source id.
|
||||
byte[] legNum = convert(boats[i].getCurrentLeg().getLegNumber(), 1);
|
||||
byte[] numPenalties = convert(0, 1); //TODO use boats in race penalties class
|
||||
byte[] numPenaltiesServed = convert(0, 1);//TODO use boats in race penalites served.
|
||||
byte[] estNextMarkTime = convert((long)0, 6);//TODO use boats estimated time to next mark.
|
||||
byte[] estFinishTime = convert((long) 0, 6);//TODO use boats estimated time to the finish.
|
||||
|
||||
raceStatusMessage.putInt(sourceID);
|
||||
raceStatusMessage.put(legNum);
|
||||
raceStatusMessage.put(numPenalties);
|
||||
raceStatusMessage.put(numPenaltiesServed);
|
||||
raceStatusMessage.put(estNextMarkTime);
|
||||
raceStatusMessage.put(estFinishTime);
|
||||
}
|
||||
|
||||
return raceStatusMessage.array();
|
||||
}
|
||||
|
||||
public byte[] displayTextMessage(RaceMessage[] message){
|
||||
//ByteBuffer result = ByteBuffer.allocate(4 + numLines * 32);
|
||||
int messageVersionNumber = 0b1;//version number
|
||||
short ackNum = 0;//no clue what this does just a placeholder for 2 bytes.
|
||||
byte[] messLines = convert(message.length, 1);
|
||||
|
||||
// result.putInt(messageVersionNumber);
|
||||
// result.putShort(ackNum);
|
||||
// result.put(messLines);
|
||||
|
||||
ArrayList<byte[]> messages = new ArrayList<byte[]>();
|
||||
int size = 4;
|
||||
|
||||
for (int i = 0; i < message.length; i ++){
|
||||
int messageLen = message[i].getMessageText().getBytes().length;
|
||||
byte[] messageAsBytes = message[i].getMessageText().getBytes();
|
||||
if (messageLen < 30){
|
||||
messageLen = 30;
|
||||
}
|
||||
ByteBuffer mess = ByteBuffer.allocate(2 + messageLen);
|
||||
mess.put(convert(message[i].getLineNumber(), 1));
|
||||
mess.put(convert(messageLen, 1));
|
||||
for (int j = 0; j < messageLen; j ++){
|
||||
mess.put(messageAsBytes[j]);
|
||||
}
|
||||
messages.add(mess.array());
|
||||
size += 2 + messageLen;
|
||||
}
|
||||
|
||||
ByteBuffer result = ByteBuffer.allocate(size);
|
||||
result.put(convert(messageVersionNumber, 1));
|
||||
result.putShort(ackNum);
|
||||
result.put(messLines);
|
||||
|
||||
for(byte[] mess: messages){
|
||||
result.put(mess);
|
||||
}
|
||||
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] raceStartStatus(long time, short ack, long startTime, int raceID, char notification){
|
||||
int messageVersion = 0b1;
|
||||
byte[] timestamp = convert(time, 6);
|
||||
byte[] ackNumber = convert(ack, 2);
|
||||
byte[] raceStartTime = convert(startTime, 6);
|
||||
int raceIdentifier = raceID;
|
||||
char notificationType = notification;
|
||||
|
||||
ByteBuffer result = ByteBuffer.allocate(20);
|
||||
result.put(convert(messageVersion, 1));
|
||||
result.put(timestamp);
|
||||
result.put(ackNumber);
|
||||
result.put(raceStartTime);
|
||||
result.putInt(raceIdentifier);
|
||||
result.putChar(notificationType);
|
||||
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] convert(int n, int size){
|
||||
byte[] result;
|
||||
if (size > 4){
|
||||
result = new byte[4];
|
||||
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];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public byte[] convert(long n, int size){
|
||||
byte[] result;
|
||||
if (size > 8){
|
||||
result = new byte[8];
|
||||
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];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public byte[] convert(short n, int size){
|
||||
byte[] result;
|
||||
if (size > 2){
|
||||
result = new byte[2];
|
||||
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];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue