From d7e99c8c537a53e5d49c409983ef5147ef36e4e3 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 5 Nov 2015 23:06:32 +1300 Subject: Refactor Intellitrac protocol decoder --- .../protocol/IntellitracProtocolDecoder.java | 147 +++++++++------------ 1 file changed, 65 insertions(+), 82 deletions(-) (limited to 'src/org/traccar/protocol') 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; } -- cgit v1.2.3