diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2016-11-23 23:22:43 +1300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-23 23:22:43 +1300 |
commit | dbbe99269140f057d3f72acca557730a6b10e902 (patch) | |
tree | 0a66b3ce6ce21964c539c4627e04640738f04c2b | |
parent | 5f867c6077f79a2a1d3b8ec18f78c3a2657ba698 (diff) | |
parent | 685fd3826b64b14106aeace0a647e71a4cc4fe81 (diff) | |
download | traccar-server-dbbe99269140f057d3f72acca557730a6b10e902.tar.gz traccar-server-dbbe99269140f057d3f72acca557730a6b10e902.tar.bz2 traccar-server-dbbe99269140f057d3f72acca557730a6b10e902.zip |
Merge pull request #2583 from Abyss777/maintenanceevents
Implement maintenance events
-rw-r--r-- | setup/default.xml | 2 | ||||
-rw-r--r-- | src/org/traccar/BasePipelineFactory.java | 9 | ||||
-rw-r--r-- | src/org/traccar/events/MaintenanceEventHandler.java | 70 | ||||
-rw-r--r-- | src/org/traccar/model/Event.java | 2 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationFormatter.java | 22 |
5 files changed, 105 insertions, 0 deletions
diff --git a/setup/default.xml b/setup/default.xml index e6d701f4c..d6f8a1752 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -24,6 +24,8 @@ <entry key='event.geofenceHandler'>true</entry> <entry key='event.alertHandler'>true</entry> <entry key='event.ignitionHandler'>true</entry> + <entry key='event.statusHandler'>true</entry> + <entry key='event.maintenanceHandler'>true</entry> <!-- DATABASE CONFIG --> diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 22bb80ca1..d73b022ae 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -32,6 +32,7 @@ import org.jboss.netty.handler.timeout.IdleStateHandler; import org.traccar.events.CommandResultEventHandler; import org.traccar.events.GeofenceEventHandler; import org.traccar.events.IgnitionEventHandler; +import org.traccar.events.MaintenanceEventHandler; import org.traccar.events.MotionEventHandler; import org.traccar.events.OverspeedEventHandler; import org.traccar.events.AlertEventHandler; @@ -58,6 +59,7 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { private GeofenceEventHandler geofenceEventHandler; private AlertEventHandler alertEventHandler; private IgnitionEventHandler ignitionEventHandler; + private MaintenanceEventHandler maintenanceEventHandler; private static final class OpenChannelHandler extends SimpleChannelHandler { @@ -164,6 +166,9 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { if (Context.getConfig().getBoolean("event.ignitionHandler")) { ignitionEventHandler = new IgnitionEventHandler(); } + if (Context.getConfig().getBoolean("event.maintenanceHandler")) { + maintenanceEventHandler = new MaintenanceEventHandler(); + } } protected abstract void addSpecificHandlers(ChannelPipeline pipeline); @@ -242,6 +247,10 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { pipeline.addLast("IgnitionEventHandler", ignitionEventHandler); } + if (maintenanceEventHandler != null) { + pipeline.addLast("MaintenanceEventHandler", maintenanceEventHandler); + } + pipeline.addLast("mainHandler", new MainEventHandler()); return pipeline; } diff --git a/src/org/traccar/events/MaintenanceEventHandler.java b/src/org/traccar/events/MaintenanceEventHandler.java new file mode 100644 index 000000000..466e0b617 --- /dev/null +++ b/src/org/traccar/events/MaintenanceEventHandler.java @@ -0,0 +1,70 @@ +/* + * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 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.events; + +import java.util.ArrayList; +import java.util.Collection; + +import org.traccar.BaseEventHandler; +import org.traccar.Context; +import org.traccar.model.Device; +import org.traccar.model.Event; +import org.traccar.model.Position; + +public class MaintenanceEventHandler extends BaseEventHandler { + + public static final String ATTRIBUTE_MAINTENANCE_START = "maintenance.start"; + public static final String ATTRIBUTE_MAINTENANCE_INTERVAL = "maintenance.interval"; + + @Override + protected Collection<Event> analyzePosition(Position position) { + Device device = Context.getIdentityManager().getDeviceById(position.getDeviceId()); + if (device == null || !Context.getIdentityManager().isLatestPosition(position)) { + return null; + } + + double maintenanceInterval = Context.getDeviceManager() + .lookupServerDouble(device.getId(), ATTRIBUTE_MAINTENANCE_INTERVAL, 0); + if (maintenanceInterval == 0) { + return null; + } + double maintenanceStart = Context.getDeviceManager() + .lookupServerDouble(device.getId(), ATTRIBUTE_MAINTENANCE_START, 0); + + Collection<Event> events = new ArrayList<>(); + double oldTotalDistance = 0.0; + double newTotalDistance = 0.0; + + Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId()); + if (lastPosition != null && lastPosition.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE)) { + oldTotalDistance = ((Number) lastPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue(); + } + + if (position.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE)) { + newTotalDistance = ((Number) position.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue(); + } + + oldTotalDistance -= maintenanceStart; + newTotalDistance -= maintenanceStart; + if ((long) (oldTotalDistance / maintenanceInterval) < (long) (newTotalDistance / maintenanceInterval)) { + events.add(new Event(Event.TYPE_MAINTENANCE_NEEDED, position.getDeviceId(), position.getId())); + } + + return events; + } + +} diff --git a/src/org/traccar/model/Event.java b/src/org/traccar/model/Event.java index 3dc4778e1..45da9a711 100644 --- a/src/org/traccar/model/Event.java +++ b/src/org/traccar/model/Event.java @@ -56,6 +56,8 @@ public class Event extends Message { public static final String TYPE_IGNITION_ON = "ignitionOn"; public static final String TYPE_IGNITION_OFF = "ignitionOff"; + public static final String TYPE_MAINTENANCE_NEEDED = "maintenanceNeeded"; + private Date serverTime; public Date getServerTime() { diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index 2355d1793..38a228beb 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -39,6 +39,10 @@ public final class NotificationFormatter { public static final String MESSAGE_TEMPLATE_TYPE_DEVICE_ONLINE = "Device: %1$s%n" + "Online%n" + "Time: %2$tc%n"; + public static final String TITLE_TEMPLATE_TYPE_DEVICE_UNKNOWN = "%1$s: status is unknown"; + public static final String MESSAGE_TEMPLATE_TYPE_DEVICE_UNKNOWN = "Device: %1$s%n" + + "Status is unknown%n" + + "Time: %2$tc%n"; public static final String TITLE_TEMPLATE_TYPE_DEVICE_OFFLINE = "%1$s: offline"; public static final String MESSAGE_TEMPLATE_TYPE_DEVICE_OFFLINE = "Device: %1$s%n" + "Offline%n" @@ -88,6 +92,11 @@ public final class NotificationFormatter { + "Ignition OFF%n" + "Point: https://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%n" + "Time: %2$tc%n"; + public static final String TITLE_TEMPLATE_TYPE_MAINTENANCE_NEEDED = "%1$s: maintenance is needed"; + public static final String MESSAGE_TEMPLATE_TYPE_MAINTENANCE_NEEDED = "Device: %1$s%n" + + "Maintenance is needed%n" + + "Point: https://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%n" + + "Time: %2$tc%n"; public static String formatTitle(long userId, Event event, Position position) { Device device = Context.getIdentityManager().getDeviceById(event.getDeviceId()); @@ -101,6 +110,9 @@ public final class NotificationFormatter { case Event.TYPE_DEVICE_ONLINE: formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_ONLINE, device.getName()); break; + case Event.TYPE_DEVICE_UNKNOWN: + formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_UNKNOWN, device.getName()); + break; case Event.TYPE_DEVICE_OFFLINE: formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_OFFLINE, device.getName()); break; @@ -128,6 +140,9 @@ public final class NotificationFormatter { case Event.TYPE_IGNITION_OFF: formatter.format(TITLE_TEMPLATE_TYPE_IGNITION_OFF, device.getName()); break; + case Event.TYPE_MAINTENANCE_NEEDED: + formatter.format(TITLE_TEMPLATE_TYPE_MAINTENANCE_NEEDED, device.getName()); + break; default: formatter.format("Unknown type"); break; @@ -150,6 +165,9 @@ public final class NotificationFormatter { case Event.TYPE_DEVICE_ONLINE: formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_ONLINE, device.getName(), event.getServerTime()); break; + case Event.TYPE_DEVICE_UNKNOWN: + formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_UNKNOWN, device.getName(), event.getServerTime()); + break; case Event.TYPE_DEVICE_OFFLINE: formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_OFFLINE, device.getName(), event.getServerTime()); break; @@ -188,6 +206,10 @@ public final class NotificationFormatter { formatter.format(MESSAGE_TEMPLATE_TYPE_IGNITION_OFF, device.getName(), position.getFixTime(), position.getLatitude(), position.getLongitude()); break; + case Event.TYPE_MAINTENANCE_NEEDED: + formatter.format(MESSAGE_TEMPLATE_TYPE_MAINTENANCE_NEEDED, device.getName(), position.getFixTime(), + position.getLatitude(), position.getLongitude()); + break; default: formatter.format("Unknown type"); break; |