diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2012-06-11 20:38:46 +0400 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2012-06-11 20:38:46 +0400 |
commit | 3687acb3a93b3f8332de5a9fa7d99be4a665bbdd (patch) | |
tree | 7728775d836b763253dc83a0d557122236a8f0a2 | |
parent | bb86570acc87ca082663986c2f9207d55c434623 (diff) | |
download | traccar-server-3687acb3a93b3f8332de5a9fa7d99be4a665bbdd.tar.gz traccar-server-3687acb3a93b3f8332de5a9fa7d99be4a665bbdd.tar.bz2 traccar-server-3687acb3a93b3f8332de5a9fa7d99be4a665bbdd.zip |
Added meiligao protocol (fix #4)
-rw-r--r-- | default.cfg | 5 | ||||
-rw-r--r-- | opengts.cfg | 5 | ||||
-rw-r--r-- | src/org/traccar/Server.java | 23 | ||||
-rw-r--r-- | src/org/traccar/helper/Crc.java | 40 | ||||
-rw-r--r-- | src/org/traccar/protocol/MeiligaoProtocolDecoder.java | 207 | ||||
-rwxr-xr-x | test.sh | 3 | ||||
-rw-r--r-- | test/org/traccar/protocol/MeiligaoProtocolDecoderTest.java | 25 |
7 files changed, 308 insertions, 0 deletions
diff --git a/default.cfg b/default.cfg index 65ead85df..a013d2808 100644 --- a/default.cfg +++ b/default.cfg @@ -132,4 +132,9 @@ <entry key="enfora.port">5008</entry> <entry key="enfora.resetDelay">0</entry> + <!-- Meiligao server configuration --> + <entry key="meiligao.enable">true</entry> + <entry key="meiligao.port">5009</entry> + <entry key="meiligao.resetDelay">0</entry> + </properties> diff --git a/opengts.cfg b/opengts.cfg index a47ca865a..273f9c0fe 100644 --- a/opengts.cfg +++ b/opengts.cfg @@ -95,4 +95,9 @@ <entry key="enfora.port">5008</entry> <entry key="enfora.resetDelay">0</entry> + <!-- Meiligao server configuration --> + <entry key="meiligao.enable">true</entry> + <entry key="meiligao.port">5009</entry> + <entry key="meiligao.resetDelay">0</entry> + </properties> 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; + } + +} @@ -35,3 +35,6 @@ echo "7. avl08" echo "8. enfora" (echo -n -e "\x00\x71\x00\x04\x02\x00 123456789012345 13 \$GPRMC,120800.00,A,6000.000000,N,13000.000000,E,0.0,0.0,010112,,,A*52\r\n";) | nc -v localhost 5008 +echo "9. meiligao" +(echo -n -e "\x24\x24\x00\x11\x12\x34\x56\xFF\xFF\xFF\xFF\x50\x00\x8B\x9B\x0D\x0A";) | nc -v localhost 5009 + diff --git a/test/org/traccar/protocol/MeiligaoProtocolDecoderTest.java b/test/org/traccar/protocol/MeiligaoProtocolDecoderTest.java new file mode 100644 index 000000000..881f6df19 --- /dev/null +++ b/test/org/traccar/protocol/MeiligaoProtocolDecoderTest.java @@ -0,0 +1,25 @@ +package org.traccar.protocol; + +import org.jboss.netty.buffer.ChannelBufferFactory; +import org.jboss.netty.buffer.HeapChannelBufferFactory; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import org.junit.Test; + +public class MeiligaoProtocolDecoderTest { + + @Test + public void testDecode() throws Exception { + + MeiligaoProtocolDecoder decoder = new MeiligaoProtocolDecoder(new TestDataManager(), 0); + ChannelBufferFactory factory = new HeapChannelBufferFactory(); + + byte[] buf1 = {0x12,0x34,0x56,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,0x50,0x00,(byte)0x8B,(byte)0x9B,0x0D,0x0A}; + assertNull(decoder.decode(null, null, factory.getBuffer(buf1, 0, buf1.length))); + + byte[] buf2 = {0x12,0x34,0x56,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0x99,0x55,0x30,0x33,0x35,0x36,0x34,0x34,0x2E,0x30,0x30,0x30,0x2C,0x41,0x2C,0x32,0x32,0x33,0x32,0x2E,0x36,0x30,0x38,0x33,0x2C,0x4E,0x2C,0x31,0x31,0x34,0x30,0x34,0x2E,0x38,0x31,0x33,0x37,0x2C,0x45,0x2C,0x30,0x2E,0x30,0x30,0x2C,0x2C,0x30,0x31,0x30,0x38,0x30,0x39,0x2C,0x2C,0x2A,0x31,0x43,0x7C,0x31,0x31,0x2E,0x35,0x7C,0x31,0x39,0x34,0x7C,0x30,0x30,0x30,0x30,0x7C,0x30,0x30,0x30,0x30,0x2C,0x30,0x30,0x30,0x30,0x69,0x62,0x0D,0x0A}; + assertNotNull(decoder.decode(null, null, factory.getBuffer(buf2, 0, buf2.length))); + + } + +} |