mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Merge branch 'wake_remake' into Story30b_correcting_boat_movement
# Conflicts: # src/main/java/seng302/App.java # src/main/java/seng302/controllers/CanvasController.java # src/main/java/seng302/models/BoatGroup.java # src/main/java/seng302/models/RaceObject.java # src/main/java/seng302/models/mark/MarkGroup.java # src/main/java/seng302/models/parsers/StreamParser.java
This commit is contained in:
@@ -179,10 +179,10 @@ public class BoatGroup extends RaceObject{
|
||||
* @param rotation Rotation to move graphics to.
|
||||
* @param raceIds RaceID of the object to move.
|
||||
*/
|
||||
public void setDestination (double newXValue, double newYValue, double rotation, double groundSpeed, int... raceIds) {
|
||||
public void setDestination (double newXValue, double newYValue, double rotation, double speed, int... raceIds) {
|
||||
if (hasRaceId(raceIds)) {
|
||||
destinationSet = true;
|
||||
boat.setVelocity(groundSpeed);
|
||||
boat.setVelocity(speed);
|
||||
if (currentRotation < 0)
|
||||
currentRotation = 360 - currentRotation;
|
||||
double dx = newXValue - boatPoly.getLayoutX();
|
||||
@@ -226,22 +226,20 @@ public class BoatGroup extends RaceObject{
|
||||
}
|
||||
}
|
||||
|
||||
public void setDestination (double newXValue, double newYValue, int... raceIDs) {
|
||||
public void setDestination (double newXValue, double newYValue, double speed, int... raceIDs) {
|
||||
destinationSet = true;
|
||||
|
||||
if (hasRaceId(raceIDs)) {
|
||||
double rotation = Math.abs(
|
||||
Math.toDegrees(
|
||||
Math.atan(
|
||||
(newYValue - boatPoly.getLayoutY()) / (newXValue - boatPoly.getLayoutX())
|
||||
)
|
||||
)
|
||||
);
|
||||
setDestination(newXValue, newYValue, rotation, speed, raceIDs);
|
||||
}
|
||||
}
|
||||
// public void setDestination (double newXValue, double newYValue, int... raceIDs) {
|
||||
// destinationSet = true;
|
||||
//
|
||||
// if (hasRaceId(raceIDs)) {
|
||||
// double rotation = Math.abs(
|
||||
// Math.toDegrees(
|
||||
// Math.atan(
|
||||
// (newYValue - boatPoly.getLayoutY()) / (newXValue - boatPoly.getLayoutX())
|
||||
// )
|
||||
// )
|
||||
// );
|
||||
// setDestination(newXValue, newYValue, rotation, raceIDs);
|
||||
// }
|
||||
// }
|
||||
|
||||
public void rotateTo (double rotation) {
|
||||
currentRotation = rotation;
|
||||
|
||||
@@ -59,10 +59,9 @@ public abstract class RaceObject extends Group {
|
||||
* @param x X co-ordinate to move the graphics to.
|
||||
* @param y Y co-ordinate to move the graphics to.
|
||||
* @param rotation Rotation to move graphics to.
|
||||
* @param groundSpeed boat groundspeed.
|
||||
* @param raceIds RaceID of the object to move.
|
||||
*/
|
||||
public abstract void setDestination (double x, double y, double rotation, double groundSpeed, int... raceIds);
|
||||
public abstract void setDestination (double x, double y, double rotation, double speed, int... raceIds);
|
||||
/**
|
||||
* Sets the destination of everything within the RaceObject that has an ID in the array raceIds. The destination is
|
||||
* set to the co-ordinates (x, y).
|
||||
@@ -70,7 +69,7 @@ public abstract class RaceObject extends Group {
|
||||
* @param y Y co-ordinate to move the graphic to.
|
||||
* @param raceIds RaceID to the object to move.
|
||||
*/
|
||||
public abstract void setDestination (double x, double y, int... raceIds);
|
||||
public abstract void setDestination (double x, double y, double speed, int... raceIds);
|
||||
|
||||
public abstract void updatePosition (long timeInterval);
|
||||
|
||||
|
||||
@@ -102,21 +102,21 @@ public class MarkGroup extends RaceObject {
|
||||
//moveTo(points[0].getX(), points[0].getY());
|
||||
}
|
||||
|
||||
public void setDestination (double x, double y, double rotation, double groundSpeed, int... raceIds) {
|
||||
setDestination(x, y, raceIds);
|
||||
public void setDestination (double x, double y, double rotation, double speed, int... raceIds) {
|
||||
setDestination(x, y, 0, raceIds);
|
||||
this.rotationalGoal = rotation;
|
||||
calculateRotationalVelocity();
|
||||
}
|
||||
|
||||
public void setDestination (double x, double y, int... raceIds) {
|
||||
public void setDestination (double x, double y, double speed, int... raceIds) {
|
||||
for (int i = 0; i < marks.size(); i++)
|
||||
for (int id : raceIds)
|
||||
if (id == marks.get(i).getId())
|
||||
setDestinationChild(x, y, Math.max(0, i-1));
|
||||
setDestinationChild(x, y, 0, Math.max(0, i-1));
|
||||
}
|
||||
|
||||
|
||||
private void setDestinationChild (double x, double y, int childIndex) {
|
||||
private void setDestinationChild (double x, double y, double speed, int childIndex) {
|
||||
//double relativeX = x - super.getLayoutX();
|
||||
//double relativeY = y - super.getLayoutY();
|
||||
Circle markCircle = (Circle) super.getChildren().get(childIndex);
|
||||
|
||||
@@ -33,6 +33,7 @@ public class StreamParser extends Thread{
|
||||
private String threadName;
|
||||
private Thread t;
|
||||
private static boolean raceStarted = false;
|
||||
public static XMLParser xmlObject;
|
||||
private static boolean raceFinished = false;
|
||||
private static boolean streamStatus = false;
|
||||
private static long timeSinceStart = -1;
|
||||
@@ -50,6 +51,7 @@ public class StreamParser extends Thread{
|
||||
try {
|
||||
System.out.println("START OF STREAM");
|
||||
streamStatus = true;
|
||||
xmlObject = new XMLParser();
|
||||
while (StreamReceiver.packetBuffer == null || StreamReceiver.packetBuffer.size() < 1) {
|
||||
Thread.sleep(1);
|
||||
}
|
||||
@@ -151,15 +153,15 @@ public class StreamParser extends Thread{
|
||||
private static void extractRaceStatus(StreamPacket packet){
|
||||
byte[] payload = packet.getPayload();
|
||||
int messageVersionNo = payload[0];
|
||||
long currentTime = extractTimeStamp(Arrays.copyOfRange(payload,1,7), 6);
|
||||
long currentTime = bytesToLong(Arrays.copyOfRange(payload,1,7));
|
||||
long raceId = bytesToLong(Arrays.copyOfRange(payload,7,11));
|
||||
int raceStatus = payload[11];
|
||||
// System.out.println("raceStatus = " + raceStatus);
|
||||
long expectedStartTime = extractTimeStamp(Arrays.copyOfRange(payload,12,18), 6);
|
||||
long expectedStartTime = bytesToLong(Arrays.copyOfRange(payload,12,18));
|
||||
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
|
||||
format.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
long timeTillStart = ((new Date (expectedStartTime)).getTime() - (new Date (currentTime)).getTime())/1000;
|
||||
if (timeTillStart > 0 && timeTillStart % 10 == 0) {
|
||||
if (timeTillStart > 0) {
|
||||
timeSinceStart = timeTillStart;
|
||||
System.out.println("Time till start: " + timeTillStart + " Seconds");
|
||||
} else {
|
||||
@@ -172,10 +174,8 @@ public class StreamParser extends Thread{
|
||||
raceFinished = false;
|
||||
System.out.println("RACE HAS STARTED");
|
||||
}
|
||||
if (timeTillStart % 10 == 0){
|
||||
System.out.println("Time since start: " + -1 * timeTillStart + " Seconds");
|
||||
timeSinceStart = timeTillStart;
|
||||
}
|
||||
System.out.println("Time since start: " + -1 * timeTillStart + " Seconds");
|
||||
timeSinceStart = timeTillStart;
|
||||
}
|
||||
long windDir = bytesToLong(Arrays.copyOfRange(payload,18,20));
|
||||
long windSpeed = bytesToLong(Arrays.copyOfRange(payload,20,22));
|
||||
@@ -188,8 +188,8 @@ public class StreamParser extends Thread{
|
||||
boatStatus += "\nLegNumber: " + (int)payload[29 + (i * 20)];
|
||||
boatStatus += "\nPenaltiesAwarded: " + (int)payload[29 + (i * 20)];
|
||||
boatStatus += "\nPenaltiesServed: " + (int)payload[30 + (i * 20)];
|
||||
boatStatus += "\nEstTimeAtNextMark: " + extractTimeStamp(Arrays.copyOfRange(payload,31 + (i * 20),37+ (i * 20)), 6);
|
||||
boatStatus += "\nEstTimeAtFinish: " + extractTimeStamp(Arrays.copyOfRange(payload,37 + (i * 20),43+ (i * 20)), 6);
|
||||
boatStatus += "\nEstTimeAtNextMark: " + bytesToLong(Arrays.copyOfRange(payload,31 + (i * 20),37+ (i * 20)));
|
||||
boatStatus += "\nEstTimeAtFinish: " + bytesToLong(Arrays.copyOfRange(payload,37 + (i * 20),43+ (i * 20)));
|
||||
boatStatuses.add(boatStatus);
|
||||
}
|
||||
}
|
||||
@@ -219,41 +219,24 @@ public class StreamParser extends Thread{
|
||||
private static void extractXmlMessage(StreamPacket packet){
|
||||
|
||||
byte[] payload = packet.getPayload();
|
||||
String xmlMessage = "";
|
||||
|
||||
ByteArrayInputStream payloadStream = new ByteArrayInputStream(payload);
|
||||
|
||||
//Bunch of data we don't want (Message Version Number, AckNumber, Timestamp)
|
||||
payloadStream.skip(9);
|
||||
int xmlMessageSubType = payloadStream.read();
|
||||
payloadStream.skip(2);
|
||||
|
||||
//checks the length of the xml message itself
|
||||
int xmlMessageLength = payloadStream.read() | payloadStream.read() << 8;
|
||||
|
||||
//Converts XML message to string to be parsed
|
||||
int currentChar;
|
||||
while (payloadStream.available() > 0 && (currentChar = payloadStream.read()) != 0) {
|
||||
xmlMessage += (char)currentChar;
|
||||
}
|
||||
|
||||
// Parse boat xml from server
|
||||
if (xmlMessageSubType == 7) {
|
||||
BoatsParser boatsParser = new BoatsParser(xmlMessage);
|
||||
boats = boatsParser.getBoats();
|
||||
}
|
||||
int messageType = payload[9];
|
||||
long messagelength = bytesToLong(Arrays.copyOfRange(payload,12,14));
|
||||
String xmlMessage = new String((Arrays.copyOfRange(payload,14,(int) (14 + messagelength)))).trim();
|
||||
//System.out.println("xmlMessage2 = " + xmlMessage);
|
||||
|
||||
//Create XML document Object
|
||||
// DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
// DocumentBuilder db = null;
|
||||
// try {
|
||||
// db = dbf.newDocumentBuilder();
|
||||
// Document doc = db.parse(new InputSource(new StringReader(xmlMessage)));
|
||||
// // TODO: 25/04/17 ajm412: Check that the object matches expected structure and return Document object.
|
||||
// } catch (ParserConfigurationException | IOException | SAXException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder db = null;
|
||||
Document doc = null;
|
||||
try {
|
||||
db = dbf.newDocumentBuilder();
|
||||
doc = db.parse(new InputSource(new StringReader(xmlMessage)));
|
||||
} catch (ParserConfigurationException | IOException | SAXException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
xmlObject.constructXML(doc, messageType);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -264,8 +247,8 @@ public class StreamParser extends Thread{
|
||||
private static void extractRaceStartStatus(StreamPacket packet){
|
||||
byte[] payload = packet.getPayload();
|
||||
int messageVersionNo = payload[0];
|
||||
long timeStamp = extractTimeStamp(Arrays.copyOfRange(payload,1,7), 6);
|
||||
long raceStartTime = extractTimeStamp(Arrays.copyOfRange(payload,9,15), 6);
|
||||
long timeStamp = bytesToLong(Arrays.copyOfRange(payload,1,7));
|
||||
long raceStartTime = bytesToLong(Arrays.copyOfRange(payload,9,15));
|
||||
long raceId = bytesToLong(Arrays.copyOfRange(payload,15,19));
|
||||
int notificationType = payload[19];
|
||||
}
|
||||
@@ -278,7 +261,7 @@ public class StreamParser extends Thread{
|
||||
private static void extractYachtEventCode(StreamPacket packet){
|
||||
byte[] payload = packet.getPayload();
|
||||
int messageVersionNo = payload[0];
|
||||
long timeStamp = extractTimeStamp(Arrays.copyOfRange(payload,1,7), 6);
|
||||
long timeStamp = bytesToLong(Arrays.copyOfRange(payload,1,7));
|
||||
long raceId = bytesToLong(Arrays.copyOfRange(payload,9,13));
|
||||
long subjectId = bytesToLong(Arrays.copyOfRange(payload,13,17));
|
||||
long incidentId = bytesToLong(Arrays.copyOfRange(payload,17,21));
|
||||
@@ -359,7 +342,7 @@ public class StreamParser extends Thread{
|
||||
private static void extractMarkRounding(StreamPacket packet){
|
||||
byte[] payload = packet.getPayload();
|
||||
int messageVersionNo = payload[0];
|
||||
long timeStamp = extractTimeStamp(Arrays.copyOfRange(payload,1,7), 6);
|
||||
long timeStamp = bytesToLong(Arrays.copyOfRange(payload,1,7));
|
||||
long raceId = bytesToLong(Arrays.copyOfRange(payload,9,13));
|
||||
long subjectId = bytesToLong(Arrays.copyOfRange(payload,13,17));
|
||||
int boatStatus = payload[17];
|
||||
@@ -376,7 +359,7 @@ public class StreamParser extends Thread{
|
||||
ArrayList<String> windInfo = new ArrayList<>();
|
||||
for (int i = 0; i < loopCount; i++){
|
||||
String wind = "WindId: " + payload[3 + (20 * i)];
|
||||
wind += "\nTime: " + extractTimeStamp(Arrays.copyOfRange(payload,4 + (20 * i),10 + (20 * i)), 6);
|
||||
wind += "\nTime: " + bytesToLong(Arrays.copyOfRange(payload,4 + (20 * i),10 + (20 * i)));
|
||||
wind += "\nRaceId: " + bytesToLong(Arrays.copyOfRange(payload,10 + (20 * i),14 + (20 * i)));
|
||||
wind += "\nWindDirection: " + bytesToLong(Arrays.copyOfRange(payload,14 + (20 * i),16 + (20 * i)));
|
||||
wind += "\nWindSpeed: " + bytesToLong(Arrays.copyOfRange(payload,16 + (20 * i),18 + (20 * i)));
|
||||
@@ -390,7 +373,7 @@ public class StreamParser extends Thread{
|
||||
private static void extractAvgWind(StreamPacket packet){
|
||||
byte[] payload = packet.getPayload();
|
||||
int messageVersionNo = payload[0];
|
||||
long timeStamp = extractTimeStamp(Arrays.copyOfRange(payload,1,7), 6);
|
||||
long timeStamp = bytesToLong(Arrays.copyOfRange(payload,1,7));
|
||||
long rawPeriod = bytesToLong(Arrays.copyOfRange(payload,7,9));
|
||||
long rawSamplePeriod = bytesToLong(Arrays.copyOfRange(payload,9,11));
|
||||
long period2 = bytesToLong(Arrays.copyOfRange(payload,11,13));
|
||||
@@ -401,16 +384,6 @@ public class StreamParser extends Thread{
|
||||
long speed4 = bytesToLong(Arrays.copyOfRange(payload,21,23));
|
||||
}
|
||||
|
||||
private static long extractTimeStamp(byte[] timeStampBytes, int noOfBytes){
|
||||
long timeStamp = 0;
|
||||
long multiplier=1;
|
||||
for(int i = 0;i < noOfBytes;i++) {
|
||||
timeStamp += timeStampBytes[i]*multiplier;
|
||||
multiplier *= 256;
|
||||
}
|
||||
return timeStamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* takes an array of up to 7 bytes and returns a positive
|
||||
* long constructed from the input bytes
|
||||
@@ -474,5 +447,9 @@ public class StreamParser extends Thread{
|
||||
public static List<Boat> getBoats() {
|
||||
return boats;
|
||||
}
|
||||
|
||||
public static XMLParser getXmlObject() {
|
||||
return xmlObject;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,35 +9,51 @@ import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Class to create an XML object from the XML Packet Messages.
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* Document doc; // some xml document
|
||||
* Integer xmlMessageType; // an Integer of value 5, 6, 7
|
||||
*
|
||||
* xmlP = new XMLParser(doc, xmlMessageType);
|
||||
* RegattaXMLObject rXmlObj = xmlP.createRegattaXML(); // creates a regattaXML object.
|
||||
*
|
||||
*/
|
||||
class XMLParser {
|
||||
public class XMLParser {
|
||||
|
||||
/**
|
||||
* Creates a Regatta XML Object from the data in a Regatta XML Message
|
||||
* @param doc XML Document Object
|
||||
* @return A new RegattaXMLObject from the input Document.
|
||||
*/
|
||||
RegattaXMLObject createRegattaXML(Document doc) {
|
||||
return new RegattaXMLObject(doc);
|
||||
private Document xmlDoc;
|
||||
|
||||
private RaceXMLObject raceXML;
|
||||
private RegattaXMLObject regattaXML;
|
||||
private BoatXMLObject boatXML;
|
||||
|
||||
public XMLParser() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Race XML Object from the data in a Regatta XML Message
|
||||
* @param doc XML Document Object
|
||||
* @return A new RaceXMLObject from the input Document.
|
||||
* Constructor for XMLParser
|
||||
* @param doc Document to create XML object.
|
||||
* @param messageType Defines if a message is a RegattaXML(5), RaceXML(6), BoatXML(7).
|
||||
*/
|
||||
RaceXMLObject createRaceXML(Document doc) {
|
||||
return new RaceXMLObject(doc);
|
||||
public void constructXML(Document doc, Integer messageType) {
|
||||
this.xmlDoc = doc;
|
||||
switch (messageType) {
|
||||
case 5:
|
||||
regattaXML = new RegattaXMLObject(this.xmlDoc);
|
||||
break;
|
||||
case 6:
|
||||
raceXML = new RaceXMLObject(this.xmlDoc);
|
||||
break;
|
||||
case 7:
|
||||
boatXML = new BoatXMLObject(this.xmlDoc);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Boat XML Object from the data in a Regatta XML Message
|
||||
* @param doc XML Document Object
|
||||
* @return A new BoatXMLObject from the input Document.
|
||||
*/
|
||||
BoatXMLObject createBoatXML(Document doc) {
|
||||
return new BoatXMLObject(doc);
|
||||
}
|
||||
public RaceXMLObject getRaceXML() { return raceXML; }
|
||||
public RegattaXMLObject getRegattaXML() { return regattaXML; }
|
||||
public BoatXMLObject getBoatXML() { return boatXML; }
|
||||
|
||||
|
||||
/**
|
||||
* Returns the text content of a given child element tag, assuming it exists, as an Integer.
|
||||
@@ -129,7 +145,7 @@ class XMLParser {
|
||||
}
|
||||
}
|
||||
|
||||
class RegattaXMLObject {
|
||||
public class RegattaXMLObject {
|
||||
//Regatta Info
|
||||
private Integer regattaID;
|
||||
private String regattaName;
|
||||
@@ -163,7 +179,7 @@ class XMLParser {
|
||||
|
||||
}
|
||||
|
||||
class RaceXMLObject {
|
||||
public class RaceXMLObject {
|
||||
|
||||
// Race Info
|
||||
private Integer raceID;
|
||||
@@ -266,7 +282,7 @@ class XMLParser {
|
||||
public ArrayList<Corner> getCompoundMarkSequence() { return compoundMarkSequence; }
|
||||
public ArrayList<Limit> getCourseLimit() { return courseLimit; }
|
||||
|
||||
class Participant {
|
||||
public class Participant {
|
||||
Integer sourceID;
|
||||
String entry;
|
||||
|
||||
@@ -279,7 +295,7 @@ class XMLParser {
|
||||
public String getEntry() { return entry; }
|
||||
}
|
||||
|
||||
class CompoundMark {
|
||||
public class CompoundMark {
|
||||
private Integer markID;
|
||||
private String cMarkName;
|
||||
private ArrayList<Mark> marks;
|
||||
@@ -302,7 +318,7 @@ class XMLParser {
|
||||
public String getcMarkName() { return cMarkName; }
|
||||
public ArrayList<Mark> getMarks() { return marks; }
|
||||
|
||||
class Mark {
|
||||
public class Mark {
|
||||
private Integer seqID;
|
||||
private Integer sourceID;
|
||||
private String markName;
|
||||
@@ -310,13 +326,11 @@ class XMLParser {
|
||||
private Double targetLng;
|
||||
|
||||
Mark(Node markNode) {
|
||||
|
||||
this.seqID = getNodeAttributeInt(markNode, "SeqID");
|
||||
this.sourceID = getNodeAttributeInt(markNode, "SourceID");
|
||||
this.markName = getNodeAttributeString(markNode, "Name");
|
||||
this.targetLat = getNodeAttributeDouble(markNode, "TargetLat");
|
||||
this.targetLng = getNodeAttributeDouble(markNode, "TargetLng");
|
||||
|
||||
}
|
||||
|
||||
public Integer getSeqID() { return seqID; }
|
||||
@@ -327,7 +341,7 @@ class XMLParser {
|
||||
}
|
||||
}
|
||||
|
||||
class Corner {
|
||||
public class Corner {
|
||||
private Integer seqID;
|
||||
private Integer compoundMarkID;
|
||||
private String rounding;
|
||||
@@ -346,7 +360,7 @@ class XMLParser {
|
||||
public Integer getZoneSize() { return zoneSize; }
|
||||
}
|
||||
|
||||
class Limit {
|
||||
public class Limit {
|
||||
private Integer seqID;
|
||||
private Double lat;
|
||||
private Double lng;
|
||||
@@ -364,7 +378,7 @@ class XMLParser {
|
||||
|
||||
}
|
||||
|
||||
class BoatXMLObject {
|
||||
public class BoatXMLObject {
|
||||
|
||||
private String lastModified;
|
||||
private Integer version;
|
||||
@@ -429,7 +443,7 @@ class XMLParser {
|
||||
public ArrayList<Double> getZoneLimits() { return zoneLimits; }
|
||||
public ArrayList<Boat> getBoats() { return boats; }
|
||||
|
||||
class Boat {
|
||||
public class Boat {
|
||||
|
||||
private String boatType;
|
||||
private Integer sourceID;
|
||||
|
||||
Reference in New Issue
Block a user