From 0f4ad48de07daa911cc382dcaabdfd6fdd570a9c Mon Sep 17 00:00:00 2001 From: Kusal Ekanayake Date: Fri, 28 Apr 2017 16:41:35 +1200 Subject: [PATCH 1/8] Fixed and enables the old wakes. Enabled the fps counter by implementing the team-27s fps counter from their code, fixed trails from starting at the start of the startline no matter at what point in the race the stream is connected to (this is means the map starts a lot cleaner). Added live tracked speeds which are taken from the boat location packet. Linked the speeds coming in to their specified boats and allowed the onscreen speed tracker to keep up with the speeds. Linked the current speeds to the wakes so the wakes are redrawn for each change in speed and size to match the speed. Also added the toggle functionality back to the fps counter so they can be toggled on an off. #story[818] --- .../seng302/controllers/CanvasController.java | 33 +++++++++- .../controllers/RaceViewController.java | 1 + src/main/java/seng302/models/Boat.java | 1 + src/main/java/seng302/models/BoatGroup.java | 65 +++++++++++++------ src/main/java/seng302/models/Wake.java | 2 +- .../seng302/models/parsers/StreamParser.java | 16 ++++- src/main/resources/config/teams.xml | 12 ++-- src/main/resources/views/RaceView.fxml | 19 +++--- 8 files changed, 110 insertions(+), 39 deletions(-) diff --git a/src/main/java/seng302/controllers/CanvasController.java b/src/main/java/seng302/controllers/CanvasController.java index 37ef3193..dbce6849 100644 --- a/src/main/java/seng302/controllers/CanvasController.java +++ b/src/main/java/seng302/controllers/CanvasController.java @@ -22,6 +22,7 @@ import seng302.models.parsers.StreamParser; import seng302.models.parsers.StreamReceiver; import java.sql.Time; +import java.text.DecimalFormat; import java.util.*; /** @@ -59,6 +60,13 @@ public class CanvasController { private double metersToPixels; private List raceObjects = new ArrayList<>(); + //FRAME RATE + private static final double UPDATE_TIME = 0.016666; // 1 / 60 ie 60fps + private final long[] frameTimes = new long[30]; + private int frameTimeIndex = 0; + private boolean arrayFilled = false; + private DecimalFormat decimalFormat2dp = new DecimalFormat("0.00"); + public AnimationTimer timer; private enum ScaleDirection { @@ -138,6 +146,8 @@ public class CanvasController { timer = new AnimationTimer() { + + private int countdown = 60; private int[] currentRaceMarker = {1, 1, 1, 1, 1, 1}; List marks = raceViewController.getRace().getCourse(); @@ -150,6 +160,20 @@ public class CanvasController { int boatIndex = 0; Mark nextMark; + + long oldFrameTime = frameTimes[frameTimeIndex] ; + frameTimes[frameTimeIndex] = now ; + frameTimeIndex = (frameTimeIndex + 1) % frameTimes.length ; + if (frameTimeIndex == 0) { + arrayFilled = true ; + } + if (arrayFilled) { + long elapsedNanos = now - oldFrameTime ; + long elapsedNanosPerFrame = elapsedNanos / frameTimes.length ; + Double frameRate = 1_000_000_000.0 / elapsedNanosPerFrame ; + drawFps(frameRate.intValue()); + } + //if (countdown == 0) { //System.out.println("called the at"); for (RaceObject raceObject : raceObjects) { @@ -163,8 +187,6 @@ public class CanvasController { //descending = nextMark.getY() > boatGroup.getLayoutY(); //leftToRight = nextMark.getX() < boatGroup.getLayoutX(); - - raceObject.updatePosition(1000 / 60); for (int id : raceObject.getRaceIds()) { //System.out.println("id = " + id); @@ -285,10 +307,15 @@ public class CanvasController { private void drawFps(int fps){ if (raceViewController.isDisplayFps()){ + gc.clearRect(5,5,50,20); gc.setFill(Color.BLACK); gc.setFont(new Font(14)); gc.setLineWidth(3); gc.fillText(fps + " FPS", 5, 20); + } else { + gc.clearRect(5,5,50,20); + gc.setFill(Color.SKYBLUE); + gc.fillRect(4,4,51,21); } } @@ -307,7 +334,7 @@ public class CanvasController { for (Boat boat : boats) { BoatGroup boatGroup = new BoatGroup(boat, Colors.getColor()); boatGroup.moveTo(startingX, startingY, 0d); - boatGroup.setDestination(firstMarkX, firstMarkY); +// boatGroup.setDestination(firstMarkX, firstMarkY); boatGroup.forceRotation(); group.getChildren().add(boatGroup); raceObjects.add(boatGroup); diff --git a/src/main/java/seng302/controllers/RaceViewController.java b/src/main/java/seng302/controllers/RaceViewController.java index e781c8d3..214cf02f 100644 --- a/src/main/java/seng302/controllers/RaceViewController.java +++ b/src/main/java/seng302/controllers/RaceViewController.java @@ -5,6 +5,7 @@ import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; +import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.control.CheckBox; diff --git a/src/main/java/seng302/models/Boat.java b/src/main/java/seng302/models/Boat.java index d275091f..04ef14ab 100644 --- a/src/main/java/seng302/models/Boat.java +++ b/src/main/java/seng302/models/Boat.java @@ -140,4 +140,5 @@ public class Boat { public int getId() { return id; } + } \ No newline at end of file diff --git a/src/main/java/seng302/models/BoatGroup.java b/src/main/java/seng302/models/BoatGroup.java index 44f5aa1f..46887e5a 100644 --- a/src/main/java/seng302/models/BoatGroup.java +++ b/src/main/java/seng302/models/BoatGroup.java @@ -8,6 +8,7 @@ import javafx.scene.shape.Polygon; import javafx.scene.text.Text; import javafx.scene.transform.Rotate; import javafx.scene.transform.Translate; +import seng302.models.parsers.StreamParser; import java.util.ArrayList; import java.util.List; @@ -28,6 +29,7 @@ public class BoatGroup extends RaceObject{ private static double expectedUpdateInterval = 200; private static int WAKE_FRAME_INTERVAL = 30; private double framesForNewLine = 0; + private boolean destinationSet; private Point2D lastPoint; private Boat boat; @@ -35,7 +37,7 @@ public class BoatGroup extends RaceObject{ private List wakes = new ArrayList<>(); private List lines = new ArrayList<>(); private Polygon boatPoly; -// private Polygon wakePoly; + private Polygon wakePoly; private Text teamNameObject; private Text velocityObject; @@ -56,12 +58,12 @@ public class BoatGroup extends RaceObject{ // boatPoly.setLayoutY(0); // boatPoly.relocate(boatPoly.getLayoutX(), boatPoly.getLayoutY()); // -// wakePoly = new Polygon( -// 5.0,0.0, -// 10.0, boat.getVelocity() * VELOCITY_WAKE_RATIO, -// 0.0, boat.getVelocity() * VELOCITY_WAKE_RATIO -// ); -// wakePoly.setFill(Color.DARKBLUE); + wakePoly = new Polygon( + 5.0,0.0, + 10.0, boat.getVelocity() * VELOCITY_WAKE_RATIO, + 0.0, boat.getVelocity() * VELOCITY_WAKE_RATIO + ); + wakePoly.setFill(Color.DARKBLUE); teamNameObject = new Text(boat.getShortName()); velocityObject = new Text(String.valueOf(boat.getVelocity())); @@ -73,9 +75,8 @@ public class BoatGroup extends RaceObject{ velocityObject.setX(VELOCITY_X_OFFSET); velocityObject.setY(VELOCITY_Y_OFFSET); velocityObject.relocate(velocityObject.getX(), velocityObject.getY()); - -// super.getChildren().addAll(wakePoly, boatPoly, teamNameObject, velocityObject); - super.getChildren().addAll(teamNameObject, velocityObject, boatPoly); + destinationSet = false; + super.getChildren().addAll(wakePoly, boatPoly, teamNameObject, velocityObject); } private void initChildren (Color color) { @@ -97,8 +98,8 @@ public class BoatGroup extends RaceObject{ teamNameObject.setLayoutY(teamNameObject.getLayoutY() + dy); velocityObject.setLayoutX(velocityObject.getLayoutX() + dx); velocityObject.setLayoutY(velocityObject.getLayoutY() + dy); -// wakePoly.setLayoutX(wakePoly.getLayoutX() + dx); -// wakePoly.setLayoutY(wakePoly.getLayoutY() + dy); + wakePoly.setLayoutX(wakePoly.getLayoutX() + dx); + wakePoly.setLayoutY(wakePoly.getLayoutY() + dy); rotateTo(currentRotation); } @@ -119,8 +120,8 @@ public class BoatGroup extends RaceObject{ teamNameObject.setLayoutY(y); velocityObject.setLayoutX(x); velocityObject.setLayoutY(y); -// wakePoly.setLayoutX(x); -// wakePoly.setLayoutY(y); + wakePoly.setLayoutX(x); + wakePoly.setLayoutY(y); } public void updatePosition (double timeInterval) { @@ -158,9 +159,8 @@ public class BoatGroup extends RaceObject{ boatPoly.getLayoutX(), boatPoly.getLayoutY(), pixelVelocityX, - pixelVelocityY, rotation); -// wake.getTransforms().clear(); -// wake.getTransforms().add(new Rotate(rotation, 0, 0)); + pixelVelocityY, + rotation); super.getChildren().add(wake); wakes.add(wake); } @@ -174,13 +174,18 @@ public class BoatGroup extends RaceObject{ lines.add(l); super.getChildren().add(l); } - lastPoint = new Point2D(boatPoly.getLayoutX(), boatPoly.getLayoutY()); + if (destinationSet){ + lastPoint = new Point2D(boatPoly.getLayoutX(), boatPoly.getLayoutY()); + } } framesForNewLine -= 1; } public void setDestination (double newXValue, double newYValue, double rotation, int... raceIds) { //System.out.println("MADE IT"); + destinationSet = true; + boat.setVelocity(StreamParser.boatSpeeds.get((long)boat.getId())); + resizeWake(); if (hasRaceId(raceIds)) { this.pixelVelocityX = (newXValue - boatPoly.getLayoutX()) / expectedUpdateInterval; this.pixelVelocityY = (newYValue - boatPoly.getLayoutY()) / expectedUpdateInterval; @@ -191,6 +196,8 @@ public class BoatGroup extends RaceObject{ } public void setDestination (double newXValue, double newYValue, int... raceIDs) { + destinationSet = true; + if (hasRaceId(raceIDs)) { double rotation = Math.abs( Math.toDegrees( @@ -212,16 +219,34 @@ public class BoatGroup extends RaceObject{ } } + void resizeWake(){ + velocityObject.setText(String.valueOf(boat.getVelocity())); + super.getChildren().remove(wakePoly); + wakePoly = new Polygon( + 5.0,0.0, + 10.0, boat.getVelocity() * VELOCITY_WAKE_RATIO, + 0.0, boat.getVelocity() * VELOCITY_WAKE_RATIO + ); + wakePoly.setLayoutX(boatPoly.getLayoutX()); + wakePoly.setLayoutY(boatPoly.getLayoutY()); + wakePoly.setFill(Color.DARKBLUE); + super.getChildren().add(wakePoly); + + } + public void rotateTo (double rotation) { if(rotation != 0) { rotationalGoal = rotation; boatPoly.getTransforms().clear(); boatPoly.getTransforms().add(new Rotate(rotation, BOAT_WIDTH / 2, 0)); + wakePoly.getTransforms().clear(); + wakePoly.getTransforms().add(new Translate(0, BOAT_HEIGHT)); + wakePoly.getTransforms().add(new Rotate(rotation, BOAT_WIDTH/2, -BOAT_HEIGHT)); } -// wakePoly.getTransforms().clear(); -// wakePoly.getTransforms().add(new Rotate(rotation, 0, 0)); } + + public void forceRotation () { rotateTo (rotationalGoal); } diff --git a/src/main/java/seng302/models/Wake.java b/src/main/java/seng302/models/Wake.java index 92b2d30d..09b037ba 100644 --- a/src/main/java/seng302/models/Wake.java +++ b/src/main/java/seng302/models/Wake.java @@ -29,7 +29,7 @@ class Wake extends Arc { super.setType(ArcType.OPEN); super.setFill(new Color(0, 0, 0 ,0)); super.setStrokeWidth(2.0); - super.getTransforms().add(new Rotate(rotation, 5, -15)); + super.getTransforms().add(new Rotate(rotation - 270, startingX + 20, startingY + 20)); // this.velocityX = -velocityX; // this.velocityY = -velocityY; this.velocityX = 0; diff --git a/src/main/java/seng302/models/parsers/StreamParser.java b/src/main/java/seng302/models/parsers/StreamParser.java index f49e4d31..c917cd3c 100644 --- a/src/main/java/seng302/models/parsers/StreamParser.java +++ b/src/main/java/seng302/models/parsers/StreamParser.java @@ -14,6 +14,8 @@ 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.*; @@ -25,6 +27,7 @@ import java.util.concurrent.ConcurrentHashMap; public class StreamParser extends Thread{ public static ConcurrentHashMap boatPositions = new ConcurrentHashMap<>(); + public static ConcurrentHashMap boatSpeeds = new ConcurrentHashMap<>(); private static ArrayList boat_IDS = new ArrayList<>(); private String threadName; private Thread t; @@ -253,6 +256,8 @@ public class StreamParser extends Thread{ byte[] lonBytes = Arrays.copyOfRange(payload,20,24); byte[] boatIdBytes = Arrays.copyOfRange(payload,7,11); byte[] headingBytes = Arrays.copyOfRange(payload,28,30); + byte[] speedBytes = Arrays.copyOfRange(payload,38,40); + long timeStamp = extractTimeStamp(Arrays.copyOfRange(payload,1,7), 6); // int boatSeq = ByteBuffer.wrap(seqBytes).getInt(); long seq = bytesToLong(seqBytes); @@ -260,7 +265,13 @@ public class StreamParser extends Thread{ long lat = bytesToLong(latBytes); long lon = bytesToLong(lonBytes); long heading = bytesToLong(headingBytes); - +// long speed = extractTimeStamp(speedBytes, 2); + ByteBuffer bb = ByteBuffer.allocate(2); + bb.order(ByteOrder.LITTLE_ENDIAN); + bb.put(speedBytes[0]); + bb.put(speedBytes[1]); + double speed = bb.getShort(0)/1000.0; + short s = (short) ((speedBytes[1] & 0xFF) << 8 | (speedBytes[0] & 0xFF)); if ((int)deviceType == 1 || (int)deviceType == 4){ // System.out.println("boatId = " + boatId); // System.out.println("deviceType = " + (long)deviceType); @@ -268,13 +279,16 @@ public class StreamParser extends Thread{ //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.putIfAbsent(boatId, point); + boatSpeeds.putIfAbsent(boatId, speed); boatPositions.replace(boatId, point); + boatSpeeds.replace(boatId, speed); // System.out.println("lon = " + ((180d * (double)lon)/Math.pow(2,31))); // System.out.println("lat = " + ((180d *(double)lat)/Math.pow(2,31))); } } + private static void extractMarkRounding(StreamPacket packet){ byte[] payload = packet.getPayload(); int messageVersionNo = payload[0]; diff --git a/src/main/resources/config/teams.xml b/src/main/resources/config/teams.xml index f8d1e58d..0ac01cac 100644 --- a/src/main/resources/config/teams.xml +++ b/src/main/resources/config/teams.xml @@ -4,37 +4,37 @@ Oracle Team USA USA - 12.9 + 0.0 102 Artemis Racing ART - 13.1 + 0.0 101 Emirates Team New Zealand NZL - 15.6 + 0.0 103 Land Rover BAR BAR - 13.3 + 0.0 104 SoftBank Team Japan JAP - 14.7 + 0.0 105 Groupama Team France FRC - 11.4 + 0.0 106 \ No newline at end of file diff --git a/src/main/resources/views/RaceView.fxml b/src/main/resources/views/RaceView.fxml index f7fcbfeb..2cae1326 100644 --- a/src/main/resources/views/RaceView.fxml +++ b/src/main/resources/views/RaceView.fxml @@ -1,15 +1,18 @@ - - - - - - - + + + + + + + + + + - + From d204bee55df67a8b9e6f77a795ea101f64c3c304 Mon Sep 17 00:00:00 2001 From: Kusal Ekanayake Date: Fri, 28 Apr 2017 17:01:28 +1200 Subject: [PATCH 2/8] Started documentation on the stream parser. #story[820] --- .../seng302/models/parsers/StreamParser.java | 88 +++++++++++++------ 1 file changed, 63 insertions(+), 25 deletions(-) diff --git a/src/main/java/seng302/models/parsers/StreamParser.java b/src/main/java/seng302/models/parsers/StreamParser.java index c917cd3c..766296a7 100644 --- a/src/main/java/seng302/models/parsers/StreamParser.java +++ b/src/main/java/seng302/models/parsers/StreamParser.java @@ -1,12 +1,10 @@ package seng302.models.parsers; -import javafx.geometry.Point2D; import javafx.geometry.Point3D; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; -import sun.awt.UNIXToolkit; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -22,14 +20,16 @@ import java.util.*; import java.util.concurrent.ConcurrentHashMap; /** + * The purpose of this class is to take in the stream of divided packets so they can be read + * and parsed in by turning the byte arrays into useful data. There are two public static hashmaps + * that are threadsafe so the visualiser can always access the latest speed and position avaible * Created by kre39 on 23/04/17. */ public class StreamParser extends Thread{ public static ConcurrentHashMap boatPositions = new ConcurrentHashMap<>(); public static ConcurrentHashMap boatSpeeds = new ConcurrentHashMap<>(); - private static ArrayList boat_IDS = new ArrayList<>(); - private String threadName; + private String threadName; private Thread t; private static boolean raceStarted = false; @@ -37,7 +37,11 @@ public class StreamParser extends Thread{ this.threadName = threadName; } - public void run(){ + /** + * Used to within threading so when the stream parser thread runs, it will keep looking for a packet to + * process until it is unable to find anymore packets + */ + public void run(){ try { System.out.println("START OF STREAM"); while (StreamReceiver.packetBuffer == null || StreamReceiver.packetBuffer.size() < 1) { @@ -57,6 +61,9 @@ public class StreamParser extends Thread{ } } + /** + * Used to start the stream parser thread when multithreading + */ public void start () { System.out.println("Starting " + threadName ); if (t == null) { @@ -65,7 +72,7 @@ public class StreamParser extends Thread{ } } - static void parsePacket(StreamPacket packet) { + private static void parsePacket(StreamPacket packet) { switch (packet.getType()){ case HEARTBEAT: extractHeartBeat(packet); @@ -109,12 +116,20 @@ public class StreamParser extends Thread{ } } + /** + * Extracts the seq num used in the heartbeat packet + * @param packet Packet parsed in to use the payload + */ private static void extractHeartBeat(StreamPacket packet) { long heartbeat = bytesToLong(packet.getPayload()); -// System.out.println("Heartbeat: " + heartbeat); - } + /** + * Extracts the useful race status data from race status type packets. This method will also print to the + * console the current state of the race (if it has started/finished or is about to start), along side + * this it'll also display the amount of time since the race has started or time till it starts + * @param packet Packet parsed in to use the payload + */ private static void extractRaceStatus(StreamPacket packet){ byte[] payload = packet.getPayload(); int messageVersionNo = payload[0]; @@ -158,6 +173,10 @@ public class StreamParser extends Thread{ } } + /** + * Used to extract the messages passed through with the display message packet + * @param packet Packet parsed in to use the payload + */ private static void extractDisplayMessage(StreamPacket packet){ byte[] payload = packet.getPayload(); int messageVersionNo = payload[0]; @@ -172,7 +191,11 @@ public class StreamParser extends Thread{ } } - static void extractXmlMessage(StreamPacket packet){ + /** + * Used to read in the xml data. Will call the specific methods to create the course and boats + * @param packet Packet parsed in to use the payload + */ + private static void extractXmlMessage(StreamPacket packet){ byte[] payload = packet.getPayload(); String xmlMessage = ""; @@ -200,16 +223,17 @@ public class StreamParser extends Thread{ 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 e) { - e.printStackTrace(); - } catch (SAXException e) { - e.printStackTrace(); - } catch (IOException e) { + } catch (ParserConfigurationException | IOException | SAXException e) { e.printStackTrace(); } } + /** + * Extracts the race start status from the packet, currently is unused within the app but + * is here for potential future use + * @param packet Packet parsed in to use the payload + */ private static void extractRaceStartStatus(StreamPacket packet){ byte[] payload = packet.getPayload(); int messageVersionNo = payload[0]; @@ -219,6 +243,11 @@ public class StreamParser extends Thread{ int notificationType = payload[19]; } + /** + * When a yacht event occurs this will parse the byte array to retrieve the necessary info, + * currently unused + * @param packet Packet parsed in to use the payload + */ private static void extractYachtEventCode(StreamPacket packet){ byte[] payload = packet.getPayload(); int messageVersionNo = payload[0]; @@ -229,6 +258,11 @@ public class StreamParser extends Thread{ int eventId = payload[21]; } + /** + * When a yacht action occurs this will parse the parse the byte array to retrieve the necessary info, + * currently unused + * @param packet Packet parsed in to use the payload + */ private static void extractYachtActionCode(StreamPacket packet){ byte[] payload = packet.getPayload(); int messageVersionNo = payload[0]; @@ -239,6 +273,10 @@ public class StreamParser extends Thread{ // System.out.println("eventId = " + eventId); } + /** + * Strips the message from the chatter text type packets, currently the message is unused + * @param packet Packet parsed in to use the payload + */ private static void extractChatterText(StreamPacket packet){ byte[] payload = packet.getPayload(); int messageVersionNo = payload[0]; @@ -247,8 +285,12 @@ public class StreamParser extends Thread{ String message = new String(Arrays.copyOfRange(payload,3,3 + length)); } - - static void extractBoatLocation(StreamPacket packet){ + /** + * Used to breakdown the boatlocation packets so the boat coordinates, id and groundspeed are all used + * All the other extra data is still being read and translated however is unused. + * @param packet Packet parsed in to use the payload + */ + private static void extractBoatLocation(StreamPacket packet){ byte[] payload = packet.getPayload(); byte deviceType = payload[15]; byte[] seqBytes = Arrays.copyOfRange(payload,11,15); @@ -256,7 +298,7 @@ public class StreamParser extends Thread{ byte[] lonBytes = Arrays.copyOfRange(payload,20,24); byte[] boatIdBytes = Arrays.copyOfRange(payload,7,11); byte[] headingBytes = Arrays.copyOfRange(payload,28,30); - byte[] speedBytes = Arrays.copyOfRange(payload,38,40); + byte[] groundSpeedBytes = Arrays.copyOfRange(payload,38,40); long timeStamp = extractTimeStamp(Arrays.copyOfRange(payload,1,7), 6); // int boatSeq = ByteBuffer.wrap(seqBytes).getInt(); @@ -266,12 +308,8 @@ public class StreamParser extends Thread{ long lon = bytesToLong(lonBytes); long heading = bytesToLong(headingBytes); // long speed = extractTimeStamp(speedBytes, 2); - ByteBuffer bb = ByteBuffer.allocate(2); - bb.order(ByteOrder.LITTLE_ENDIAN); - bb.put(speedBytes[0]); - bb.put(speedBytes[1]); - double speed = bb.getShort(0)/1000.0; - short s = (short) ((speedBytes[1] & 0xFF) << 8 | (speedBytes[0] & 0xFF)); + 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); @@ -279,9 +317,9 @@ public class StreamParser extends Thread{ //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.putIfAbsent(boatId, point); - boatSpeeds.putIfAbsent(boatId, speed); + boatSpeeds.putIfAbsent(boatId, groundSpeed); boatPositions.replace(boatId, point); - boatSpeeds.replace(boatId, speed); + 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))); } From ffdfc24e65bc91e6ad58e25f472de4a604df4ed2 Mon Sep 17 00:00:00 2001 From: Zhi You Tan Date: Fri, 28 Apr 2017 20:09:17 +1200 Subject: [PATCH 3/8] Created a start screen with a timer which shows the race progress #story[572] --- .../java/seng302/controllers/Controller.java | 49 +++++++++++++++++++ .../seng302/models/parsers/StreamParser.java | 34 ++++++++++++- src/main/resources/views/MainView.fxml | 39 ++++++++++++++- 3 files changed, 119 insertions(+), 3 deletions(-) diff --git a/src/main/java/seng302/controllers/Controller.java b/src/main/java/seng302/controllers/Controller.java index 1892d472..94847735 100644 --- a/src/main/java/seng302/controllers/Controller.java +++ b/src/main/java/seng302/controllers/Controller.java @@ -1,14 +1,25 @@ package seng302.controllers; +import javafx.application.Platform; +import javafx.concurrent.Task; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; +import javafx.scene.control.Button; +import javafx.scene.control.Label; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.Pane; +import javafx.scene.paint.*; +import javafx.scene.paint.Color; +import seng302.models.parsers.StreamPacket; +import seng302.models.parsers.StreamParser; +import java.awt.*; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; +import java.util.Timer; +import java.util.TimerTask; /** * Created by michaelrausch on 21/03/17. @@ -16,6 +27,12 @@ import java.util.ResourceBundle; public class Controller implements Initializable { @FXML private AnchorPane contentPane; + @FXML + private Label timeTillLive; + @FXML + private Button streamButton; + @FXML + private Button switchToRaceViewButton; private void setContentPane(String jfxUrl){ try{ @@ -33,6 +50,38 @@ public class Controller implements Initializable { @Override public void initialize(URL location, ResourceBundle resources) { + + } + + public void startStream() { + if (StreamParser.isStreamStatus()) { + streamButton.setVisible(false); + timeTillLive.setVisible(true); + timeTillLive.setTextFill(Color.GREEN); + timeTillLive.setText("Connecting..."); + Timer timer = new Timer(); + timer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + Platform.runLater(() -> { + if (StreamParser.getTimeSinceStart() != 0) { + timeTillLive.setTextFill(Color.BLACK); + switchToRaceViewButton.setDisable(false); + Long timerMinute = -1 * StreamParser.getTimeSinceStart() / 60; + Long timerSecond = -1 * StreamParser.getTimeSinceStart() % 60; + String timerString = timerMinute + "." + timerSecond + " minutes"; + timeTillLive.setText(timerString); + } + }); + } + }, 0, 500); + } else { + timeTillLive.setText("Stream not available."); + timeTillLive.setTextFill(Color.RED); + } + } + + public void switchToRaceView() { setContentPane("/views/RaceView.fxml"); } } diff --git a/src/main/java/seng302/models/parsers/StreamParser.java b/src/main/java/seng302/models/parsers/StreamParser.java index 766296a7..32d2eb4e 100644 --- a/src/main/java/seng302/models/parsers/StreamParser.java +++ b/src/main/java/seng302/models/parsers/StreamParser.java @@ -29,9 +29,11 @@ public class StreamParser extends Thread{ public static ConcurrentHashMap boatPositions = new ConcurrentHashMap<>(); public static ConcurrentHashMap boatSpeeds = new ConcurrentHashMap<>(); - private String threadName; + private String threadName; private Thread t; private static boolean raceStarted = false; + private static boolean streamStatus = false; + private static long timeSinceStart = 0; public StreamParser(String threadName){ this.threadName = threadName; @@ -44,6 +46,7 @@ public class StreamParser extends Thread{ public void run(){ try { System.out.println("START OF STREAM"); + streamStatus = true; while (StreamReceiver.packetBuffer == null || StreamReceiver.packetBuffer.size() < 1) { Thread.sleep(1); } @@ -152,7 +155,7 @@ public class StreamParser extends Thread{ } if (timeTillStart % 10 == 0){ System.out.println("Time since start: " + -1 * timeTillStart + " Seconds"); - + timeSinceStart = timeTillStart; } } long windDir = bytesToLong(Arrays.copyOfRange(payload,18,20)); @@ -400,5 +403,32 @@ public class StreamParser extends Thread{ } return partialLong; } + + /** + * returns false if race not started, true otherwise + * + * @return race started status + */ + public static boolean isRaceStarted() { + return raceStarted; + } + + /** + * returns false if stream not connected, true otherwise + * + * @return stream started status + */ + public static boolean isStreamStatus() { + return streamStatus; + } + + /** + * returns race timer + * + * @return race timer in long + */ + public static long getTimeSinceStart() { + return timeSinceStart; + } } diff --git a/src/main/resources/views/MainView.fxml b/src/main/resources/views/MainView.fxml index ac0b944e..15559900 100644 --- a/src/main/resources/views/MainView.fxml +++ b/src/main/resources/views/MainView.fxml @@ -1,11 +1,48 @@ + + - + + + + + + + + + + + + + + + + +