diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-01-21 13:30:01 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-01-21 13:30:01 +1300 |
commit | d2cc0c9370fd9713e674f0f8622d35296f041d17 (patch) | |
tree | 29cc3234f3a6e6826b004f713278e3c74ee2f26e | |
parent | ddc9dbefd717b1b037a4397e55dae34bf13598c0 (diff) | |
download | traccar-server-d2cc0c9370fd9713e674f0f8622d35296f041d17.tar.gz traccar-server-d2cc0c9370fd9713e674f0f8622d35296f041d17.tar.bz2 traccar-server-d2cc0c9370fd9713e674f0f8622d35296f041d17.zip |
Begin AutoFon protocol implementation
-rw-r--r-- | default.cfg | 4 | ||||
-rw-r--r-- | src/org/traccar/ServerManager.java | 13 | ||||
-rw-r--r-- | src/org/traccar/protocol/AutoFonFrameDecoder.java | 61 | ||||
-rw-r--r-- | src/org/traccar/protocol/AutoFonProtocolDecoder.java | 124 | ||||
-rw-r--r-- | test/org/traccar/protocol/AutoFonProtocolDecoderTest.java | 29 |
5 files changed, 231 insertions, 0 deletions
diff --git a/default.cfg b/default.cfg index 5f4279c15..781f41136 100644 --- a/default.cfg +++ b/default.cfg @@ -377,4 +377,8 @@ <entry key='xt013.enable'>true</entry> <entry key='xt013.port'>5076</entry> + <!-- AutoFon server configuration --> + <entry key='autofon.enable'>true</entry> + <entry key='autofon.port'>5077</entry> + </properties> diff --git a/src/org/traccar/ServerManager.java b/src/org/traccar/ServerManager.java index 88a443a24..abda51787 100644 --- a/src/org/traccar/ServerManager.java +++ b/src/org/traccar/ServerManager.java @@ -177,6 +177,7 @@ public class ServerManager { initTr900Server("tr900"); initArdi01Server("ardi01"); initXt013Server("xt013"); + initAutoFonServer("autofon"); initProtocolDetector(); @@ -1283,4 +1284,16 @@ public class ServerManager { } } + private void initAutoFonServer(final String protocol) throws SQLException { + if (isProtocolEnabled(properties, protocol)) { + serverList.add(new TrackerServer(this, new ServerBootstrap(), protocol) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new AutoFonFrameDecoder()); + pipeline.addLast("objectDecoder", new AutoFonProtocolDecoder(dataManager, protocol, properties)); + } + }); + } + } + } diff --git a/src/org/traccar/protocol/AutoFonFrameDecoder.java b/src/org/traccar/protocol/AutoFonFrameDecoder.java new file mode 100644 index 000000000..f4b5c8f0b --- /dev/null +++ b/src/org/traccar/protocol/AutoFonFrameDecoder.java @@ -0,0 +1,61 @@ +/* + * 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.buffer.ChannelBuffer; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.handler.codec.frame.FrameDecoder; + +public class AutoFonFrameDecoder extends FrameDecoder { + + private static final int MSG_LOGIN = 0x10; + private static final int MSG_LOCATION = 0x11; + private static final int MSG_HISTORY = 0x12; + + @Override + protected Object decode( + ChannelHandlerContext ctx, + Channel channel, + ChannelBuffer buf) throws Exception { + + // Check minimum length + if (buf.readableBytes() < 12) { + return null; + } + + int length = 0; + switch (buf.getUnsignedByte(buf.readerIndex())) { + case MSG_LOGIN: + length = 12; + break; + case MSG_LOCATION: + length = 78; + break; + case MSG_HISTORY: + length = 257; + 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/AutoFonProtocolDecoder.java b/src/org/traccar/protocol/AutoFonProtocolDecoder.java new file mode 100644 index 000000000..6218a30a4 --- /dev/null +++ b/src/org/traccar/protocol/AutoFonProtocolDecoder.java @@ -0,0 +1,124 @@ +/* + * 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.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.traccar.BaseProtocolDecoder; +import org.traccar.database.DataManager; +import org.traccar.helper.Log; +import org.traccar.model.ExtendedInfoFormatter; +import org.traccar.model.Position; + +import java.util.Calendar; +import java.util.Properties; +import java.util.TimeZone; + +public class AutoFonProtocolDecoder extends BaseProtocolDecoder { + + public AutoFonProtocolDecoder(DataManager dataManager, String protocol, Properties properties) { + super(dataManager, protocol, properties); + } + + private static final int MSG_LOGIN = 0x10; + private static final int MSG_LOCATION = 0x11; + private static final int MSG_HISTORY = 0x12; + + @Override + protected Object decode( + ChannelHandlerContext ctx, Channel channel, Object msg) + throws Exception { + + ChannelBuffer buf = (ChannelBuffer) msg; + + /*buf.skipBytes(2); // header + buf.readByte(); // size + + // Zero for location messages + buf.readByte(); // voltage + buf.readByte(); // gsm signal + + String imei = readImei(buf); + long index = buf.readUnsignedShort(); + int type = buf.readUnsignedByte(); + + if (type == MSG_HEARTBEAT) { + if (channel != null) { + byte[] response = {0x54, 0x68, 0x1A, 0x0D, 0x0A}; + channel.write(ChannelBuffers.wrappedBuffer(response)); + } + } + + else if (type == MSG_DATA) { + + // Create new position + Position position = new Position(); + ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter(getProtocol()); + extendedInfo.set("index", index); + + // Get device id + try { + position.setDeviceId(getDataManager().getDeviceByImei(imei).getId()); + } catch(Exception error) { + Log.warning("Unknown device - " + imei); + return null; + } + + // Date and time + Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + time.clear(); + time.set(Calendar.YEAR, 2000 + buf.readUnsignedByte()); + time.set(Calendar.MONTH, buf.readUnsignedByte() - 1); + time.set(Calendar.DAY_OF_MONTH, buf.readUnsignedByte()); + time.set(Calendar.HOUR_OF_DAY, buf.readUnsignedByte()); + time.set(Calendar.MINUTE, buf.readUnsignedByte()); + time.set(Calendar.SECOND, buf.readUnsignedByte()); + position.setTime(time.getTime()); + + // Latitude + double latitude = buf.readUnsignedInt() / (60.0 * 30000.0); + + // Longitude + double longitude = buf.readUnsignedInt() / (60.0 * 30000.0); + + // Speed + position.setSpeed((double) buf.readUnsignedByte()); + + // Course + position.setCourse((double) buf.readUnsignedShort()); + + buf.skipBytes(3); // reserved + + // Flags + long flags = buf.readUnsignedInt(); + position.setValid((flags & 0x1) == 0x1); + if ((flags & 0x2) == 0) latitude = -latitude; + if ((flags & 0x4) == 0) longitude = -longitude; + + position.setLatitude(latitude); + position.setLongitude(longitude); + position.setAltitude(0.0); + + position.setExtendedInfo(extendedInfo.toString()); + return position; + }*/ + + return null; + } + +} diff --git a/test/org/traccar/protocol/AutoFonProtocolDecoderTest.java b/test/org/traccar/protocol/AutoFonProtocolDecoderTest.java new file mode 100644 index 000000000..13b58c464 --- /dev/null +++ b/test/org/traccar/protocol/AutoFonProtocolDecoderTest.java @@ -0,0 +1,29 @@ +package org.traccar.protocol;
+
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.junit.Test;
+import org.traccar.helper.ChannelBufferTools;
+import org.traccar.helper.TestDataManager;
+
+import static org.junit.Assert.assertNull;
+import static org.traccar.helper.DecoderVerifier.verify;
+
+public class AutoFonProtocolDecoderTest {
+
+ @Test
+ public void testDecode() throws Exception {
+
+ AutoFonProtocolDecoder decoder = new AutoFonProtocolDecoder(new TestDataManager(), null, null);
+
+ assertNull(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ChannelBufferTools.convertHexString(
+ "10556103592310314825728F"))));
+
+ //verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ChannelBufferTools.convertHexString(
+ // "111E00000000000000000100007101010B0C020302010B0C0005A053FFFFFFFF02010B0C00276047FFFFFFFF1F5600FA000176F218C7850C0B0B0C203A033DBD46035783EF009E00320014FFFF45"))));
+
+ //verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ChannelBufferTools.convertHexString(
+ // "12060000007501010B0C00089CFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000003E7FFFF02007601010B0C00269CFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000003E7FFFF4A007601010B0C01089CFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000003E7FFFF04007501010B0C01269CFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000003E7FFFF80007601010B0C02089CFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000003E7FFFFA6007501010B0C02231F5600FA000176F218C70000000000000000000000000000000000000003E7FFFF9629"))));
+
+ }
+
+}
|