Discovered the time valid timestamp in the boat location packet is quite inconsistent and either the stream or my implementation is making the display really buggy. Because the way it was before I changed things is more reliable at the moment, I have decided to wait until our mock stream is merged before continuing this development. #story[820]

This commit is contained in:
Peter Galloway
2017-05-01 16:56:53 +12:00
parent 99e50aa7ac
commit a5ca9218da
5 changed files with 63 additions and 28 deletions
@@ -18,10 +18,13 @@ import seng302.models.BoatGroup;
import seng302.models.Colors; import seng302.models.Colors;
import seng302.models.RaceObject; import seng302.models.RaceObject;
import seng302.models.mark.*; import seng302.models.mark.*;
import seng302.models.parsers.StreamPacket;
import seng302.models.parsers.StreamParser; import seng302.models.parsers.StreamParser;
import seng302.models.parsers.packets.BoatPositionPacket;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.*; import java.util.*;
import java.util.concurrent.PriorityBlockingQueue;
/** /**
* Created by ptg19 on 15/03/17. * Created by ptg19 on 15/03/17.
@@ -105,12 +108,7 @@ public class CanvasController {
@Override @Override
public void handle(long now) { public void handle(long now) {
boolean raceFinished = true;
boolean descending;
boolean leftToRight;
int boatIndex = 0;
Mark nextMark;
long oldFrameTime = frameTimes[frameTimeIndex] ; long oldFrameTime = frameTimes[frameTimeIndex] ;
frameTimes[frameTimeIndex] = now ; frameTimes[frameTimeIndex] = now ;
@@ -126,20 +124,52 @@ public class CanvasController {
} }
for (RaceObject raceObject : raceObjects) { for (RaceObject raceObject : raceObjects) {
raceObject.updatePosition(1000 / 60); raceObject.updatePosition(1000 / 60);
for (int id : raceObject.getRaceIds()) { for (long id : raceObject.getRaceIds()) {
if (StreamParser.boatPositions.containsKey((long) id)) { if (StreamParser.boatPositions.containsKey(id)) {
Point3D p = StreamParser.boatPositions.get((long) id);
Point2D p2d = latLonToXY(p.getX(), p.getY()); PriorityBlockingQueue<BoatPositionPacket> movementQueue = StreamParser.boatPositions.get(id);
double heading = 360.0 / 0xffff * p.getZ(); if (movementQueue.size() > 0){
raceObject.setDestination(p2d.getX(), p2d.getY(), heading, id); BoatPositionPacket positionPacket = movementQueue.peek();
int delayTime = 5000;
int loopTime = delayTime + 1000;
//System.out.println("id = " + id);
long timeDiff = (System.currentTimeMillis()%loopTime - positionPacket.getTimeValid()%loopTime);
if (timeDiff < 0){
timeDiff = loopTime + timeDiff;
}
if (id == 106) {
//System.out.println("timeDiff = " + timeDiff);
//System.out.println("movementQueue.size() = " + movementQueue.size());
}
//System.out.println("timeDiff = " + timeDiff);
//System.out.println("movementQueue.size() = " + movementQueue.size());
if (timeDiff > delayTime) {
if (id == 106){
//System.out.println("processing");
}
try {
positionPacket = movementQueue.take();
Point2D p2d = latLonToXY(positionPacket.getLat(), positionPacket.getLon());
double heading = 360.0 / 0xffff * positionPacket.getHeading();
raceObject.setDestination(p2d.getX(), p2d.getY(), heading, (int) id);
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
} }
StreamParser.boatPositions.remove((long) id); //StreamParser.boatPositions.remove((long) id);
} }
} }
} }
}; };
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();
} }
@@ -58,13 +58,13 @@ public class StreamParser extends Thread{
long sleepTime = 0; long sleepTime = 0;
long transitTime = (System.currentTimeMillis()%loopTime - packet.getTimeStamp()%loopTime); long transitTime = (System.currentTimeMillis()%loopTime - packet.getTimeStamp()%loopTime);
if (transitTime < 0){ if (transitTime < 0){
transitTime = loopTime + delayTime; transitTime = loopTime + transitTime;
} }
if (transitTime < delayTime) { if (transitTime < delayTime) {
sleepTime = delayTime - (transitTime); sleepTime = delayTime - (transitTime);
Thread.sleep(sleepTime); Thread.sleep(sleepTime);
} }
System.out.println(sleepTime); // System.out.println(sleepTime);
packet = StreamReceiver.packetBuffer.take(); packet = StreamReceiver.packetBuffer.take();
parsePacket(packet); parsePacket(packet);
@@ -169,7 +169,7 @@ public class StreamParser extends Thread{
System.out.println("RACE HAS STARTED"); System.out.println("RACE HAS STARTED");
} }
if (timeTillStart % 10 == 0){ if (timeTillStart % 10 == 0){
System.out.println("Time since start: " + -1 * timeTillStart + " Seconds"); //System.out.println("Time since start: " + -1 * timeTillStart + " Seconds");
} }
} }
long windDir = bytesToLong(Arrays.copyOfRange(payload,18,20)); long windDir = bytesToLong(Arrays.copyOfRange(payload,18,20));
@@ -332,6 +332,9 @@ public class StreamParser extends Thread{
if ((int)deviceType == 1){ if ((int)deviceType == 1){
BoatPositionPacket boatPacket = new BoatPositionPacket(boatId, timeValid, lat, lon, heading, groundSpeed); BoatPositionPacket boatPacket = new BoatPositionPacket(boatId, timeValid, lat, lon, heading, groundSpeed);
if (boatId == 106){
System.out.println("timeValid = " + timeValid);
}
if (!boatPositions.containsKey(boatId)){ if (!boatPositions.containsKey(boatId)){
boatPositions.put(boatId, new PriorityBlockingQueue<BoatPositionPacket>(256, new Comparator<BoatPositionPacket>() { boatPositions.put(boatId, new PriorityBlockingQueue<BoatPositionPacket>(256, new Comparator<BoatPositionPacket>() {
@@ -78,9 +78,6 @@ public class StreamReceiver extends Thread {
long computedCrc = checksum.getValue(); long computedCrc = checksum.getValue();
long packetCrc = bytesToLong(getBytes(4)); long packetCrc = bytesToLong(getBytes(4));
if (computedCrc == packetCrc) { if (computedCrc == packetCrc) {
// System.out.println("message type: " + type);
// System.out.println("timeStamp = " + timeStamp);
// System.out.println("payload length: " + payloadLength);
packetBuffer.add(new StreamPacket(type, payloadLength, timeStamp, payload)); packetBuffer.add(new StreamPacket(type, payloadLength, timeStamp, payload));
} else { } else {
System.err.println("Packet has been dropped"); System.err.println("Packet has been dropped");
@@ -126,7 +123,7 @@ public class StreamReceiver extends Thread {
* takes an array of up to 7 bytes and returns a positive * takes an array of up to 7 bytes and returns a positive
* long constructed from the input bytes * long constructed from the input bytes
* *
* @return a positive long if there is less than 7 bytes -1 otherwise * @return a positive long if there is less than 8 bytes -1 otherwise
*/ */
private long bytesToLong(byte[] bytes){ private long bytesToLong(byte[] bytes){
long partialLong = 0; long partialLong = 0;
@@ -20,4 +20,16 @@ public class BoatPositionPacket {
public long getTimeValid() { public long getTimeValid() {
return timeValid; return timeValid;
} }
public double getLat() {
return lat;
}
public double getLon() {
return lon;
}
public double getHeading() {
return heading;
}
} }
@@ -12,18 +12,11 @@ public class StreamPacket {
private long timeStamp; private long timeStamp;
private byte[] payload; private byte[] payload;
StreamPacket(int type, long messageLength, long timeStamp, byte[] payload) { public StreamPacket(int type, long messageLength, long timeStamp, byte[] payload) {
this.type = PacketType.assignPacketType(type); this.type = PacketType.assignPacketType(type);
this.messageLength = messageLength; this.messageLength = messageLength;
this.timeStamp = timeStamp; this.timeStamp = timeStamp;
this.payload = payload; 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() { public PacketType getType() {