From 53494de4e41e1c4d4440c9afc0c4a9dfa6480558 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 28 Sep 2013 13:40:00 +1200 Subject: Remove optional fields --- src/org/traccar/model/DatabaseDataManager.java | 30 ++++++++++++++++-- src/org/traccar/model/Position.java | 13 -------- src/org/traccar/protocol/ApelProtocolDecoder.java | 2 +- .../traccar/protocol/AplicomProtocolDecoder.java | 2 +- src/org/traccar/protocol/Avl08ProtocolDecoder.java | 2 +- .../traccar/protocol/EasyTrackProtocolDecoder.java | 2 +- .../traccar/protocol/GalileoProtocolDecoder.java | 2 +- src/org/traccar/protocol/GatorProtocolDecoder.java | 2 +- src/org/traccar/protocol/Gt02ProtocolDecoder.java | 5 ++- src/org/traccar/protocol/Gt06ProtocolDecoder.java | 4 +-- .../protocol/IntellitracProtocolDecoder.java | 2 +- src/org/traccar/protocol/Jt600ProtocolDecoder.java | 4 +-- .../traccar/protocol/MegastekProtocolDecoder.java | 4 +-- src/org/traccar/protocol/Mta6ProtocolDecoder.java | 4 +-- .../traccar/protocol/NavigilProtocolDecoder.java | 22 ++++++------- src/org/traccar/protocol/NavisProtocolDecoder.java | 37 ++++++++++++++++------ .../traccar/protocol/ProgressProtocolDecoder.java | 2 +- .../traccar/protocol/SkypatrolProtocolDecoder.java | 4 +-- src/org/traccar/protocol/TotemProtocolDecoder.java | 4 +-- .../traccar/protocol/Xexun2ProtocolDecoder.java | 3 +- src/org/traccar/protocol/Xt7ProtocolDecoder.java | 2 +- 21 files changed, 93 insertions(+), 59 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/model/DatabaseDataManager.java b/src/org/traccar/model/DatabaseDataManager.java index ce781da1a..f56782b44 100644 --- a/src/org/traccar/model/DatabaseDataManager.java +++ b/src/org/traccar/model/DatabaseDataManager.java @@ -16,13 +16,21 @@ package org.traccar.model; import java.io.File; +import java.io.StringReader; import java.net.URL; import java.net.URLClassLoader; import java.sql.*; import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; import org.traccar.helper.AdvancedConnection; import org.traccar.helper.DriverDelegate; +import org.traccar.helper.Log; import org.traccar.helper.NamedParameterStatement; +import org.xml.sax.InputSource; /** * Database abstraction class @@ -140,7 +148,6 @@ public class DatabaseDataManager implements DataManager { if (queryAddPosition != null) { queryAddPosition.prepare(Statement.RETURN_GENERATED_KEYS); - queryAddPosition.setLong("id", position.getId()); queryAddPosition.setLong("device_id", position.getDeviceId()); queryAddPosition.setTimestamp("time", position.getTime()); queryAddPosition.setBoolean("valid", position.getValid()); @@ -149,9 +156,28 @@ public class DatabaseDataManager implements DataManager { queryAddPosition.setDouble("longitude", position.getLongitude()); queryAddPosition.setDouble("speed", position.getSpeed()); queryAddPosition.setDouble("course", position.getCourse()); - queryAddPosition.setDouble("power", position.getPower()); queryAddPosition.setString("address", position.getAddress()); queryAddPosition.setString("extended_info", position.getExtendedInfo()); + + // DELME: Temporary compatibility support + XPath xpath = XPathFactory.newInstance().newXPath(); + InputSource source = new InputSource(new StringReader(position.getExtendedInfo())); + try { + String index = xpath.evaluate("/info/index", source); + if (!index.isEmpty()) { + queryAddPosition.setLong("id", Long.valueOf(index)); + } else { + queryAddPosition.setLong("id", null); + } + String power = xpath.evaluate("/info/power", source); + if (!power.isEmpty()) { + queryAddPosition.setDouble("power", Double.valueOf(power)); + } else { + queryAddPosition.setLong("power", null); + } + } catch (XPathExpressionException ex) { + Log.warning(ex); + } queryAddPosition.executeUpdate(); diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java index b408dbbf2..744019bc0 100644 --- a/src/org/traccar/model/Position.java +++ b/src/org/traccar/model/Position.java @@ -139,19 +139,6 @@ public class Position { this.course = course; } - /** - * Power - */ - private Double power; - - public Double getPower() { - return power; - } - - public void setPower(Double power) { - this.power = power; - } - /** * Address */ diff --git a/src/org/traccar/protocol/ApelProtocolDecoder.java b/src/org/traccar/protocol/ApelProtocolDecoder.java index 5f626a496..9ffc473c7 100644 --- a/src/org/traccar/protocol/ApelProtocolDecoder.java +++ b/src/org/traccar/protocol/ApelProtocolDecoder.java @@ -177,7 +177,7 @@ public class ApelProtocolDecoder extends BaseProtocolDecoder { if (type == MSG_TYPE_LOG_RECORDS) { extendedInfo.set("archive", true); lastIndex = buf.readUnsignedInt() + 1; - position.setId(lastIndex); + extendedInfo.set("index", lastIndex); subtype = buf.readUnsignedShort(); if (subtype != MSG_TYPE_CURRENT_GPS_DATA && subtype != MSG_TYPE_STATE_FULL_INFO_T104) { diff --git a/src/org/traccar/protocol/AplicomProtocolDecoder.java b/src/org/traccar/protocol/AplicomProtocolDecoder.java index 978b84b6d..21a3127d1 100644 --- a/src/org/traccar/protocol/AplicomProtocolDecoder.java +++ b/src/org/traccar/protocol/AplicomProtocolDecoder.java @@ -181,7 +181,7 @@ public class AplicomProtocolDecoder extends BaseProtocolDecoder { // Power if ((selector & 0x8000) != 0) { - position.setPower(buf.readUnsignedShort() / 1000.0); + extendedInfo.set("power", buf.readUnsignedShort() / 1000.0); extendedInfo.set("battery", buf.readUnsignedShort()); } diff --git a/src/org/traccar/protocol/Avl08ProtocolDecoder.java b/src/org/traccar/protocol/Avl08ProtocolDecoder.java index 6ccb333d9..825e84cf6 100644 --- a/src/org/traccar/protocol/Avl08ProtocolDecoder.java +++ b/src/org/traccar/protocol/Avl08ProtocolDecoder.java @@ -157,7 +157,7 @@ public class Avl08ProtocolDecoder extends BaseProtocolDecoder { // Voltage String voltage = parser.group(index++); - position.setPower(Double.valueOf(voltage.substring(1, 4)) / 100); + extendedInfo.set("power", Double.valueOf(voltage.substring(1, 4)) / 100); extendedInfo.set("voltage", voltage); // ADC diff --git a/src/org/traccar/protocol/EasyTrackProtocolDecoder.java b/src/org/traccar/protocol/EasyTrackProtocolDecoder.java index d6a14fd08..7152955f5 100644 --- a/src/org/traccar/protocol/EasyTrackProtocolDecoder.java +++ b/src/org/traccar/protocol/EasyTrackProtocolDecoder.java @@ -123,7 +123,7 @@ public class EasyTrackProtocolDecoder extends BaseProtocolDecoder { extendedInfo.set("signal", parser.group(index++)); // Power - position.setPower(Double.valueOf(parser.group(index++))); + extendedInfo.set("power", Double.valueOf(parser.group(index++))); // Oil extendedInfo.set("oil", Integer.parseInt(parser.group(index++), 16)); diff --git a/src/org/traccar/protocol/GalileoProtocolDecoder.java b/src/org/traccar/protocol/GalileoProtocolDecoder.java index 94ffa60af..3f652dcb0 100644 --- a/src/org/traccar/protocol/GalileoProtocolDecoder.java +++ b/src/org/traccar/protocol/GalileoProtocolDecoder.java @@ -137,7 +137,7 @@ public class GalileoProtocolDecoder extends BaseProtocolDecoder { break; case TAG_POWER: - position.setPower((double) buf.readUnsignedShort()); + extendedInfo.set("power", buf.readUnsignedShort()); break; case TAG_BATTERY: diff --git a/src/org/traccar/protocol/GatorProtocolDecoder.java b/src/org/traccar/protocol/GatorProtocolDecoder.java index 9fa50cc88..041e44588 100644 --- a/src/org/traccar/protocol/GatorProtocolDecoder.java +++ b/src/org/traccar/protocol/GatorProtocolDecoder.java @@ -126,7 +126,7 @@ public class GatorProtocolDecoder extends BaseProtocolDecoder { extendedInfo.set("oil", buf.readUnsignedShort() / 10.0); // Power - position.setPower(buf.readUnsignedByte() + buf.readUnsignedByte() / 100.0); + extendedInfo.set("power", buf.readUnsignedByte() + buf.readUnsignedByte() / 100.0); // Milage extendedInfo.set("milage", buf.readUnsignedInt()); diff --git a/src/org/traccar/protocol/Gt02ProtocolDecoder.java b/src/org/traccar/protocol/Gt02ProtocolDecoder.java index 22402ce45..65c3260e7 100644 --- a/src/org/traccar/protocol/Gt02ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gt02ProtocolDecoder.java @@ -24,6 +24,7 @@ import org.jboss.netty.channel.ChannelHandlerContext; import org.traccar.BaseProtocolDecoder; import org.traccar.ServerManager; import org.traccar.helper.Log; +import org.traccar.model.ExtendedInfoFormatter; import org.traccar.model.Position; public class Gt02ProtocolDecoder extends BaseProtocolDecoder { @@ -76,7 +77,8 @@ public class Gt02ProtocolDecoder extends BaseProtocolDecoder { // Create new position Position position = new Position(); - position.setId(index); + ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter("gt02"); + extendedInfo.set("index", index); // Get device id try { @@ -120,6 +122,7 @@ public class Gt02ProtocolDecoder extends BaseProtocolDecoder { position.setLongitude(longitude); position.setAltitude(0.0); + position.setExtendedInfo(extendedInfo.toString()); return position; } diff --git a/src/org/traccar/protocol/Gt06ProtocolDecoder.java b/src/org/traccar/protocol/Gt06ProtocolDecoder.java index 9b5bcc441..d67e6843e 100644 --- a/src/org/traccar/protocol/Gt06ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gt06ProtocolDecoder.java @@ -169,7 +169,7 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { extendedInfo.set("alarm", true); // Voltage - position.setPower((double) buf.readUnsignedByte()); + extendedInfo.set("power", buf.readUnsignedByte()); // GSM signal extendedInfo.set("gsm", buf.readUnsignedByte()); @@ -181,7 +181,7 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { buf.skipBytes(buf.readableBytes() - 6); } int index = buf.readUnsignedShort(); - position.setId((long) index); + extendedInfo.set("index", index); sendResponse(channel, type, index); position.setExtendedInfo(extendedInfo.toString()); diff --git a/src/org/traccar/protocol/IntellitracProtocolDecoder.java b/src/org/traccar/protocol/IntellitracProtocolDecoder.java index de1ce35de..b71237e7f 100644 --- a/src/org/traccar/protocol/IntellitracProtocolDecoder.java +++ b/src/org/traccar/protocol/IntellitracProtocolDecoder.java @@ -100,7 +100,7 @@ public class IntellitracProtocolDecoder extends BaseProtocolDecoder { extendedInfo.set("satellites", satellites); // Report identifier - position.setId(Long.valueOf(parser.group(index++))); + extendedInfo.set("index", Long.valueOf(parser.group(index++))); // Input extendedInfo.set("input", parser.group(index++)); diff --git a/src/org/traccar/protocol/Jt600ProtocolDecoder.java b/src/org/traccar/protocol/Jt600ProtocolDecoder.java index df6d94e47..d12b09740 100644 --- a/src/org/traccar/protocol/Jt600ProtocolDecoder.java +++ b/src/org/traccar/protocol/Jt600ProtocolDecoder.java @@ -98,7 +98,7 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { extendedInfo.set("satellites", buf.readUnsignedByte()); // Power - position.setPower((double) buf.readUnsignedByte()); + extendedInfo.set("power", buf.readUnsignedByte()); buf.readByte(); // other flags and sensors @@ -204,7 +204,7 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { position.setCourse(Double.valueOf(parser.group(index++))); // Power - position.setPower(Double.valueOf(parser.group(index++))); + extendedInfo.set("power", Double.valueOf(parser.group(index++))); position.setExtendedInfo(extendedInfo.toString()); return position; diff --git a/src/org/traccar/protocol/MegastekProtocolDecoder.java b/src/org/traccar/protocol/MegastekProtocolDecoder.java index ee7c5df2c..f73d04be4 100644 --- a/src/org/traccar/protocol/MegastekProtocolDecoder.java +++ b/src/org/traccar/protocol/MegastekProtocolDecoder.java @@ -218,7 +218,7 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { position.setAltitude(Double.valueOf(parser.group(index++))); // Battery - position.setPower(Double.valueOf(parser.group(index++))); + extendedInfo.set("power", Double.valueOf(parser.group(index++))); // Charger String charger = parser.group(index++); @@ -253,7 +253,7 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { extendedInfo.set("gsm", parser.group(index++)); // Battery - position.setPower(Double.valueOf(parser.group(index++))); + extendedInfo.set("battery", Double.valueOf(parser.group(index++))); extendedInfo.set("flags", parser.group(index++)); extendedInfo.set("input", parser.group(index++)); diff --git a/src/org/traccar/protocol/Mta6ProtocolDecoder.java b/src/org/traccar/protocol/Mta6ProtocolDecoder.java index 108e74963..62fa87e68 100644 --- a/src/org/traccar/protocol/Mta6ProtocolDecoder.java +++ b/src/org/traccar/protocol/Mta6ProtocolDecoder.java @@ -189,7 +189,7 @@ public class Mta6ProtocolDecoder extends BaseProtocolDecoder { if (checkBit(flags, 7)) { extendedInfo.set("battery", buf.getUnsignedByte(buf.readerIndex()) >> 2); - position.setPower((double) (buf.readUnsignedShort() & 0x03ff)); + extendedInfo.set("power", buf.readUnsignedShort() & 0x03ff); buf.readByte(); // microcontroller temperature extendedInfo.set("gsm", (buf.getUnsignedByte(buf.readerIndex()) >> 4) & 0x07); @@ -269,7 +269,7 @@ public class Mta6ProtocolDecoder extends BaseProtocolDecoder { if (checkBit(flags, 5)) { extendedInfo.set("battery", buf.getUnsignedByte(buf.readerIndex()) >> 2); - position.setPower((double) (buf.readUnsignedShort() & 0x03ff)); + extendedInfo.set("power", buf.readUnsignedShort() & 0x03ff); buf.readByte(); // microcontroller temperature extendedInfo.set("gsm", buf.getUnsignedByte(buf.readerIndex()) >> 5); diff --git a/src/org/traccar/protocol/NavigilProtocolDecoder.java b/src/org/traccar/protocol/NavigilProtocolDecoder.java index 34642496a..29be1e5ad 100644 --- a/src/org/traccar/protocol/NavigilProtocolDecoder.java +++ b/src/org/traccar/protocol/NavigilProtocolDecoder.java @@ -83,7 +83,7 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter("navigil"); position.setValid(true); - position.setId((long) sequenceNumber); + extendedInfo.set("index", sequenceNumber); position.setDeviceId(deviceId); buf.readUnsignedShort(); // report trigger @@ -102,8 +102,8 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedInt(); // distance buf.readUnsignedInt(); // delta distance - - position.setPower(buf.readUnsignedShort() * 0.001); + + extendedInfo.set("battery", buf.readUnsignedShort() * 0.001); buf.readUnsignedShort(); // battery charger status @@ -120,7 +120,7 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter("navigil"); position.setValid(true); - position.setId((long) sequenceNumber); + extendedInfo.set("index", sequenceNumber); position.setDeviceId(deviceId); buf.readUnsignedShort(); // report trigger @@ -146,7 +146,7 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedShort(); // VSAUT1 voltage buf.readUnsignedShort(); // VSAUT2 voltage buf.readUnsignedShort(); // solar voltage - position.setPower(buf.readUnsignedShort() * 0.001); // battery voltage + extendedInfo.set("battery", buf.readUnsignedShort() * 0.001); // TODO: a lot of other stuff @@ -158,7 +158,7 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter("navigil"); - position.setId((long) sequenceNumber); + extendedInfo.set("index", sequenceNumber); position.setDeviceId(deviceId); position.setTime(convertTimestamp(timestamp)); @@ -182,7 +182,7 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter("navigil"); - position.setId((long) sequenceNumber); + extendedInfo.set("index", sequenceNumber); position.setDeviceId(deviceId); position.setTime(convertTimestamp(timestamp)); @@ -209,7 +209,7 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter("navigil"); - position.setId((long) sequenceNumber); + extendedInfo.set("index", sequenceNumber); position.setDeviceId(deviceId); buf.readUnsignedByte(); // report trigger @@ -238,7 +238,7 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedByte(); // supply voltage 1 buf.readUnsignedByte(); // supply voltage 2 - position.setPower(buf.readUnsignedByte() * 0.01 + 2.5); // battery voltage + extendedInfo.set("battery", buf.readUnsignedShort() * 0.001); // TODO: a lot of other stuff @@ -250,7 +250,7 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter("navigil"); - position.setId((long) sequenceNumber); + extendedInfo.set("index", sequenceNumber); position.setDeviceId(deviceId); position.setTime(convertTimestamp(timestamp)); @@ -270,7 +270,7 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedByte(); // satellites in fix - position.setPower(buf.readUnsignedByte() * 0.005 + 3.0); // battery voltage + extendedInfo.set("battery", buf.readUnsignedShort() * 0.001); buf.readUnsignedInt(); // distance diff --git a/src/org/traccar/protocol/NavisProtocolDecoder.java b/src/org/traccar/protocol/NavisProtocolDecoder.java index 9de70595a..70a76a2f5 100644 --- a/src/org/traccar/protocol/NavisProtocolDecoder.java +++ b/src/org/traccar/protocol/NavisProtocolDecoder.java @@ -62,8 +62,26 @@ public class NavisProtocolDecoder extends BaseProtocolDecoder { } return false; } + + private class ParseResult { + private long id; + private Position position; + + public ParseResult(long id, Position position) { + this.id = id; + this.position = position; + } + + public long getId() { + return id; + } + + public Position getPosition() { + return position; + } + } - private Position parsePosition(ChannelBuffer buf) { + private ParseResult parsePosition(ChannelBuffer buf) { Position position = new Position(); ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter("navis"); @@ -79,7 +97,8 @@ public class NavisProtocolDecoder extends BaseProtocolDecoder { } extendedInfo.set("format", format); - position.setId(buf.readUnsignedInt()); // sequence number + long index = buf.readUnsignedInt(); + extendedInfo.set("index", index); // Event type extendedInfo.set("event", buf.readUnsignedShort()); @@ -118,7 +137,7 @@ public class NavisProtocolDecoder extends BaseProtocolDecoder { extendedInfo.set("input", buf.readUnsignedByte()); } - position.setPower(buf.readUnsignedShort() / 1000.0); // power + extendedInfo.set("power", buf.readUnsignedShort() / 1000.0); // Battery power extendedInfo.set("battery", buf.readUnsignedShort()); @@ -195,23 +214,23 @@ public class NavisProtocolDecoder extends BaseProtocolDecoder { // Extended info position.setExtendedInfo(extendedInfo.toString()); - return position; + return new ParseResult(index, position); } private Object processSingle(Channel channel, ChannelBuffer buf) { - Position position = parsePosition(buf); + ParseResult result = parsePosition(buf); ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 8); response.writeBytes(ChannelBuffers.copiedBuffer(ByteOrder.LITTLE_ENDIAN, "*