From 1a34d34e216bb60025d972a57e668673c021f20a Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 18 Mar 2016 10:19:33 +1300 Subject: Implement GNX communication protocol --- debug.xml | 1 + src/org/traccar/protocol/GnxProtocol.java | 45 +++++++++ src/org/traccar/protocol/GnxProtocolDecoder.java | 104 +++++++++++++++++++++ .../traccar/protocol/GnxProtocolDecoderTest.java | 27 ++++++ 4 files changed, 177 insertions(+) create mode 100644 src/org/traccar/protocol/GnxProtocol.java create mode 100644 src/org/traccar/protocol/GnxProtocolDecoder.java create mode 100644 test/org/traccar/protocol/GnxProtocolDecoderTest.java diff --git a/debug.xml b/debug.xml index cadfdd9f2..91caf842b 100644 --- a/debug.xml +++ b/debug.xml @@ -298,5 +298,6 @@ 5103 5104 5105 + 5106 diff --git a/src/org/traccar/protocol/GnxProtocol.java b/src/org/traccar/protocol/GnxProtocol.java new file mode 100644 index 000000000..34d9554b1 --- /dev/null +++ b/src/org/traccar/protocol/GnxProtocol.java @@ -0,0 +1,45 @@ +/* + * Copyright 2016 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 GnxProtocol extends BaseProtocol { + + public GnxProtocol() { + super("gnx"); + } + + @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 GnxProtocolDecoder(GnxProtocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/GnxProtocolDecoder.java b/src/org/traccar/protocol/GnxProtocolDecoder.java new file mode 100644 index 000000000..88cc90df2 --- /dev/null +++ b/src/org/traccar/protocol/GnxProtocolDecoder.java @@ -0,0 +1,104 @@ +/* + * Copyright 2016 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.helper.UnitsConverter; +import org.traccar.model.Event; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.util.TimeZone; +import java.util.regex.Pattern; + +public class GnxProtocolDecoder extends BaseProtocolDecoder { + + public GnxProtocolDecoder(GnxProtocol protocol) { + super(protocol); + } + + private static final Pattern PATTERN = new PatternBuilder() + .text("$GNX_") + .expression("...,") // type + .number("(d+),") // imei + .number("d+,") // length + .expression("([01]),") // history + .number("(dd)(dd)(dd),") // device time + .number("(dd)(dd)(dd),") // device date + .number("(dd)(dd)(dd),") // fix time + .number("(dd)(dd)(dd),") // fix date + .number("(d),") // valid + .number("(dd.d+),") // latitude + .expression("([NS]),") + .number("(ddd.d+),") // longitude + .expression("([EW]),") + .number("(d+.?d*),") // speed + .number("(d+.?d*),") // course + .number("(d+),") // satellites + .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()); + + if (parser.nextInt() == 1) { + position.set(Event.KEY_ARCHIVE, true); + } + + DateBuilder dateBuilder; + + dateBuilder = new DateBuilder(TimeZone.getTimeZone("GMT+5:30")) + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setDeviceTime(dateBuilder.getDate()); + + dateBuilder = new DateBuilder(TimeZone.getTimeZone("GMT+5:30")) + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setFixTime(dateBuilder.getDate()); + + position.setValid(parser.nextInt() != 0); + + position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM)); + position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM)); + + position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble())); + position.setCourse(parser.nextDouble()); + + position.set(Event.KEY_SATELLITES, parser.nextInt()); + + return position; + } + +} diff --git a/test/org/traccar/protocol/GnxProtocolDecoderTest.java b/test/org/traccar/protocol/GnxProtocolDecoderTest.java new file mode 100644 index 000000000..c1b8860d8 --- /dev/null +++ b/test/org/traccar/protocol/GnxProtocolDecoderTest.java @@ -0,0 +1,27 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class GnxProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + GnxProtocolDecoder decoder = new GnxProtocolDecoder(new GnxProtocol()); + + verifyPosition(decoder, text( + "$GNX_LOC,865733022352132,095,0,102134,280914,102134,280914,1,18.765432,N,073.752811,W,032,165.32,12,25,0,A,E,2,000099.9,000099.5,GNX01001,12*")); + + verifyNothing(decoder, text( + "$GNX_LOC,865733022354161,139,0,142838,160316,142825,160316,0,000000000,N,0000000000,E,000,0.00,00,48,0,e,C,2,000000.0,000000.0,GNX04008,BB*")); + + verifyPosition(decoder, text( + "$GNX_DIO,863071015071563,110,1,155627,121214,151244,121214,1,08.878321,N,076.643154,E,0,0,0,0,0,0,GNX01001,B1*")); + + verifyNothing(decoder, text( + "$GNX_DIO,865733022354161,112,1,142849,160316,142714,160316,0,000000000,N,0000000000,E,0,0,0,0,0,0,0,GNX04008,1A*")); + + } + +} -- cgit v1.2.3