aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2012-06-11 20:38:46 +0400
committerAnton Tananaev <anton.tananaev@gmail.com>2012-06-11 20:38:46 +0400
commit3687acb3a93b3f8332de5a9fa7d99be4a665bbdd (patch)
tree7728775d836b763253dc83a0d557122236a8f0a2 /src/org/traccar
parentbb86570acc87ca082663986c2f9207d55c434623 (diff)
downloadtrackermap-server-3687acb3a93b3f8332de5a9fa7d99be4a665bbdd.tar.gz
trackermap-server-3687acb3a93b3f8332de5a9fa7d99be4a665bbdd.tar.bz2
trackermap-server-3687acb3a93b3f8332de5a9fa7d99be4a665bbdd.zip
Added meiligao protocol (fix #4)
Diffstat (limited to 'src/org/traccar')
-rw-r--r--src/org/traccar/Server.java23
-rw-r--r--src/org/traccar/helper/Crc.java40
-rw-r--r--src/org/traccar/protocol/MeiligaoProtocolDecoder.java207
3 files changed, 270 insertions, 0 deletions
diff --git a/src/org/traccar/Server.java b/src/org/traccar/Server.java
index a6b0a67da..81a1988d1 100644
--- a/src/org/traccar/Server.java
+++ b/src/org/traccar/Server.java
@@ -90,6 +90,7 @@ public class Server {
initXexun2Server(properties);
initAvl08Server(properties);
initEnforaServer(properties);
+ initMeiligaoServer(properties);
// Initialize web server
if (Boolean.valueOf(properties.getProperty("http.enable"))) {
@@ -416,4 +417,26 @@ public class Server {
}
}
+ /**
+ * Init Meiligao server
+ */
+ public void initMeiligaoServer(Properties properties) throws SQLException {
+
+ String protocol = "meiligao";
+ if (isProtocolEnabled(properties, protocol)) {
+
+ TrackerServer server = new TrackerServer(getProtocolPort(properties, protocol));
+ final Integer resetDelay = getProtocolResetDelay(properties, protocol);
+
+ server.setPipelineFactory(new GenericPipelineFactory(server, dataManager, isLoggerEnabled()) {
+ protected void addSpecificHandlers(ChannelPipeline pipeline) {
+ pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1024, 2, 2, -4, 4));
+ pipeline.addLast("objectDecoder", new MeiligaoProtocolDecoder(getDataManager(), resetDelay));
+ }
+ });
+
+ serverList.add(server);
+ }
+ }
+
}
diff --git a/src/org/traccar/helper/Crc.java b/src/org/traccar/helper/Crc.java
new file mode 100644
index 000000000..a0ad83e72
--- /dev/null
+++ b/src/org/traccar/helper/Crc.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2012 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.helper;
+
+/**
+ * CRC functions
+ */
+public class Crc {
+
+ public static int crc16Ccitt(byte[] bytes) {
+ int crc = 0xFFFF; // initial value
+ int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
+
+ for (byte b : bytes) {
+ for (int i = 0; i < 8; i++) {
+ boolean bit = ((b >> (7-i) & 1) == 1);
+ boolean c15 = ((crc >> 15 & 1) == 1);
+ crc <<= 1;
+ if (c15 ^ bit) crc ^= polynomial;
+ }
+ }
+
+ crc &= 0xffff;
+ return crc;
+ }
+
+}
diff --git a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java
new file mode 100644
index 000000000..e836289e4
--- /dev/null
+++ b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java
@@ -0,0 +1,207 @@
+/*
+ * Copyright 2012 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 java.nio.charset.Charset;
+import java.text.ParseException;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.TimeZone;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.HeapChannelBufferFactory;
+import org.jboss.netty.channel.Channel;
+import org.jboss.netty.channel.ChannelHandlerContext;
+import org.traccar.GenericProtocolDecoder;
+import org.traccar.helper.Crc;
+import org.traccar.model.DataManager;
+import org.traccar.model.Position;
+
+/**
+ * Meiligao protocol decoder
+ */
+public class MeiligaoProtocolDecoder extends GenericProtocolDecoder {
+
+ /**
+ * Initialize
+ */
+ public MeiligaoProtocolDecoder(DataManager dataManager, Integer resetDelay) {
+ super(dataManager, resetDelay);
+ }
+
+ /**
+ * Regular expressions pattern
+ */
+ static private Pattern pattern = Pattern.compile(
+ "([\\d]{2})([\\d]{2})([\\d]{2}).([\\d]{3})," + // Time (HHMMSS.SSS)
+ "([AV])," + // Validity
+ "([\\d]{2})([\\d]{2}.[\\d]{4})," + // Latitude (DDMM.MMMM)
+ "([NS])," +
+ "([\\d]{3})([\\d]{2}.[\\d]{4})," + // Longitude (DDDMM.MMMM)
+ "([EW])," +
+ "([\\d]+.[\\d]+)," + // Speed
+ "([\\d]+.[\\d]+)?," + // Course
+ "([\\d]{2})([\\d]{2})([\\d]{2})," + // Date (DDMMYY)
+ "[^\\|]+\\|(\\d+.\\d)\\|" + // Dilution of precision
+ "(\\d+)\\|" + // Altitude
+ "([0-9a-fA-F]+)\\|" + // State
+ ".*"); // TODO: parse ADC
+
+ /**
+ * Parse device id
+ */
+ private String getId(ChannelBuffer buf) {
+ String id = "";
+
+ for (int i = 0; i < 7; i++) {
+ int b = buf.getUnsignedByte(i);
+
+ // First digit
+ int d1 = (b & 0xf0) >> 4;
+ if (d1 == 0xf) break;
+ id += d1;
+
+ // Second digit
+ int d2 = (b & 0x0f);
+ if (d2 == 0xf) break;
+ id += d2;
+ }
+
+ return id;
+ }
+
+ /**
+ * Decode message
+ */
+ protected Object decode(
+ ChannelHandlerContext ctx, Channel channel, Object msg)
+ throws Exception {
+
+ ChannelBuffer buf = (ChannelBuffer) msg;
+ int command = buf.getUnsignedShort(7);
+
+ // Login confirmation
+ if (command == 0x5000) {
+ ChannelBuffer sendBuf = HeapChannelBufferFactory.getInstance().getBuffer(18);
+ sendBuf.writeByte('@');
+ sendBuf.writeByte('@');
+ sendBuf.writeShort(sendBuf.capacity());
+ byte[] array = new byte[7];
+ buf.getBytes(0, array);
+ sendBuf.writeBytes(array);
+ sendBuf.writeShort(0x4000);
+ sendBuf.writeByte(0x01);
+ array = new byte[sendBuf.readableBytes()];
+ sendBuf.getBytes(0, array);
+ sendBuf.writeShort(Crc.crc16Ccitt(array));
+ sendBuf.writeByte('\r');
+ sendBuf.writeByte('\n');
+ if (channel != null) {
+ channel.write(sendBuf);
+ }
+ }
+
+ // Data offset
+ int offset = 7 + 2;
+ if (command == 0x9955) {
+ offset += 0;
+ } else if (command == 0x9016) {
+ offset += 6;
+ } else if (command == 0x9999) {
+ offset += 1;
+ } else {
+ return null;
+ }
+
+ // Create new position
+ Position position = new Position();
+ String extendedInfo = "<protocol>meiligao</protocol>";
+
+ // Get device by id
+ // TODO: change imei to unique id
+ position.setDeviceId(getDataManager().getDeviceByImei(getId(buf)).getId());
+
+ // Parse message
+ String sentence = buf.toString(offset, buf.readableBytes() - offset - 4, Charset.defaultCharset());
+ Matcher parser = pattern.matcher(sentence);
+ if (!parser.matches()) {
+ throw new ParseException(null, 0);
+ }
+
+ Integer index = 1;
+
+ // Time
+ Calendar time = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
+ time.clear();
+ time.set(Calendar.HOUR, Integer.valueOf(parser.group(index++)));
+ time.set(Calendar.MINUTE, Integer.valueOf(parser.group(index++)));
+ time.set(Calendar.SECOND, Integer.valueOf(parser.group(index++)));
+ time.set(Calendar.MILLISECOND, Integer.valueOf(parser.group(index++)));
+
+ // Validity
+ position.setValid(parser.group(index++).compareTo("A") == 0 ? true : false);
+
+ // Latitude
+ Double latitude = Double.valueOf(parser.group(index++));
+ latitude += Double.valueOf(parser.group(index++)) / 60;
+ if (parser.group(index++).compareTo("S") == 0) latitude = -latitude;
+ position.setLatitude(latitude);
+
+ // Longitude
+ Double lonlitude = Double.valueOf(parser.group(index++));
+ lonlitude += Double.valueOf(parser.group(index++)) / 60;
+ if (parser.group(index++).compareTo("W") == 0) lonlitude = -lonlitude;
+ position.setLongitude(lonlitude);
+
+ // Speed
+ position.setSpeed(Double.valueOf(parser.group(index++)));
+
+ // Course
+ String course = parser.group(index++);
+ if (course != null) {
+ position.setCourse(Double.valueOf(course));
+ } else {
+ position.setCourse(0.0);
+ }
+
+ // Date
+ time.set(Calendar.DAY_OF_MONTH, Integer.valueOf(parser.group(index++)));
+ time.set(Calendar.MONTH, Integer.valueOf(parser.group(index++)) - 1);
+ time.set(Calendar.YEAR, 2000 + Integer.valueOf(parser.group(index++)));
+ position.setTime(time.getTime());
+
+ // Dilution of precision
+ extendedInfo += "<hdop>" + parser.group(index++) + "</hdop>";
+
+ // Altitude
+ String altitude = parser.group(index++);
+ if (altitude != null) {
+ position.setAltitude(Double.valueOf(altitude));
+ } else {
+ position.setAltitude(0.0);
+ }
+
+ // State
+ extendedInfo += "<state>" + parser.group(index++) + "</state>";
+
+ // Extended info
+ position.setExtendedInfo(extendedInfo);
+
+ return position;
+ }
+
+}