From d2cc0c9370fd9713e674f0f8622d35296f041d17 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 21 Jan 2015 13:30:01 +1300 Subject: Begin AutoFon protocol implementation --- src/org/traccar/ServerManager.java | 13 +++ src/org/traccar/protocol/AutoFonFrameDecoder.java | 61 ++++++++++ .../traccar/protocol/AutoFonProtocolDecoder.java | 124 +++++++++++++++++++++ 3 files changed, 198 insertions(+) create mode 100644 src/org/traccar/protocol/AutoFonFrameDecoder.java create mode 100644 src/org/traccar/protocol/AutoFonProtocolDecoder.java (limited to 'src/org/traccar') 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; + } + +} -- cgit v1.2.3