From f7a4d1977e71797b231ebb9a55308fee7c2b64d7 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 11 Apr 2018 14:55:11 +0500 Subject: Add support of multiple Maintenances --- src/org/traccar/Context.java | 14 +++++- src/org/traccar/api/resource/EventResource.java | 4 ++ .../traccar/api/resource/MaintenanceResource.java | 36 +++++++++++++++ src/org/traccar/database/DataManager.java | 3 ++ src/org/traccar/database/MaintenancesManager.java | 27 ++++++++++++ src/org/traccar/database/NotificationManager.java | 6 ++- src/org/traccar/database/PermissionsManager.java | 11 ++++- .../traccar/events/MaintenanceEventHandler.java | 43 ++++++++---------- src/org/traccar/model/Event.java | 10 +++++ src/org/traccar/model/Maintenance.java | 51 ++++++++++++++++++++++ src/org/traccar/notification/EventForwarder.java | 8 ++++ .../notification/NotificationFormatter.java | 7 ++- src/org/traccar/reports/Events.java | 22 ++++++++-- 13 files changed, 208 insertions(+), 34 deletions(-) create mode 100644 src/org/traccar/api/resource/MaintenanceResource.java create mode 100644 src/org/traccar/database/MaintenancesManager.java create mode 100644 src/org/traccar/model/Maintenance.java (limited to 'src/org/traccar') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 09e3c619b..a1a5f5a8e 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,6 +35,7 @@ import org.traccar.database.DeviceManager; import org.traccar.database.DriversManager; import org.traccar.database.IdentityManager; import org.traccar.database.LdapProvider; +import org.traccar.database.MaintenancesManager; import org.traccar.database.MediaManager; import org.traccar.database.NotificationManager; import org.traccar.database.PermissionsManager; @@ -64,6 +65,7 @@ import org.traccar.model.Device; import org.traccar.model.Driver; import org.traccar.model.Geofence; import org.traccar.model.Group; +import org.traccar.model.Maintenance; import org.traccar.model.Notification; import org.traccar.model.User; import org.traccar.geolocation.GoogleGeolocationProvider; @@ -232,6 +234,12 @@ public final class Context { return commandsManager; } + private static MaintenancesManager maintenancesManager; + + public static MaintenancesManager getMaintenancesManager() { + return maintenancesManager; + } + private static StatisticsManager statisticsManager; public static StatisticsManager getStatisticsManager() { @@ -385,6 +393,8 @@ public final class Context { commandsManager = new CommandsManager(dataManager); + maintenancesManager = new MaintenancesManager(dataManager); + statisticsManager = new StatisticsManager(); if (config.getBoolean("sms.smpp.enable")) { @@ -469,6 +479,8 @@ public final class Context { return (BaseObjectManager) driversManager; } else if (clazz.equals(Command.class)) { return (BaseObjectManager) commandsManager; + } else if (clazz.equals(Maintenance.class)) { + return (BaseObjectManager) maintenancesManager; } else if (clazz.equals(Notification.class)) { return (BaseObjectManager) notificationManager; } diff --git a/src/org/traccar/api/resource/EventResource.java b/src/org/traccar/api/resource/EventResource.java index a7cf9edbd..e0ccf7020 100644 --- a/src/org/traccar/api/resource/EventResource.java +++ b/src/org/traccar/api/resource/EventResource.java @@ -13,6 +13,7 @@ import org.traccar.Context; import org.traccar.api.BaseResource; import org.traccar.model.Event; import org.traccar.model.Geofence; +import org.traccar.model.Maintenance; @Path("events") @Produces(MediaType.APPLICATION_JSON) @@ -28,6 +29,9 @@ public class EventResource extends BaseResource { if (event.getGeofenceId() != 0) { Context.getPermissionsManager().checkPermission(Geofence.class, getUserId(), event.getGeofenceId()); } + if (event.getMaintenanceId() != 0) { + Context.getPermissionsManager().checkPermission(Maintenance.class, getUserId(), event.getMaintenanceId()); + } return event; } diff --git a/src/org/traccar/api/resource/MaintenanceResource.java b/src/org/traccar/api/resource/MaintenanceResource.java new file mode 100644 index 000000000..b3726b429 --- /dev/null +++ b/src/org/traccar/api/resource/MaintenanceResource.java @@ -0,0 +1,36 @@ +/* + * 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.api.resource; + +import javax.ws.rs.Consumes; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +import org.traccar.api.ExtendedObjectResource; +import org.traccar.model.Maintenance; + +@Path("maintenances") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class MaintenanceResource extends ExtendedObjectResource { + + public MaintenanceResource() { + super(Maintenance.class); + } + +} diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index a997a89f6..84f8f0394 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -48,6 +48,7 @@ import org.traccar.model.Driver; import org.traccar.model.Event; import org.traccar.model.Geofence; import org.traccar.model.Group; +import org.traccar.model.Maintenance; import org.traccar.model.ManagedUser; import org.traccar.model.Notification; import org.traccar.model.Permission; @@ -405,6 +406,8 @@ public class DataManager { return Calendar.class; case "command": return Command.class; + case "maintenance": + return Maintenance.class; case "notification": return Notification.class; default: diff --git a/src/org/traccar/database/MaintenancesManager.java b/src/org/traccar/database/MaintenancesManager.java new file mode 100644 index 000000000..4e266cb78 --- /dev/null +++ b/src/org/traccar/database/MaintenancesManager.java @@ -0,0 +1,27 @@ +/* + * 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.database; + +import org.traccar.model.Maintenance; + +public class MaintenancesManager extends ExtendedObjectManager { + + public MaintenancesManager(DataManager dataManager) { + super(dataManager, Maintenance.class); + } + +} diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index 1c59a8666..9f2606f7f 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -78,8 +78,10 @@ public class NotificationManager extends ExtendedObjectManager { usersToForward = new HashSet<>(); } for (long userId : users) { - if (event.getGeofenceId() == 0 || Context.getGeofenceManager() != null - && Context.getGeofenceManager().checkItemPermission(userId, event.getGeofenceId())) { + if ((event.getGeofenceId() == 0 || Context.getGeofenceManager() != null + && Context.getGeofenceManager().checkItemPermission(userId, event.getGeofenceId())) + && (event.getMaintenanceId() == 0 + || Context.getMaintenancesManager().checkItemPermission(userId, event.getMaintenanceId()))) { if (usersToForward != null) { usersToForward.add(userId); } diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 3ae5961ce..b5f062d2b 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import org.traccar.model.Device; import org.traccar.model.Driver; import org.traccar.model.Geofence; import org.traccar.model.Group; +import org.traccar.model.Maintenance; import org.traccar.model.ManagedUser; import org.traccar.model.Notification; import org.traccar.model.Permission; @@ -337,6 +338,8 @@ public class PermissionsManager { manager = Context.getCalendarManager(); } else if (object.equals(Command.class)) { manager = Context.getCommandsManager(); + } else if (object.equals(Maintenance.class)) { + manager = Context.getMaintenancesManager(); } else if (object.equals(Notification.class)) { manager = Context.getNotificationManager(); } else { @@ -362,6 +365,7 @@ public class PermissionsManager { Context.getDriversManager().refreshUserItems(); Context.getAttributesManager().refreshUserItems(); Context.getCommandsManager().refreshUserItems(); + Context.getMaintenancesManager().refreshUserItems(); if (Context.getNotificationManager() != null) { Context.getNotificationManager().refreshUserItems(); } @@ -374,6 +378,7 @@ public class PermissionsManager { Context.getDriversManager().refreshExtendedPermissions(); Context.getAttributesManager().refreshExtendedPermissions(); Context.getCommandsManager().refreshExtendedPermissions(); + Context.getMaintenancesManager().refreshExtendedPermissions(); } public void refreshPermissions(Permission permission) { @@ -394,6 +399,8 @@ public class PermissionsManager { Context.getCalendarManager().refreshUserItems(); } else if (permission.getPropertyClass().equals(Command.class)) { Context.getCommandsManager().refreshUserItems(); + } else if (permission.getPropertyClass().equals(Maintenance.class)) { + Context.getMaintenancesManager().refreshUserItems(); } else if (permission.getPropertyClass().equals(Notification.class) && Context.getNotificationManager() != null) { Context.getNotificationManager().refreshUserItems(); @@ -407,6 +414,8 @@ public class PermissionsManager { Context.getAttributesManager().refreshExtendedPermissions(); } else if (permission.getPropertyClass().equals(Command.class)) { Context.getCommandsManager().refreshExtendedPermissions(); + } else if (permission.getPropertyClass().equals(Maintenance.class)) { + Context.getMaintenancesManager().refreshExtendedPermissions(); } else if (permission.getPropertyClass().equals(Notification.class) && Context.getNotificationManager() != null) { Context.getNotificationManager().refreshExtendedPermissions(); diff --git a/src/org/traccar/events/MaintenanceEventHandler.java b/src/org/traccar/events/MaintenanceEventHandler.java index 86abf7c17..001b414c0 100644 --- a/src/org/traccar/events/MaintenanceEventHandler.java +++ b/src/org/traccar/events/MaintenanceEventHandler.java @@ -1,6 +1,6 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) - * Copyright 2016 Andrey Kunitsyn (andrey@traccar.org) + * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 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. @@ -16,35 +16,24 @@ */ package org.traccar.events; -import java.util.Collections; +import java.util.HashMap; import java.util.Map; import org.traccar.BaseEventHandler; import org.traccar.Context; -import org.traccar.model.Device; import org.traccar.model.Event; +import org.traccar.model.Maintenance; 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 Map analyzePosition(Position position) { - Device device = Context.getIdentityManager().getById(position.getDeviceId()); - if (device == null || !Context.getIdentityManager().isLatestPosition(position)) { + if (Context.getIdentityManager().getById(position.getDeviceId()) == null + || !Context.getIdentityManager().isLatestPosition(position)) { return null; } - double maintenanceInterval = Context.getDeviceManager() - .lookupAttributeDouble(device.getId(), ATTRIBUTE_MAINTENANCE_INTERVAL, 0, false); - if (maintenanceInterval == 0) { - return null; - } - double maintenanceStart = Context.getDeviceManager() - .lookupAttributeDouble(device.getId(), ATTRIBUTE_MAINTENANCE_START, 0, false); - double oldTotalDistance = 0.0; double newTotalDistance = 0.0; @@ -54,16 +43,20 @@ public class MaintenanceEventHandler extends BaseEventHandler { } newTotalDistance = position.getDouble(Position.KEY_TOTAL_DISTANCE); - oldTotalDistance -= maintenanceStart; - newTotalDistance -= maintenanceStart; - - if ((long) (oldTotalDistance / maintenanceInterval) < (long) (newTotalDistance / maintenanceInterval)) { - Event event = new Event(Event.TYPE_MAINTENANCE, position.getDeviceId(), position.getId()); - event.set(Position.KEY_TOTAL_DISTANCE, newTotalDistance); - return Collections.singletonMap(event, position); + Map events = new HashMap<>(); + for (long maintenanceId : Context.getMaintenancesManager().getAllDeviceItems(position.getDeviceId())) { + Maintenance maintenance = Context.getMaintenancesManager().getById(maintenanceId); + if (maintenance.getLapse() != 0 + && (long) ((oldTotalDistance - maintenance.getStart()) / maintenance.getLapse()) + < (long) ((newTotalDistance - maintenance.getStart()) / maintenance.getLapse())) { + Event event = new Event(Event.TYPE_MAINTENANCE, position.getDeviceId(), position.getId()); + event.setMaintenanceId(maintenanceId); + event.set(Position.KEY_TOTAL_DISTANCE, newTotalDistance); + events.put(event, position); + } } - return null; + return events; } } diff --git a/src/org/traccar/model/Event.java b/src/org/traccar/model/Event.java index 465afeb35..ee7fcc679 100644 --- a/src/org/traccar/model/Event.java +++ b/src/org/traccar/model/Event.java @@ -91,4 +91,14 @@ public class Event extends Message { this.geofenceId = geofenceId; } + private long maintenanceId = 0; + + public long getMaintenanceId() { + return maintenanceId; + } + + public void setMaintenanceId(long maintenanceId) { + this.maintenanceId = maintenanceId; + } + } diff --git a/src/org/traccar/model/Maintenance.java b/src/org/traccar/model/Maintenance.java new file mode 100644 index 000000000..827fd3b85 --- /dev/null +++ b/src/org/traccar/model/Maintenance.java @@ -0,0 +1,51 @@ +/* + * 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.model; + +public class Maintenance extends ExtendedModel { + + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + private double start; + + public double getStart() { + return start; + } + + public void setStart(double start) { + this.start = start; + } + + private double lapse; + + public double getLapse() { + return lapse; + } + + public void setLapse(double lapse) { + this.lapse = lapse; + } + +} diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index b13f8fe43..9d2181e21 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -25,6 +25,7 @@ import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Geofence; +import org.traccar.model.Maintenance; import org.traccar.model.Position; import java.nio.charset.StandardCharsets; @@ -48,6 +49,7 @@ public abstract class EventForwarder { private static final String KEY_EVENT = "event"; private static final String KEY_GEOFENCE = "geofence"; private static final String KEY_DEVICE = "device"; + private static final String KEY_MAINTENANCE = "maintenance"; private static final String KEY_USERS = "users"; public final void forwardEvent(Event event, Position position, Set users) { @@ -98,6 +100,12 @@ public abstract class EventForwarder { data.put(KEY_GEOFENCE, geofence); } } + if (event.getMaintenanceId() != 0) { + Maintenance maintenance = Context.getMaintenancesManager().getById(event.getMaintenanceId()); + if (maintenance != null) { + data.put(KEY_MAINTENANCE, maintenance); + } + } data.put(KEY_USERS, Context.getUsersManager().getItems(users)); try { return Context.getObjectMapper().writeValueAsString(data); diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index 114825a83..524153721 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -1,6 +1,6 @@ /* - * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 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. @@ -57,6 +57,9 @@ public final class NotificationFormatter { if (event.getGeofenceId() != 0) { velocityContext.put("geofence", Context.getGeofenceManager().getById(event.getGeofenceId())); } + if (event.getMaintenanceId() != 0) { + velocityContext.put("maintenance", Context.getMaintenancesManager().getById(event.getMaintenanceId())); + } String driverUniqueId = event.getString(Position.KEY_DRIVER_UNIQUE_ID); if (driverUniqueId != null) { velocityContext.put("driver", Context.getDriversManager().getDriverByUniqueId(driverUniqueId)); diff --git a/src/org/traccar/reports/Events.java b/src/org/traccar/reports/Events.java index 7cb6ef6eb..66d9e708d 100644 --- a/src/org/traccar/reports/Events.java +++ b/src/org/traccar/reports/Events.java @@ -1,6 +1,6 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) - * Copyright 2016 Andrey Kunitsyn (andrey@traccar.org) + * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 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. @@ -33,6 +33,7 @@ import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Geofence; import org.traccar.model.Group; +import org.traccar.model.Maintenance; import org.traccar.reports.model.DeviceReport; public final class Events { @@ -51,7 +52,10 @@ public final class Events { for (Event event : events) { if (all || types.contains(event.getType())) { long geofenceId = event.getGeofenceId(); - if (geofenceId == 0 || Context.getGeofenceManager().checkItemPermission(userId, geofenceId)) { + long maintenanceId = event.getMaintenanceId(); + if ((geofenceId == 0 || Context.getGeofenceManager().checkItemPermission(userId, geofenceId)) + && (maintenanceId == 0 + || Context.getMaintenancesManager().checkItemPermission(userId, maintenanceId))) { result.add(event); } } @@ -67,6 +71,7 @@ public final class Events { ArrayList devicesEvents = new ArrayList<>(); ArrayList sheetNames = new ArrayList<>(); HashMap geofenceNames = new HashMap<>(); + HashMap maintenanceNames = new HashMap<>(); for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); Collection events = Context.getDataManager().getEvents(deviceId, from, to); @@ -75,6 +80,7 @@ public final class Events { Event event = iterator.next(); if (all || types.contains(event.getType())) { long geofenceId = event.getGeofenceId(); + long maintenanceId = event.getMaintenanceId(); if (geofenceId != 0) { if (Context.getGeofenceManager().checkItemPermission(userId, geofenceId)) { Geofence geofence = Context.getGeofenceManager().getById(geofenceId); @@ -84,6 +90,15 @@ public final class Events { } else { iterator.remove(); } + } else if (maintenanceId != 0) { + if (Context.getMaintenancesManager().checkItemPermission(userId, maintenanceId)) { + Maintenance maintenance = Context.getMaintenancesManager().getById(maintenanceId); + if (maintenance != null) { + maintenanceNames.put(maintenanceId, maintenance.getName()); + } + } else { + iterator.remove(); + } } } else { iterator.remove(); @@ -109,6 +124,7 @@ public final class Events { jxlsContext.putVar("devices", devicesEvents); jxlsContext.putVar("sheetNames", sheetNames); jxlsContext.putVar("geofenceNames", geofenceNames); + jxlsContext.putVar("maintenanceNames", maintenanceNames); jxlsContext.putVar("from", from); jxlsContext.putVar("to", to); ReportUtils.processTemplateWithSheets(inputStream, outputStream, jxlsContext); -- cgit v1.2.3