diff --git a/src/main/java/seng302/models/BoatGroup.java b/src/main/java/seng302/models/BoatGroup.java index a262e61d..8171bdeb 100644 --- a/src/main/java/seng302/models/BoatGroup.java +++ b/src/main/java/seng302/models/BoatGroup.java @@ -146,21 +146,23 @@ public class BoatGroup extends RaceObject{ } public void setDestination (double newXValue, double newYValue, double rotation, int... raceIds) { - destinationSet = true; - boat.setVelocity(StreamParser.boatSpeeds.get((long)boat.getId())); - if (hasRaceId(raceIds)) { - this.pixelVelocityX = (newXValue - boatPoly.getLayoutX()) / expectedUpdateInterval; - this.pixelVelocityY = (newYValue - boatPoly.getLayoutY()) / expectedUpdateInterval; - this.rotationalGoal = rotation; - calculateRotationalVelocity(); - rotateTo(rotation); - if (wakeGenerationDelay > 0) { - wake.rotate(rotationalGoal); - wakeGenerationDelay--; - } else { - wake.setRotationalVelocity(rotationalVelocity, rotationalGoal, pixelVelocityX, pixelVelocityY); - } - } + moveGroupBy(newXValue - boatPoly.getLayoutX(), newYValue - boatPoly.getLayoutY(), rotation); + +// destinationSet = true; +// boat.setVelocity(StreamParser.boatSpeeds.get((long)boat.getId())); +// if (hasRaceId(raceIds)) { +// this.pixelVelocityX = (newXValue - boatPoly.getLayoutX()) / expectedUpdateInterval; +// this.pixelVelocityY = (newYValue - boatPoly.getLayoutY()) / expectedUpdateInterval; +// this.rotationalGoal = rotation; +// calculateRotationalVelocity(); +// rotateTo(rotation); +// if (wakeGenerationDelay > 0) { +// wake.rotate(rotationalGoal); +// wakeGenerationDelay--; +// } else { +// wake.setRotationalVelocity(rotationalVelocity, rotationalGoal, pixelVelocityX, pixelVelocityY); +// } +// } } public void setDestination (double newXValue, double newYValue, int... raceIDs) { diff --git a/src/main/java/seng302/models/parsers/StreamParser.java b/src/main/java/seng302/models/parsers/StreamParser.java index 7cbfc90d..3c7890c8 100644 --- a/src/main/java/seng302/models/parsers/StreamParser.java +++ b/src/main/java/seng302/models/parsers/StreamParser.java @@ -5,6 +5,8 @@ import javafx.geometry.Point3D; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; +import seng302.models.parsers.packets.BoatPositionPacket; +import seng302.models.parsers.packets.StreamPacket; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -12,12 +14,11 @@ import javax.xml.parsers.ParserConfigurationException; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.StringReader; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.PriorityBlockingQueue; /** * The purpose of this class is to take in the stream of divided packets so they can be read @@ -27,14 +28,17 @@ import java.util.concurrent.ConcurrentHashMap; */ public class StreamParser extends Thread{ - public static ConcurrentHashMap boatPositions = new ConcurrentHashMap<>(); - public static ConcurrentHashMap boatSpeeds = new ConcurrentHashMap<>(); + public static ConcurrentHashMap> boatPositions = new ConcurrentHashMap<>(); private String threadName; private Thread t; private static boolean raceStarted = false; + + + + public StreamParser(String threadName){ - this.threadName = threadName; + this.threadName = threadName; } /** @@ -313,28 +317,31 @@ public class StreamParser extends Thread{ byte[] headingBytes = Arrays.copyOfRange(payload,28,30); byte[] groundSpeedBytes = Arrays.copyOfRange(payload,38,40); - long timeStamp = extractTimeStamp(Arrays.copyOfRange(payload,1,7), 6); + long timeValid = extractTimeStamp(Arrays.copyOfRange(payload,1,7), 6); // int boatSeq = ByteBuffer.wrap(seqBytes).getInt(); long seq = bytesToLong(seqBytes); long boatId = bytesToLong(boatIdBytes); - long lat = bytesToLong(latBytes); - long lon = bytesToLong(lonBytes); + long rawLat = bytesToLong(latBytes); + long rawLon = bytesToLong(lonBytes); + double lat = ((180d * (double)rawLat)/Math.pow(2,31)); + double lon = ((180d *(double)rawLon)/Math.pow(2,31)); long heading = bytesToLong(headingBytes); // long speed = extractTimeStamp(speedBytes, 2); double groundSpeed = bytesToLong(groundSpeedBytes)/1000.0; short s = (short) ((groundSpeedBytes[1] & 0xFF) << 8 | (groundSpeedBytes[0] & 0xFF)); - if ((int)deviceType == 1 || (int)deviceType == 4){ -// System.out.println("boatId = " + boatId); -// System.out.println("deviceType = " + (long)deviceType); -// System.out.println("seq = " + seq); - //needs to be validated - Point3D point = new Point3D(((180d * (double)lat)/Math.pow(2,31)),((180d *(double)lon)/Math.pow(2,31)),(double)heading); - boatPositions.put(boatId, point); - boatSpeeds.put(boatId, groundSpeed); -// boatPositions.replace(boatId, point); -// boatSpeeds.replace(boatId, groundSpeed); -// System.out.println("lon = " + ((180d * (double)lon)/Math.pow(2,31))); -// System.out.println("lat = " + ((180d *(double)lat)/Math.pow(2,31))); + if ((int)deviceType == 1){ + + BoatPositionPacket boatPacket = new BoatPositionPacket(boatId, timeValid, lat, lon, heading, groundSpeed); + + if (!boatPositions.containsKey(boatId)){ + boatPositions.put(boatId, new PriorityBlockingQueue(256, new Comparator() { + @Override + public int compare(BoatPositionPacket p1, BoatPositionPacket p2) { + return (int) (p1.getTimeValid() - p2.getTimeValid()); + } + })); + } + boatPositions.get(boatId).put(boatPacket); } } diff --git a/src/main/java/seng302/models/parsers/StreamReceiver.java b/src/main/java/seng302/models/parsers/StreamReceiver.java index 74ce4748..7636b6ae 100644 --- a/src/main/java/seng302/models/parsers/StreamReceiver.java +++ b/src/main/java/seng302/models/parsers/StreamReceiver.java @@ -1,5 +1,7 @@ package seng302.models.parsers; +import seng302.models.parsers.packets.StreamPacket; + import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; diff --git a/src/main/java/seng302/models/parsers/packets/BoatPositionPacket.java b/src/main/java/seng302/models/parsers/packets/BoatPositionPacket.java new file mode 100644 index 00000000..b2f07c5c --- /dev/null +++ b/src/main/java/seng302/models/parsers/packets/BoatPositionPacket.java @@ -0,0 +1,23 @@ +package seng302.models.parsers.packets; + +public class BoatPositionPacket { + private long boatId; + private long timeValid; + private double lat; + private double lon; + private double heading; + private double groundSpeed; + + public BoatPositionPacket(long boatId, long timeValid, double lat, double lon, double heading, double groundSpeed) { + this.boatId = boatId; + this.timeValid = timeValid; + this.lat = lat; + this.lon = lon; + this.heading = heading; + this.groundSpeed = groundSpeed; + } + + public long getTimeValid() { + return timeValid; + } +} diff --git a/src/main/java/seng302/models/parsers/packets/PacketType.java b/src/main/java/seng302/models/parsers/packets/PacketType.java new file mode 100644 index 00000000..f522dec9 --- /dev/null +++ b/src/main/java/seng302/models/parsers/packets/PacketType.java @@ -0,0 +1,53 @@ +package seng302.models.parsers.packets; + +/** + * Created by Kusal on 4/24/2017. + */ +public enum PacketType { + HEARTBEAT, + RACE_STATUS, + DISPLAY_TEXT_MESSAGE, + XML_MESSAGE, + RACE_START_STATUS, + YACHT_EVENT_CODE, + YACHT_ACTION_CODE, + CHATTER_TEXT, + BOAT_LOCATION, + MARK_ROUNDING, + COURSE_WIND, + AVG_WIND, + OTHER; + + public static PacketType assignPacketType(int packetType){ + switch(packetType){ + case 1: + return HEARTBEAT; + case 12: + return RACE_STATUS; + case 20: + return DISPLAY_TEXT_MESSAGE; + case 26: + return XML_MESSAGE; + case 27: + return RACE_START_STATUS; + case 29: + return YACHT_EVENT_CODE; + case 31: + return YACHT_ACTION_CODE; + case 36: + return CHATTER_TEXT; + case 37: + return BOAT_LOCATION; + case 38: + return MARK_ROUNDING; + case 44: + return COURSE_WIND; + case 47: + return AVG_WIND; + default: + } + return OTHER; + } + + +} diff --git a/src/main/java/seng302/models/parsers/packets/StreamPacket.java b/src/main/java/seng302/models/parsers/packets/StreamPacket.java new file mode 100644 index 00000000..3b5c4faa --- /dev/null +++ b/src/main/java/seng302/models/parsers/packets/StreamPacket.java @@ -0,0 +1,44 @@ +package seng302.models.parsers.packets; + +/** + * Created by kre39 on 23/04/17. + */ +public class StreamPacket { + + //Change int to an ENUM for the type + private PacketType type; + + private long messageLength; + private long timeStamp; + private byte[] payload; + + StreamPacket(int type, long messageLength, long timeStamp, byte[] payload) { + this.type = PacketType.assignPacketType(type); + this.messageLength = messageLength; + this.timeStamp = timeStamp; + this.payload = payload; +// System.out.println("type = " + this.type.toString()); + //switch the packet type to deal with what ever specific packet you want to deal with +// if (this.type == PacketType.XML_MESSAGE){ +// //System.out.println("--------"); +// System.out.println(new String(payload)); +// StreamParser.parsePacket(this); +// } + } + + public PacketType getType() { + return type; + } + + public long getMessageLength() { + return messageLength; + } + + public byte[] getPayload() { + return payload; + } + + public long getTimeStamp() { + return timeStamp; + } +}