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 ++++++++++++ 8 files changed, 364 insertions(+), 384 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 (limited to 'src/main/java/org/traccar') 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; + } + +} -- cgit v1.2.3