mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Linking up course stream with visualiser. Boats moving, and course drawing. Boats however are not moving as intended. Needs to be fixed/looked into.
#story[820] #pair[kre39,cir27]
This commit is contained in:
@@ -4,6 +4,7 @@ import javafx.animation.*;
|
|||||||
import javafx.beans.property.SimpleDoubleProperty;
|
import javafx.beans.property.SimpleDoubleProperty;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.geometry.Point2D;
|
import javafx.geometry.Point2D;
|
||||||
|
import javafx.geometry.Point3D;
|
||||||
import javafx.scene.Group;
|
import javafx.scene.Group;
|
||||||
import javafx.scene.canvas.Canvas;
|
import javafx.scene.canvas.Canvas;
|
||||||
import javafx.scene.canvas.GraphicsContext;
|
import javafx.scene.canvas.GraphicsContext;
|
||||||
@@ -18,6 +19,8 @@ import seng302.models.mark.GateMark;
|
|||||||
import seng302.models.mark.Mark;
|
import seng302.models.mark.Mark;
|
||||||
import seng302.models.mark.MarkType;
|
import seng302.models.mark.MarkType;
|
||||||
import seng302.models.mark.SingleMark;
|
import seng302.models.mark.SingleMark;
|
||||||
|
import seng302.models.parsers.StreamParser;
|
||||||
|
import seng302.models.parsers.StreamReceiver;
|
||||||
|
|
||||||
import java.sql.Time;
|
import java.sql.Time;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -132,8 +135,12 @@ public class CanvasController {
|
|||||||
//}catch (Exception e) {
|
//}catch (Exception e) {
|
||||||
// e.printStackTrace();
|
// e.printStackTrace();
|
||||||
//}
|
//}
|
||||||
timer = new AnimationTimer() {
|
|
||||||
|
|
||||||
|
StreamReceiver sr = new StreamReceiver("csse-s302staff.canterbury.ac.nz", 4941,"TestThread1");
|
||||||
|
// StreamReceiver sr = new StreamReceiver("livedata.americascup.com", 4941, "TestThread1");
|
||||||
|
sr.start();
|
||||||
|
|
||||||
|
timer = new AnimationTimer() {
|
||||||
private int countdown = 60;
|
private int countdown = 60;
|
||||||
private int[] currentRaceMarker = {1, 1, 1, 1, 1, 1};
|
private int[] currentRaceMarker = {1, 1, 1, 1, 1, 1};
|
||||||
List<Mark> marks = raceViewController.getRace().getCourse();
|
List<Mark> marks = raceViewController.getRace().getCourse();
|
||||||
@@ -144,44 +151,56 @@ public class CanvasController {
|
|||||||
boolean descending;
|
boolean descending;
|
||||||
boolean leftToRight;
|
boolean leftToRight;
|
||||||
int boatIndex = 0;
|
int boatIndex = 0;
|
||||||
|
|
||||||
Mark nextMark;
|
Mark nextMark;
|
||||||
if (countdown == 0) {
|
//if (countdown == 0) {
|
||||||
for (BoatGroup boatGroup : boatGroups) {
|
for (BoatGroup boatGroup : boatGroups) {
|
||||||
if (currentRaceMarker[boatIndex] < marks.size()) {
|
//if (currentRaceMarker[boatIndex] < marks.size()) {
|
||||||
if (currentRaceMarker[boatIndex] == 6) {
|
//if (currentRaceMarker[boatIndex] == 6) {
|
||||||
int debugLine = 4;
|
// int debugLine = 4;
|
||||||
}
|
//}
|
||||||
double xb4 = boatGroup.getLayoutX();
|
double xb4 = boatGroup.getLayoutX();
|
||||||
double yb4 = boatGroup.getLayoutY();
|
double yb4 = boatGroup.getLayoutY();
|
||||||
nextMark = marks.get(currentRaceMarker[boatIndex]);
|
nextMark = marks.get(currentRaceMarker[boatIndex]);
|
||||||
|
|
||||||
descending = nextMark.getY() > boatGroup.getLayoutY();
|
//descending = nextMark.getY() > boatGroup.getLayoutY();
|
||||||
leftToRight = nextMark.getX() < boatGroup.getLayoutX();
|
//leftToRight = nextMark.getX() < boatGroup.getLayoutX();
|
||||||
|
|
||||||
boatGroup.updatePosition(1000 / 60);
|
boatGroup.updatePosition(1000 / 6);
|
||||||
// Point2D p = latLonToXY(lat, lon);
|
Point3D p = StreamParser.boatPositions.get((long)boatGroup.getBoat().getId());
|
||||||
// boatGroup.setDestination(p.getX(), p.getY());
|
//System.out.println("boatGroup = " + boatGroup.getBoat().getId());
|
||||||
if (descending && nextMark.getY() < boatGroup.getLayoutY()) {
|
//System.out.println("StreamParser.boatPositions.toString() = " + StreamParser.boatPositions.toString());
|
||||||
currentRaceMarker[boatIndex]++;
|
if (p != null) {
|
||||||
boatGroup.setDestination(
|
Point2D p2d = latLonToXY(p.getX(), p.getY());
|
||||||
marks.get(currentRaceMarker[boatIndex]).getX(), marks.get(currentRaceMarker[boatIndex]).getY()
|
System.out.println("p2d = " + p2d);
|
||||||
);
|
if (!boatGroup.getBoat().isSamePos(p2d)) {
|
||||||
} else if (!descending && nextMark.getY() > boatGroup.getLayoutY()) {
|
//System.out.println("p.toString() = " + p.toString());
|
||||||
currentRaceMarker[boatIndex]++;
|
double heading = 360.0 / 0xffff * p.getZ();
|
||||||
boatGroup.setDestination(
|
System.out.println("heading = " + heading);
|
||||||
marks.get(currentRaceMarker[boatIndex]).getX(), marks.get(currentRaceMarker[boatIndex]).getY()
|
boatGroup.setDestination(p.getX(), p.getY(), heading);
|
||||||
);
|
|
||||||
} else if (leftToRight && nextMark.getX() > boatGroup.getLayoutX()) {
|
|
||||||
currentRaceMarker[boatIndex]++;
|
|
||||||
boatGroup.setDestination(
|
|
||||||
marks.get(currentRaceMarker[boatIndex]).getX(), marks.get(currentRaceMarker[boatIndex]).getY()
|
|
||||||
);
|
|
||||||
} else if (!leftToRight && nextMark.getX() < boatGroup.getLayoutX()) {
|
|
||||||
currentRaceMarker[boatIndex]++;
|
|
||||||
boatGroup.setDestination(
|
|
||||||
marks.get(currentRaceMarker[boatIndex]).getX(), marks.get(currentRaceMarker[boatIndex]).getY()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
// if (descending && nextMark.getY() < boatGroup.getLayoutY()) {
|
||||||
|
// currentRaceMarker[boatIndex]++;
|
||||||
|
// boatGroup.setDestination(
|
||||||
|
// marks.get(currentRaceMarker[boatIndex]).getX(), marks.get(currentRaceMarker[boatIndex]).getY()
|
||||||
|
// );
|
||||||
|
// } else if (!descending && nextMark.getY() > boatGroup.getLayoutY()) {
|
||||||
|
// currentRaceMarker[boatIndex]++;
|
||||||
|
// boatGroup.setDestination(
|
||||||
|
// marks.get(currentRaceMarker[boatIndex]).getX(), marks.get(currentRaceMarker[boatIndex]).getY()
|
||||||
|
// );
|
||||||
|
// } else if (leftToRight && nextMark.getX() > boatGroup.getLayoutX()) {
|
||||||
|
// currentRaceMarker[boatIndex]++;
|
||||||
|
// boatGroup.setDestination(
|
||||||
|
// marks.get(currentRaceMarker[boatIndex]).getX(), marks.get(currentRaceMarker[boatIndex]).getY()
|
||||||
|
// );
|
||||||
|
// } else if (!leftToRight && nextMark.getX() < boatGroup.getLayoutX()) {
|
||||||
|
// currentRaceMarker[boatIndex]++;
|
||||||
|
// boatGroup.setDestination(
|
||||||
|
// marks.get(currentRaceMarker[boatIndex]).getX(), marks.get(currentRaceMarker[boatIndex]).getY()
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
double xnew = boatGroup.getLayoutX();
|
double xnew = boatGroup.getLayoutX();
|
||||||
double ynew = boatGroup.getLayoutY();
|
double ynew = boatGroup.getLayoutY();
|
||||||
@@ -190,18 +209,19 @@ public class CanvasController {
|
|||||||
raceFinished = false;
|
raceFinished = false;
|
||||||
boatIndex++;
|
boatIndex++;
|
||||||
}
|
}
|
||||||
}
|
//}
|
||||||
if (raceFinished) {
|
//if (raceFinished) {
|
||||||
System.out.println("DONZEO LADS");
|
// System.out.println("DONZEO LADS");
|
||||||
this.stop();
|
// this.stop();
|
||||||
}
|
//}
|
||||||
} else {
|
//} else {
|
||||||
countdown--;
|
// countdown--;
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
for (Mark m : raceViewController.getRace().getCourse())
|
for (Mark m : raceViewController.getRace().getCourse()) {
|
||||||
System.out.println(m.getName());
|
System.out.println(m.getName());
|
||||||
|
}
|
||||||
//timer.start();
|
//timer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,7 +281,7 @@ public class CanvasController {
|
|||||||
for (Boat boat : boats) {
|
for (Boat boat : boats) {
|
||||||
BoatGroup boatGroup = new BoatGroup(boat, Colors.getColor());
|
BoatGroup boatGroup = new BoatGroup(boat, Colors.getColor());
|
||||||
boatGroup.moveBoatTo(startingX, startingY, 0d);
|
boatGroup.moveBoatTo(startingX, startingY, 0d);
|
||||||
boatGroup.setDestination(firstMarkX, firstMarkY);
|
// boatGroup.setDestination(firstMarkX, firstMarkY);
|
||||||
boatGroup.forceRotation();
|
boatGroup.forceRotation();
|
||||||
group.getChildren().add(boatGroup);
|
group.getChildren().add(boatGroup);
|
||||||
boatGroups.add(boatGroup);
|
boatGroups.add(boatGroup);
|
||||||
@@ -515,6 +535,7 @@ public class CanvasController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Point2D findScaledXY (Mark unscaled) {
|
private Point2D findScaledXY (Mark unscaled) {
|
||||||
|
System.out.println("unscaled.getName() = " + unscaled.getName());
|
||||||
return findScaledXY (minLatPoint.getLatitude(), minLatPoint.getLongitude(),
|
return findScaledXY (minLatPoint.getLatitude(), minLatPoint.getLongitude(),
|
||||||
unscaled.getLatitude(), unscaled.getLongitude());
|
unscaled.getLatitude(), unscaled.getLongitude());
|
||||||
}
|
}
|
||||||
@@ -527,15 +548,36 @@ public class CanvasController {
|
|||||||
|
|
||||||
angleFromReference = Mark.calculateHeadingRad(latA, lonA, latB, lonB);
|
angleFromReference = Mark.calculateHeadingRad(latA, lonA, latB, lonB);
|
||||||
distanceFromReference = Mark.calculateDistance(latA, lonA, latB, lonB);
|
distanceFromReference = Mark.calculateDistance(latA, lonA, latB, lonB);
|
||||||
|
if (angleFromReference >= 0 && angleFromReference <= Math.PI / 2) {
|
||||||
if (angleFromReference > (Math.PI / 2)) {
|
//System.out.println("1");
|
||||||
angleFromReference = (Math.PI * 2) - angleFromReference;
|
|
||||||
xAxisLocation -= (int) Math.round(distanceScaleFactor * Math.sin(angleFromReference) * distanceFromReference);
|
|
||||||
} else {
|
|
||||||
xAxisLocation += (int) Math.round(distanceScaleFactor * Math.sin(angleFromReference) * distanceFromReference);
|
xAxisLocation += (int) Math.round(distanceScaleFactor * Math.sin(angleFromReference) * distanceFromReference);
|
||||||
}
|
|
||||||
yAxisLocation -= (int) Math.round(distanceScaleFactor * Math.cos(angleFromReference) * distanceFromReference);
|
yAxisLocation -= (int) Math.round(distanceScaleFactor * Math.cos(angleFromReference) * distanceFromReference);
|
||||||
|
} else if (angleFromReference >= 0) {
|
||||||
|
//System.out.println("2");
|
||||||
|
angleFromReference = angleFromReference - Math.PI / 2;
|
||||||
|
xAxisLocation += (int) Math.round(distanceScaleFactor * Math.cos(angleFromReference) * distanceFromReference);
|
||||||
|
yAxisLocation += (int) Math.round(distanceScaleFactor * Math.sin(angleFromReference) * distanceFromReference);
|
||||||
|
} else if (angleFromReference < 0 && angleFromReference >= -Math.PI / 2) {
|
||||||
|
//System.out.println("3");
|
||||||
|
System.out.println(distanceFromReference);
|
||||||
|
angleFromReference = Math.abs(angleFromReference);
|
||||||
|
System.out.println(Math.cos(angleFromReference) * distanceFromReference);
|
||||||
|
xAxisLocation -= (int) Math.round(distanceScaleFactor * Math.sin(angleFromReference) * distanceFromReference);
|
||||||
|
yAxisLocation -= (int) Math.round(distanceScaleFactor * Math.cos(angleFromReference) * distanceFromReference);
|
||||||
|
} else {
|
||||||
|
//System.out.println("4");
|
||||||
|
angleFromReference = Math.abs(angleFromReference) - Math.PI / 2;
|
||||||
|
xAxisLocation -= (int) Math.round(distanceScaleFactor * Math.cos(angleFromReference) * distanceFromReference);
|
||||||
|
yAxisLocation += (int) Math.round(distanceScaleFactor * Math.sin(angleFromReference) * distanceFromReference);
|
||||||
|
}
|
||||||
|
// if (angleFromReference > (Math.PI / 2)) {
|
||||||
|
// angleFromReference = (Math.PI * 2) - angleFromReference;
|
||||||
|
// xAxisLocation -= (int) Math.round(distanceScaleFactor * Math.sin(angleFromReference) * distanceFromReference);
|
||||||
|
// } else {
|
||||||
|
// xAxisLocation += (int) Math.round(distanceScaleFactor * Math.sin(angleFromReference) * distanceFromReference);
|
||||||
|
// }
|
||||||
|
// yAxisLocation -= (int) Math.round(distanceScaleFactor * Math.cos(angleFromReference) * distanceFromReference);
|
||||||
|
//System.out.println(xAxisLocation + " *** " + yAxisLocation);
|
||||||
return new Point2D(xAxisLocation, yAxisLocation);
|
return new Point2D(xAxisLocation, yAxisLocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import java.util.*;
|
|||||||
/**
|
/**
|
||||||
* Created by ptg19 on 29/03/17.
|
* Created by ptg19 on 29/03/17.
|
||||||
*/
|
*/
|
||||||
public class RaceViewController {
|
public class RaceViewController extends Thread{
|
||||||
@FXML
|
@FXML
|
||||||
private VBox positionVbox;
|
private VBox positionVbox;
|
||||||
@FXML
|
@FXML
|
||||||
@@ -48,6 +48,7 @@ public class RaceViewController {
|
|||||||
private Race race;
|
private Race race;
|
||||||
|
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
|
|
||||||
RaceController raceController = new RaceController();
|
RaceController raceController = new RaceController();
|
||||||
raceController.initializeRace();
|
raceController.initializeRace();
|
||||||
race = raceController.getRace();
|
race = raceController.getRace();
|
||||||
@@ -73,6 +74,8 @@ public class RaceViewController {
|
|||||||
includedCanvasController.timer.start();
|
includedCanvasController.timer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void initializeSettings(){
|
private void initializeSettings(){
|
||||||
displayAnnotations = true;
|
displayAnnotations = true;
|
||||||
displayFps = true;
|
displayFps = true;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package seng302.models;
|
package seng302.models;
|
||||||
|
|
||||||
|
import javafx.geometry.Point2D;
|
||||||
import javafx.scene.paint.Color;
|
import javafx.scene.paint.Color;
|
||||||
import javafx.scene.shape.Polygon;
|
import javafx.scene.shape.Polygon;
|
||||||
import javafx.scene.text.Text;
|
import javafx.scene.text.Text;
|
||||||
@@ -19,6 +20,8 @@ public class Boat {
|
|||||||
private double heading;
|
private double heading;
|
||||||
private int markLastPast;
|
private int markLastPast;
|
||||||
private String shortName;
|
private String shortName;
|
||||||
|
private int id;
|
||||||
|
private Point2D currentPos;
|
||||||
|
|
||||||
public Boat(String teamName) {
|
public Boat(String teamName) {
|
||||||
this.teamName = teamName;
|
this.teamName = teamName;
|
||||||
@@ -26,6 +29,16 @@ public class Boat {
|
|||||||
this.lat = 0.0;
|
this.lat = 0.0;
|
||||||
this.lon = 0.0;
|
this.lon = 0.0;
|
||||||
this.shortName = "";
|
this.shortName = "";
|
||||||
|
currentPos = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSamePos(Point2D newPos){
|
||||||
|
if(newPos.equals(currentPos)){
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
currentPos = newPos;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -35,10 +48,13 @@ public class Boat {
|
|||||||
* @param boatVelocity The speed of the boat in meters/second
|
* @param boatVelocity The speed of the boat in meters/second
|
||||||
* @param shortName A shorter version of the teams name
|
* @param shortName A shorter version of the teams name
|
||||||
*/
|
*/
|
||||||
public Boat(String teamName, double boatVelocity, String shortName) {
|
public Boat(String teamName, double boatVelocity, String shortName, int id) {
|
||||||
this.teamName = teamName;
|
this.teamName = teamName;
|
||||||
this.velocity = boatVelocity;
|
this.velocity = boatVelocity;
|
||||||
this.shortName = shortName;
|
this.shortName = shortName;
|
||||||
|
this.id = id;
|
||||||
|
currentPos = null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -133,4 +149,7 @@ public class Boat {
|
|||||||
return this.shortName;
|
return this.shortName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -117,6 +117,15 @@ public class BoatGroup extends Group{
|
|||||||
moveBy(dx, dy, rotation);
|
moveBy(dx, dy, rotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setDestination (double newXValue, double newYValue, double rotation) {
|
||||||
|
this.pixelVelocityX = (newXValue - super.getLayoutX()) / expectedUpdateInterval;
|
||||||
|
this.pixelVelocityY = (newYValue - super.getLayoutY()) / expectedUpdateInterval;
|
||||||
|
//this.destinationX = newXValue;
|
||||||
|
//this.destinationY = newYValue;
|
||||||
|
this.rotationalGoal = rotation;
|
||||||
|
this.rotationalVelocity = (rotationalGoal - currentRotation) / expectedUpdateInterval;
|
||||||
|
}
|
||||||
|
|
||||||
public void setDestination (double newXValue, double newYValue) {
|
public void setDestination (double newXValue, double newYValue) {
|
||||||
this.pixelVelocityX = (newXValue - super.getLayoutX()) / expectedUpdateInterval;
|
this.pixelVelocityX = (newXValue - super.getLayoutX()) / expectedUpdateInterval;
|
||||||
this.pixelVelocityY = (newYValue - super.getLayoutY()) / expectedUpdateInterval;
|
this.pixelVelocityY = (newYValue - super.getLayoutY()) / expectedUpdateInterval;
|
||||||
@@ -174,4 +183,8 @@ public class BoatGroup extends Group{
|
|||||||
super.getChildren().get(2).setVisible(false);
|
super.getChildren().get(2).setVisible(false);
|
||||||
super.getChildren().get(3).setVisible(false);
|
super.getChildren().get(3).setVisible(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Boat getBoat() {
|
||||||
|
return boat;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,11 +19,11 @@ public class StreamPacket {
|
|||||||
this.payload = payload;
|
this.payload = payload;
|
||||||
// System.out.println("type = " + this.type.toString());
|
// System.out.println("type = " + this.type.toString());
|
||||||
//switch the packet type to deal with what ever specific packet you want to deal with
|
//switch the packet type to deal with what ever specific packet you want to deal with
|
||||||
if (this.type == PacketType.XML_MESSAGE){
|
// if (this.type == PacketType.XML_MESSAGE){
|
||||||
System.out.println("--------");
|
// System.out.println("--------");
|
||||||
System.out.println(new String(payload));
|
// System.out.println(new String(payload));
|
||||||
StreamParser.parsePacket(this);
|
// StreamParser.parsePacket(this);
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
PacketType getType() {
|
PacketType getType() {
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package seng302.models.parsers;
|
package seng302.models.parsers;
|
||||||
|
|
||||||
|
|
||||||
|
import javafx.geometry.Point2D;
|
||||||
|
import javafx.geometry.Point3D;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.xml.sax.InputSource;
|
import org.xml.sax.InputSource;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
@@ -13,13 +15,48 @@ import java.io.IOException;
|
|||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by kre39 on 23/04/17.
|
* Created by kre39 on 23/04/17.
|
||||||
*/
|
*/
|
||||||
public class StreamParser {
|
public class StreamParser extends Thread{
|
||||||
|
|
||||||
|
public static ConcurrentHashMap<Long,Point3D> boatPositions = new ConcurrentHashMap<>();
|
||||||
private static ArrayList<Long> boat_IDS = new ArrayList<>();
|
private static ArrayList<Long> boat_IDS = new ArrayList<>();
|
||||||
|
private String threadName;
|
||||||
|
private Thread t;
|
||||||
|
|
||||||
|
StreamParser(String threadName){
|
||||||
|
this.threadName = threadName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run(){
|
||||||
|
try {
|
||||||
|
while (StreamReceiver.packetBuffer.size() <= 1) {
|
||||||
|
Thread.sleep(1);
|
||||||
|
}
|
||||||
|
StreamPacket packet = StreamReceiver.packetBuffer.take();
|
||||||
|
while (packet != null){
|
||||||
|
parsePacket(packet);
|
||||||
|
Thread.sleep(10);
|
||||||
|
packet = StreamReceiver.packetBuffer.take();
|
||||||
|
}
|
||||||
|
} catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start () {
|
||||||
|
System.out.println("Starting " + threadName );
|
||||||
|
if (t == null) {
|
||||||
|
t = new Thread (this, threadName);
|
||||||
|
t.start ();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void parsePacket(StreamPacket packet) {
|
static void parsePacket(StreamPacket packet) {
|
||||||
switch (packet.getType()){
|
switch (packet.getType()){
|
||||||
case HEARTBEAT:
|
case HEARTBEAT:
|
||||||
@@ -65,7 +102,7 @@ public class StreamParser {
|
|||||||
|
|
||||||
private static void extractHeartBeat(StreamPacket packet) {
|
private static void extractHeartBeat(StreamPacket packet) {
|
||||||
long heartbeat = bytesToLong(packet.getPayload());
|
long heartbeat = bytesToLong(packet.getPayload());
|
||||||
System.out.println("Heartbeat: " + heartbeat);
|
// System.out.println("Heartbeat: " + heartbeat);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +112,7 @@ public class StreamParser {
|
|||||||
long currentTime = extractTimeStamp(Arrays.copyOfRange(payload,1,7), 6);
|
long currentTime = extractTimeStamp(Arrays.copyOfRange(payload,1,7), 6);
|
||||||
long raceId = bytesToLong(Arrays.copyOfRange(payload,7,11));
|
long raceId = bytesToLong(Arrays.copyOfRange(payload,7,11));
|
||||||
int raceStatus = payload[11];
|
int raceStatus = payload[11];
|
||||||
System.out.println("raceStatus = " + raceStatus);
|
// System.out.println("raceStatus = " + raceStatus);
|
||||||
long expectedStartTime = extractTimeStamp(Arrays.copyOfRange(payload,12,18), 6);
|
long expectedStartTime = extractTimeStamp(Arrays.copyOfRange(payload,12,18), 6);
|
||||||
long windDir = bytesToLong(Arrays.copyOfRange(payload,18,20));
|
long windDir = bytesToLong(Arrays.copyOfRange(payload,18,20));
|
||||||
long windSpeed = bytesToLong(Arrays.copyOfRange(payload,20,22));
|
long windSpeed = bytesToLong(Arrays.copyOfRange(payload,20,22));
|
||||||
@@ -172,7 +209,7 @@ public class StreamParser {
|
|||||||
long subjectId = bytesToLong(Arrays.copyOfRange(payload,9,13));
|
long subjectId = bytesToLong(Arrays.copyOfRange(payload,9,13));
|
||||||
long incidentId = bytesToLong(Arrays.copyOfRange(payload,13,17));
|
long incidentId = bytesToLong(Arrays.copyOfRange(payload,13,17));
|
||||||
int eventId = payload[17];
|
int eventId = payload[17];
|
||||||
System.out.println("eventId = " + eventId);
|
// System.out.println("eventId = " + eventId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void extractChatterText(StreamPacket packet){
|
private static void extractChatterText(StreamPacket packet){
|
||||||
@@ -191,27 +228,26 @@ public class StreamParser {
|
|||||||
byte[] latBytes = Arrays.copyOfRange(payload,16,20);
|
byte[] latBytes = Arrays.copyOfRange(payload,16,20);
|
||||||
byte[] lonBytes = Arrays.copyOfRange(payload,20,24);
|
byte[] lonBytes = Arrays.copyOfRange(payload,20,24);
|
||||||
byte[] boatIdBytes = Arrays.copyOfRange(payload,7,11);
|
byte[] boatIdBytes = Arrays.copyOfRange(payload,7,11);
|
||||||
|
byte[] headingBytes = Arrays.copyOfRange(payload,28,30);
|
||||||
long timeStamp = extractTimeStamp(Arrays.copyOfRange(payload,1,7), 6);
|
long timeStamp = extractTimeStamp(Arrays.copyOfRange(payload,1,7), 6);
|
||||||
// int boatSeq = ByteBuffer.wrap(seqBytes).getInt();
|
// int boatSeq = ByteBuffer.wrap(seqBytes).getInt();
|
||||||
long seq = bytesToLong(seqBytes);
|
long seq = bytesToLong(seqBytes);
|
||||||
long boatId = bytesToLong(boatIdBytes);
|
long boatId = bytesToLong(boatIdBytes);
|
||||||
long lat = bytesToLong(latBytes);
|
long lat = bytesToLong(latBytes);
|
||||||
long lon = bytesToLong(lonBytes);
|
long lon = bytesToLong(lonBytes);
|
||||||
|
long heading = bytesToLong(headingBytes);
|
||||||
|
|
||||||
if ((int)deviceType == 1){
|
if ((int)deviceType == 1){
|
||||||
if (!boat_IDS.contains(boatId)){
|
// System.out.println("boatId = " + boatId);
|
||||||
boat_IDS.add(boatId);
|
// System.out.println("deviceType = " + (long)deviceType);
|
||||||
}
|
// System.out.println("seq = " + seq);
|
||||||
System.out.println("boatId = " + boatId);
|
|
||||||
System.out.println("deviceType = " + (long)deviceType);
|
|
||||||
System.out.println("seq = " + seq);
|
|
||||||
//needs to be validated
|
//needs to be validated
|
||||||
System.out.println("lon = " + ((180d * (double)lon)/Math.pow(2,31)));
|
Point3D point = new Point3D(((180d * (double)lat)/Math.pow(2,31)),((180d *(double)lon)/Math.pow(2,31)),(double)heading);
|
||||||
System.out.println("lat = " + ((180d *(double)lat)/Math.pow(2,31)));
|
boatPositions.putIfAbsent(boatId, point);
|
||||||
|
boatPositions.replace(boatId, point);
|
||||||
|
// System.out.println("lon = " + ((180d * (double)lon)/Math.pow(2,31)));
|
||||||
|
// System.out.println("lat = " + ((180d *(double)lat)/Math.pow(2,31)));
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("boat_IDS = " + boat_IDS);
|
|
||||||
System.out.println("boat_IDS = " + boat_IDS.size());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -12,22 +12,47 @@ import java.util.zip.CRC32;
|
|||||||
import java.util.zip.Checksum;
|
import java.util.zip.Checksum;
|
||||||
|
|
||||||
|
|
||||||
public class StreamReceiver {
|
public class StreamReceiver extends Thread {
|
||||||
private InputStream stream;
|
private InputStream stream;
|
||||||
private Socket host;
|
private Socket host;
|
||||||
private ByteArrayOutputStream crcBuffer;
|
private ByteArrayOutputStream crcBuffer;
|
||||||
public PriorityBlockingQueue<StreamPacket> packetBuffer;
|
private Thread t;
|
||||||
|
private String threadName;
|
||||||
|
public static PriorityBlockingQueue<StreamPacket> packetBuffer;
|
||||||
|
|
||||||
public StreamReceiver(String hostAddress, int hostPort, PriorityBlockingQueue packetBuffer) {
|
public StreamReceiver(String hostAddress, int hostPort, String threadName) {
|
||||||
|
this.threadName = threadName;
|
||||||
try {
|
try {
|
||||||
host = new Socket(hostAddress, hostPort);
|
host = new Socket(hostAddress, hostPort);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
this.packetBuffer = packetBuffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void run(){
|
||||||
|
PriorityBlockingQueue<StreamPacket> pq = new PriorityBlockingQueue<>(256, new Comparator<StreamPacket>() {
|
||||||
|
@Override
|
||||||
|
public int compare(StreamPacket s1, StreamPacket s2) {
|
||||||
|
return (int) (s1.getTimeStamp() - s2.getTimeStamp());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
packetBuffer = pq;
|
||||||
|
connect();
|
||||||
|
StreamParser streamParser = new StreamParser("TestThread2");
|
||||||
|
|
||||||
|
streamParser.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start () {
|
||||||
|
System.out.println("Starting " + threadName );
|
||||||
|
if (t == null) {
|
||||||
|
t = new Thread (this, threadName);
|
||||||
|
t.start ();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void connect(){
|
public void connect(){
|
||||||
try {
|
try {
|
||||||
stream = host.getInputStream();
|
stream = host.getInputStream();
|
||||||
@@ -121,15 +146,13 @@ public class StreamReceiver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
PriorityBlockingQueue<StreamPacket> pq = new PriorityBlockingQueue<>(256, new Comparator<StreamPacket>() {
|
|
||||||
@Override
|
StreamReceiver sr = new StreamReceiver("csse-s302staff.canterbury.ac.nz", 4941,"TestThread1");
|
||||||
public int compare(StreamPacket s1, StreamPacket s2) {
|
//StreamReceiver sr = new StreamReceiver("livedata.americascup.com", 4941, pq);
|
||||||
return (int) (s1.getTimeStamp() - s2.getTimeStamp());
|
sr.start();
|
||||||
}
|
|
||||||
});
|
|
||||||
StreamReceiver sr = new StreamReceiver("csse-s302staff.canterbury.ac.nz", 4941, pq);
|
|
||||||
// StreamReceiver sr = new StreamReceiver("livedata.americascup.com", 4941, pq);
|
|
||||||
sr.connect();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,8 @@ public class TeamsParser extends FileParser {
|
|||||||
String name = element.getElementsByTagName("name").item(0).getTextContent();
|
String name = element.getElementsByTagName("name").item(0).getTextContent();
|
||||||
String alias = element.getElementsByTagName("alias").item(0).getTextContent();
|
String alias = element.getElementsByTagName("alias").item(0).getTextContent();
|
||||||
double velocity = Double.valueOf(element.getElementsByTagName("velocity").item(0).getTextContent());
|
double velocity = Double.valueOf(element.getElementsByTagName("velocity").item(0).getTextContent());
|
||||||
Boat boat = new Boat(name, velocity, alias);
|
int id = Integer.valueOf(element.getElementsByTagName("id").item(0).getTextContent());
|
||||||
|
Boat boat = new Boat(name, velocity, alias, id);
|
||||||
return boat;
|
return boat;
|
||||||
} else {
|
} else {
|
||||||
throw new NoSuchElementException("Cannot generate a boat by given node");
|
throw new NoSuchElementException("Cannot generate a boat by given node");
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ public class BoatTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetVelocity() {
|
public void testSetVelocity() {
|
||||||
Boat boat1 = new Boat("Team 1", 29.0, "");
|
Boat boat1 = new Boat("Team 1", 29.0, "", 100);
|
||||||
assertEquals(boat1.getVelocity(), (double) 29.0, 1e-15);
|
assertEquals(boat1.getVelocity(), (double) 29.0, 1e-15);
|
||||||
|
|
||||||
boat1.setVelocity(12.0);
|
boat1.setVelocity(12.0);
|
||||||
|
|||||||
Reference in New Issue
Block a user