mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Further work on new wake system. Wakes turn correctly but need to scale with velocity and
eventually desync with the boats. Needs to reset to the boats position on straights.
This commit is contained in:
@@ -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;
|
||||
@@ -14,19 +12,24 @@ 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;
|
||||
|
||||
/**
|
||||
* 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<Long,Point3D> boatPositions = new ConcurrentHashMap<>();
|
||||
private static ArrayList<Long> boat_IDS = new ArrayList<>();
|
||||
private String threadName;
|
||||
public static ConcurrentHashMap<Long,Double> boatSpeeds = new ConcurrentHashMap<>();
|
||||
private String threadName;
|
||||
private Thread t;
|
||||
private static boolean raceStarted = false;
|
||||
|
||||
@@ -34,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) {
|
||||
@@ -54,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) {
|
||||
@@ -62,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);
|
||||
@@ -106,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];
|
||||
@@ -155,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];
|
||||
@@ -169,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 = "";
|
||||
@@ -197,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];
|
||||
@@ -216,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];
|
||||
@@ -226,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];
|
||||
@@ -236,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];
|
||||
@@ -244,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);
|
||||
@@ -253,6 +298,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[] groundSpeedBytes = 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 +307,9 @@ public class StreamParser extends Thread{
|
||||
long lat = bytesToLong(latBytes);
|
||||
long lon = bytesToLong(lonBytes);
|
||||
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);
|
||||
@@ -268,13 +317,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, 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)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static void extractMarkRounding(StreamPacket packet){
|
||||
byte[] payload = packet.getPayload();
|
||||
int messageVersionNo = payload[0];
|
||||
|
||||
Reference in New Issue
Block a user