aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-11-05 23:06:32 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-11-05 23:06:32 +1300
commitd7e99c8c537a53e5d49c409983ef5147ef36e4e3 (patch)
tree10e48b26e76899abe4fa5e7cedb68c0ab63d3a9e
parent6b4475237201b4e4bb6b161966cf6c8518b25f85 (diff)
downloadtraccar-server-d7e99c8c537a53e5d49c409983ef5147ef36e4e3.tar.gz
traccar-server-d7e99c8c537a53e5d49c409983ef5147ef36e4e3.tar.bz2
traccar-server-d7e99c8c537a53e5d49c409983ef5147ef36e4e3.zip
Refactor Intellitrac protocol decoder
-rw-r--r--src/org/traccar/protocol/IntellitracProtocolDecoder.java147
-rw-r--r--test/org/traccar/protocol/IntellitracProtocolDecoderTest.java3
2 files changed, 67 insertions, 83 deletions
diff --git a/src/org/traccar/protocol/IntellitracProtocolDecoder.java b/src/org/traccar/protocol/IntellitracProtocolDecoder.java
index 6c5c0f58a..20a0f9160 100644
--- a/src/org/traccar/protocol/IntellitracProtocolDecoder.java
+++ b/src/org/traccar/protocol/IntellitracProtocolDecoder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2013 - 2014 Anton Tananaev (anton.tananaev@gmail.com)
+ * Copyright 2013 - 2015 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.
@@ -16,12 +16,12 @@
package org.traccar.protocol;
import java.net.SocketAddress;
-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.traccar.BaseProtocolDecoder;
+import org.traccar.helper.DateBuilder;
+import org.traccar.helper.Parser;
+import org.traccar.helper.PatternBuilder;
import org.traccar.model.Event;
import org.traccar.model.Position;
@@ -31,105 +31,88 @@ public class IntellitracProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private static final Pattern PATTERN = Pattern.compile(
- "(?:.+,)?(\\d+)," + // Device Identifier
- "(\\d{4})(\\d{2})(\\d{2})" + // Date (YYYYMMDD)
- "(\\d{2})(\\d{2})(\\d{2})," + // Time (HHMMSS)
- "(-?\\d+\\.\\d+)," + // Longitude
- "(-?\\d+\\.\\d+)," + // Latitude
- "(\\d+\\.?\\d*)," + // Speed
- "(\\d+\\.?\\d*)," + // Course
- "(-?\\d+\\.?\\d*)," + // Altitude
- "(\\d+)," + // Satellites
- "(\\d+)," + // Report Identifier
- "(\\d+)," + // Input
- "(\\d+),?" + // Output
- "(\\d+\\.\\d+)?,?" + // ADC1
- "(\\d+\\.\\d+)?,?" + // ADC2
- "(?:\\d{14},\\d+," +
- "(\\d+)," + // VSS
- "(\\d+)," + // RPM
- "(-?\\d+)," + // Coolant
- "(\\d+)," + // Fuel
- "(\\d+)," + // Fuel Consumption
- "(-?\\d+)," + // Fuel Temperature
- "(\\d+)," + // Charger Pressure
- "(\\d+)," + // TPL
- "(\\d+)," + // Axle Weight
- "(\\d+))?" + // Odometer
- ".*");
+ private static final Pattern PATTERN = new PatternBuilder()
+ .expression(".+,").optional()
+ .number("(d+),") // identifier
+ .number("(dddd)(dd)(dd)") // date
+ .number("(dd)(dd)(dd),") // time
+ .number("(-?d+.d+),") // longitude
+ .number("(-?d+.d+),") // latitude
+ .number("(d+.?d*),") // speed
+ .number("(d+.?d*),") // course
+ .number("(-?d+.?d*),") // altitude
+ .number("(d+),") // satellites
+ .number("(d+),") // index
+ .number("(d+),") // input
+ .number("(d+),?") // output
+ .number("(d+.d+)?,?") // adc1
+ .number("(d+.d+)?,?") // adc2
+ .groupBegin()
+ .number("d{14},d+,")
+ .number("(d+),") // vss
+ .number("(d+),") // rpm
+ .number("(-?d+),") // coolant
+ .number("(d+),") // fuel
+ .number("(d+),") // fuel consumption
+ .number("(-?d+),") // fuel temperature
+ .number("(d+),") // charger pressure
+ .number("(d+),") // tpl
+ .number("(d+),") // axle weight
+ .number("(d+)") // odometer
+ .groupEnd("?")
+ .any()
+ .compile();
@Override
protected Object decode(
- Channel channel, SocketAddress remoteAddress, Object msg)
- throws Exception {
+ Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
- String sentence = (String) msg;
-
- // Parse message
- Matcher parser = PATTERN.matcher(sentence);
+ Parser parser = new Parser(PATTERN, (String) msg);
if (!parser.matches()) {
return null;
}
- // Create new position
Position position = new Position();
position.setProtocol(getProtocolName());
- Integer index = 1;
- // Detect device
- if (!identify(parser.group(index++), channel)) {
+ if (!identify(parser.next(), channel)) {
return null;
}
position.setDeviceId(getDeviceId());
- // Date and time
- Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
- time.clear();
- time.set(Calendar.YEAR, Integer.parseInt(parser.group(index++)));
- time.set(Calendar.MONTH, Integer.parseInt(parser.group(index++)) - 1);
- time.set(Calendar.DAY_OF_MONTH, Integer.parseInt(parser.group(index++)));
- time.set(Calendar.HOUR_OF_DAY, Integer.parseInt(parser.group(index++)));
- time.set(Calendar.MINUTE, Integer.parseInt(parser.group(index++)));
- time.set(Calendar.SECOND, Integer.parseInt(parser.group(index++)));
- position.setTime(time.getTime());
-
- // Location data
- position.setLongitude(Double.parseDouble(parser.group(index++)));
- position.setLatitude(Double.parseDouble(parser.group(index++)));
- position.setSpeed(Double.parseDouble(parser.group(index++)));
- position.setCourse(Double.parseDouble(parser.group(index++)));
- position.setAltitude(Double.parseDouble(parser.group(index++)));
-
- // Satellites
- int satellites = Integer.parseInt(parser.group(index++));
- position.setValid(satellites >= 3);
- position.set(Event.KEY_SATELLITES, satellites);
+ DateBuilder dateBuilder = new DateBuilder()
+ .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt())
+ .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
+ position.setTime(dateBuilder.getDate());
- // Report identifier
- position.set(Event.KEY_INDEX, Long.parseLong(parser.group(index++)));
+ position.setLongitude(parser.nextDouble());
+ position.setLatitude(parser.nextDouble());
+ position.setSpeed(parser.nextDouble());
+ position.setCourse(parser.nextDouble());
+ position.setAltitude(parser.nextDouble());
- // Input
- position.set(Event.KEY_INPUT, parser.group(index++));
+ int satellites = parser.nextInt();
+ position.setValid(satellites >= 3);
+ position.set(Event.KEY_SATELLITES, satellites);
- // Output
- position.set(Event.KEY_OUTPUT, parser.group(index++));
+ position.set(Event.KEY_INDEX, parser.nextLong());
+ position.set(Event.KEY_INPUT, parser.next());
+ position.set(Event.KEY_OUTPUT, parser.next());
- // ADC
- position.set(Event.PREFIX_ADC + 1, parser.group(index++));
- position.set(Event.PREFIX_ADC + 2, parser.group(index++));
+ position.set(Event.PREFIX_ADC + 1, parser.next());
+ position.set(Event.PREFIX_ADC + 2, parser.next());
// J1939 data
- position.set(Event.KEY_OBD_SPEED, parser.group(index++));
- position.set(Event.KEY_RPM, parser.group(index++));
- position.set("coolant", parser.group(index++));
- position.set(Event.KEY_FUEL, parser.group(index++));
- position.set("consumption", parser.group(index++));
- position.set(Event.PREFIX_TEMP + 1, parser.group(index++));
- position.set(Event.KEY_CHARGE, parser.group(index++));
- position.set("tpl", parser.group(index++));
- position.set("axle", parser.group(index++));
- position.set(Event.KEY_ODOMETER, parser.group(index++));
+ position.set(Event.KEY_OBD_SPEED, parser.next());
+ position.set(Event.KEY_RPM, parser.next());
+ position.set("coolant", parser.next());
+ position.set(Event.KEY_FUEL, parser.next());
+ position.set("consumption", parser.next());
+ position.set(Event.PREFIX_TEMP + 1, parser.next());
+ position.set(Event.KEY_CHARGE, parser.next());
+ position.set("tpl", parser.next());
+ position.set("axle", parser.next());
+ position.set(Event.KEY_ODOMETER, parser.next());
return position;
}
diff --git a/test/org/traccar/protocol/IntellitracProtocolDecoderTest.java b/test/org/traccar/protocol/IntellitracProtocolDecoderTest.java
index aace40f15..23d7fb48a 100644
--- a/test/org/traccar/protocol/IntellitracProtocolDecoderTest.java
+++ b/test/org/traccar/protocol/IntellitracProtocolDecoderTest.java
@@ -14,7 +14,8 @@ public class IntellitracProtocolDecoderTest extends ProtocolDecoderTest {
"$OK:TRACKING"));
verifyPosition(decoder, text(
- "101000001,20100304075545,121.64547,25.06200,0,0,61,7,2,1,0,0.046,0.000,20100304075546,0"));
+ "101000001,20100304075545,121.64547,25.06200,0,0,61,7,2,1,0,0.046,0.000,20100304075546,0"),
+ position("2010-03-04 07:55:45.000", true, 25.06200, 121.64547));
verifyPosition(decoder, text(
"1010000002,20030217132813,121.646060,25.061725,20,157,133,7,0,11,15,0.096,0.000"));