From 71735f0c9b17ad1c64a4335a196b4c765b4499c1 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 2 Feb 2020 16:26:46 -0800 Subject: Rename classes --- .../org/traccar/protocol/Arnavi4FrameDecoder.java | 67 -------- .../java/org/traccar/protocol/Arnavi4Protocol.java | 35 ---- .../traccar/protocol/Arnavi4ProtocolDecoder.java | 176 -------------------- .../protocol/ArnaviBinaryProtocolDecoder.java | 177 +++++++++++++++++++++ .../org/traccar/protocol/ArnaviFrameDecoder.java | 81 ++++++++++ .../java/org/traccar/protocol/ArnaviProtocol.java | 2 +- .../traccar/protocol/ArnaviProtocolDecoder.java | 105 ------------ .../protocol/ArnaviTextProtocolDecoder.java | 105 ++++++++++++ .../traccar/protocol/Arnavi4FrameDecoderTest.java | 47 ------ .../protocol/Arnavi4ProtocolDecoderTest.java | 38 ----- .../protocol/ArnaviBinaryProtocolDecoderTest.java | 38 +++++ .../traccar/protocol/ArnaviFrameDecoderTest.java | 51 ++++++ .../protocol/ArnaviProtocolDecoderTest.java | 42 ----- .../protocol/ArnaviTextProtocolDecoderTest.java | 42 +++++ 14 files changed, 495 insertions(+), 511 deletions(-) delete mode 100644 src/main/java/org/traccar/protocol/Arnavi4FrameDecoder.java delete mode 100644 src/main/java/org/traccar/protocol/Arnavi4Protocol.java delete mode 100644 src/main/java/org/traccar/protocol/Arnavi4ProtocolDecoder.java create mode 100644 src/main/java/org/traccar/protocol/ArnaviBinaryProtocolDecoder.java create mode 100644 src/main/java/org/traccar/protocol/ArnaviFrameDecoder.java delete mode 100644 src/main/java/org/traccar/protocol/ArnaviProtocolDecoder.java create mode 100644 src/main/java/org/traccar/protocol/ArnaviTextProtocolDecoder.java delete mode 100644 src/test/java/org/traccar/protocol/Arnavi4FrameDecoderTest.java delete mode 100644 src/test/java/org/traccar/protocol/Arnavi4ProtocolDecoderTest.java create mode 100644 src/test/java/org/traccar/protocol/ArnaviBinaryProtocolDecoderTest.java create mode 100644 src/test/java/org/traccar/protocol/ArnaviFrameDecoderTest.java delete mode 100644 src/test/java/org/traccar/protocol/ArnaviProtocolDecoderTest.java create mode 100644 src/test/java/org/traccar/protocol/ArnaviTextProtocolDecoderTest.java diff --git a/src/main/java/org/traccar/protocol/Arnavi4FrameDecoder.java b/src/main/java/org/traccar/protocol/Arnavi4FrameDecoder.java deleted file mode 100644 index 8a2681b55..000000000 --- a/src/main/java/org/traccar/protocol/Arnavi4FrameDecoder.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2020 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Ivan Muratov (binakot@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 io.netty.buffer.ByteBuf; -import io.netty.channel.Channel; -import io.netty.channel.ChannelHandlerContext; -import org.traccar.BaseFrameDecoder; - -public class Arnavi4FrameDecoder extends BaseFrameDecoder { - - private static final int HEADER_LENGTH = 10; - private static final int PACKET_WRAPPER_LENGTH = 8; - private static final int RESULT_TYPE = 0xfd; - private static final byte PACKAGE_END_SIGN = 0x5d; - - private boolean firstPacket = true; - - @Override - protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { - - if (buf.readableBytes() < 4) { - return null; - } - - int length; - if (firstPacket) { - firstPacket = false; - length = HEADER_LENGTH; - } else { - int type = buf.getUnsignedByte(1); - if (type == RESULT_TYPE) { - length = 4; - } else { - int index = 2; - while (index + PACKET_WRAPPER_LENGTH < buf.readableBytes() && buf.getByte(index) != PACKAGE_END_SIGN) { - index += PACKET_WRAPPER_LENGTH + buf.getUnsignedShortLE(index + 1); - } - if (buf.getByte(index) != PACKAGE_END_SIGN) { - return null; - } - length = index + 1; - } - } - - if (buf.readableBytes() >= length) { - return buf.readBytes(length); - } - - return null; - } - -} diff --git a/src/main/java/org/traccar/protocol/Arnavi4Protocol.java b/src/main/java/org/traccar/protocol/Arnavi4Protocol.java deleted file mode 100644 index 8a9337b56..000000000 --- a/src/main/java/org/traccar/protocol/Arnavi4Protocol.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2020 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Ivan Muratov (binakot@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.traccar.BaseProtocol; -import org.traccar.PipelineBuilder; -import org.traccar.TrackerServer; - -public class Arnavi4Protocol extends BaseProtocol { - - public Arnavi4Protocol() { - TrackerServer server = new TrackerServer(false, getName()) { - @Override - protected void addProtocolHandlers(PipelineBuilder pipeline) { - pipeline.addLast(new Arnavi4FrameDecoder()); - pipeline.addLast(new Arnavi4ProtocolDecoder(Arnavi4Protocol.this)); - } - }; - } - -} diff --git a/src/main/java/org/traccar/protocol/Arnavi4ProtocolDecoder.java b/src/main/java/org/traccar/protocol/Arnavi4ProtocolDecoder.java deleted file mode 100644 index 3e0fa5d9f..000000000 --- a/src/main/java/org/traccar/protocol/Arnavi4ProtocolDecoder.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright 2020 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Ivan Muratov (binakot@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 io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import io.netty.channel.Channel; -import org.traccar.BaseProtocolDecoder; -import org.traccar.DeviceSession; -import org.traccar.NetworkMessage; -import org.traccar.helper.Checksum; -import org.traccar.model.Position; - -import java.net.SocketAddress; -import java.nio.ByteBuffer; -import java.util.Date; -import java.util.LinkedList; -import java.util.List; - -public class Arnavi4ProtocolDecoder extends BaseProtocolDecoder { - - private static final byte HEADER_START_SIGN = (byte) 0xff; - private static final byte HEADER_VERSION_1 = 0x22; - private static final byte HEADER_VERSION_2 = 0x23; - - private static final byte RECORD_PING = 0x00; - private static final byte RECORD_DATA = 0x01; - private static final byte RECORD_TEXT = 0x03; - private static final byte RECORD_FILE = 0x04; - private static final byte RECORD_BINARY = 0x06; - - private static final byte TAG_LATITUDE = 3; - private static final byte TAG_LONGITUDE = 4; - private static final byte TAG_COORD_PARAMS = 5; - - public Arnavi4ProtocolDecoder(Arnavi4Protocol protocol) { - super(protocol); - } - - private void sendResponse(Channel channel, byte version, int index) { - if (channel != null) { - ByteBuf response = Unpooled.buffer(); - response.writeByte(0x7b); - if (version == HEADER_VERSION_1) { - response.writeByte(0x00); - response.writeByte((byte) index); - } else if (version == HEADER_VERSION_2) { - response.writeByte(0x04); - response.writeByte(0x00); - ByteBuffer time = ByteBuffer.allocate(4).putInt((int) (System.currentTimeMillis() / 1000)); - response.writeByte(Checksum.modulo256(time)); - response.writeBytes(time); - } - response.writeByte(0x7d); - channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress())); - } - } - - private Position decodePosition(DeviceSession deviceSession, ByteBuf buf, int length, Date time) { - - final Position position = new Position(); - position.setProtocol(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); - - position.setTime(time); - - int readBytes = 0; - while (readBytes < length) { - short tag = buf.readUnsignedByte(); - switch (tag) { - case TAG_LATITUDE: - position.setLatitude(buf.readFloatLE()); - position.setValid(true); - break; - - case TAG_LONGITUDE: - position.setLongitude(buf.readFloatLE()); - position.setValid(true); - break; - - case TAG_COORD_PARAMS: - position.setCourse(buf.readUnsignedByte() * 2); - position.setAltitude(buf.readUnsignedByte() * 10); - byte satellites = buf.readByte(); - position.set(Position.KEY_SATELLITES, satellites & 0x0F + (satellites >> 4) & 0x0F); - position.setSpeed(buf.readUnsignedByte()); - break; - - default: - buf.skipBytes(4); - break; - } - - readBytes += 1 + 4; - } - - return position; - } - - @Override - protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - - ByteBuf buf = (ByteBuf) msg; - - byte startSign = buf.readByte(); - - if (startSign == HEADER_START_SIGN) { - - byte version = buf.readByte(); - - String imei = String.valueOf(buf.readLongLE()); - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); - - if (deviceSession != null) { - sendResponse(channel, version, 0); - } - - return null; - } - - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); - if (deviceSession == null) { - return null; - } - - List positions = new LinkedList<>(); - - int index = buf.readUnsignedByte(); - - byte recordType = buf.readByte(); - while (buf.readableBytes() > 0) { - switch (recordType) { - case RECORD_PING: - case RECORD_DATA: - case RECORD_TEXT: - case RECORD_FILE: - case RECORD_BINARY: - int length = buf.readUnsignedShortLE(); - Date time = new Date(buf.readUnsignedIntLE() * 1000); - - if (recordType == RECORD_DATA) { - positions.add(decodePosition(deviceSession, buf, length, time)); - } else { - buf.readBytes(length); - } - - buf.readUnsignedByte(); // checksum - break; - - default: - return null; - } - - recordType = buf.readByte(); - } - - sendResponse(channel, HEADER_VERSION_1, index); - - return positions; - } - -} diff --git a/src/main/java/org/traccar/protocol/ArnaviBinaryProtocolDecoder.java b/src/main/java/org/traccar/protocol/ArnaviBinaryProtocolDecoder.java new file mode 100644 index 000000000..0924cfc40 --- /dev/null +++ b/src/main/java/org/traccar/protocol/ArnaviBinaryProtocolDecoder.java @@ -0,0 +1,177 @@ +/* + * Copyright 2020 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Ivan Muratov (binakot@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 io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.NetworkMessage; +import org.traccar.Protocol; +import org.traccar.helper.Checksum; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.nio.ByteBuffer; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; + +public class ArnaviBinaryProtocolDecoder extends BaseProtocolDecoder { + + private static final byte HEADER_START_SIGN = (byte) 0xff; + private static final byte HEADER_VERSION_1 = 0x22; + private static final byte HEADER_VERSION_2 = 0x23; + + private static final byte RECORD_PING = 0x00; + private static final byte RECORD_DATA = 0x01; + private static final byte RECORD_TEXT = 0x03; + private static final byte RECORD_FILE = 0x04; + private static final byte RECORD_BINARY = 0x06; + + private static final byte TAG_LATITUDE = 3; + private static final byte TAG_LONGITUDE = 4; + private static final byte TAG_COORD_PARAMS = 5; + + public ArnaviBinaryProtocolDecoder(Protocol protocol) { + super(protocol); + } + + private void sendResponse(Channel channel, byte version, int index) { + if (channel != null) { + ByteBuf response = Unpooled.buffer(); + response.writeByte(0x7b); + if (version == HEADER_VERSION_1) { + response.writeByte(0x00); + response.writeByte((byte) index); + } else if (version == HEADER_VERSION_2) { + response.writeByte(0x04); + response.writeByte(0x00); + ByteBuffer time = ByteBuffer.allocate(4).putInt((int) (System.currentTimeMillis() / 1000)); + response.writeByte(Checksum.modulo256(time)); + response.writeBytes(time); + } + response.writeByte(0x7d); + channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress())); + } + } + + private Position decodePosition(DeviceSession deviceSession, ByteBuf buf, int length, Date time) { + + final Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + position.setTime(time); + + int readBytes = 0; + while (readBytes < length) { + short tag = buf.readUnsignedByte(); + switch (tag) { + case TAG_LATITUDE: + position.setLatitude(buf.readFloatLE()); + position.setValid(true); + break; + + case TAG_LONGITUDE: + position.setLongitude(buf.readFloatLE()); + position.setValid(true); + break; + + case TAG_COORD_PARAMS: + position.setCourse(buf.readUnsignedByte() * 2); + position.setAltitude(buf.readUnsignedByte() * 10); + byte satellites = buf.readByte(); + position.set(Position.KEY_SATELLITES, satellites & 0x0F + (satellites >> 4) & 0x0F); + position.setSpeed(buf.readUnsignedByte()); + break; + + default: + buf.skipBytes(4); + break; + } + + readBytes += 1 + 4; + } + + return position; + } + + @Override + protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + ByteBuf buf = (ByteBuf) msg; + + byte startSign = buf.readByte(); + + if (startSign == HEADER_START_SIGN) { + + byte version = buf.readByte(); + + String imei = String.valueOf(buf.readLongLE()); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + + if (deviceSession != null) { + sendResponse(channel, version, 0); + } + + return null; + } + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } + + List positions = new LinkedList<>(); + + int index = buf.readUnsignedByte(); + + byte recordType = buf.readByte(); + while (buf.readableBytes() > 0) { + switch (recordType) { + case RECORD_PING: + case RECORD_DATA: + case RECORD_TEXT: + case RECORD_FILE: + case RECORD_BINARY: + int length = buf.readUnsignedShortLE(); + Date time = new Date(buf.readUnsignedIntLE() * 1000); + + if (recordType == RECORD_DATA) { + positions.add(decodePosition(deviceSession, buf, length, time)); + } else { + buf.readBytes(length); + } + + buf.readUnsignedByte(); // checksum + break; + + default: + return null; + } + + recordType = buf.readByte(); + } + + sendResponse(channel, HEADER_VERSION_1, index); + + return positions; + } + +} diff --git a/src/main/java/org/traccar/protocol/ArnaviFrameDecoder.java b/src/main/java/org/traccar/protocol/ArnaviFrameDecoder.java new file mode 100644 index 000000000..3db11113d --- /dev/null +++ b/src/main/java/org/traccar/protocol/ArnaviFrameDecoder.java @@ -0,0 +1,81 @@ +/* + * Copyright 2020 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Ivan Muratov (binakot@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 io.netty.buffer.ByteBuf; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import org.traccar.BaseFrameDecoder; +import org.traccar.helper.BufferUtil; + +public class ArnaviFrameDecoder extends BaseFrameDecoder { + + private static final int HEADER_LENGTH = 10; + private static final int PACKET_WRAPPER_LENGTH = 8; + private static final int RESULT_TYPE = 0xfd; + private static final byte PACKAGE_END_SIGN = 0x5d; + + private boolean firstPacket = true; + + @Override + protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { + + if (buf.readableBytes() < 4) { + return null; + } + + if (buf.getByte(buf.readerIndex()) == '$') { + + int index = BufferUtil.indexOf("\r\n", buf); + if (index > 0) { + ByteBuf frame = buf.readRetainedSlice(index - buf.readerIndex()); + buf.skipBytes(2); + return frame; + } + + } else { + + int length; + if (firstPacket) { + firstPacket = false; + length = HEADER_LENGTH; + } else { + int type = buf.getUnsignedByte(1); + if (type == RESULT_TYPE) { + length = 4; + } else { + int index = 2; + while (index + PACKET_WRAPPER_LENGTH < buf.readableBytes() && buf.getByte(index) != PACKAGE_END_SIGN) { + index += PACKET_WRAPPER_LENGTH + buf.getUnsignedShortLE(index + 1); + } + if (buf.getByte(index) != PACKAGE_END_SIGN) { + return null; + } + length = index + 1; + } + } + + if (buf.readableBytes() >= length) { + return buf.readRetainedSlice(length); + } + + } + + return null; + } + +} diff --git a/src/main/java/org/traccar/protocol/ArnaviProtocol.java b/src/main/java/org/traccar/protocol/ArnaviProtocol.java index afe491865..11101097b 100644 --- a/src/main/java/org/traccar/protocol/ArnaviProtocol.java +++ b/src/main/java/org/traccar/protocol/ArnaviProtocol.java @@ -31,7 +31,7 @@ public class ArnaviProtocol extends BaseProtocol { pipeline.addLast(new LineBasedFrameDecoder(1024)); pipeline.addLast(new StringDecoder()); pipeline.addLast(new StringEncoder()); - pipeline.addLast(new ArnaviProtocolDecoder(ArnaviProtocol.this)); + pipeline.addLast(new ArnaviTextProtocolDecoder(ArnaviProtocol.this)); } }); } diff --git a/src/main/java/org/traccar/protocol/ArnaviProtocolDecoder.java b/src/main/java/org/traccar/protocol/ArnaviProtocolDecoder.java deleted file mode 100644 index 7996cf429..000000000 --- a/src/main/java/org/traccar/protocol/ArnaviProtocolDecoder.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright 2015 - 2019 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. - * 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 io.netty.channel.Channel; -import org.traccar.BaseProtocolDecoder; -import org.traccar.DeviceSession; -import org.traccar.Protocol; -import org.traccar.helper.DateBuilder; -import org.traccar.helper.Parser; -import org.traccar.helper.PatternBuilder; -import org.traccar.model.Position; - -import java.net.SocketAddress; -import java.util.regex.Pattern; - -public class ArnaviProtocolDecoder extends BaseProtocolDecoder { - - public ArnaviProtocolDecoder(Protocol protocol) { - super(protocol); - } - - private static final Pattern PATTERN = new PatternBuilder() - .text("$AV,") - .number("Vd,") // type - .number("(d+),") // device id - .number("(d+),") // index - .number("(d+),") // power - .number("(d+),") // battery - .number("-?d+,") - .expression("[01],") // movement - .expression("([01]),") // ignition - .number("(d+),") // input - .number("d+,d+,") // input 1 - .number("d+,d+,").optional() // input 2 - .expression("[01],") // fix type - .number("(d+),") // satellites - .groupBegin() - .number("(d+.d+)?,") // altitude - .number("(?:d+.d+)?,") // geoid height - .groupEnd("?") - .number("(dd)(dd)(dd),") // time (hhmmss) - .number("(dd)(dd.d+)([NS]),") // latitude - .number("(ddd)(dd.d+)([EW]),") // longitude - .number("(d+.d+),") // speed - .number("(d+.d+),") // course - .number("(dd)(dd)(dd)") // date (ddmmyy) - .any() - .compile(); - - @Override - protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - - Parser parser = new Parser(PATTERN, (String) msg); - if (!parser.matches()) { - return null; - } - - Position position = new Position(getProtocolName()); - - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); - if (deviceSession == null) { - return null; - } - position.setDeviceId(deviceSession.getDeviceId()); - - position.set(Position.KEY_INDEX, parser.nextInt()); - position.set(Position.KEY_POWER, parser.nextInt() * 0.01); - position.set(Position.KEY_BATTERY, parser.nextInt() * 0.01); - position.set(Position.KEY_IGNITION, parser.nextInt() == 1); - position.set(Position.KEY_INPUT, parser.nextInt()); - position.set(Position.KEY_SATELLITES, parser.nextInt()); - - position.setAltitude(parser.nextDouble(0)); - - DateBuilder dateBuilder = new DateBuilder() - .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); - - position.setValid(true); - position.setLatitude(parser.nextCoordinate()); - position.setLongitude(parser.nextCoordinate()); - position.setSpeed(parser.nextDouble()); - position.setCourse(parser.nextDouble()); - - dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); - position.setTime(dateBuilder.getDate()); - - return position; - } - -} diff --git a/src/main/java/org/traccar/protocol/ArnaviTextProtocolDecoder.java b/src/main/java/org/traccar/protocol/ArnaviTextProtocolDecoder.java new file mode 100644 index 000000000..8c4f743bc --- /dev/null +++ b/src/main/java/org/traccar/protocol/ArnaviTextProtocolDecoder.java @@ -0,0 +1,105 @@ +/* + * Copyright 2015 - 2019 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. + * 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 io.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.Protocol; +import org.traccar.helper.DateBuilder; +import org.traccar.helper.Parser; +import org.traccar.helper.PatternBuilder; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.util.regex.Pattern; + +public class ArnaviTextProtocolDecoder extends BaseProtocolDecoder { + + public ArnaviTextProtocolDecoder(Protocol protocol) { + super(protocol); + } + + private static final Pattern PATTERN = new PatternBuilder() + .text("$AV,") + .number("Vd,") // type + .number("(d+),") // device id + .number("(d+),") // index + .number("(d+),") // power + .number("(d+),") // battery + .number("-?d+,") + .expression("[01],") // movement + .expression("([01]),") // ignition + .number("(d+),") // input + .number("d+,d+,") // input 1 + .number("d+,d+,").optional() // input 2 + .expression("[01],") // fix type + .number("(d+),") // satellites + .groupBegin() + .number("(d+.d+)?,") // altitude + .number("(?:d+.d+)?,") // geoid height + .groupEnd("?") + .number("(dd)(dd)(dd),") // time (hhmmss) + .number("(dd)(dd.d+)([NS]),") // latitude + .number("(ddd)(dd.d+)([EW]),") // longitude + .number("(d+.d+),") // speed + .number("(d+.d+),") // course + .number("(dd)(dd)(dd)") // date (ddmmyy) + .any() + .compile(); + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + Parser parser = new Parser(PATTERN, (String) msg); + if (!parser.matches()) { + return null; + } + + Position position = new Position(getProtocolName()); + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { + return null; + } + position.setDeviceId(deviceSession.getDeviceId()); + + position.set(Position.KEY_INDEX, parser.nextInt()); + position.set(Position.KEY_POWER, parser.nextInt() * 0.01); + position.set(Position.KEY_BATTERY, parser.nextInt() * 0.01); + position.set(Position.KEY_IGNITION, parser.nextInt() == 1); + position.set(Position.KEY_INPUT, parser.nextInt()); + position.set(Position.KEY_SATELLITES, parser.nextInt()); + + position.setAltitude(parser.nextDouble(0)); + + DateBuilder dateBuilder = new DateBuilder() + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + + position.setValid(true); + position.setLatitude(parser.nextCoordinate()); + position.setLongitude(parser.nextCoordinate()); + position.setSpeed(parser.nextDouble()); + position.setCourse(parser.nextDouble()); + + dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); + + return position; + } + +} diff --git a/src/test/java/org/traccar/protocol/Arnavi4FrameDecoderTest.java b/src/test/java/org/traccar/protocol/Arnavi4FrameDecoderTest.java deleted file mode 100644 index aa3f85820..000000000 --- a/src/test/java/org/traccar/protocol/Arnavi4FrameDecoderTest.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.traccar.protocol; - -import org.junit.Test; -import org.traccar.ProtocolTest; - -public class Arnavi4FrameDecoderTest extends ProtocolTest { - - @Test - public void testDecodeValidPackets() throws Exception { - - Arnavi4FrameDecoder decoder = new Arnavi4FrameDecoder(); - - verifyFrame( - binary("ff22f30c45f5c90f0300"), - decoder.decode(null, null, binary("ff22f30c45f5c90f0300"))); - - verifyFrame( - binary("5b01012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa37010000295d"), - decoder.decode(null, null, binary("5b01012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa37010000295d"))); - - verifyFrame( - binary("5b01012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa3701000029012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa37010000295d"), - decoder.decode(null, null, binary("5b01012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa3701000029012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa37010000295d"))); - - verifyFrame( - binary("5b01030700e3f16b50747261636361721b5d"), - decoder.decode(null, null, binary("5b01030700e3f16b50747261636361721b5d"))); - - verifyFrame( - binary("5b01030700e3f16b50747261636361721b030700e3f16b50747261636361721b5d"), - decoder.decode(null, null, binary("5b01030700e3f16b50747261636361721b030700e3f16b50747261636361721b5d"))); - - verifyFrame( - binary("5b01061400e3f16b5003298b5e4204cbd514420500191000080400ff021b5d"), - decoder.decode(null, null, binary("5b01061400e3f16b5003298b5e4204cbd514420500191000080400ff021b5d"))); - - verifyFrame( - binary("5b01061400e3f16b5003298b5e4204cbd514420500191000080400ff021b061400e3f16b5003298b5e4204cbd514420500191000080400ff021b5d"), - decoder.decode(null, null, binary("5b01061400e3f16b5003298b5e4204cbd514420500191000080400ff021b061400e3f16b5003298b5e4204cbd514420500191000080400ff021b5d"))); - - verifyFrame( - binary("5bfd005d"), - decoder.decode(null, null, binary("5bfd005d"))); - - } - -} diff --git a/src/test/java/org/traccar/protocol/Arnavi4ProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/Arnavi4ProtocolDecoderTest.java deleted file mode 100644 index 2628188b9..000000000 --- a/src/test/java/org/traccar/protocol/Arnavi4ProtocolDecoderTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.traccar.protocol; - -import org.junit.Test; -import org.traccar.ProtocolTest; - -public class Arnavi4ProtocolDecoderTest extends ProtocolTest { - - @Test - public void testHeader1Decode() throws Exception { - - Arnavi4ProtocolDecoder decoder; - - decoder = new Arnavi4ProtocolDecoder(new Arnavi4Protocol()); - - verifyNull(decoder, binary( - "ff22f30c45f5c90f0300")); - - verifyPositions(decoder, binary( - "5b01012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa37010000295d"), - position("2017-07-07 05:09:55.000", true, 45.05597, 39.03347)); - } - - @Test - public void testHeader2Decode() throws Exception { - - Arnavi4ProtocolDecoder decoder; - - decoder = new Arnavi4ProtocolDecoder(new Arnavi4Protocol()); - - verifyNull(decoder, binary( - "ff23f30c45f5c90f0300")); - - verifyPositions(decoder, binary( - "5b01012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa3701000029012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa37010000295d"), - position("2017-07-07 05:09:55.000", true, 45.05597, 39.03347)); - } - -} diff --git a/src/test/java/org/traccar/protocol/ArnaviBinaryProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/ArnaviBinaryProtocolDecoderTest.java new file mode 100644 index 000000000..f2940de59 --- /dev/null +++ b/src/test/java/org/traccar/protocol/ArnaviBinaryProtocolDecoderTest.java @@ -0,0 +1,38 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class ArnaviBinaryProtocolDecoderTest extends ProtocolTest { + + @Test + public void testHeader1Decode() throws Exception { + + ArnaviBinaryProtocolDecoder decoder; + + decoder = new ArnaviBinaryProtocolDecoder(null); + + verifyNull(decoder, binary( + "ff22f30c45f5c90f0300")); + + verifyPositions(decoder, binary( + "5b01012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa37010000295d"), + position("2017-07-07 05:09:55.000", true, 45.05597, 39.03347)); + } + + @Test + public void testHeader2Decode() throws Exception { + + ArnaviBinaryProtocolDecoder decoder; + + decoder = new ArnaviBinaryProtocolDecoder(null); + + verifyNull(decoder, binary( + "ff23f30c45f5c90f0300")); + + verifyPositions(decoder, binary( + "5b01012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa3701000029012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa37010000295d"), + position("2017-07-07 05:09:55.000", true, 45.05597, 39.03347)); + } + +} diff --git a/src/test/java/org/traccar/protocol/ArnaviFrameDecoderTest.java b/src/test/java/org/traccar/protocol/ArnaviFrameDecoderTest.java new file mode 100644 index 000000000..90eb20296 --- /dev/null +++ b/src/test/java/org/traccar/protocol/ArnaviFrameDecoderTest.java @@ -0,0 +1,51 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class ArnaviFrameDecoderTest extends ProtocolTest { + + @Test + public void testDecodeValidPackets() throws Exception { + + ArnaviFrameDecoder decoder = new ArnaviFrameDecoder(); + + verifyFrame( + binary("2441562c563344492c38353136342c3231342c2d312c31392c30303030344634462c30303030303935452c30433030303030322c3836333037313031333034313631382c38393939373031353630333832353236363232462c2a3039"), + decoder.decode(null, null, binary("2441562c563344492c38353136342c3231342c2d312c31392c30303030344634462c30303030303935452c30433030303030322c3836333037313031333034313631382c38393939373031353630333832353236363232462c2a30390d0a"))); + + verifyFrame( + binary("ff22f30c45f5c90f0300"), + decoder.decode(null, null, binary("ff22f30c45f5c90f0300"))); + + verifyFrame( + binary("5b01012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa37010000295d"), + decoder.decode(null, null, binary("5b01012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa37010000295d"))); + + verifyFrame( + binary("5b01012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa3701000029012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa37010000295d"), + decoder.decode(null, null, binary("5b01012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa3701000029012800a3175f5903513934420447221c42055402781E0900f0c5215b4e0084005c00007c005d0000a300fa37010000295d"))); + + verifyFrame( + binary("5b01030700e3f16b50747261636361721b5d"), + decoder.decode(null, null, binary("5b01030700e3f16b50747261636361721b5d"))); + + verifyFrame( + binary("5b01030700e3f16b50747261636361721b030700e3f16b50747261636361721b5d"), + decoder.decode(null, null, binary("5b01030700e3f16b50747261636361721b030700e3f16b50747261636361721b5d"))); + + verifyFrame( + binary("5b01061400e3f16b5003298b5e4204cbd514420500191000080400ff021b5d"), + decoder.decode(null, null, binary("5b01061400e3f16b5003298b5e4204cbd514420500191000080400ff021b5d"))); + + verifyFrame( + binary("5b01061400e3f16b5003298b5e4204cbd514420500191000080400ff021b061400e3f16b5003298b5e4204cbd514420500191000080400ff021b5d"), + decoder.decode(null, null, binary("5b01061400e3f16b5003298b5e4204cbd514420500191000080400ff021b061400e3f16b5003298b5e4204cbd514420500191000080400ff021b5d"))); + + verifyFrame( + binary("5bfd005d"), + decoder.decode(null, null, binary("5bfd005d"))); + + } + +} diff --git a/src/test/java/org/traccar/protocol/ArnaviProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/ArnaviProtocolDecoderTest.java deleted file mode 100644 index 6b075facc..000000000 --- a/src/test/java/org/traccar/protocol/ArnaviProtocolDecoderTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.traccar.protocol; - -import org.junit.Test; -import org.traccar.ProtocolTest; - -public class ArnaviProtocolDecoderTest extends ProtocolTest { - - @Test - public void testDecode() throws Exception { - - ArnaviProtocolDecoder decoder = new ArnaviProtocolDecoder(null); - - verifyPosition(decoder, text( - "$AV,V4,999999,12487,2277,203,65534,0,0,193,65535,65535,65535,65535,1,13,80.0,56.1,200741,5950.6773N,03029.1043E,300.0,360.0,121012,65535,65535,65535,SF*6E")); - - verifyNull(decoder, text( - "$AV,V3DI,85164,20707,-1,19,0008C56A,000879AC,0C000002,863071013041618,89997077111301204297,*0B")); - - verifyNull(decoder, text( - "$AV,V6SD,85164,20708,-1,3,6,37,33,*52")); - - verifyAttributes(decoder, text( - "$AV,V4,85164,20709,1148,418,-1,0,1,192,0,0,0,0,0,0,,,000023,0000.0000N,00000.0000E,0.0,0.0,060180,0,0,32767,*4F")); - - verifyNull(decoder, text( - "$AV,V3GSMINFO,85164,-1,20450,KMOBILE,1,2,1,23,0,40101,cc3,1,ce19,401,16,65304,5613,72,,SF*7F")); - - verifyAttributes(decoder, text( - "$AV,V4,85164,20451,1146,418,-1,1,1,192,0,0,0,0,0,0,,,104340,0000.0000N,00000.0000E,0.0,0.0,060219,11,0,32767,,SF*47")); - - verifyNull(decoder, text( - "$AV,V6SD,85164,20452,-1,3,3,5,6769,,SF*5D")); - - verifyPosition(decoder, text( - "$AV,V2,32768,12487,2277,203,-1,0,0,193,0,0,1,13,200741,5950.6773N,03029.1043E,0.0,0.0,121012,*6E")); - - verifyPosition(decoder, text( - "$AV,V3,999999,12487,2277,203,65534,0,0,193,65535,65535,65535,65535,1,13,200741,5950.6773N,03029.1043E,300.0,360.0,121012,65535,65535,65535,SF*6E")); - - } - -} diff --git a/src/test/java/org/traccar/protocol/ArnaviTextProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/ArnaviTextProtocolDecoderTest.java new file mode 100644 index 000000000..065a28580 --- /dev/null +++ b/src/test/java/org/traccar/protocol/ArnaviTextProtocolDecoderTest.java @@ -0,0 +1,42 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class ArnaviTextProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + ArnaviTextProtocolDecoder decoder = new ArnaviTextProtocolDecoder(null); + + verifyPosition(decoder, text( + "$AV,V4,999999,12487,2277,203,65534,0,0,193,65535,65535,65535,65535,1,13,80.0,56.1,200741,5950.6773N,03029.1043E,300.0,360.0,121012,65535,65535,65535,SF*6E")); + + verifyNull(decoder, text( + "$AV,V3DI,85164,20707,-1,19,0008C56A,000879AC,0C000002,863071013041618,89997077111301204297,*0B")); + + verifyNull(decoder, text( + "$AV,V6SD,85164,20708,-1,3,6,37,33,*52")); + + verifyAttributes(decoder, text( + "$AV,V4,85164,20709,1148,418,-1,0,1,192,0,0,0,0,0,0,,,000023,0000.0000N,00000.0000E,0.0,0.0,060180,0,0,32767,*4F")); + + verifyNull(decoder, text( + "$AV,V3GSMINFO,85164,-1,20450,KMOBILE,1,2,1,23,0,40101,cc3,1,ce19,401,16,65304,5613,72,,SF*7F")); + + verifyAttributes(decoder, text( + "$AV,V4,85164,20451,1146,418,-1,1,1,192,0,0,0,0,0,0,,,104340,0000.0000N,00000.0000E,0.0,0.0,060219,11,0,32767,,SF*47")); + + verifyNull(decoder, text( + "$AV,V6SD,85164,20452,-1,3,3,5,6769,,SF*5D")); + + verifyPosition(decoder, text( + "$AV,V2,32768,12487,2277,203,-1,0,0,193,0,0,1,13,200741,5950.6773N,03029.1043E,0.0,0.0,121012,*6E")); + + verifyPosition(decoder, text( + "$AV,V3,999999,12487,2277,203,65534,0,0,193,65535,65535,65535,65535,1,13,200741,5950.6773N,03029.1043E,300.0,360.0,121012,65535,65535,65535,SF*6E")); + + } + +} -- cgit v1.2.3