From 663231ec30ed41d60ef352e89ea61e9eb2b735c2 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 9 Jan 2016 11:34:42 +1300 Subject: Merge AutoFon and AutoFon45 protocols --- debug.xml | 1 - .../traccar/protocol/AutoFon45FrameDecoder.java | 60 ---------- src/org/traccar/protocol/AutoFon45Protocol.java | 42 ------- .../traccar/protocol/AutoFon45ProtocolDecoder.java | 130 --------------------- src/org/traccar/protocol/AutoFonFrameDecoder.java | 19 +-- .../traccar/protocol/AutoFonProtocolDecoder.java | 71 ++++++++++- .../protocol/AutoFon45ProtocolDecoderTest.java | 25 ---- .../protocol/AutoFonProtocolDecoderTest.java | 13 +++ 8 files changed, 94 insertions(+), 267 deletions(-) delete mode 100644 src/org/traccar/protocol/AutoFon45FrameDecoder.java delete mode 100644 src/org/traccar/protocol/AutoFon45Protocol.java delete mode 100644 src/org/traccar/protocol/AutoFon45ProtocolDecoder.java delete mode 100644 test/org/traccar/protocol/AutoFon45ProtocolDecoderTest.java diff --git a/debug.xml b/debug.xml index 81eff271e..7096e4cde 100644 --- a/debug.xml +++ b/debug.xml @@ -247,7 +247,6 @@ 5076 5077 5078 - 5079 5080 5081 5082 diff --git a/src/org/traccar/protocol/AutoFon45FrameDecoder.java b/src/org/traccar/protocol/AutoFon45FrameDecoder.java deleted file mode 100644 index f382f4784..000000000 --- a/src/org/traccar/protocol/AutoFon45FrameDecoder.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2015 Vitaly Litvak (vitavaque@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.channel.Channel; -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.handler.codec.frame.FrameDecoder; - -public class AutoFon45FrameDecoder extends FrameDecoder { - - static final int MSG_LOGIN = 0x41; - static final int MSG_LOCATION = 0x02; - - @Override - protected Object decode( - ChannelHandlerContext ctx, - Channel channel, - ChannelBuffer buf) throws Exception { - - // Check minimum length - if (buf.readableBytes() < 12) { - return null; - } - - int length; - switch (buf.getUnsignedByte(buf.readerIndex())) { - case MSG_LOGIN: - length = 19; - break; - case MSG_LOCATION: - length = 34; - break; - default: - length = 0; - break; - } - - // Check length and return buffer - if (length != 0 && buf.readableBytes() >= length) { - return buf.readBytes(length); - } - - return null; - } - -} diff --git a/src/org/traccar/protocol/AutoFon45Protocol.java b/src/org/traccar/protocol/AutoFon45Protocol.java deleted file mode 100644 index cceb3e476..000000000 --- a/src/org/traccar/protocol/AutoFon45Protocol.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2015 Anton Tananaev (anton.tananaev@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.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelPipeline; -import org.traccar.BaseProtocol; -import org.traccar.TrackerServer; - -import java.util.List; - -public class AutoFon45Protocol extends BaseProtocol { - - public AutoFon45Protocol() { - super("autofon45"); - } - - @Override - public void initTrackerServers(List serverList) { - serverList.add(new TrackerServer(new ServerBootstrap(), this.getName()) { - @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { - pipeline.addLast("frameDecoder", new AutoFon45FrameDecoder()); - pipeline.addLast("objectDecoder", new AutoFon45ProtocolDecoder(AutoFon45Protocol.this)); - } - }); - } - -} diff --git a/src/org/traccar/protocol/AutoFon45ProtocolDecoder.java b/src/org/traccar/protocol/AutoFon45ProtocolDecoder.java deleted file mode 100644 index ad3097a68..000000000 --- a/src/org/traccar/protocol/AutoFon45ProtocolDecoder.java +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) - * Copyright 2015 Vitaly Litvak (vitavaque@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 java.net.SocketAddress; -import java.util.Arrays; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBuffers; -import org.jboss.netty.channel.Channel; -import org.traccar.BaseProtocolDecoder; -import org.traccar.helper.BitUtil; -import org.traccar.helper.DateBuilder; -import org.traccar.model.Event; -import org.traccar.model.Position; -import static org.traccar.protocol.AutoFon45FrameDecoder.MSG_LOCATION; -import static org.traccar.protocol.AutoFon45FrameDecoder.MSG_LOGIN; - -public class AutoFon45ProtocolDecoder extends BaseProtocolDecoder { - - public AutoFon45ProtocolDecoder(AutoFon45Protocol protocol) { - super(protocol); - } - - private static double convertCoordinate(short degrees, int minutes) { - double value = degrees + BitUtil.from(minutes, 4) / 600000.0; - if (BitUtil.check(minutes, 0)) { - return value; - } else { - return -value; - } - } - - private static byte checksum(byte[] bytes, int offset, int len) { - byte result = 0x3B; - for (int i = offset; i < offset + len; i++) { - result += 0x56 ^ bytes[i]; - result++; - result ^= 0xC5 + bytes[i]; - result--; - } - return result; - } - - @Override - protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - - ChannelBuffer buf = (ChannelBuffer) msg; - - int type = buf.getUnsignedByte(buf.readerIndex()); - - if (type == MSG_LOGIN) { - - byte[] bytes = new byte[19]; - buf.readBytes(bytes); - - String imei = ChannelBuffers.hexDump(ChannelBuffers.wrappedBuffer(bytes, 1, 8)).substring(1); - if (!identify(imei, channel, remoteAddress)) { - return null; - } - - // Send response (checksum) - if (channel != null) { - byte[] response = "resp_crc=".getBytes("US-ASCII"); - response = Arrays.copyOf(response, response.length + 1); - response[response.length - 1] = checksum(bytes, 0, 18); - channel.write(ChannelBuffers.wrappedBuffer(response)); - } - - } else if (type == MSG_LOCATION) { - - buf.readUnsignedByte(); - - Position position = new Position(); - position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); - - short status = buf.readUnsignedByte(); - position.set(Event.KEY_ALARM, BitUtil.check(status, 7)); - position.set(Event.KEY_BATTERY, BitUtil.to(status, 7)); - - buf.skipBytes(2); // remaining time - - position.set(Event.PREFIX_TEMP + 1, buf.readByte()); - - buf.skipBytes(2); // timer (interval and units) - buf.readByte(); // mode - buf.readByte(); // gprs sending interval - - buf.skipBytes(6); // mcc, mnc, lac, cid - - int valid = buf.readUnsignedByte(); - position.setValid(BitUtil.from(valid, 6) != 0); - position.set(Event.KEY_SATELLITES, BitUtil.from(valid, 6)); - - int time = buf.readUnsignedMedium(); - int date = buf.readUnsignedMedium(); - - DateBuilder dateBuilder = new DateBuilder() - .setTime(time / 10000, time / 100 % 100, time % 100) - .setDateReverse(date / 10000, date / 100 % 100, date % 100); - position.setTime(dateBuilder.getDate()); - - position.setLatitude(convertCoordinate(buf.readUnsignedByte(), buf.readUnsignedMedium())); - position.setLongitude(convertCoordinate(buf.readUnsignedByte(), buf.readUnsignedMedium())); - position.setSpeed(buf.readUnsignedByte()); - position.setCourse(buf.readUnsignedShort()); - - return position; - - } - - return null; - } - -} diff --git a/src/org/traccar/protocol/AutoFonFrameDecoder.java b/src/org/traccar/protocol/AutoFonFrameDecoder.java index 1ad9e6d3b..2c547d822 100644 --- a/src/org/traccar/protocol/AutoFonFrameDecoder.java +++ b/src/org/traccar/protocol/AutoFonFrameDecoder.java @@ -1,5 +1,6 @@ /* - * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2015 - 2016 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2015 Vitaly Litvak (vitavaque@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,10 +23,6 @@ import org.jboss.netty.handler.codec.frame.FrameDecoder; public class AutoFonFrameDecoder extends FrameDecoder { - public static final int MSG_LOGIN = 0x10; - public static final int MSG_LOCATION = 0x11; - public static final int MSG_HISTORY = 0x12; - @Override protected Object decode( ChannelHandlerContext ctx, @@ -39,15 +36,21 @@ public class AutoFonFrameDecoder extends FrameDecoder { int length; switch (buf.getUnsignedByte(buf.readerIndex())) { - case MSG_LOGIN: + case AutoFonProtocolDecoder.MSG_LOGIN: length = 12; break; - case MSG_LOCATION: + case AutoFonProtocolDecoder.MSG_LOCATION: length = 78; break; - case MSG_HISTORY: + case AutoFonProtocolDecoder.MSG_HISTORY: length = 257; break; + case AutoFonProtocolDecoder.MSG_45_LOGIN: + length = 19; + break; + case AutoFonProtocolDecoder.MSG_45_LOCATION: + length = 34; + break; default: length = 0; break; diff --git a/src/org/traccar/protocol/AutoFonProtocolDecoder.java b/src/org/traccar/protocol/AutoFonProtocolDecoder.java index 84df09a29..07dd56ca5 100644 --- a/src/org/traccar/protocol/AutoFonProtocolDecoder.java +++ b/src/org/traccar/protocol/AutoFonProtocolDecoder.java @@ -1,5 +1,6 @@ /* - * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2015 - 2016 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2015 Vitaly Litvak (vitavaque@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,12 +17,15 @@ package org.traccar.protocol; import java.net.SocketAddress; +import java.nio.charset.Charset; +import java.util.Arrays; import java.util.LinkedList; import java.util.List; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; import org.traccar.model.Event; import org.traccar.model.Position; @@ -36,12 +40,24 @@ public class AutoFonProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_LOCATION = 0x11; public static final int MSG_HISTORY = 0x12; + public static final int MSG_45_LOGIN = 0x41; + public static final int MSG_45_LOCATION = 0x02; + private static double convertCoordinate(int raw) { int degrees = raw / 1000000; double minutes = (raw % 1000000) / 10000.0; return degrees + minutes / 60; } + private static double convertCoordinate(short degrees, int minutes) { + double value = degrees + BitUtil.from(minutes, 4) / 600000.0; + if (BitUtil.check(minutes, 0)) { + return value; + } else { + return -value; + } + } + private Position decodePosition(ChannelBuffer buf, boolean history) { Position position = new Position(); @@ -134,6 +150,59 @@ public class AutoFonProtocolDecoder extends BaseProtocolDecoder { return positions; + } else if (type == MSG_45_LOGIN) { + + String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1); + if (!identify(imei, channel, remoteAddress)) { + return null; + } + + if (channel != null) { + ChannelBuffer response = ChannelBuffers.dynamicBuffer(); + response.writeBytes("resp_crc=".getBytes(Charset.defaultCharset())); + response.writeByte(buf.getByte(buf.writerIndex() - 1)); + channel.write(response); + } + + } else if (type == MSG_45_LOCATION) { + + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(getDeviceId()); + + short status = buf.readUnsignedByte(); + position.set(Event.KEY_ALARM, BitUtil.check(status, 7)); + position.set(Event.KEY_BATTERY, BitUtil.to(status, 7)); + + buf.skipBytes(2); // remaining time + + position.set(Event.PREFIX_TEMP + 1, buf.readByte()); + + buf.skipBytes(2); // timer (interval and units) + buf.readByte(); // mode + buf.readByte(); // gprs sending interval + + buf.skipBytes(6); // mcc, mnc, lac, cid + + int valid = buf.readUnsignedByte(); + position.setValid(BitUtil.from(valid, 6) != 0); + position.set(Event.KEY_SATELLITES, BitUtil.from(valid, 6)); + + int time = buf.readUnsignedMedium(); + int date = buf.readUnsignedMedium(); + + DateBuilder dateBuilder = new DateBuilder() + .setTime(time / 10000, time / 100 % 100, time % 100) + .setDateReverse(date / 10000, date / 100 % 100, date % 100); + position.setTime(dateBuilder.getDate()); + + position.setLatitude(convertCoordinate(buf.readUnsignedByte(), buf.readUnsignedMedium())); + position.setLongitude(convertCoordinate(buf.readUnsignedByte(), buf.readUnsignedMedium())); + position.setSpeed(buf.readUnsignedByte()); + position.setCourse(buf.readUnsignedShort()); + + return position; + } return null; diff --git a/test/org/traccar/protocol/AutoFon45ProtocolDecoderTest.java b/test/org/traccar/protocol/AutoFon45ProtocolDecoderTest.java deleted file mode 100644 index 2b4154b27..000000000 --- a/test/org/traccar/protocol/AutoFon45ProtocolDecoderTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.traccar.protocol; - -import org.junit.Test; -import org.traccar.ProtocolTest; - - -public class AutoFon45ProtocolDecoderTest extends ProtocolTest { - @Test - public void testDecode() throws Exception { - AutoFon45ProtocolDecoder decoder = new AutoFon45ProtocolDecoder(new AutoFon45Protocol()); - - verifyNothing(decoder, binary( - "41035151305289931441139602662095148807")); - - verifyNothing(decoder, binary( - "41032125656985547543619173484002123481")); - - verifyPosition(decoder, binary( - "023E00001E004D411EFA01772F185285009C48041F1E366C2961380F26B10B00911C"), - position("2010-01-27 04:00:08.000", true, 54.73838, 56.10343)); - - verifyPosition(decoder, binary( - "023E00001E004D411EFA01772F185285009C48041F1E366C2961380F26B10B00911C")); - } -} diff --git a/test/org/traccar/protocol/AutoFonProtocolDecoderTest.java b/test/org/traccar/protocol/AutoFonProtocolDecoderTest.java index 5c2e52d09..4a0a42a7b 100644 --- a/test/org/traccar/protocol/AutoFonProtocolDecoderTest.java +++ b/test/org/traccar/protocol/AutoFonProtocolDecoderTest.java @@ -20,6 +20,19 @@ public class AutoFonProtocolDecoderTest extends ProtocolTest { //verifyPosition(decoder, binary( // "12060000007501010B0C00089CFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000003E7FFFF02007601010B0C00269CFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000003E7FFFF4A007601010B0C01089CFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000003E7FFFF04007501010B0C01269CFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000003E7FFFF80007601010B0C02089CFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000003E7FFFFA6007501010B0C02231F5600FA000176F218C70000000000000000000000000000000000000003E7FFFF9629")); + verifyNothing(decoder, binary( + "41035151305289931441139602662095148807")); + + verifyNothing(decoder, binary( + "41032125656985547543619173484002123481")); + + verifyPosition(decoder, binary( + "023E00001E004D411EFA01772F185285009C48041F1E366C2961380F26B10B00911C"), + position("2010-01-27 04:00:08.000", true, 54.73838, 56.10343)); + + verifyPosition(decoder, binary( + "023E00001E004D411EFA01772F185285009C48041F1E366C2961380F26B10B00911C")); + } } -- cgit v1.2.3