diff options
-rw-r--r-- | src/org/traccar/protocol/GlobalSatProtocolDecoder.java | 205 | ||||
-rw-r--r-- | test/org/traccar/protocol/GlobalSatProtocolDecoderTest.java | 5 |
2 files changed, 109 insertions, 101 deletions
diff --git a/src/org/traccar/protocol/GlobalSatProtocolDecoder.java b/src/org/traccar/protocol/GlobalSatProtocolDecoder.java index a2b115445..ee94158be 100644 --- a/src/org/traccar/protocol/GlobalSatProtocolDecoder.java +++ b/src/org/traccar/protocol/GlobalSatProtocolDecoder.java @@ -28,6 +28,7 @@ import org.traccar.ServerManager; import org.traccar.helper.AdvancedConnection; import org.traccar.helper.Log; import org.traccar.helper.NamedParameterStatement; +import org.traccar.model.ExtendedInfoFormatter; import org.traccar.model.Position; public class GlobalSatProtocolDecoder extends BaseProtocolDecoder { @@ -52,30 +53,17 @@ public class GlobalSatProtocolDecoder extends BaseProtocolDecoder { } } - /** - * 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*)?," + // Speed - "(\\d+\\.?\\d*)?," + // Course - "(\\d{2})(\\d{2})(\\d{2})" + // Date (DDMMYY) - ".+"); // Other (Checksumm)*/ - - // TODO: Acknowledgement - @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { String sentence = (String) msg; + + // Send acknowledgement + if (channel != null) { + channel.write("ACK\r"); + } // Message type String format; @@ -87,92 +75,111 @@ public class GlobalSatProtocolDecoder extends BaseProtocolDecoder { return null; } - // TODO: check that message contains required parameters - - - - // Detect device ID - /*if (sentence.contains("$PGID")) { - String imei = sentence.substring(6, sentence.length() - 3); - try { - deviceId = getDataManager().getDeviceByImei(imei).getId(); - } catch(Exception error) { - Log.warning("Unknown device - " + imei); - } + // Check that message contains required parameters + if (!format.contains("B") || !format.contains("S") || + !(format.contains("1") || format.contains("2") || format.contains("3")) || + !(format.contains("6") || format.contains("7") || format.contains("8"))) { + return null; } - // 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); + // Tokenise + if (format.contains("*")) { + format = format.substring(0, format.indexOf('*')); + sentence = sentence.substring(0, sentence.indexOf('*')); + } + String[] values = sentence.split(","); + + // Parse data + Position position = new Position(); + ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter("globalsat"); + + for (int formatIndex = 0, valueIndex = 1; formatIndex < format.length() && valueIndex < values.length; formatIndex++) { + String value = values[valueIndex]; + + switch(format.charAt(formatIndex)) { + case 'S': + try { + position.setDeviceId(getDataManager().getDeviceByImei(value).getId()); + } catch(Exception error) { + Log.warning("Unknown device - " + value); + return null; + } + break; + case 'A': + if (value.isEmpty()) { + position.setValid(false); + } else { + position.setValid(Integer.valueOf(value) != 1); + } + break; + case 'B': + Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + time.clear(); + time.set(Calendar.DAY_OF_MONTH, Integer.valueOf(value.substring(0, 2))); + time.set(Calendar.MONTH, Integer.valueOf(value.substring(2, 4)) - 1); + time.set(Calendar.YEAR, 2000 + Integer.valueOf(value.substring(4))); + value = values[++valueIndex]; + time.set(Calendar.HOUR, Integer.valueOf(value.substring(0, 2))); + time.set(Calendar.MINUTE, Integer.valueOf(value.substring(2, 4))); + time.set(Calendar.SECOND, Integer.valueOf(value.substring(4))); + position.setTime(time.getTime()); + break; + case 'C': + valueIndex += 1; + break; + case '1': + double longitude = Double.valueOf(value.substring(1)); + if (value.charAt(0) == 'E') longitude = -longitude; + position.setLongitude(longitude); + break; + case '2': + longitude = Double.valueOf(value.substring(4)) / 60; + longitude += Integer.valueOf(value.substring(1, 4)); + if (value.charAt(0) == 'E') longitude = -longitude; + position.setLongitude(longitude); + break; + case '3': + position.setLongitude(Double.valueOf(value) * 0.000001); + break; + case '6': + double latitude = Double.valueOf(value.substring(1)); + if (value.charAt(0) == 'S') latitude = -latitude; + position.setLatitude(latitude); + break; + case '7': + latitude = Double.valueOf(value.substring(3)) / 60; + latitude += Integer.valueOf(value.substring(1, 3)); + if (value.charAt(0) == 'S') latitude = -latitude; + position.setLatitude(latitude); + break; + case '8': + position.setLatitude(Double.valueOf(value) * 0.000001); + break; + case 'G': + position.setAltitude(Double.valueOf(value)); + break; + case 'H': + position.setSpeed(Double.valueOf(value)); + break; + case 'I': + position.setSpeed(Double.valueOf(value) * 0.539957); + break; + case 'J': + position.setSpeed(Double.valueOf(value) * 0.868976); + break; + case 'K': + position.setCourse(Double.valueOf(value)); + break; + default: + // Unsupported + break; } - // 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; - }*/ + valueIndex += 1; + } - return null; + position.setExtendedInfo(extendedInfo.toString()); + return position; } } diff --git a/test/org/traccar/protocol/GlobalSatProtocolDecoderTest.java b/test/org/traccar/protocol/GlobalSatProtocolDecoderTest.java index eae55d534..6025560b0 100644 --- a/test/org/traccar/protocol/GlobalSatProtocolDecoderTest.java +++ b/test/org/traccar/protocol/GlobalSatProtocolDecoderTest.java @@ -14,8 +14,9 @@ public class GlobalSatProtocolDecoderTest { //assertNull(decoder.decode(null, null, "GSh,131826789036289,3,M,ea04*3d!")); - /*assertNotNull(decoder.decode(null, null, - "GSr,135785412249986,01,I,EA02,,3,230410,153318,E12129.2839,N2459.8570,0,1.17,212,8,1.0,12.3V*55!"));*/ + //TSPRXAB27GHKLMnaicz + assertNotNull(decoder.decode(null, null, + "GSr,1,135785412249986,01,I,EA02,3,230410,153318,E12129.2839,N2459.8570,0,1.17,212,8,1.0,12.3V*55!")); } |