From 056b1cfb785aa106b62f791b9476002471c2a8c3 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 5 May 2021 19:49:18 -0700 Subject: Update code formatting --- .../traccar/protocol/Mavlink2ProtocolDecoder.java | 50 +++++++++++++--------- .../protocol/Mavlink2ProtocolDecoderTest.java | 11 ++++- 2 files changed, 39 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/main/java/org/traccar/protocol/Mavlink2ProtocolDecoder.java b/src/main/java/org/traccar/protocol/Mavlink2ProtocolDecoder.java index 666538479..431258388 100644 --- a/src/main/java/org/traccar/protocol/Mavlink2ProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/Mavlink2ProtocolDecoder.java @@ -15,18 +15,16 @@ */ package org.traccar.protocol; -import java.net.SocketAddress; -import java.time.Instant; -import java.util.Date; - +import io.netty.buffer.ByteBuf; +import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; import org.traccar.Protocol; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; -import io.netty.buffer.ByteBuf; -import io.netty.channel.Channel; +import java.net.SocketAddress; +import java.util.Date; public class Mavlink2ProtocolDecoder extends BaseProtocolDecoder { @@ -36,39 +34,51 @@ public class Mavlink2ProtocolDecoder extends BaseProtocolDecoder { @Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + ByteBuf buf = (ByteBuf) msg; - if (buf.readUnsignedByte() != 0xFD) { //Packet start marker + if (buf.readUnsignedByte() != 0xFD) { return null; } - buf.readUnsignedByte(); // Payload length - buf.readUnsignedByte(); // Incompatibility Flags - buf.readUnsignedByte(); // Compatibility Flags - buf.readUnsignedByte(); // Packet sequence number - int senderSystemId = buf.readUnsignedByte(); // System ID (sender) + + buf.readUnsignedByte(); // length + buf.readUnsignedByte(); // incompatibility flags + buf.readUnsignedByte(); // compatibility flags + buf.readUnsignedByte(); // index + + int senderSystemId = buf.readUnsignedByte(); + buf.readUnsignedByte(); // component id DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, Integer.toString(senderSystemId)); - buf.readUnsignedByte(); // Component ID (sender) if (deviceSession == null) { return null; } - int messageId = buf.readUnsignedMediumLE(); // Message ID (low, middle, high bytes) - if (messageId == 33) { + + int type = buf.readUnsignedMediumLE(); + if (type == 33) { + Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); + + position.set("timeBoot", buf.readUnsignedIntLE()); // time since system boot + position.setValid(true); - position.setTime(Date.from(Instant.now())); - position.set("timeBootms", buf.readUnsignedIntLE()); // Timestamp (time since system boot). + position.setTime(new Date()); position.setLatitude(buf.readIntLE() / 10000000.0); position.setLongitude(buf.readIntLE() / 10000000.0); - position.setAltitude(buf.readIntLE() / 1000.0); // Altitude (MSL). - position.set("relativeAltitude", buf.readIntLE() / 1000.0); // Altitude above ground + position.setAltitude(buf.readIntLE() / 1000.0); + position.set("relativeAltitude", buf.readIntLE() / 1000.0); + int groundSpeedX = buf.readShortLE(); int groundSpeedY = buf.readShortLE(); - buf.readShortLE(); // Ground Z Speed + buf.readShortLE(); // ground speed z double speed = Math.sqrt(Math.pow(groundSpeedX, 2) + Math.pow(groundSpeedY, 2)); position.setSpeed(UnitsConverter.knotsFromCps(speed)); + position.setCourse(buf.readUnsignedShortLE() / 100.0); + return position; + } + return null; } diff --git a/src/test/java/org/traccar/protocol/Mavlink2ProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/Mavlink2ProtocolDecoderTest.java index f4b6d8229..ccddb6a11 100644 --- a/src/test/java/org/traccar/protocol/Mavlink2ProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/Mavlink2ProtocolDecoderTest.java @@ -7,8 +7,15 @@ public class Mavlink2ProtocolDecoderTest extends ProtocolTest { @Test public void testDecode() throws Exception { + var decoder = new Mavlink2ProtocolDecoder(null); - verifyAttributes(decoder, binary("fd1c0000ce01012100004da91f004005d323b89aa30ea6ed070099fb0100f7fffdff0000942c4a88")); - verifyAttributes(decoder, binary("fd1c0000e7010121000047aa1f004005d323b89aa30e9ced070093fb0100f8fffdff0000952c70ff")); + + verifyAttributes(decoder, binary( + "fd1c0000ce01012100004da91f004005d323b89aa30ea6ed070099fb0100f7fffdff0000942c4a88")); + + verifyAttributes(decoder, binary( + "fd1c0000e7010121000047aa1f004005d323b89aa30e9ced070093fb0100f8fffdff0000952c70ff")); + } + } -- cgit v1.2.3