aboutsummaryrefslogtreecommitdiff
path: root/src/org
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-11-06 15:46:00 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-11-06 15:46:00 +1300
commitdbcfd8488213623e68607fa946df63256dbabf18 (patch)
treedb6d5a1aef5d5446d1599bc039737c78eaa2a5a5 /src/org
parentc5f53243c67faa2d1663ea357e24e37d2c4735d0 (diff)
downloadtraccar-server-dbcfd8488213623e68607fa946df63256dbabf18.tar.gz
traccar-server-dbcfd8488213623e68607fa946df63256dbabf18.tar.bz2
traccar-server-dbcfd8488213623e68607fa946df63256dbabf18.zip
Refactor Wondex protocol decoder
Diffstat (limited to 'src/org')
-rw-r--r--src/org/traccar/protocol/WondexProtocolDecoder.java109
1 files changed, 44 insertions, 65 deletions
diff --git a/src/org/traccar/protocol/WondexProtocolDecoder.java b/src/org/traccar/protocol/WondexProtocolDecoder.java
index c2a5a2201..1a639b05c 100644
--- a/src/org/traccar/protocol/WondexProtocolDecoder.java
+++ b/src/org/traccar/protocol/WondexProtocolDecoder.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.helper.UnitsConverter;
import org.traccar.model.Event;
import org.traccar.model.Position;
@@ -32,88 +32,67 @@ public class WondexProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private static final Pattern PATTERN = Pattern.compile(
- "[^\\d]*" + // Header
- "(\\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+)," + // Speed
- "(\\d+)," + // Course
- "(-?\\d+\\.?\\d*)," + // Altitude
- "(\\d+)," + // Satellites
- "(\\d+),?" + // Event
- "(?:(\\d+\\.\\d+)V,)?" + // Battery
- "(\\d+\\.\\d+)?,?" + // Odometer
- "(\\d+)?,?" + // Input
- "(\\d+\\.\\d+)?,?" + // ADC1
- "(\\d+\\.\\d+)?,?" + // ADC2
- "(\\d+)?.*"); // Output
+ private static final Pattern PATTERN = new PatternBuilder()
+ .number("[^d]*") // deader
+ .number("(d+),") // device identifier
+ .number("(dddd)(dd)(dd)") // date
+ .number("(dd)(dd)(dd),") // time
+ .number("(-?d+.d+),") // longitude
+ .number("(-?d+.d+),") // latitude
+ .number("(d+),") // speed
+ .number("(d+),") // course
+ .number("(-?d+.?d*),") // altitude
+ .number("(d+),") // satellites
+ .number("(d+),?") // event
+ .number("(d+.d+)V,").optional() // battery
+ .number("(d+.d+)?,?") // odometer
+ .number("(d+)?,?") // input
+ .number("(d+.d+)?,?") // adc1
+ .number("(d+.d+)?,?") // adc2
+ .number("(d+)?") // output
+ .any()
+ .compile();
@Override
protected Object decode(
- Channel channel, SocketAddress remoteAddress, Object msg)
- throws Exception {
+ Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
- // Parse message
- Matcher parser = PATTERN.matcher((String) msg);
+ Parser parser = new Parser(PATTERN, (String) msg);
if (!parser.matches()) {
return null;
}
- // Create new position
Position position = new Position();
position.setProtocol(getProtocolName());
- int index = 1;
- // Device identifier
- if (!identify(parser.group(index++), channel)) {
+ if (!identify(parser.next(), channel)) {
return null;
}
position.setDeviceId(getDeviceId());
- // 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());
+ DateBuilder dateBuilder = new DateBuilder()
+ .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt())
+ .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
+ position.setTime(dateBuilder.getDate());
- // Position data
- position.setLongitude(Double.parseDouble(parser.group(index++)));
- position.setLatitude(Double.parseDouble(parser.group(index++)));
- position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(parser.group(index++))));
- position.setCourse(Double.parseDouble(parser.group(index++)));
- position.setAltitude(Double.parseDouble(parser.group(index++)));
+ position.setLongitude(parser.nextDouble());
+ position.setLatitude(parser.nextDouble());
+ position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble()));
+ position.setCourse(parser.nextDouble());
+ position.setAltitude(parser.nextDouble());
- // Satellites
- int satellites = Integer.parseInt(parser.group(index++));
+ int satellites = parser.nextInt();
position.setValid(satellites >= 3);
position.set(Event.KEY_SATELLITES, satellites);
- // Event
- position.set(Event.KEY_EVENT, parser.group(index++));
+ position.set(Event.KEY_EVENT, parser.next());
+ position.set(Event.KEY_BATTERY, parser.next());
+ position.set(Event.KEY_ODOMETER, parser.next());
+ position.set(Event.KEY_INPUT, parser.next());
+ position.set(Event.PREFIX_ADC + 1, parser.next());
+ position.set(Event.PREFIX_ADC + 2, parser.next());
+ position.set(Event.KEY_OUTPUT, parser.next());
- // Battery
- position.set(Event.KEY_BATTERY, parser.group(index++));
-
- // Odometer
- position.set(Event.KEY_ODOMETER, parser.group(index++));
-
- // Input
- position.set(Event.KEY_INPUT, parser.group(index++));
-
- // ADC
- position.set(Event.PREFIX_ADC + 1, parser.group(index++));
- position.set(Event.PREFIX_ADC + 2, parser.group(index++));
-
- // Output
- position.set(Event.KEY_OUTPUT, parser.group(index++));
return position;
}