From c71701136245a63ea82a8a595dd51cc011ab2c35 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 29 May 2018 17:03:15 +0500 Subject: Add units converter helper --- src/org/traccar/helper/UnitsConverter.java | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/org/traccar/helper/UnitsConverter.java') diff --git a/src/org/traccar/helper/UnitsConverter.java b/src/org/traccar/helper/UnitsConverter.java index 56d15e4e7..7519879c0 100644 --- a/src/org/traccar/helper/UnitsConverter.java +++ b/src/org/traccar/helper/UnitsConverter.java @@ -23,6 +23,7 @@ public final class UnitsConverter { private static final double KNOTS_TO_CPS_RATIO = 0.0194384449; private static final double METERS_TO_FEET_RATIO = 0.3048; private static final double METERS_TO_MILE_RATIO = 1609.34; + private static final long MILLISECONDS_TO_HOURS_RATIO = 3600000; private UnitsConverter() { } @@ -71,4 +72,12 @@ public final class UnitsConverter { return value * METERS_TO_MILE_RATIO; } + public static long millisecondsFromHours(int value) { + return value * MILLISECONDS_TO_HOURS_RATIO; + } + + public static long millisecondsFromHours(double value) { + return (long) (value * MILLISECONDS_TO_HOURS_RATIO); + } + } -- cgit v1.2.3 From 0e9f1f9bd0df2bc6771f3f5819eb6dbe8c3ce191 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 29 May 2018 17:44:01 +0500 Subject: Make function name shorter --- src/org/traccar/helper/UnitsConverter.java | 4 ++-- src/org/traccar/protocol/AplicomProtocolDecoder.java | 2 +- src/org/traccar/protocol/AstraProtocolDecoder.java | 2 +- src/org/traccar/protocol/ContinentalProtocolDecoder.java | 2 +- src/org/traccar/protocol/Gl200TextProtocolDecoder.java | 3 +-- src/org/traccar/protocol/Gps103ProtocolDecoder.java | 2 +- src/org/traccar/protocol/Mta6ProtocolDecoder.java | 3 +-- src/org/traccar/protocol/VtfmsProtocolDecoder.java | 2 +- src/org/traccar/protocol/XirgoProtocolDecoder.java | 2 +- 9 files changed, 10 insertions(+), 12 deletions(-) (limited to 'src/org/traccar/helper/UnitsConverter.java') diff --git a/src/org/traccar/helper/UnitsConverter.java b/src/org/traccar/helper/UnitsConverter.java index 7519879c0..74f5a90e6 100644 --- a/src/org/traccar/helper/UnitsConverter.java +++ b/src/org/traccar/helper/UnitsConverter.java @@ -72,11 +72,11 @@ public final class UnitsConverter { return value * METERS_TO_MILE_RATIO; } - public static long millisecondsFromHours(int value) { + public static long msFromHours(long value) { return value * MILLISECONDS_TO_HOURS_RATIO; } - public static long millisecondsFromHours(double value) { + public static long msFromHours(double value) { return (long) (value * MILLISECONDS_TO_HOURS_RATIO); } diff --git a/src/org/traccar/protocol/AplicomProtocolDecoder.java b/src/org/traccar/protocol/AplicomProtocolDecoder.java index adfa88f71..e649b52a6 100644 --- a/src/org/traccar/protocol/AplicomProtocolDecoder.java +++ b/src/org/traccar/protocol/AplicomProtocolDecoder.java @@ -549,7 +549,7 @@ public class AplicomProtocolDecoder extends BaseProtocolDecoder { } if ((selector & 0x0020) != 0) { - position.set(Position.KEY_HOURS, UnitsConverter.millisecondsFromHours(buf.readUnsignedInt())); + position.set(Position.KEY_HOURS, UnitsConverter.msFromHours(buf.readUnsignedInt())); position.set("serviceDistance", buf.readInt()); position.set("driverActivity", buf.readUnsignedByte()); position.set(Position.KEY_THROTTLE, buf.readUnsignedByte()); diff --git a/src/org/traccar/protocol/AstraProtocolDecoder.java b/src/org/traccar/protocol/AstraProtocolDecoder.java index 44547e3cd..7f247251d 100644 --- a/src/org/traccar/protocol/AstraProtocolDecoder.java +++ b/src/org/traccar/protocol/AstraProtocolDecoder.java @@ -106,7 +106,7 @@ public class AstraProtocolDecoder extends BaseProtocolDecoder { if (BitUtil.check(status, 8)) { position.set(Position.KEY_DRIVER_UNIQUE_ID, buf.readBytes(7).toString(StandardCharsets.US_ASCII)); position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium() * 1000); - position.set(Position.KEY_HOURS, UnitsConverter.millisecondsFromHours(buf.readUnsignedShort())); + position.set(Position.KEY_HOURS, UnitsConverter.msFromHours(buf.readUnsignedShort())); } if (BitUtil.check(status, 6)) { diff --git a/src/org/traccar/protocol/ContinentalProtocolDecoder.java b/src/org/traccar/protocol/ContinentalProtocolDecoder.java index 186b2235e..5ea1d3246 100644 --- a/src/org/traccar/protocol/ContinentalProtocolDecoder.java +++ b/src/org/traccar/protocol/ContinentalProtocolDecoder.java @@ -94,7 +94,7 @@ public class ContinentalProtocolDecoder extends BaseProtocolDecoder { } if (buf.readableBytes() > 4) { - position.set(Position.KEY_HOURS, UnitsConverter.millisecondsFromHours(buf.readUnsignedInt())); + position.set(Position.KEY_HOURS, UnitsConverter.msFromHours(buf.readUnsignedInt())); } return position; diff --git a/src/org/traccar/protocol/Gl200TextProtocolDecoder.java b/src/org/traccar/protocol/Gl200TextProtocolDecoder.java index 7342b1263..1c7fe4688 100644 --- a/src/org/traccar/protocol/Gl200TextProtocolDecoder.java +++ b/src/org/traccar/protocol/Gl200TextProtocolDecoder.java @@ -609,8 +609,7 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_THROTTLE, Integer.parseInt(values[index - 1])); } if (BitUtil.check(reportMask, 11)) { - position.set( - Position.KEY_HOURS, UnitsConverter.millisecondsFromHours(Double.parseDouble(values[index++]))); + position.set(Position.KEY_HOURS, UnitsConverter.msFromHours(Double.parseDouble(values[index++]))); } if (BitUtil.check(reportMask, 12)) { position.set("drivingHours", Double.parseDouble(values[index++])); diff --git a/src/org/traccar/protocol/Gps103ProtocolDecoder.java b/src/org/traccar/protocol/Gps103ProtocolDecoder.java index 3575d423e..3d31326ae 100644 --- a/src/org/traccar/protocol/Gps103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gps103ProtocolDecoder.java @@ -256,7 +256,7 @@ public class Gps103ProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_FUEL_CONSUMPTION, parser.nextDouble(0)); Integer hours = parser.nextInt(); if (hours != null) { - position.set(Position.KEY_HOURS, UnitsConverter.millisecondsFromHours(hours)); + position.set(Position.KEY_HOURS, UnitsConverter.msFromHours(hours)); } position.set(Position.KEY_OBD_SPEED, parser.nextInt(0)); position.set(Position.KEY_ENGINE_LOAD, parser.next()); diff --git a/src/org/traccar/protocol/Mta6ProtocolDecoder.java b/src/org/traccar/protocol/Mta6ProtocolDecoder.java index 6ec5faea8..13b187e80 100644 --- a/src/org/traccar/protocol/Mta6ProtocolDecoder.java +++ b/src/org/traccar/protocol/Mta6ProtocolDecoder.java @@ -232,8 +232,7 @@ public class Mta6ProtocolDecoder extends BaseProtocolDecoder { if (BitUtil.check(flags, 1)) { position.set(Position.KEY_FUEL_CONSUMPTION, new FloatReader().readFloat(buf)); - position.set( - Position.KEY_HOURS, UnitsConverter.millisecondsFromHours(new FloatReader().readFloat(buf))); + position.set(Position.KEY_HOURS, UnitsConverter.msFromHours(new FloatReader().readFloat(buf))); position.set("tank", buf.readUnsignedByte() * 0.4); } diff --git a/src/org/traccar/protocol/VtfmsProtocolDecoder.java b/src/org/traccar/protocol/VtfmsProtocolDecoder.java index 76fcf69fa..736a27a64 100644 --- a/src/org/traccar/protocol/VtfmsProtocolDecoder.java +++ b/src/org/traccar/protocol/VtfmsProtocolDecoder.java @@ -133,7 +133,7 @@ public class VtfmsProtocolDecoder extends BaseProtocolDecoder { position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0))); - position.set(Position.KEY_HOURS, UnitsConverter.millisecondsFromHours(parser.nextInt())); + position.set(Position.KEY_HOURS, UnitsConverter.msFromHours(parser.nextInt())); position.set("idleHours", parser.nextInt()); position.set(Position.KEY_ODOMETER, parser.nextInt() * 100); position.set(Position.KEY_CHARGE, parser.next().equals("1")); diff --git a/src/org/traccar/protocol/XirgoProtocolDecoder.java b/src/org/traccar/protocol/XirgoProtocolDecoder.java index 828893d62..5cda4aed3 100644 --- a/src/org/traccar/protocol/XirgoProtocolDecoder.java +++ b/src/org/traccar/protocol/XirgoProtocolDecoder.java @@ -232,7 +232,7 @@ public class XirgoProtocolDecoder extends BaseProtocolDecoder { position.set(Position.PREFIX_OUT + 1, parser.nextInt()); position.set(Position.PREFIX_ADC + 1, parser.nextDouble()); position.set(Position.KEY_FUEL_LEVEL, parser.nextDouble()); - position.set(Position.KEY_HOURS, UnitsConverter.millisecondsFromHours(parser.nextInt())); + position.set(Position.KEY_HOURS, UnitsConverter.msFromHours(parser.nextInt())); position.set("oilPressure", parser.nextInt()); position.set("oilLevel", parser.nextInt()); position.set("oilTemp", parser.nextInt()); -- cgit v1.2.3 From 9c2830b50b667b188c844102dd9ed91fd487d386 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 30 May 2018 09:19:03 +0500 Subject: Add converter from minutes to milliseconds --- src/org/traccar/helper/UnitsConverter.java | 5 +++++ src/org/traccar/protocol/MxtProtocolDecoder.java | 2 +- src/org/traccar/protocol/SuntechProtocolDecoder.java | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) (limited to 'src/org/traccar/helper/UnitsConverter.java') diff --git a/src/org/traccar/helper/UnitsConverter.java b/src/org/traccar/helper/UnitsConverter.java index 74f5a90e6..3dd435df4 100644 --- a/src/org/traccar/helper/UnitsConverter.java +++ b/src/org/traccar/helper/UnitsConverter.java @@ -24,6 +24,7 @@ public final class UnitsConverter { private static final double METERS_TO_FEET_RATIO = 0.3048; private static final double METERS_TO_MILE_RATIO = 1609.34; private static final long MILLISECONDS_TO_HOURS_RATIO = 3600000; + private static final long MILLISECONDS_TO_MINUTES_RATIO = 60000; private UnitsConverter() { } @@ -80,4 +81,8 @@ public final class UnitsConverter { return (long) (value * MILLISECONDS_TO_HOURS_RATIO); } + public static long msFromMinutes(long value) { + return value * MILLISECONDS_TO_MINUTES_RATIO; + } + } diff --git a/src/org/traccar/protocol/MxtProtocolDecoder.java b/src/org/traccar/protocol/MxtProtocolDecoder.java index d9dcd9e93..a97ae380d 100644 --- a/src/org/traccar/protocol/MxtProtocolDecoder.java +++ b/src/org/traccar/protocol/MxtProtocolDecoder.java @@ -145,7 +145,7 @@ public class MxtProtocolDecoder extends BaseProtocolDecoder { } if (BitUtil.check(infoGroups, 4)) { - position.set(Position.KEY_HOURS, buf.readUnsignedInt() * 60000); + position.set(Position.KEY_HOURS, UnitsConverter.msFromMinutes(buf.readUnsignedInt())); } if (BitUtil.check(infoGroups, 5)) { diff --git a/src/org/traccar/protocol/SuntechProtocolDecoder.java b/src/org/traccar/protocol/SuntechProtocolDecoder.java index c6b755770..efaf19b93 100644 --- a/src/org/traccar/protocol/SuntechProtocolDecoder.java +++ b/src/org/traccar/protocol/SuntechProtocolDecoder.java @@ -239,7 +239,7 @@ public class SuntechProtocolDecoder extends BaseProtocolDecoder { if (hbm) { if (index < values.length) { - position.set(Position.KEY_HOURS, Integer.parseInt(values[index++]) * 60000); + position.set(Position.KEY_HOURS, UnitsConverter.msFromMinutes(Integer.parseInt(values[index++]))); } if (index < values.length) { -- cgit v1.2.3