From f278d3ff3e7b17b458a512d7bb16131eb9d97b2b Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 14 Oct 2012 09:56:37 +1300 Subject: Add H02 support (fix #56) --- default.cfg | 8 ++ src/org/traccar/Server.java | 27 ++++- src/org/traccar/protocol/H02ProtocolDecoder.java | 127 +++++++++++++++++++++ .../traccar/protocol/H02ProtocolDecoderTest.java | 19 +++ 4 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 src/org/traccar/protocol/H02ProtocolDecoder.java create mode 100644 test/org/traccar/protocol/H02ProtocolDecoderTest.java diff --git a/default.cfg b/default.cfg index 2504842c2..de2fa3dec 100644 --- a/default.cfg +++ b/default.cfg @@ -139,4 +139,12 @@ true 5011 + + true + 5012 + + + true + 5013 + diff --git a/src/org/traccar/Server.java b/src/org/traccar/Server.java index 7dcabe084..79e69fd87 100644 --- a/src/org/traccar/Server.java +++ b/src/org/traccar/Server.java @@ -517,6 +517,31 @@ public class Server { serverList.add(server); } - } + } + + /** + * Init H02 server + */ + private void initH02Server(Properties properties) throws SQLException { + + String protocol = "h02"; + if (isProtocolEnabled(properties, protocol)) { + + TrackerServer server = new TrackerServer(getProtocolPort(properties, protocol)); + final Integer resetDelay = getProtocolResetDelay(properties, protocol); + + server.setPipelineFactory(new GenericPipelineFactory(server, dataManager, isLoggerEnabled(), geocoder) { + protected void addSpecificHandlers(ChannelPipeline pipeline) { + byte delimiter[] = { (byte) '#' }; + pipeline.addLast("frameDecoder", + new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("objectDecoder", new H02ProtocolDecoder(getDataManager(), resetDelay)); + } + }); + + serverList.add(server); + } + } } diff --git a/src/org/traccar/protocol/H02ProtocolDecoder.java b/src/org/traccar/protocol/H02ProtocolDecoder.java new file mode 100644 index 000000000..183311817 --- /dev/null +++ b/src/org/traccar/protocol/H02ProtocolDecoder.java @@ -0,0 +1,127 @@ +/* + * 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.util.Calendar; +import java.util.TimeZone; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.traccar.GenericProtocolDecoder; +import org.traccar.model.DataManager; +import org.traccar.model.Position; + +/** + * H02 tracker protocol decoder + */ +public class H02ProtocolDecoder extends GenericProtocolDecoder { + + /** + * Initialize + */ + public H02ProtocolDecoder(DataManager dataManager, Integer resetDelay) { + super(dataManager, resetDelay); + } + + /** + * Regular expressions pattern + */ + static private Pattern pattern = Pattern.compile( + "\\*HQ," + + "(\\d+)," + // IMEI + "V\\d," + // Version? + "(\\d{2})(\\d{2})(\\d{2})," + // Time (HHMMSS) + "([AV])," + // Validity + "(\\d{2})(\\d{2}.\\d{4})," + // Latitude (DDMM.MMMM) + "([NS])," + + "(\\d+)(\\d{2}.\\d{4})," + // Longitude (DDMM.MMMM) + "([EW])," + + "(\\d+.\\d+)," + // Speed + "(\\d+.\\d+)?," + // Course + "(\\d{2})(\\d{2})(\\d{2})," + // Date (DDMMYY) + ".*"); + + /** + * Decode message + */ + protected Object decode( + ChannelHandlerContext ctx, Channel channel, Object msg) + throws Exception { + + // Parse message + String sentence = (String) msg; + Matcher parser = pattern.matcher(sentence); + if (!parser.matches()) { + return null; + } + + // Create new position + Position position = new Position(); + StringBuilder extendedInfo = new StringBuilder("h02"); + + Integer index = 1; + + // Get device by IMEI + String imei = parser.group(index++); + position.setDeviceId(getDataManager().getDeviceByImei(imei).getId()); + + // Time + Calendar time = Calendar.getInstance(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++))); + + // 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()); + + // Extended info + position.setExtendedInfo(extendedInfo.toString()); + + return position; + } + +} diff --git a/test/org/traccar/protocol/H02ProtocolDecoderTest.java b/test/org/traccar/protocol/H02ProtocolDecoderTest.java new file mode 100644 index 000000000..25920e8dd --- /dev/null +++ b/test/org/traccar/protocol/H02ProtocolDecoderTest.java @@ -0,0 +1,19 @@ +package org.traccar.protocol; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import org.junit.Test; + +public class H02ProtocolDecoderTest { + + @Test + public void testDecode() throws Exception { + + H02ProtocolDecoder decoder = new H02ProtocolDecoder(new TestDataManager(), 0); + + assertNotNull(decoder.decode(null, null, + "*HQ,123456789012345,V1,155850,A,5214.5346,N,2117.4683,E,0.00,270.90,131012,ffffffff,000000,000000,000000,000000")); + + } + +} -- cgit v1.2.3