From 5f2fc476b549fa8555d6d764dbd8279fa754fc49 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 13 Jul 2019 17:07:00 -0700 Subject: Improve GoSafe decoder --- .../traccar/protocol/GoSafeProtocolDecoder.java | 59 ++++++++-------------- 1 file changed, 22 insertions(+), 37 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/protocol/GoSafeProtocolDecoder.java b/src/main/java/org/traccar/protocol/GoSafeProtocolDecoder.java index 95ef18f20..8164a016d 100644 --- a/src/main/java/org/traccar/protocol/GoSafeProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/GoSafeProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2019 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,7 +30,8 @@ import org.traccar.model.Network; import org.traccar.model.Position; import java.net.SocketAddress; -import java.util.Date; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.LinkedList; import java.util.List; import java.util.regex.Pattern; @@ -45,8 +46,6 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { .text("*GS") // header .number("d+,") // protocol version .number("(d+),") // imei - .number("(dd)(dd)(dd)") // time (hhmmss) - .number("(dd)(dd)(dd),") // date (ddmmyy) .expression("([^#]*)#?") // data .compile(); @@ -68,6 +67,7 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { .compile(); private void decodeFragment(Position position, String fragment) { + int dataIndex = fragment.indexOf(':'); int index = 0; String[] values; @@ -76,6 +76,7 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { } else { values = fragment.substring(dataIndex + 1).split(";"); } + switch (fragment.substring(0, dataIndex)) { case "GPS": position.setValid(values[index++].equals("A")); @@ -171,42 +172,27 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { } } - private Object decodeData(DeviceSession deviceSession, Date time, String data) { - - List positions = new LinkedList<>(); - Position position = null; - int index = 0; - String[] fragments = data.split(","); + private Position decodePosition(DeviceSession deviceSession, String sentence) throws ParseException { - while (index < fragments.length) { + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); - if (fragments[index].isEmpty() || Character.isDigit(fragments[index].charAt(0))) { - - if (position != null) { - positions.add(position); - } + int index = 0; + String[] fragments = sentence.split(","); - position = new Position(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); - position.setTime(time); + position.setTime(new SimpleDateFormat("HHmmssddMMyy").parse(fragments[index++])); - if (!fragments[index++].isEmpty()) { - position.set(Position.KEY_EVENT, Integer.parseInt(fragments[index - 1])); + for (; index < fragments.length; index += 1) { + if (!fragments[index].isEmpty()) { + if (Character.isDigit(fragments[index].charAt(0))) { + position.set(Position.KEY_EVENT, Integer.parseInt(fragments[index])); + } else { + decodeFragment(position, fragments[index]); } - - } else { - - decodeFragment(position, fragments[index++]); - } - } - if (position != null) { - positions.add(position); - } - - return positions; + return position; } @Override @@ -256,12 +242,11 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { } else { - Date time = new Date(); - if (parser.hasNext(6)) { - time = parser.nextDateTime(Parser.DateTimeFormat.HMS_DMY); + List positions = new LinkedList<>(); + for (String item : parser.next().split("$")) { + positions.add(decodePosition(deviceSession, item)); } - - return decodeData(deviceSession, time, parser.next()); + return positions; } } -- cgit v1.2.3