From 7ccf052eec0d2d71903c99bd8620c2bbfa45f311 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 17 Dec 2015 20:43:07 +1300 Subject: Implement Arnavi communication protocol --- debug.xml | 1 + src/org/traccar/protocol/ArnaviProtocol.java | 45 ++++++++++ .../traccar/protocol/ArnaviProtocolDecoder.java | 98 ++++++++++++++++++++++ .../protocol/ArnaviProtocolDecoderTest.java | 21 +++++ 4 files changed, 165 insertions(+) create mode 100644 src/org/traccar/protocol/ArnaviProtocol.java create mode 100644 src/org/traccar/protocol/ArnaviProtocolDecoder.java create mode 100644 test/org/traccar/protocol/ArnaviProtocolDecoderTest.java diff --git a/debug.xml b/debug.xml index 53126404a..ccfb5579e 100644 --- a/debug.xml +++ b/debug.xml @@ -269,5 +269,6 @@ 5097 5098 5099 + 5100 diff --git a/src/org/traccar/protocol/ArnaviProtocol.java b/src/org/traccar/protocol/ArnaviProtocol.java new file mode 100644 index 000000000..6afd0282d --- /dev/null +++ b/src/org/traccar/protocol/ArnaviProtocol.java @@ -0,0 +1,45 @@ +/* + * 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.jboss.netty.handler.codec.frame.LineBasedFrameDecoder; +import org.jboss.netty.handler.codec.string.StringDecoder; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; + +import java.util.List; + +public class ArnaviProtocol extends BaseProtocol { + + public ArnaviProtocol() { + super("arnavi"); + } + + @Override + public void initTrackerServers(List serverList) { + serverList.add(new TrackerServer(new ServerBootstrap(), this.getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024)); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("objectDecoder", new ArnaviProtocolDecoder(ArnaviProtocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/ArnaviProtocolDecoder.java b/src/org/traccar/protocol/ArnaviProtocolDecoder.java new file mode 100644 index 000000000..47c459446 --- /dev/null +++ b/src/org/traccar/protocol/ArnaviProtocolDecoder.java @@ -0,0 +1,98 @@ +/* + * 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.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.helper.DateBuilder; +import org.traccar.helper.Parser; +import org.traccar.helper.PatternBuilder; +import org.traccar.model.Event; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.util.regex.Pattern; + +public class ArnaviProtocolDecoder extends BaseProtocolDecoder { + + public ArnaviProtocolDecoder(ArnaviProtocol 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 + .number("(dd)(dd)(dd),") // time + .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 + .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(); + position.setProtocol(getProtocolName()); + + if (!identify(parser.next(), channel, remoteAddress)) { + return null; + } + position.setDeviceId(getDeviceId()); + + position.set(Event.KEY_INDEX, parser.nextInt()); + position.set(Event.KEY_POWER, parser.nextInt() * 0.01); + position.set(Event.KEY_BATTERY, parser.nextInt() * 0.01); + position.set(Event.KEY_IGNITION, parser.nextInt() == 1); + position.set(Event.KEY_INPUT, parser.nextInt()); + position.set(Event.KEY_SATELLITES, parser.nextInt()); + + 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/test/org/traccar/protocol/ArnaviProtocolDecoderTest.java b/test/org/traccar/protocol/ArnaviProtocolDecoderTest.java new file mode 100644 index 000000000..31daedf24 --- /dev/null +++ b/test/org/traccar/protocol/ArnaviProtocolDecoderTest.java @@ -0,0 +1,21 @@ +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(new ArnaviProtocol()); + + 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