From 9eea2f26f64a3026f61f9deb13d1deac00cddcd6 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Sat, 22 Oct 2016 17:33:12 +0500 Subject: Implement reset total distance API --- src/org/traccar/api/resource/DeviceResource.java | 9 ++++++ src/org/traccar/database/DeviceManager.java | 12 +++++++ src/org/traccar/model/DeviceTotalDistance.java | 41 ++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 src/org/traccar/model/DeviceTotalDistance.java diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index 56787b7bb..449083cb1 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -18,6 +18,7 @@ package org.traccar.api.resource; import org.traccar.Context; import org.traccar.api.BaseResource; import org.traccar.model.Device; +import org.traccar.model.DeviceTotalDistance; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -92,4 +93,12 @@ public class DeviceResource extends BaseResource { return Response.noContent().build(); } + @Path("{id}/totaldistance") + @PUT + public Response updateTotalDistance(@PathParam("id") long id, DeviceTotalDistance entity) throws SQLException { + Context.getPermissionsManager().checkAdmin(getUserId()); + Context.getDeviceManager().resetTotalDistance(entity); + return Response.noContent().build(); + } + } diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index f32c7edd2..7d16c1c32 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -30,6 +30,7 @@ import org.traccar.Config; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Device; +import org.traccar.model.DeviceTotalDistance; import org.traccar.model.Group; import org.traccar.model.Position; import org.traccar.model.Server; @@ -428,4 +429,15 @@ public class DeviceManager implements IdentityManager { } return result; } + + public void resetTotalDistance(DeviceTotalDistance deviceTotalDistance) throws SQLException { + Position last = positions.get(deviceTotalDistance.getDeviceId()); + if (last != null) { + last.getAttributes().put(Position.KEY_TOTAL_DISTANCE, deviceTotalDistance.getTotalDistance()); + dataManager.addPosition(last); + updateLatestPosition(last); + } else { + throw new IllegalArgumentException(); + } + } } diff --git a/src/org/traccar/model/DeviceTotalDistance.java b/src/org/traccar/model/DeviceTotalDistance.java new file mode 100644 index 000000000..1faa7ce11 --- /dev/null +++ b/src/org/traccar/model/DeviceTotalDistance.java @@ -0,0 +1,41 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2016 Andrey Kunitsyn (abyss@fox5.ru) + * + * 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 DeviceTotalDistance { + + private long deviceId; + + public long getDeviceId() { + return deviceId; + } + + public void setDeviceId(long deviceId) { + this.deviceId = deviceId; + } + + private double totalDistance; + + public double getTotalDistance() { + return totalDistance; + } + + public void setTotalDistance(double totalDistance) { + this.totalDistance = totalDistance; + } + +} -- cgit v1.2.3 From 75a86394ec26725cf9460d336c03a3f55a82d599 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Sun, 23 Oct 2016 06:36:44 +0500 Subject: Rename API path --- src/org/traccar/api/resource/DeviceResource.java | 2 +- src/org/traccar/database/DeviceManager.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index 449083cb1..1f65fa041 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -93,7 +93,7 @@ public class DeviceResource extends BaseResource { return Response.noContent().build(); } - @Path("{id}/totaldistance") + @Path("{id}/distance") @PUT public Response updateTotalDistance(@PathParam("id") long id, DeviceTotalDistance entity) throws SQLException { Context.getPermissionsManager().checkAdmin(getUserId()); diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 7d16c1c32..f71b9308c 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -430,7 +430,7 @@ public class DeviceManager implements IdentityManager { return result; } - public void resetTotalDistance(DeviceTotalDistance deviceTotalDistance) throws SQLException { + public void resetTotalDistance(DeviceTotalDistance deviceTotalDistance) throws SQLException { Position last = positions.get(deviceTotalDistance.getDeviceId()); if (last != null) { last.getAttributes().put(Position.KEY_TOTAL_DISTANCE, deviceTotalDistance.getTotalDistance()); -- cgit v1.2.3