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 @@ + + - + + + + + + + + + + + + + + + + +