Started looking into boat location packets, am able to extract the lats an lons but needs validations. Can also see the device type, timestamp, and sequence number. Code needs to be cleaned up and will need to start looking into the set up packets, specifically the packets containing xml data so the course can be created.

#story[820]
This commit is contained in:
Kusal Ekanayake
2017-04-24 16:47:41 +12:00
parent 403dc480c4
commit 71e14259f6
3 changed files with 35 additions and 19 deletions
@@ -19,7 +19,6 @@ public class StreamPacket {
this.payload = payload; this.payload = payload;
// System.out.println("type = " + type); // System.out.println("type = " + type);
if (this.type == PacketType.BOAT_LOCATION){ if (this.type == PacketType.BOAT_LOCATION){
System.out.println(this.type.toString());
StreamParser.extractBoatLocation(payload); StreamParser.extractBoatLocation(payload);
} }
} }
@@ -9,7 +9,10 @@ import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map;
/** /**
* Created by kre39 on 23/04/17. * Created by kre39 on 23/04/17.
@@ -17,7 +20,7 @@ import java.util.Arrays;
public class StreamParser { public class StreamParser {
private static boolean isWithinTag; private static boolean isWithinTag;
public static ArrayList<Long> ids = new ArrayList<>();
static void parseLine(byte[] bytes) { static void parseLine(byte[] bytes) {
//TODO overhaul all of this to treat packets as appropriate //TODO overhaul all of this to treat packets as appropriate
@@ -43,26 +46,41 @@ public class StreamParser {
} }
static void extractBoatLocation(byte[] payload){ static void extractBoatLocation(byte[] payload){
byte deviceType = payload[15];
byte[] seqBytes = Arrays.copyOfRange(payload,11,15);
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,8,12); byte[] boatIdBytes = Arrays.copyOfRange(payload,8,12);
int boatId = ByteBuffer.wrap(boatIdBytes).getInt(); extractTimeStamp(Arrays.copyOfRange(payload,1,7));
int lat = ByteBuffer.wrap(latBytes).getInt(); // int boatSeq = ByteBuffer.wrap(seqBytes).getInt();
int lon = ByteBuffer.wrap(lonBytes).getInt(); long seq = StreamReceiver.bytesToLong(seqBytes);
// System.out.println("boatId = " + boatId); long boatId = StreamReceiver.bytesToLong(boatIdBytes);
// System.out.println("lon = " + 180 * (lon/Math.pow(2,31))); long lat = StreamReceiver.bytesToLong(latBytes);
// System.out.println("lat = " + 180 * (lat/Math.pow(2,31))); long lon = StreamReceiver.bytesToLong(lonBytes);
if (!ids.contains(boatId)) {
ids.add(boatId);
} }
if (boatId != 0){
public static int toInt(byte[] bytes, int offset) { System.out.println("boatId = " + boatId);
System.out.println("deviceType = " + (long)deviceType);
int ret = 0; // System.out.println("seq = " + seq);
for (int i=0; i<4 && i+offset<bytes.length; i++) { //needs to be validated
ret <<= 8; System.out.println("lon = " + ((180d * (double)lon)/Math.pow(2,31)));
ret |= (int)bytes[i] & 0xFF; System.out.println("lat = " + ((180d *(double)lat)/Math.pow(2,31)));
}
return ret;
} }
} }
private static void extractTimeStamp(byte[] timeStampBytes){
long timeStamp = 0;
long multiplier=1;
for(int i = 0;i < 6;i++) {
timeStamp += timeStampBytes[i]*multiplier;
multiplier *= 256;
}
System.out.println("timeStamp = " + timeStamp);
}
}
@@ -103,7 +103,6 @@ public class StreamReceiver {
e.printStackTrace(); e.printStackTrace();
} }
} }
private static byte[] getBytes(int n){ private static byte[] getBytes(int n){
@@ -124,7 +123,7 @@ public class StreamReceiver {
* *
* @return a positive long if there is less than 4 bytes -1 otherwise * @return a positive long if there is less than 4 bytes -1 otherwise
*/ */
private static long bytesToLong(byte[] bytes){ static long bytesToLong(byte[] bytes){
long partialLong = 0; long partialLong = 0;
int index = 0; int index = 0;
for (byte b: bytes){ for (byte b: bytes){