From 3d14b6504b01b26e529717b88318be54094b8816 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 31 May 2018 09:04:38 +0500 Subject: Add engine hours handler --- setup/default.xml | 2 ++ src/org/traccar/BasePipelineFactory.java | 9 +++++++ src/org/traccar/EngineHoursHandler.java | 40 ++++++++++++++++++++++++++++++++ src/org/traccar/reports/ReportUtils.java | 14 +++++++---- src/org/traccar/reports/Summary.java | 11 ++++++++- 5 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 src/org/traccar/EngineHoursHandler.java diff --git a/setup/default.xml b/setup/default.xml index b1c175534..8f3fb988f 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -21,6 +21,8 @@ true 3600 + true + true true true diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 5a077da7c..e325a6033 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -51,6 +51,7 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { private FilterHandler filterHandler; private DistanceHandler distanceHandler; + private EngineHoursHandler engineHoursHandler; private RemoteAddressHandler remoteAddressHandler; private MotionHandler motionHandler; private GeocoderHandler geocoderHandler; @@ -155,6 +156,10 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { motionHandler = new MotionHandler(Context.getTripsConfig().getSpeedThreshold()); + if (Context.getConfig().getBoolean("engineHours.enable")) { + engineHoursHandler = new EngineHoursHandler(); + } + if (Context.getConfig().hasKey("location.latitudeHemisphere") || Context.getConfig().hasKey("location.longitudeHemisphere")) { hemisphereHandler = new HemisphereHandler(); @@ -225,6 +230,10 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { pipeline.addLast("motion", motionHandler); } + if (engineHoursHandler != null) { + pipeline.addLast("engineHours", engineHoursHandler); + } + if (copyAttributesHandler != null) { pipeline.addLast("copyAttributes", copyAttributesHandler); } diff --git a/src/org/traccar/EngineHoursHandler.java b/src/org/traccar/EngineHoursHandler.java new file mode 100644 index 000000000..41e4dd116 --- /dev/null +++ b/src/org/traccar/EngineHoursHandler.java @@ -0,0 +1,40 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar; + +import org.traccar.model.Position; + +public class EngineHoursHandler extends BaseDataHandler { + + @Override + protected Position handlePosition(Position position) { + if (!position.getAttributes().containsKey(Position.KEY_HOURS)) { + Position last = Context.getIdentityManager().getLastPosition(position.getDeviceId()); + if (last != null) { + long hours = last.getLong(Position.KEY_HOURS); + if (last.getBoolean(Position.KEY_IGNITION) && position.getBoolean(Position.KEY_IGNITION)) { + hours += position.getFixTime().getTime() - last.getFixTime().getTime(); + } + if (hours != 0) { + position.set(Position.KEY_HOURS, hours); + } + } + } + return position; + } + +} diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index 010c88b22..94cc7664f 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -263,10 +263,16 @@ public final class ReportUtils { stop.setSpentFuel(calculateFuel(startStop, endStop)); long engineHours = 0; - for (int i = startIndex + 1; i <= endIndex; i++) { - if (positions.get(i).getBoolean(Position.KEY_IGNITION) - && positions.get(i - 1).getBoolean(Position.KEY_IGNITION)) { - engineHours += positions.get(i).getFixTime().getTime() - positions.get(i - 1).getFixTime().getTime(); + if (startStop.getAttributes().containsKey(Position.KEY_HOURS) + && endStop.getAttributes().containsKey(Position.KEY_HOURS)) { + engineHours = endStop.getLong(Position.KEY_HOURS) - startStop.getLong(Position.KEY_HOURS); + } else if (Context.getConfig().getBoolean("engineHours.enable")) { + for (int i = startIndex + 1; i <= endIndex; i++) { + if (positions.get(i).getBoolean(Position.KEY_IGNITION) + && positions.get(i - 1).getBoolean(Position.KEY_IGNITION)) { + engineHours += positions.get(i).getFixTime().getTime() + - positions.get(i - 1).getFixTime().getTime(); + } } } stop.setEngineHours(engineHours); diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java index d789a0a9b..f57372e8d 100644 --- a/src/org/traccar/reports/Summary.java +++ b/src/org/traccar/reports/Summary.java @@ -44,11 +44,13 @@ public final class Summary { Position firstPosition = null; Position previousPosition = null; double speedSum = 0; + boolean needCalculateEngineHours = Context.getConfig().getBoolean("engineHours.enable"); for (Position position : positions) { if (firstPosition == null) { firstPosition = position; } - if (previousPosition != null && position.getBoolean(Position.KEY_IGNITION) + if (needCalculateEngineHours && previousPosition != null + && position.getBoolean(Position.KEY_IGNITION) && previousPosition.getBoolean(Position.KEY_IGNITION)) { result.addEngineHours(position.getFixTime().getTime() - previousPosition.getFixTime().getTime()); @@ -63,6 +65,13 @@ public final class Summary { result.setAverageSpeed(speedSum / positions.size()); result.setSpentFuel(ReportUtils.calculateFuel(firstPosition, previousPosition)); + if (needCalculateEngineHours + && firstPosition.getAttributes().containsKey(Position.KEY_HOURS) + && previousPosition.getAttributes().containsKey(Position.KEY_HOURS)) { + result.setEngineHours( + previousPosition.getLong(Position.KEY_HOURS) - firstPosition.getLong(Position.KEY_HOURS)); + } + if (!ignoreOdometer && firstPosition.getDouble(Position.KEY_ODOMETER) != 0 && previousPosition.getDouble(Position.KEY_ODOMETER) != 0) { -- cgit v1.2.3 From 786f101fab6a81007f67e868d381302f4a502d77 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 31 May 2018 10:18:29 +0500 Subject: - Rename switch and variable - Add temporary comment --- setup/default.xml | 3 +-- src/org/traccar/BasePipelineFactory.java | 2 +- src/org/traccar/reports/ReportUtils.java | 3 ++- src/org/traccar/reports/Summary.java | 7 ++++--- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/setup/default.xml b/setup/default.xml index 8f3fb988f..5d79f563f 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -21,11 +21,10 @@ true 3600 - true - true true true + true ./media diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index e325a6033..b0de67a15 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -156,7 +156,7 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { motionHandler = new MotionHandler(Context.getTripsConfig().getSpeedThreshold()); - if (Context.getConfig().getBoolean("engineHours.enable")) { + if (Context.getConfig().getBoolean("processing.engineHours.enable")) { engineHoursHandler = new EngineHoursHandler(); } diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index 94cc7664f..ea383d598 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -266,7 +266,8 @@ public final class ReportUtils { if (startStop.getAttributes().containsKey(Position.KEY_HOURS) && endStop.getAttributes().containsKey(Position.KEY_HOURS)) { engineHours = endStop.getLong(Position.KEY_HOURS) - startStop.getLong(Position.KEY_HOURS); - } else if (Context.getConfig().getBoolean("engineHours.enable")) { + } else if (Context.getConfig().getBoolean("processing.engineHours.enable")) { + // Temporary fallback for old data, to be removed in May 2019 for (int i = startIndex + 1; i <= endIndex; i++) { if (positions.get(i).getBoolean(Position.KEY_IGNITION) && positions.get(i - 1).getBoolean(Position.KEY_IGNITION)) { diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java index f57372e8d..9810424d8 100644 --- a/src/org/traccar/reports/Summary.java +++ b/src/org/traccar/reports/Summary.java @@ -44,14 +44,15 @@ public final class Summary { Position firstPosition = null; Position previousPosition = null; double speedSum = 0; - boolean needCalculateEngineHours = Context.getConfig().getBoolean("engineHours.enable"); + boolean engineHoursEnabled = Context.getConfig().getBoolean("processing.engineHours.enable"); for (Position position : positions) { if (firstPosition == null) { firstPosition = position; } - if (needCalculateEngineHours && previousPosition != null + if (engineHoursEnabled && previousPosition != null && position.getBoolean(Position.KEY_IGNITION) && previousPosition.getBoolean(Position.KEY_IGNITION)) { + // Temporary fallback for old data, to be removed in May 2019 result.addEngineHours(position.getFixTime().getTime() - previousPosition.getFixTime().getTime()); } @@ -65,7 +66,7 @@ public final class Summary { result.setAverageSpeed(speedSum / positions.size()); result.setSpentFuel(ReportUtils.calculateFuel(firstPosition, previousPosition)); - if (needCalculateEngineHours + if (engineHoursEnabled && firstPosition.getAttributes().containsKey(Position.KEY_HOURS) && previousPosition.getAttributes().containsKey(Position.KEY_HOURS)) { result.setEngineHours( -- cgit v1.2.3