diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2013-08-12 22:45:47 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2013-08-12 22:45:47 +1200 |
commit | c42b05ceffcf56c696b20b34cacb80f7ce690497 (patch) | |
tree | 6b5c9a1c85638d20cc1ac395c6122b589fcafdea | |
parent | 2bd65faa3ca6e78727277c6daa1c97bf01689c8a (diff) | |
download | traccar-server-c42b05ceffcf56c696b20b34cacb80f7ce690497.tar.gz traccar-server-c42b05ceffcf56c696b20b34cacb80f7ce690497.tar.bz2 traccar-server-c42b05ceffcf56c696b20b34cacb80f7ce690497.zip |
Added Gotop protocol
-rw-r--r-- | default.cfg | 4 | ||||
-rw-r--r-- | src/org/traccar/ServerManager.java | 16 | ||||
-rw-r--r-- | src/org/traccar/protocol/GotopProtocolDecoder.java | 115 | ||||
-rw-r--r-- | test/org/traccar/protocol/GotopProtocolDecoderTest.java | 28 |
4 files changed, 163 insertions, 0 deletions
diff --git a/default.cfg b/default.cfg index fbd1278a5..a13debed4 100644 --- a/default.cfg +++ b/default.cfg @@ -262,4 +262,8 @@ <entry key='aplicom.enable'>true</entry> <entry key='aplicom.port'>5049</entry> + <!-- Gotop server configuration --> + <entry key='gotop.enable'>true</entry> + <entry key='gotop.port'>5050</entry> + </properties> diff --git a/src/org/traccar/ServerManager.java b/src/org/traccar/ServerManager.java index f66eda351..3f7bd9986 100644 --- a/src/org/traccar/ServerManager.java +++ b/src/org/traccar/ServerManager.java @@ -145,6 +145,7 @@ public class ServerManager { initTopflytechServer("topflytech"); //initLaipacServer("laipac"); initAplicomServer("aplicom"); + initGotopServer("gotop"); // Initialize web server if (Boolean.valueOf(properties.getProperty("http.enable"))) { @@ -914,4 +915,19 @@ public class ServerManager { } } + private void initGotopServer(String protocol) throws SQLException { + if (isProtocolEnabled(properties, protocol)) { + serverList.add(new TrackerServer(this, new ServerBootstrap(), protocol) { + @Override + 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 GotopProtocolDecoder(ServerManager.this)); + } + }); + } + } + } diff --git a/src/org/traccar/protocol/GotopProtocolDecoder.java b/src/org/traccar/protocol/GotopProtocolDecoder.java new file mode 100644 index 000000000..09385d884 --- /dev/null +++ b/src/org/traccar/protocol/GotopProtocolDecoder.java @@ -0,0 +1,115 @@ +/* + * Copyright 2013 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.BaseProtocolDecoder; +import org.traccar.ServerManager; +import org.traccar.helper.Log; +import org.traccar.model.ExtendedInfoFormatter; +import org.traccar.model.Position; + +public class GotopProtocolDecoder extends BaseProtocolDecoder { + + public GotopProtocolDecoder(ServerManager serverManager) { + super(serverManager); + } + + static private Pattern pattern = Pattern.compile( + "(\\d+)," + // IMEI + "[^,]+," + // Type + "([AV])," + // Validity + "DATE:(\\d{2})(\\d{2})(\\d{2})," + // Date (YYMMDD) + "TIME:(\\d{2})(\\d{2})(\\d{2})," + // Time (HHMMSS) + "LAT:(\\d+.\\d+)([NS])," + // Latitude + "LOT:(\\d+.\\d+)([EW])," + // Longitude + "Speed:(\\d+.\\d+)," + // Speed + "([^,]+)," + // Status + "(\\d+)" + // Course + ".*"); + + @Override + protected Object decode( + ChannelHandlerContext ctx, Channel channel, Object msg) + throws Exception { + + // Parse message + String sentence = (String) msg; + Matcher parser = pattern.matcher(sentence); + if (sentence.isEmpty() || !parser.matches()) { + return null; + } + + // Create new position + Position position = new Position(); + ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter("gotop"); + Integer index = 1; + + // Get device by IMEI + String imei = parser.group(index++); + try { + position.setDeviceId(getDataManager().getDeviceByImei(imei).getId()); + } catch(Exception error) { + Log.warning("Unknown device - " + imei); + return null; + } + + // Validity + position.setValid(parser.group(index++).compareTo("A") == 0 ? true : false); + + // Time + Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + time.clear(); + time.set(Calendar.YEAR, 2000 + Integer.valueOf(parser.group(index++))); + time.set(Calendar.MONTH, Integer.valueOf(parser.group(index++)) - 1); + time.set(Calendar.DAY_OF_MONTH, Integer.valueOf(parser.group(index++))); + 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++))); + position.setTime(time.getTime()); + + // Latitude + Double latitude = Double.valueOf(parser.group(index++)); + if (parser.group(index++).compareTo("S") == 0) latitude = -latitude; + position.setLatitude(latitude); + + // Longitude + Double longitude = Double.valueOf(parser.group(index++)); + if (parser.group(index++).compareTo("W") == 0) longitude = -longitude; + position.setLongitude(longitude); + + // Altitude + position.setAltitude(0.0); + + // Speed + position.setSpeed(Double.valueOf(parser.group(index++)) * 0.539957); + + // Status + extendedInfo.set("status", parser.group(index++)); + + // Course + position.setCourse(Double.valueOf(parser.group(index++))); + + position.setExtendedInfo(extendedInfo.toString()); + return position; + } + +} diff --git a/test/org/traccar/protocol/GotopProtocolDecoderTest.java b/test/org/traccar/protocol/GotopProtocolDecoderTest.java new file mode 100644 index 000000000..64376a4bb --- /dev/null +++ b/test/org/traccar/protocol/GotopProtocolDecoderTest.java @@ -0,0 +1,28 @@ +package org.traccar.protocol; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import org.junit.Test; + +public class GotopProtocolDecoderTest { + + @Test + public void testDecode() throws Exception { + + GotopProtocolDecoder decoder = new GotopProtocolDecoder(null); + decoder.setDataManager(new TestDataManager()); + + assertNull(decoder.decode(null, null, "")); + + assertNotNull(decoder.decode(null, null, + "013226009991924,CMD-T,A,DATE:130802,TIME:153721,LAT:25.9757433S,LOT:028.1087816E,Speed:000.0,X-X-X-X-81-26,000,65501-00A0-4B8E")); + + assertNotNull(decoder.decode(null, null, + "353327020115804,CMD-T,A,DATE:090329,TIME:223252,LAT:22.7634066N,LOT:114.3964783E,Speed:000.0,84-20,000")); + + assertNotNull(decoder.decode(null, null, + "353327020115804,CMD-T,A,DATE:090329,TIME:223252,LAT:22.7634066N,LOT:114.3964783E,Speed:000.0,1-1-0-84-20,000")); + + } + +} |