diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2012-12-11 21:02:10 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2012-12-11 21:02:10 +1300 |
commit | 77841ad1ad5f50c41437361a9c3b885935478a4f (patch) | |
tree | 660f5fb86d8cbe80b645cc181f7253363778fc82 | |
parent | a5514d3497e2cfbcbecdff606af8b30f340c1b32 (diff) | |
download | traccar-server-77841ad1ad5f50c41437361a9c3b885935478a4f.tar.gz traccar-server-77841ad1ad5f50c41437361a9c3b885935478a4f.tar.bz2 traccar-server-77841ad1ad5f50c41437361a9c3b885935478a4f.zip |
Start Skypatrol implementation
-rw-r--r-- | src/org/traccar/protocol/SkypatrolProtocolDecoder.java | 155 | ||||
-rw-r--r-- | test/org/traccar/protocol/SkypatrolProtocolDecoderTest.java | 24 |
2 files changed, 179 insertions, 0 deletions
diff --git a/src/org/traccar/protocol/SkypatrolProtocolDecoder.java b/src/org/traccar/protocol/SkypatrolProtocolDecoder.java new file mode 100644 index 000000000..184f5e40b --- /dev/null +++ b/src/org/traccar/protocol/SkypatrolProtocolDecoder.java @@ -0,0 +1,155 @@ +/* + * 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.helper.Log; +import org.traccar.model.DataManager; +import org.traccar.model.Position; + +/** + * T55 tracker protocol decoder + */ +public class T55ProtocolDecoder extends GenericProtocolDecoder { + + /** + * Device ID + */ + private Long deviceId; + + /** + * Initialize + */ + public T55ProtocolDecoder(DataManager dataManager) { + super(dataManager); + } + + /** + * Regular expressions pattern + */ + static private Pattern pattern = Pattern.compile( + "\\$GPRMC," + + "(\\d{2})(\\d{2})(\\d{2})\\.(\\d+)," + // Time (HHMMSS.SSS) + "([AV])," + // Validity + "(\\d{2})(\\d{2}\\.\\d+)," + // Latitude (DDMM.MMMM) + "([NS])," + + "(\\d{3})(\\d{2}\\.\\d+)," + // Longitude (DDDMM.MMMM) + "([EW])," + + "(\\d+\\.\\d{2})?," + // Speed + "(\\d+\\.\\d{2})?," + // Course + "(\\d{2})(\\d{2})(\\d{2})" + // Date (DDMMYY) + ".+"); // Other (Checksumm) + + /** + * Decode message + */ + protected Object decode( + ChannelHandlerContext ctx, Channel channel, Object msg) + throws Exception { + + String sentence = (String) msg; + + // Detect device ID + if (sentence.contains("$PGID")) { + String imei = sentence.substring(6, 6 + 15); + try { + deviceId = getDataManager().getDeviceByImei(imei).getId(); + } catch(Exception error) { + Log.warning("Unknown device - " + imei); + } + } + + // Parse message + else if (sentence.contains("$GPRMC") && deviceId != null) { + + // Send response + if (channel != null) { + channel.write("OK1\r\n"); + } + + // Parse message + Matcher parser = pattern.matcher(sentence); + if (!parser.matches()) { + return null; + } + + // Create new position + Position position = new Position(); + position.setDeviceId(deviceId); + + Integer index = 1; + + // 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++))); + index += 1; // Skip milliseconds + + // 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 + String speed = parser.group(index++); + if (speed != null) { + position.setSpeed(Double.valueOf(speed)); + } else { + position.setSpeed(0.0); + } + + // 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()); + + // Altitude + position.setAltitude(0.0); + + return position; + } + + return null; + } + +} diff --git a/test/org/traccar/protocol/SkypatrolProtocolDecoderTest.java b/test/org/traccar/protocol/SkypatrolProtocolDecoderTest.java new file mode 100644 index 000000000..5d9ae4e3a --- /dev/null +++ b/test/org/traccar/protocol/SkypatrolProtocolDecoderTest.java @@ -0,0 +1,24 @@ +package org.traccar.protocol; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import org.junit.Test; + +public class T55ProtocolDecoderTest { + + @Test + public void testDecode() throws Exception { + + T55ProtocolDecoder decoder = new T55ProtocolDecoder(new TestDataManager()); + + assertNull(decoder.decode(null, null, "$PGID,359853000144328*0F")); + + assertNotNull(decoder.decode(null, null, + "$GPRMC,094907.000,A,6000.5332,N,03020.5192,E,1.17,60.26,091111,,*33")); + + assertNotNull(decoder.decode(null, null, + "$GPRMC,115528.000,A,6000.5432,N,03020.4948,E,,,091111,,*06")); + + } + +} |