package network.MessageDecoders; import network.Messages.BoatLocation; import java.util.Arrays; import static network.Utils.ByteConverter.bytesToInt; import static network.Utils.ByteConverter.bytesToLong; import static network.Utils.ByteConverter.bytesToShort; /** * Created by hba56 on 21/04/17. */ public class BoatLocationDecoder { private byte messageVersionNumber; private byte[] time; private byte[] sourceID; private byte[] seqNum; private byte deviceType; private byte[] latitude; private byte[] longitude; private byte[] altitude; private byte[] heading; private byte[] pitch; private byte[] roll; private byte[] boatSpeed; private byte[] cog; private byte[] sog; private byte[] apparentWindSpeed; private byte[] apparentWindAngle; private byte[] trueWindSpeed; private byte[] trueWindDirection; private byte[] trueWindAngle; private byte[] currentDrift; private byte[] currentSet; private byte[] rudderAngle; private BoatLocation message; public BoatLocationDecoder(byte[] encodedBoatLocation) { byte numMessageVersionNumber = 0; long numTime = 0; int numSourceID = 0; int numSeqNum = 0; byte numDeviceType = 0; int numLatitude = 0; int numLongitude = 0; int numAltitude = 0; int numHeading = 0; short numPitch = 0; short numRoll = 0; int numBoatSpeed = 0; int numCog = 0; int numSog = 0; int numApparentWindSpeed = 0; short numApparentWindAngle = 0; int numTrueWindSpeed = 0; short numTrueWindDirection = 0; short numTrueWindAngle = 0; int numCurrentDrift = 0; int numCurrentSet = 0; short numRudderAngle = 0; try { messageVersionNumber = encodedBoatLocation[0]; numMessageVersionNumber = messageVersionNumber; time = Arrays.copyOfRange(encodedBoatLocation, 1, 7); numTime = bytesToLong(time); sourceID = Arrays.copyOfRange(encodedBoatLocation, 7, 11); numSourceID = bytesToInt(sourceID); seqNum = Arrays.copyOfRange(encodedBoatLocation, 11, 15); numSeqNum = bytesToInt(seqNum); deviceType = encodedBoatLocation[15]; numDeviceType = deviceType; latitude = Arrays.copyOfRange(encodedBoatLocation, 16, 20); numLatitude = bytesToInt(latitude); longitude = Arrays.copyOfRange(encodedBoatLocation, 20, 24); numLongitude = bytesToInt(longitude); altitude = Arrays.copyOfRange(encodedBoatLocation, 24, 28); numAltitude = bytesToInt(altitude); heading = Arrays.copyOfRange(encodedBoatLocation, 28, 30); numHeading = bytesToInt(heading); pitch = Arrays.copyOfRange(encodedBoatLocation, 30, 32); numPitch = bytesToShort(pitch); roll = Arrays.copyOfRange(encodedBoatLocation, 32, 34); numRoll = bytesToShort(roll); boatSpeed = Arrays.copyOfRange(encodedBoatLocation, 34, 36); numBoatSpeed = bytesToInt(boatSpeed); cog = Arrays.copyOfRange(encodedBoatLocation, 36, 38); numCog = bytesToInt(cog); sog = Arrays.copyOfRange(encodedBoatLocation, 38, 40); numSog = bytesToInt(sog); apparentWindSpeed = Arrays.copyOfRange(encodedBoatLocation, 40, 42); numApparentWindSpeed = bytesToInt(apparentWindSpeed); apparentWindAngle = Arrays.copyOfRange(encodedBoatLocation, 42, 44); numApparentWindAngle = bytesToShort(apparentWindAngle); trueWindSpeed = Arrays.copyOfRange(encodedBoatLocation, 44, 46); numTrueWindSpeed = bytesToInt(trueWindSpeed); trueWindDirection = Arrays.copyOfRange(encodedBoatLocation, 46, 48); numTrueWindDirection = bytesToShort(trueWindDirection); trueWindAngle = Arrays.copyOfRange(encodedBoatLocation, 48, 50); numTrueWindAngle = bytesToShort(trueWindAngle); currentDrift = Arrays.copyOfRange(encodedBoatLocation, 50, 52); numCurrentDrift = bytesToInt(currentDrift); currentSet = Arrays.copyOfRange(encodedBoatLocation, 52, 54); numCurrentSet = bytesToShort(currentSet); rudderAngle = Arrays.copyOfRange(encodedBoatLocation, 54, 56); numRudderAngle = bytesToShort(rudderAngle); } catch(ArrayIndexOutOfBoundsException e){ } message = new BoatLocation(numMessageVersionNumber, numTime, numSourceID, numSeqNum, numDeviceType, numLatitude, numLongitude, numAltitude, numHeading, numPitch, numRoll, numBoatSpeed, numCog, numSog, numApparentWindSpeed, numApparentWindAngle, numTrueWindSpeed, numTrueWindDirection, numTrueWindAngle, numCurrentDrift, numCurrentSet, numRudderAngle );/* message = new BoatLocation(messageVersionNumber, bytesToLong(time), bytesToInt(sourceID), bytesToInt(seqNum), deviceType, bytesToInt(latitude), bytesToInt(longitude), bytesToInt(altitude), bytesToInt(heading), bytesToShort(pitch), bytesToShort(roll), bytesToInt(boatSpeed), bytesToInt(cog), bytesToInt(sog), bytesToInt(apparentWindSpeed), bytesToShort(apparentWindAngle), bytesToInt(trueWindSpeed), bytesToShort(trueWindDirection), bytesToShort(trueWindAngle), bytesToInt(currentDrift), bytesToInt(currentSet), bytesToShort(rudderAngle) );*/ // System.out.println(bytesToInt(sourceID)); // System.out.println(bytesToInt(boatSpeed)); } public BoatLocation getMessage() { return message; } }