From 5ab6ae8d6217f3d7565a9d1073e50c1d479c5625 Mon Sep 17 00:00:00 2001 From: Anatoliy Golubev Date: Sat, 9 Sep 2017 22:48:02 +0300 Subject: Improve AdmProtocolDecoder --- src/org/traccar/protocol/AdmProtocolDecoder.java | 105 ++++++++++++++++------- 1 file changed, 73 insertions(+), 32 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/protocol/AdmProtocolDecoder.java b/src/org/traccar/protocol/AdmProtocolDecoder.java index f4a21cad0..4b6a5d3fc 100644 --- a/src/org/traccar/protocol/AdmProtocolDecoder.java +++ b/src/org/traccar/protocol/AdmProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,76 +33,80 @@ public class AdmProtocolDecoder extends BaseProtocolDecoder { super(protocol); } + public static final int CMD_RESPONSE_SIZE = 0x84; public static final int MSG_IMEI = 0x03; public static final int MSG_PHOTO = 0x0A; public static final int MSG_ADM5 = 0x01; - @Override - protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - - ChannelBuffer buf = (ChannelBuffer) msg; - - buf.readUnsignedShort(); // device id - buf.readUnsignedByte(); // length - - int type = buf.readUnsignedByte(); - - DeviceSession deviceSession; - if (type == MSG_IMEI) { - deviceSession = getDeviceSession( - channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII)); - } else { - deviceSession = getDeviceSession(channel, remoteAddress); - } + private DeviceSession parseIdentification(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { + String imei = buf.readBytes(15).toString(StandardCharsets.UTF_8); + return getDeviceSession(channel, remoteAddress, imei); + } + private Position parseData(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf, int type) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { return null; } if (BitUtil.to(type, 2) == 0) { - Position position = new Position(); position.setProtocol(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte()); - buf.readUnsignedShort(); // index + position.set(Position.KEY_INDEX, buf.readUnsignedShort()); + + int status = buf.readUnsignedShort(); + position.set(Position.KEY_STATUS, status); - position.set(Position.KEY_STATUS, buf.readUnsignedShort()); + boolean isValid = !BitUtil.check(status, 5); + position.setValid(isValid); - position.setValid(true); position.setLatitude(buf.readFloat()); position.setLongitude(buf.readFloat()); position.setCourse(buf.readUnsignedShort() * 0.1); position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort() * 0.1)); - position.set(Position.KEY_ACCELERATION, buf.readUnsignedByte()); - + position.set(Position.KEY_ACCELERATION, buf.readUnsignedByte() * 0.1); position.setAltitude(buf.readUnsignedShort()); - position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1); position.set(Position.KEY_SATELLITES, buf.readUnsignedByte() & 0x0f); position.setTime(new Date(buf.readUnsignedInt() * 1000)); - position.set(Position.KEY_POWER, buf.readUnsignedShort()); - position.set(Position.KEY_BATTERY, buf.readUnsignedShort()); + position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001); + position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001); if (BitUtil.check(type, 2)) { - buf.skipBytes(4); + buf.skipBytes(2); // vib, vib_count + + int out = buf.readUnsignedByte(); + for (int i = 0; i <= 3; ++i) { + position.set(Position.PREFIX_OUT + (i + 1), BitUtil.check(out, i) ? 1 : 0); + } + + buf.skipBytes(1); // in_alarm } if (BitUtil.check(type, 3)) { - buf.skipBytes(12); + for (int i = 1; i <= 6; ++i) { + position.set(Position.PREFIX_ADC + i, buf.readUnsignedShort() * 0.001); + } } if (BitUtil.check(type, 4)) { - buf.skipBytes(8); + for (int i = 1; i <= 2; ++i) { + position.set(Position.PREFIX_COUNT + i, buf.readUnsignedInt()); + } } if (BitUtil.check(type, 5)) { - buf.skipBytes(9); + buf.skipBytes(6); // fuel level 0..2 + + for (int i = 1; i <= 3; ++i) { + position.set(Position.PREFIX_TEMP + i, buf.readUnsignedByte()); + } } if (BitUtil.check(type, 6)) { @@ -119,4 +123,41 @@ public class AdmProtocolDecoder extends BaseProtocolDecoder { return null; } + private Position parseCommandResponse(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } + + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + getLastLocation(position, null); + + position.set(Position.KEY_RESULT, buf.readBytes(129).toString(StandardCharsets.UTF_8)); + + return position; + } + + @Override + protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + ChannelBuffer buf = (ChannelBuffer) msg; + + buf.readUnsignedShort(); // device id + + int size = buf.readUnsignedByte(); + if (size != CMD_RESPONSE_SIZE) { + int type = buf.readUnsignedByte(); + if (type == MSG_IMEI) { + parseIdentification(channel, remoteAddress, buf); + } else { + return parseData(channel, remoteAddress, buf, type); + } + } else { + return parseCommandResponse(channel, remoteAddress, buf); + } + + return null; + } } -- cgit v1.2.3 From 03844cbfc53d676f41b8c9fb18b74b86a08e95f0 Mon Sep 17 00:00:00 2001 From: Anatoliy Golubev Date: Sat, 9 Sep 2017 22:52:18 +0300 Subject: Implement ADM protocol commands --- src/org/traccar/protocol/AdmProtocol.java | 5 +++ src/org/traccar/protocol/AdmProtocolEncoder.java | 53 ++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/org/traccar/protocol/AdmProtocolEncoder.java (limited to 'src/org/traccar') diff --git a/src/org/traccar/protocol/AdmProtocol.java b/src/org/traccar/protocol/AdmProtocol.java index 442121f0a..db2adfeaa 100644 --- a/src/org/traccar/protocol/AdmProtocol.java +++ b/src/org/traccar/protocol/AdmProtocol.java @@ -20,6 +20,7 @@ import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder; import org.traccar.BaseProtocol; import org.traccar.TrackerServer; +import org.traccar.model.Command; import java.nio.ByteOrder; import java.util.List; @@ -28,6 +29,9 @@ public class AdmProtocol extends BaseProtocol { public AdmProtocol() { super("adm"); + setSupportedDataCommands( + Command.TYPE_GET_DEVICE_STATUS, + Command.TYPE_CUSTOM); } @Override @@ -36,6 +40,7 @@ public class AdmProtocol extends BaseProtocol { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1024, 2, 1, -3, 0)); + pipeline.addLast("objectEncoder", new AdmProtocolEncoder()); pipeline.addLast("objectDecoder", new AdmProtocolDecoder(AdmProtocol.this)); } }; diff --git a/src/org/traccar/protocol/AdmProtocolEncoder.java b/src/org/traccar/protocol/AdmProtocolEncoder.java new file mode 100644 index 000000000..1cdfbf0b0 --- /dev/null +++ b/src/org/traccar/protocol/AdmProtocolEncoder.java @@ -0,0 +1,53 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Anatoliy Golubev (darth.naihil@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.protocol; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.traccar.BaseProtocolEncoder; +import org.traccar.helper.Log; +import org.traccar.model.Command; + +import java.nio.charset.StandardCharsets; + +public class AdmProtocolEncoder extends BaseProtocolEncoder { + + private ChannelBuffer encodeContent(String data) { + ChannelBuffer buf = ChannelBuffers.copiedBuffer(data, StandardCharsets.UTF_8); + buf.writeByte(0x0D); + buf.writeByte(0x0A); + return buf; + } + + @Override + protected Object encodeCommand(Command command) { + + switch (command.getType()) { + case Command.TYPE_GET_DEVICE_STATUS: + return encodeContent("STATUS"); + + case Command.TYPE_CUSTOM: + return encodeContent(command.getString(Command.KEY_DATA)); + + default: + Log.warning(new UnsupportedOperationException(command.getType())); + break; + } + + return null; + } +} -- cgit v1.2.3 From cdc7a0a0f8ba48b9581efeaeaa902f5a4ddcc306 Mon Sep 17 00:00:00 2001 From: Anatoliy Golubev Date: Sun, 10 Sep 2017 01:48:01 +0300 Subject: Simplify ADM protocol decoder --- src/org/traccar/protocol/AdmProtocolDecoder.java | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/protocol/AdmProtocolDecoder.java b/src/org/traccar/protocol/AdmProtocolDecoder.java index 4b6a5d3fc..b5a5c64dc 100644 --- a/src/org/traccar/protocol/AdmProtocolDecoder.java +++ b/src/org/traccar/protocol/AdmProtocolDecoder.java @@ -38,11 +38,6 @@ public class AdmProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_PHOTO = 0x0A; public static final int MSG_ADM5 = 0x01; - private DeviceSession parseIdentification(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { - String imei = buf.readBytes(15).toString(StandardCharsets.UTF_8); - return getDeviceSession(channel, remoteAddress, imei); - } - private Position parseData(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf, int type) { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { @@ -59,10 +54,7 @@ public class AdmProtocolDecoder extends BaseProtocolDecoder { int status = buf.readUnsignedShort(); position.set(Position.KEY_STATUS, status); - - boolean isValid = !BitUtil.check(status, 5); - position.setValid(isValid); - + position.setValid(!BitUtil.check(status, 5)); position.setLatitude(buf.readFloat()); position.setLongitude(buf.readFloat()); position.setCourse(buf.readUnsignedShort() * 0.1); @@ -150,7 +142,7 @@ public class AdmProtocolDecoder extends BaseProtocolDecoder { if (size != CMD_RESPONSE_SIZE) { int type = buf.readUnsignedByte(); if (type == MSG_IMEI) { - parseIdentification(channel, remoteAddress, buf); + getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.UTF_8)); } else { return parseData(channel, remoteAddress, buf, type); } -- cgit v1.2.3 From 07e4e89be6e658239125a8b99e05ce4d132e1d9e Mon Sep 17 00:00:00 2001 From: Anatoliy Golubev Date: Sun, 10 Sep 2017 01:51:31 +0300 Subject: Inherit AdmProtocolEncoder from StringProtocolEncoder --- src/org/traccar/protocol/AdmProtocol.java | 4 +++- src/org/traccar/protocol/AdmProtocolEncoder.java | 19 ++++--------------- test/org/traccar/protocol/AdmProtocolEncoderTest.java | 5 +++-- 3 files changed, 10 insertions(+), 18 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/protocol/AdmProtocol.java b/src/org/traccar/protocol/AdmProtocol.java index db2adfeaa..4d2cbe7b3 100644 --- a/src/org/traccar/protocol/AdmProtocol.java +++ b/src/org/traccar/protocol/AdmProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.traccar.protocol; import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder; +import org.jboss.netty.handler.codec.string.StringEncoder; import org.traccar.BaseProtocol; import org.traccar.TrackerServer; import org.traccar.model.Command; @@ -40,6 +41,7 @@ public class AdmProtocol extends BaseProtocol { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1024, 2, 1, -3, 0)); + pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("objectEncoder", new AdmProtocolEncoder()); pipeline.addLast("objectDecoder", new AdmProtocolDecoder(AdmProtocol.this)); } diff --git a/src/org/traccar/protocol/AdmProtocolEncoder.java b/src/org/traccar/protocol/AdmProtocolEncoder.java index 1cdfbf0b0..8cbd8618d 100644 --- a/src/org/traccar/protocol/AdmProtocolEncoder.java +++ b/src/org/traccar/protocol/AdmProtocolEncoder.java @@ -16,32 +16,21 @@ */ package org.traccar.protocol; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBuffers; -import org.traccar.BaseProtocolEncoder; +import org.traccar.StringProtocolEncoder; import org.traccar.helper.Log; import org.traccar.model.Command; -import java.nio.charset.StandardCharsets; - -public class AdmProtocolEncoder extends BaseProtocolEncoder { - - private ChannelBuffer encodeContent(String data) { - ChannelBuffer buf = ChannelBuffers.copiedBuffer(data, StandardCharsets.UTF_8); - buf.writeByte(0x0D); - buf.writeByte(0x0A); - return buf; - } +public class AdmProtocolEncoder extends StringProtocolEncoder { @Override protected Object encodeCommand(Command command) { switch (command.getType()) { case Command.TYPE_GET_DEVICE_STATUS: - return encodeContent("STATUS"); + return formatCommand(command, "STATUS\r\n"); case Command.TYPE_CUSTOM: - return encodeContent(command.getString(Command.KEY_DATA)); + return formatCommand(command, "{%s}\r\n", Command.KEY_DATA); default: Log.warning(new UnsupportedOperationException(command.getType())); diff --git a/test/org/traccar/protocol/AdmProtocolEncoderTest.java b/test/org/traccar/protocol/AdmProtocolEncoderTest.java index 3a72036bf..6d2452e26 100644 --- a/test/org/traccar/protocol/AdmProtocolEncoderTest.java +++ b/test/org/traccar/protocol/AdmProtocolEncoderTest.java @@ -16,6 +16,7 @@ */ package org.traccar.protocol; +import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; import org.traccar.model.Command; @@ -30,13 +31,13 @@ public class AdmProtocolEncoderTest extends ProtocolTest { Command command = new Command(); command.setDeviceId(1); command.setType(Command.TYPE_GET_DEVICE_STATUS); - verifyCommand(encoder, command, binary("5354415455530D0A")); + Assert.assertEquals("STATUS\r\n", encoder.encodeCommand(command)); command = new Command(); command.setDeviceId(1); command.setType(Command.TYPE_CUSTOM); command.set(Command.KEY_DATA, "INPUT 0"); - verifyCommand(encoder, command, binary("494E50555420300D0A")); + Assert.assertEquals("INPUT 0\r\n", encoder.encodeCommand(command)); } } -- cgit v1.2.3 From 31d46d03bd29e9d29c2797f5e72735450a9bf45b Mon Sep 17 00:00:00 2001 From: Anatoliy Golubev Date: Sun, 10 Sep 2017 02:11:43 +0300 Subject: Rename method --- src/org/traccar/protocol/AdmProtocolDecoder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/protocol/AdmProtocolDecoder.java b/src/org/traccar/protocol/AdmProtocolDecoder.java index b5a5c64dc..df96c6b8d 100644 --- a/src/org/traccar/protocol/AdmProtocolDecoder.java +++ b/src/org/traccar/protocol/AdmProtocolDecoder.java @@ -38,7 +38,7 @@ public class AdmProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_PHOTO = 0x0A; public static final int MSG_ADM5 = 0x01; - private Position parseData(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf, int type) { + private Position decodeData(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf, int type) { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { return null; @@ -144,7 +144,7 @@ public class AdmProtocolDecoder extends BaseProtocolDecoder { if (type == MSG_IMEI) { getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.UTF_8)); } else { - return parseData(channel, remoteAddress, buf, type); + return decodeData(channel, remoteAddress, buf, type); } } else { return parseCommandResponse(channel, remoteAddress, buf); -- cgit v1.2.3 From fa3e682e2b309ca89d0cc7dcb11957f9b380c6d4 Mon Sep 17 00:00:00 2001 From: Anatoliy Golubev Date: Sun, 10 Sep 2017 11:43:13 +0300 Subject: Fix command response decode --- src/org/traccar/protocol/AdmProtocolDecoder.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/protocol/AdmProtocolDecoder.java b/src/org/traccar/protocol/AdmProtocolDecoder.java index df96c6b8d..80c580967 100644 --- a/src/org/traccar/protocol/AdmProtocolDecoder.java +++ b/src/org/traccar/protocol/AdmProtocolDecoder.java @@ -127,7 +127,11 @@ public class AdmProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, null); - position.set(Position.KEY_RESULT, buf.readBytes(129).toString(StandardCharsets.UTF_8)); + int responseTextLength = buf.bytesBefore((byte) 0); + if (responseTextLength < 0) { + responseTextLength = CMD_RESPONSE_SIZE - 3; + } + position.set(Position.KEY_RESULT, buf.readBytes(responseTextLength).toString(StandardCharsets.UTF_8)); return position; } -- cgit v1.2.3