diff options
-rw-r--r-- | src/org/traccar/protocol/SviasProtocolDecoder.java | 134 |
1 files changed, 82 insertions, 52 deletions
diff --git a/src/org/traccar/protocol/SviasProtocolDecoder.java b/src/org/traccar/protocol/SviasProtocolDecoder.java index cca21467a..ef631bf64 100644 --- a/src/org/traccar/protocol/SviasProtocolDecoder.java +++ b/src/org/traccar/protocol/SviasProtocolDecoder.java @@ -1,5 +1,5 @@ /*
- * Copyright 2013 - 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2015 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.
@@ -17,104 +17,134 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
-import org.traccar.DeviceSession;
-import org.traccar.model.Position;
-import java.text.ParseException;
+import org.traccar.helper.PatternBuilder;
import java.net.SocketAddress;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
+import java.util.regex.Pattern;
+import org.traccar.DeviceSession;
+import org.traccar.helper.Parser;
import org.traccar.helper.UnitsConverter;
+import org.traccar.model.Position;
public class SviasProtocolDecoder extends BaseProtocolDecoder {
- public static final String MSG_KEEPALIVE = "@";
+ private static final String MSG_KEEPALIVE = "@";
public SviasProtocolDecoder(SviasProtocol protocol) {
super(protocol);
}
+ private static final Pattern PATTERN = new PatternBuilder()
+ .text("[") // delimiter init
+ .number("(dddd),") // version hardware
+ .number("(dddd),") // version software
+ .number("(d+),") // counter
+ .number("(d+),") // imei
+ .any() // model or hourmeter
+ .number("(d+),") // date (yyyymmdd)
+ .number("(d+),") // time (hhmmss)
+ .number("(-?d+),") // longitude
+ .number("(-?d+),") // latitude
+ .number("(d+),") // speed
+ .number("(d+),") // course
+ .number("(d+),") // odometer
+ .number("(d+),") // input
+ .number("(d+),") // output / status
+ .number("(d+),") // flag pack input 1
+ .number("(d+),") // flag pack input 2
+ .number("(d+),") // main power voltage
+ .number("(d+),") // percentual power internal battery
+ .number("(d+),") // RSSID
+ .any()
+ .compile();
+
private double convertCoordinates(long v) {
return Double.valueOf(((float) ((((float) v / 1.0E7F)
- ((int) (v / 10000000L))) * 1.6666666666666667D)) + ((int) (v / 10000000L)));
}
- private String toBin(String v) {
- return Integer.toString(Integer.parseInt(v), 2);
- }
+ @Override
+ protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg)
+ throws Exception {
- private Position decodePosition(Channel channel, SocketAddress remoteAddress, String substring)
- throws ParseException {
+ String sentence = (String) msg;
- String[] values = substring.split(",");
+ if (!sentence.contains(":")) {
- DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, values[3]);
- if (deviceSession == null) {
- return null;
- }
+ Parser parser = new Parser(PATTERN, (String) sentence);
+ if (!parser.matches()) {
+ return null;
+ }
- Position position = new Position();
- position.setProtocol(getProtocolName());
- position.setDeviceId(deviceSession.getDeviceId());
+ Position position = new Position(getProtocolName());
- position.set(Position.KEY_INDEX, Integer.valueOf(values[2]));
+ String versionHard = parser.next();
+ String versionSoft = parser.next();
+ String counterInternal = parser.next();
- position.set(Position.KEY_TYPE, values[4]);
+ DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
+ if (deviceSession == null) {
+ return null;
+ }
- DateFormat dateFormat = new SimpleDateFormat("ddMMyyHHmmss");
- dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
+ position.setDeviceId(deviceSession.getDeviceId());
- String date = String.format("%06d", Integer.parseInt(values[5]));
- String time = String.format("%06d", Integer.parseInt(values[6]));
+ DateFormat dateFormat = new SimpleDateFormat("ddMMyyHHmmss");
+ dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
- position.setTime(dateFormat.parse(date + time));
+ String date = String.format("%06d", parser.nextInt());
+ String time = String.format("%06d", parser.nextInt());
- position.setLatitude(convertCoordinates(Long.valueOf(values[7])));
- position.setLongitude(convertCoordinates(Long.valueOf(values[8])));
+ position.setTime(dateFormat.parse(date + time));
- position.setSpeed(UnitsConverter.knotsFromKph(Integer.valueOf(values[9]) / 100));
- position.setCourse(Integer.valueOf(values[10]) / 100);
+ position.setLatitude(convertCoordinates(parser.nextLong()));
+ position.setLongitude(convertCoordinates(parser.nextLong()));
+ position.setSpeed(UnitsConverter.knotsFromKph(parser.nextInt() / 100));
+ position.setCourse(parser.nextInt() / 100);
+ position.setAltitude(0);
- position.set(Position.KEY_ODOMETER, Integer.valueOf(values[11]));
+ position.set(Position.KEY_ODOMETER, parser.nextInt());
- String input = new StringBuilder(String.format("%08d",
- Integer.parseInt(toBin(values[12])))).reverse().toString();
+ String input = new StringBuilder(String.format("%08d",
+ Integer.parseInt(Integer.toString(parser.nextInt(), 2)))).reverse().toString();
- String output = new StringBuilder(String.format("%08d",
- Integer.parseInt(toBin(values[13])))).reverse().toString();
+ String output = new StringBuilder(String.format("%08d",
+ Integer.parseInt(Integer.toString(parser.nextInt(), 2)))).reverse().toString();
- position.set(Position.KEY_ALARM, (input.substring(0, 1).equals("1") ? Position.ALARM_SOS : null));
+ position.set(Position.KEY_ALARM, (input.substring(0, 1).equals("1")
+ ? Position.ALARM_SOS : null));
- position.set(Position.KEY_IGNITION, input.substring(4, 5).equals("1"));
+ position.set(Position.KEY_IGNITION, input.substring(4, 5).equals("1"));
- position.setValid(output.substring(0, 1).equals("1"));
+ position.setValid(output.substring(0, 1).equals("1"));
- return position;
+ String pck1 = parser.next();
+ String pck2 = parser.next();
- }
+ position.set(Position.KEY_POWER, parser.nextInt() / 1000);
- @Override
- protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg)
- throws Exception {
+ position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt());
- String sentence = (String) msg;
+ position.set(Position.KEY_RSSI, parser.nextInt());
- if (!sentence.contains(":")) {
+ if (channel != null) {
+ channel.write(MSG_KEEPALIVE);
+ }
- Position position = decodePosition(channel, remoteAddress, sentence.substring(1));
+ return position;
- if (position != null) {
- return position;
+ } else {
+ //send keepalive for message check
+ if (channel != null) {
+ channel.write(MSG_KEEPALIVE);
}
- }
- if (channel != null) {
- channel.write(MSG_KEEPALIVE);
+ return null;
}
- return null;
-
}
}
|