diff options
Diffstat (limited to 'src/org/traccar')
43 files changed, 512 insertions, 161 deletions
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/BaseProtocol.java b/src/org/traccar/BaseProtocol.java index 4cbb5bd7c..59331d7cc 100644 --- a/src/org/traccar/BaseProtocol.java +++ b/src/org/traccar/BaseProtocol.java @@ -56,7 +56,7 @@ public abstract class BaseProtocol implements Protocol { if (supportedCommands.contains(command.getType())) { activeDevice.write(command); } else if (command.getType().equals(Command.TYPE_CUSTOM)) { - String data = (String) command.getAttributes().get(Command.KEY_DATA); + String data = command.getString(Command.KEY_DATA); if (activeDevice.getChannel().getPipeline().get(StringEncoder.class) != null) { activeDevice.write(data); } else { diff --git a/src/org/traccar/BaseProtocolEncoder.java b/src/org/traccar/BaseProtocolEncoder.java index 5c2c503e4..3c2d08471 100644 --- a/src/org/traccar/BaseProtocolEncoder.java +++ b/src/org/traccar/BaseProtocolEncoder.java @@ -31,8 +31,8 @@ public abstract class BaseProtocolEncoder extends OneToOneEncoder { protected void initDevicePassword(Command command, String defaultPassword) { if (!command.getAttributes().containsKey(Command.KEY_DEVICE_PASSWORD)) { Device device = Context.getIdentityManager().getDeviceById(command.getDeviceId()); - if (device.getAttributes().containsKey(Command.KEY_DEVICE_PASSWORD)) { - String password = (String) device.getAttributes().get(Command.KEY_DEVICE_PASSWORD); + String password = device.getString(Command.KEY_DEVICE_PASSWORD); + if (password != null) { command.set(Command.KEY_DEVICE_PASSWORD, password); } else { command.set(Command.KEY_DEVICE_PASSWORD, defaultPassword); diff --git a/src/org/traccar/CopyAttributesHandler.java b/src/org/traccar/CopyAttributesHandler.java index c4cac2e7f..f6326ea84 100644 --- a/src/org/traccar/CopyAttributesHandler.java +++ b/src/org/traccar/CopyAttributesHandler.java @@ -29,8 +29,8 @@ public class CopyAttributesHandler extends BaseDataHandler { @Override protected Position handlePosition(Position position) { - String attributesString = Context.getDeviceManager().lookupConfigString(position.getDeviceId(), - "processing.copyAttributes", null); + String attributesString = Context.getDeviceManager().lookupAttributeString( + position.getDeviceId(), "processing.copyAttributes", null, true); Position last = getLastPosition(position.getDeviceId()); if (attributesString != null && last != null) { for (String attribute : attributesString.split(" ")) { diff --git a/src/org/traccar/DistanceHandler.java b/src/org/traccar/DistanceHandler.java index 138fc85d6..fdf9847a7 100644 --- a/src/org/traccar/DistanceHandler.java +++ b/src/org/traccar/DistanceHandler.java @@ -38,9 +38,7 @@ public class DistanceHandler extends BaseDataHandler { Position last = getLastPosition(position.getDeviceId()); if (last != null) { - if (last.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE)) { - totalDistance = ((Number) last.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue(); - } + totalDistance = last.getDouble(Position.KEY_TOTAL_DISTANCE); if (!position.getAttributes().containsKey(Position.KEY_DISTANCE)) { distance = DistanceCalculator.distance( @@ -49,7 +47,7 @@ public class DistanceHandler extends BaseDataHandler { distance = BigDecimal.valueOf(distance).setScale(2, RoundingMode.HALF_EVEN).doubleValue(); } else { - distance = ((Number) position.getAttributes().get(Position.KEY_DISTANCE)).doubleValue(); + distance = position.getDouble(Position.KEY_DISTANCE); } } if (!position.getAttributes().containsKey(Position.KEY_DISTANCE)) { diff --git a/src/org/traccar/FilterHandler.java b/src/org/traccar/FilterHandler.java index 40676c72d..898651837 100644 --- a/src/org/traccar/FilterHandler.java +++ b/src/org/traccar/FilterHandler.java @@ -111,7 +111,7 @@ public class FilterHandler extends BaseDataHandler { } private boolean filterApproximate(Position position) { - Boolean approximate = (Boolean) position.getAttributes().get(Position.KEY_APPROXIMATE); + Boolean approximate = position.getBoolean(Position.KEY_APPROXIMATE); return filterApproximate && approximate != null && approximate; } diff --git a/src/org/traccar/api/SecurityRequestFilter.java b/src/org/traccar/api/SecurityRequestFilter.java index 3f2390754..ca3ebf04d 100644 --- a/src/org/traccar/api/SecurityRequestFilter.java +++ b/src/org/traccar/api/SecurityRequestFilter.java @@ -83,7 +83,7 @@ public class SecurityRequestFilter implements ContainerRequestFilter { Long userId = (Long) request.getSession().getAttribute(SessionResource.USER_ID_KEY); if (userId != null) { - Context.getPermissionsManager().checkUser(userId); + Context.getPermissionsManager().checkUserEnabled(userId); Context.getStatisticsManager().registerRequest(userId); securityContext = new UserSecurityContext(new UserPrincipal(userId)); } diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java index 9010babf2..c43cca122 100644 --- a/src/org/traccar/api/resource/NotificationResource.java +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -56,4 +56,5 @@ public class NotificationResource extends BaseResource { Context.getNotificationManager().updateNotification(entity); return Response.ok(entity).build(); } + } diff --git a/src/org/traccar/api/resource/ReportResource.java b/src/org/traccar/api/resource/ReportResource.java index 2717fe01e..e37d6f01d 100644 --- a/src/org/traccar/api/resource/ReportResource.java +++ b/src/org/traccar/api/resource/ReportResource.java @@ -51,7 +51,7 @@ public class ReportResource extends BaseResource { @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException, IOException { ByteArrayOutputStream stream = new ByteArrayOutputStream(); Route.getExcel(stream, getUserId(), deviceIds, groupIds, - DateUtil.parseDate(from), DateUtil.parseDate(to)); + DateUtil.parseDateTime(from), DateUtil.parseDateTime(to)); return Response.ok(stream.toByteArray()) .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build(); @@ -77,7 +77,7 @@ public class ReportResource extends BaseResource { @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException, IOException { ByteArrayOutputStream stream = new ByteArrayOutputStream(); Events.getExcel(stream, getUserId(), deviceIds, groupIds, types, - DateUtil.parseDate(from), DateUtil.parseDate(to)); + DateUtil.parseDateTime(from), DateUtil.parseDateTime(to)); return Response.ok(stream.toByteArray()) .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build(); @@ -101,7 +101,7 @@ public class ReportResource extends BaseResource { @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException, IOException { ByteArrayOutputStream stream = new ByteArrayOutputStream(); Summary.getExcel(stream, getUserId(), deviceIds, groupIds, - DateUtil.parseDate(from), DateUtil.parseDate(to)); + DateUtil.parseDateTime(from), DateUtil.parseDateTime(to)); return Response.ok(stream.toByteArray()) .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build(); @@ -125,7 +125,7 @@ public class ReportResource extends BaseResource { @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException, IOException { ByteArrayOutputStream stream = new ByteArrayOutputStream(); Trips.getExcel(stream, getUserId(), deviceIds, groupIds, - DateUtil.parseDate(from), DateUtil.parseDate(to)); + DateUtil.parseDateTime(from), DateUtil.parseDateTime(to)); return Response.ok(stream.toByteArray()) .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build(); diff --git a/src/org/traccar/api/resource/SessionResource.java b/src/org/traccar/api/resource/SessionResource.java index 996865c4b..5f1c597d1 100644 --- a/src/org/traccar/api/resource/SessionResource.java +++ b/src/org/traccar/api/resource/SessionResource.java @@ -80,7 +80,7 @@ public class SessionResource extends BaseResource { } if (userId != null) { - Context.getPermissionsManager().checkUser(userId); + Context.getPermissionsManager().checkUserEnabled(userId); return Context.getPermissionsManager().getUser(userId); } else { throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).build()); diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java index a9edced25..678daac9b 100644 --- a/src/org/traccar/api/resource/UserResource.java +++ b/src/org/traccar/api/resource/UserResource.java @@ -32,6 +32,7 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.sql.SQLException; import java.util.Collection; +import java.util.Date; @Path("users") @Produces(MediaType.APPLICATION_JSON) @@ -49,6 +50,13 @@ public class UserResource extends BaseResource { public Response add(User entity) throws SQLException { if (!Context.getPermissionsManager().isAdmin(getUserId())) { Context.getPermissionsManager().checkRegistration(getUserId()); + Context.getPermissionsManager().checkUserUpdate(getUserId(), new User(), entity); + entity.setDeviceLimit(Context.getConfig().getInteger("users.defaultDeviceLimit")); + int expirationDays = Context.getConfig().getInteger("users.defaultExpirationDays"); + if (expirationDays > 0) { + entity.setExpirationTime( + new Date(System.currentTimeMillis() + (long) expirationDays * 24 * 3600 * 1000)); + } } Context.getPermissionsManager().addUser(entity); if (Context.getNotificationManager() != null) { @@ -60,19 +68,9 @@ public class UserResource extends BaseResource { @Path("{id}") @PUT public Response update(User entity) throws SQLException { - User old = Context.getPermissionsManager().getUser(entity.getId()); - if (old.getExpirationTime() == null && entity.getExpirationTime() != null - || old.getExpirationTime() != null && !old.getExpirationTime().equals(entity.getExpirationTime()) - || old.getAdmin() != entity.getAdmin() - || old.getReadonly() != entity.getReadonly() - || old.getDisabled() != entity.getDisabled() - || old.getDeviceLimit() != entity.getDeviceLimit() - || old.getToken() == null && entity.getToken() != null - || old.getToken() != null && !old.getToken().equals(entity.getToken())) { - Context.getPermissionsManager().checkAdmin(getUserId()); - } else { - Context.getPermissionsManager().checkUser(getUserId(), entity.getId()); - } + User before = Context.getPermissionsManager().getUser(entity.getId()); + Context.getPermissionsManager().checkUser(getUserId(), entity.getId()); + Context.getPermissionsManager().checkUserUpdate(getUserId(), before, entity); Context.getPermissionsManager().updateUser(entity); if (Context.getNotificationManager() != null) { Context.getNotificationManager().refresh(); diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index 216c90bc2..bc44c31ae 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -76,10 +76,19 @@ public class ConnectionManager { } if (enableStatusEvents && !status.equals(device.getStatus())) { - Event event = new Event(Event.TYPE_DEVICE_OFFLINE, deviceId); - if (status.equals(Device.STATUS_ONLINE)) { - event.setType(Event.TYPE_DEVICE_ONLINE); + String eventType; + switch (status) { + case Device.STATUS_ONLINE: + eventType = Event.TYPE_DEVICE_ONLINE; + break; + case Device.STATUS_UNKNOWN: + eventType = Event.TYPE_DEVICE_UNKNOWN; + break; + default: + eventType = Event.TYPE_DEVICE_OFFLINE; + break; } + Event event = new Event(eventType, deviceId); if (Context.getNotificationManager() != null) { Context.getNotificationManager().updateEvent(event, null); } @@ -91,7 +100,6 @@ public class ConnectionManager { timeout.cancel(); } - if (time != null) { device.setLastUpdate(time); } diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 5681602fe..c70e67231 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -317,98 +317,60 @@ public class DeviceManager implements IdentityManager { groupsById.remove(groupId); } - public boolean lookupServerBoolean(long deviceId, String attributeName, boolean defaultValue) { - String result = lookupAttribute(deviceId, attributeName, true); + public boolean lookupAttributeBoolean( + long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) { + String result = lookupAttribute(deviceId, attributeName, lookupConfig); if (result != null) { return Boolean.parseBoolean(result); } return defaultValue; } - public String lookupServerString(long deviceId, String attributeName, String defaultValue) { - String result = lookupAttribute(deviceId, attributeName, true); + public String lookupAttributeString( + long deviceId, String attributeName, String defaultValue, boolean lookupConfig) { + String result = lookupAttribute(deviceId, attributeName, lookupConfig); if (result != null) { return result; } return defaultValue; } - public int lookupServerInteger(long deviceId, String attributeName, int defaultValue) { - String result = lookupAttribute(deviceId, attributeName, true); + public int lookupAttributeInteger(long deviceId, String attributeName, int defaultValue, boolean lookupConfig) { + String result = lookupAttribute(deviceId, attributeName, lookupConfig); if (result != null) { return Integer.parseInt(result); } return defaultValue; } - public long lookupServerLong(long deviceId, String attributeName, long defaultValue) { - String result = lookupAttribute(deviceId, attributeName, true); + public long lookupAttributeLong( + long deviceId, String attributeName, long defaultValue, boolean lookupConfig) { + String result = lookupAttribute(deviceId, attributeName, lookupConfig); if (result != null) { return Long.parseLong(result); } return defaultValue; } - public double lookupServerDouble(long deviceId, String attributeName, double defaultValue) { - String result = lookupAttribute(deviceId, attributeName, true); + public double lookupAttributeDouble( + long deviceId, String attributeName, double defaultValue, boolean lookupConfig) { + String result = lookupAttribute(deviceId, attributeName, lookupConfig); if (result != null) { return Double.parseDouble(result); } return defaultValue; } - public boolean lookupConfigBoolean(long deviceId, String attributeName, boolean defaultValue) { - String result = lookupAttribute(deviceId, attributeName, false); - if (result != null) { - return Boolean.parseBoolean(result); - } - return defaultValue; - } - - public String lookupConfigString(long deviceId, String attributeName, String defaultValue) { - String result = lookupAttribute(deviceId, attributeName, false); - if (result != null) { - return result; - } - return defaultValue; - } - - public int lookupConfigInteger(long deviceId, String attributeName, int defaultValue) { - String result = lookupAttribute(deviceId, attributeName, false); - if (result != null) { - return Integer.parseInt(result); - } - return defaultValue; - } - - public long lookupConfigLong(long deviceId, String attributeName, long defaultValue) { - String result = lookupAttribute(deviceId, attributeName, false); - if (result != null) { - return Long.parseLong(result); - } - return defaultValue; - } - - public double lookupConfigDouble(long deviceId, String attributeName, double defaultValue) { - String result = lookupAttribute(deviceId, attributeName, false); - if (result != null) { - return Double.parseDouble(result); - } - return defaultValue; - } - - private String lookupAttribute(long deviceId, String attributeName, boolean lookupServer) { + private String lookupAttribute(long deviceId, String attributeName, boolean lookupConfig) { String result = null; Device device = getDeviceById(deviceId); if (device != null) { - if (device.getAttributes().containsKey(attributeName)) { - result = (String) device.getAttributes().get(attributeName); - } + result = device.getString(attributeName); if (result == null && lookupGroupsAttribute) { long groupId = device.getGroupId(); while (groupId != 0) { if (getGroupById(groupId) != null) { - result = (String) getGroupById(groupId).getAttributes().get(attributeName); + result = getGroupById(groupId).getString(attributeName); if (result != null) { break; } @@ -419,11 +381,11 @@ public class DeviceManager implements IdentityManager { } } if (result == null) { - if (lookupServer) { - Server server = Context.getPermissionsManager().getServer(); - result = (String) server.getAttributes().get(attributeName); - } else { + if (lookupConfig) { result = Context.getConfig().getString(attributeName); + } else { + Server server = Context.getPermissionsManager().getServer(); + result = server.getString(attributeName); } } } diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index 110f699b7..7e79e289f 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -74,7 +74,6 @@ public class NotificationManager { } public void updateEvents(Collection<Event> events, Position position) { - for (Event event : events) { updateEvent(event, position); } @@ -176,9 +175,8 @@ public class NotificationManager { } public Set<Notification> getAllNotifications() { - Set<Notification> notifications = new HashSet<>(); - long id = 0; + long id = 1; Field[] fields = Event.class.getDeclaredFields(); for (Field field : fields) { if (Modifier.isStatic(field.getModifiers()) && field.getName().startsWith("TYPE_")) { @@ -194,4 +192,5 @@ public class NotificationManager { } return notifications; } + } diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 71633f6ef..078a5f935 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -29,6 +29,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -155,7 +156,7 @@ public class PermissionsManager { } } - public void checkUser(long userId) throws SecurityException { + public void checkUserEnabled(long userId) throws SecurityException { User user = getUser(userId); if (user.getDisabled()) { throw new SecurityException("Account is disabled"); @@ -165,6 +166,17 @@ public class PermissionsManager { } } + public void checkUserUpdate(long userId, User before, User after) throws SecurityException { + if (before.getAdmin() != after.getAdmin() + || before.getReadonly() != after.getReadonly() + || before.getDisabled() != after.getDisabled() + || before.getDeviceLimit() != after.getDeviceLimit() + || !Objects.equals(before.getExpirationTime(), after.getExpirationTime()) + || !Objects.equals(before.getToken(), after.getToken())) { + checkAdmin(userId); + } + } + public void checkUser(long userId, long otherUserId) throws SecurityException { if (userId != otherUserId) { checkAdmin(userId); @@ -244,7 +256,7 @@ public class PermissionsManager { public User login(String email, String password) throws SQLException { User user = dataManager.login(email, password); if (user != null) { - checkUser(user.getId()); + checkUserEnabled(user.getId()); return users.get(user.getId()); } return null; diff --git a/src/org/traccar/events/IgnitionEventHandler.java b/src/org/traccar/events/IgnitionEventHandler.java index b52083f57..3086adb13 100644 --- a/src/org/traccar/events/IgnitionEventHandler.java +++ b/src/org/traccar/events/IgnitionEventHandler.java @@ -39,20 +39,12 @@ public class IgnitionEventHandler extends BaseEventHandler { Collection<Event> result = null; - boolean ignition = false; - Object ignitionObject = position.getAttributes().get(Position.KEY_IGNITION); - if (ignitionObject != null && ignitionObject instanceof Boolean) { - ignition = (Boolean) ignitionObject; - } + boolean ignition = position.getBoolean(Position.KEY_IGNITION); boolean oldIgnition = false; - Object oldIgnitionObject = null; Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId()); if (lastPosition != null) { - oldIgnitionObject = lastPosition.getAttributes().get(Position.KEY_IGNITION); - } - if (oldIgnitionObject != null && oldIgnitionObject instanceof Boolean) { - oldIgnition = (Boolean) oldIgnitionObject; + oldIgnition = lastPosition.getBoolean(Position.KEY_IGNITION); } if (ignition && !oldIgnition) { diff --git a/src/org/traccar/events/MaintenanceEventHandler.java b/src/org/traccar/events/MaintenanceEventHandler.java new file mode 100644 index 000000000..9e0da97f5 --- /dev/null +++ b/src/org/traccar/events/MaintenanceEventHandler.java @@ -0,0 +1,67 @@ +/* + * 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() + .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); + + Collection<Event> events = new ArrayList<>(); + double oldTotalDistance = 0.0; + double newTotalDistance = 0.0; + + Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId()); + if (lastPosition != null) { + oldTotalDistance = lastPosition.getDouble(Position.KEY_TOTAL_DISTANCE); + } + newTotalDistance = position.getDouble(Position.KEY_TOTAL_DISTANCE); + + oldTotalDistance -= maintenanceStart; + newTotalDistance -= maintenanceStart; + if ((long) (oldTotalDistance / maintenanceInterval) < (long) (newTotalDistance / maintenanceInterval)) { + events.add(new Event(Event.TYPE_MAINTENANCE, position.getDeviceId(), position.getId())); + } + + return events; + } + +} diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index c3e3e19cb..505d706f4 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -48,7 +48,7 @@ public class OverspeedEventHandler extends BaseEventHandler { Collection<Event> events = new ArrayList<>(); double speed = position.getSpeed(); double speedLimit = Context.getDeviceManager() - .lookupServerDouble(device.getId(), ATTRIBUTE_SPEED_LIMIT, 0); + .lookupAttributeDouble(device.getId(), ATTRIBUTE_SPEED_LIMIT, 0, false); if (speedLimit == 0) { return null; } diff --git a/src/org/traccar/helper/DateUtil.java b/src/org/traccar/helper/DateUtil.java index ad8534eb8..de36d4420 100644 --- a/src/org/traccar/helper/DateUtil.java +++ b/src/org/traccar/helper/DateUtil.java @@ -18,12 +18,13 @@ package org.traccar.helper; import java.util.Calendar; import java.util.Date; +import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.ISODateTimeFormat; public final class DateUtil { - private static final DateTimeFormatter DATE_FORMAT = ISODateTimeFormat.dateTime(); + private static final DateTimeFormatter DATE_FORMAT = ISODateTimeFormat.dateTimeParser(); private DateUtil() { } @@ -64,4 +65,8 @@ public final class DateUtil { return DATE_FORMAT.parseDateTime(value).toDate(); } + public static DateTime parseDateTime(String value) { + return DATE_FORMAT.withOffsetParsed().parseDateTime(value); + } + } diff --git a/src/org/traccar/model/Event.java b/src/org/traccar/model/Event.java index cdb79f04d..ee62f9776 100644 --- a/src/org/traccar/model/Event.java +++ b/src/org/traccar/model/Event.java @@ -40,6 +40,7 @@ public class Event extends Message { public static final String TYPE_COMMAND_RESULT = "commandResult"; public static final String TYPE_DEVICE_ONLINE = "deviceOnline"; + public static final String TYPE_DEVICE_UNKNOWN = "deviceUnknown"; public static final String TYPE_DEVICE_OFFLINE = "deviceOffline"; public static final String TYPE_DEVICE_MOVING = "deviceMoving"; @@ -55,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 = "maintenance"; + private Date serverTime; public Date getServerTime() { diff --git a/src/org/traccar/model/Extensible.java b/src/org/traccar/model/Extensible.java index efe60fb75..b8c83bb21 100644 --- a/src/org/traccar/model/Extensible.java +++ b/src/org/traccar/model/Extensible.java @@ -68,4 +68,44 @@ public class Extensible { } } + public String getString(String key) { + if (attributes.containsKey(key)) { + return (String) attributes.get(key); + } else { + return null; + } + } + + public double getDouble(String key) { + if (attributes.containsKey(key)) { + return ((Number) attributes.get(key)).doubleValue(); + } else { + return 0.0; + } + } + + public boolean getBoolean(String key) { + if (attributes.containsKey(key)) { + return Boolean.parseBoolean(attributes.get(key).toString()); + } else { + return false; + } + } + + public int getInteger(String key) { + if (attributes.containsKey(key)) { + return ((Number) attributes.get(key)).intValue(); + } else { + return 0; + } + } + + public long getLong(String key) { + if (attributes.containsKey(key)) { + return ((Number) attributes.get(key)).longValue(); + } else { + return 0; + } + } + } diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index b48fe9efc..6753873ed 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" @@ -47,46 +51,51 @@ public final class NotificationFormatter { public static final String TITLE_TEMPLATE_TYPE_DEVICE_MOVING = "%1$s: moving"; public static final String MESSAGE_TEMPLATE_TYPE_DEVICE_MOVING = "Device: %1$s%n" + "Moving%n" - + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%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_DEVICE_STOPPED = "%1$s: stopped"; public static final String MESSAGE_TEMPLATE_TYPE_DEVICE_STOPPED = "Device: %1$s%n" + "Stopped%n" - + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%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_DEVICE_OVERSPEED = "%1$s: exceeds the speed"; public static final String MESSAGE_TEMPLATE_TYPE_DEVICE_OVERSPEED = "Device: %1$s%n" + "Exceeds the speed: %5$s%n" - + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%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_GEOFENCE_ENTER = "%1$s: has entered geofence"; public static final String MESSAGE_TEMPLATE_TYPE_GEOFENCE_ENTER = "Device: %1$s%n" + "Has entered geofence: %5$s%n" - + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%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_GEOFENCE_EXIT = "%1$s: has exited geofence"; public static final String MESSAGE_TEMPLATE_TYPE_GEOFENCE_EXIT = "Device: %1$s%n" + "Has exited geofence: %5$s%n" - + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%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_ALARM = "%1$s: alarm!"; public static final String MESSAGE_TEMPLATE_TYPE_ALARM = "Device: %1$s%n" + "Alarm: %5$s%n" - + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%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_IGNITION_ON = "%1$s: ignition ON"; public static final String MESSAGE_TEMPLATE_TYPE_IGNITION_ON = "Device: %1$s%n" + "Ignition ON%n" - + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%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_IGNITION_OFF = "%1$s: ignition OFF"; public static final String MESSAGE_TEMPLATE_TYPE_IGNITION_OFF = "Device: %1$s%n" + "Ignition OFF%n" - + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%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 = "%1$s: maintenance is required"; + public static final String MESSAGE_TEMPLATE_TYPE_MAINTENANCE = "Device: %1$s%n" + + "Maintenance is required%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) { @@ -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: + formatter.format(TITLE_TEMPLATE_TYPE_MAINTENANCE, 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: + formatter.format(MESSAGE_TEMPLATE_TYPE_MAINTENANCE, device.getName(), position.getFixTime(), + position.getLatitude(), position.getLongitude()); + break; default: formatter.format("Unknown type"); break; diff --git a/src/org/traccar/notification/PropertiesProvider.java b/src/org/traccar/notification/PropertiesProvider.java index 9136288f8..e7cac8d0f 100644 --- a/src/org/traccar/notification/PropertiesProvider.java +++ b/src/org/traccar/notification/PropertiesProvider.java @@ -36,7 +36,7 @@ public class PropertiesProvider { if (config != null) { return config.getString(key); } else { - return (String) extensible.getAttributes().get(key); + return extensible.getString(key); } } diff --git a/src/org/traccar/protocol/At2000FrameDecoder.java b/src/org/traccar/protocol/At2000FrameDecoder.java new file mode 100644 index 000000000..d0be3f5f4 --- /dev/null +++ b/src/org/traccar/protocol/At2000FrameDecoder.java @@ -0,0 +1,57 @@ +/* + * Copyright 2016 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. + * 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.protocol; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.handler.codec.frame.FrameDecoder; + +public class At2000FrameDecoder extends FrameDecoder { + + private static final int BLOCK_LENGTH = 16; + + private boolean firstPacket = true; + + @Override + protected Object decode( + ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { + + if (buf.readableBytes() < 5) { + return null; + } + + int length; + if (firstPacket) { + firstPacket = false; + length = buf.getUnsignedMedium(buf.readerIndex() + 2); + } else { + length = buf.getUnsignedMedium(buf.readerIndex() + 1); + } + + length += BLOCK_LENGTH; + if (length % BLOCK_LENGTH != 0) { + length = (length / BLOCK_LENGTH + 1) * BLOCK_LENGTH; + } + + if (buf.readableBytes() >= length) { + return buf.readBytes(length); + } + + return null; + } + +} diff --git a/src/org/traccar/protocol/At2000Protocol.java b/src/org/traccar/protocol/At2000Protocol.java new file mode 100644 index 000000000..418619cb4 --- /dev/null +++ b/src/org/traccar/protocol/At2000Protocol.java @@ -0,0 +1,42 @@ +/* + * Copyright 2016 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. + * 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.protocol; + +import org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.ChannelPipeline; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; + +import java.util.List; + +public class At2000Protocol extends BaseProtocol { + + public At2000Protocol() { + super("at2000"); + } + + @Override + public void initTrackerServers(List<TrackerServer> serverList) { + serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new At2000FrameDecoder()); + pipeline.addLast("objectDecoder", new At2000ProtocolDecoder(At2000Protocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/At2000ProtocolDecoder.java b/src/org/traccar/protocol/At2000ProtocolDecoder.java new file mode 100644 index 000000000..17da0eef7 --- /dev/null +++ b/src/org/traccar/protocol/At2000ProtocolDecoder.java @@ -0,0 +1,134 @@ +/* + * Copyright 2016 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. + * 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.protocol; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.helper.UnitsConverter; +import org.traccar.model.Position; + +import javax.crypto.Cipher; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; +import javax.xml.bind.DatatypeConverter; +import java.net.SocketAddress; +import java.nio.ByteOrder; +import java.nio.charset.StandardCharsets; +import java.util.Date; + +public class At2000ProtocolDecoder extends BaseProtocolDecoder { + + private static final int BLOCK_LENGTH = 16; + + public At2000ProtocolDecoder(At2000Protocol protocol) { + super(protocol); + } + + public static final int MSG_ACKNOWLEDGEMENT = 0x00; + public static final int MSG_DEVICE_ID = 0x01; + public static final int MSG_TRACK_REQUEST = 0x88; + public static final int MSG_TRACK_RESPONSE = 0x89; + public static final int MSG_SESSION_END = 0x0c; + + private Cipher cipher; + + private static void sendResponse(Channel channel) { + if (channel != null) { + ChannelBuffer response = ChannelBuffers.directBuffer(BLOCK_LENGTH); + response.writeByte(MSG_ACKNOWLEDGEMENT); + response.writeMedium(1); + response.writeByte(0x00); // success + response.writerIndex(BLOCK_LENGTH); + channel.write(response); + } + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + ChannelBuffer buf = (ChannelBuffer) msg; + + if (buf.getUnsignedByte(buf.readerIndex()) == 0x01) { + buf.readUnsignedByte(); // codec id + } + + int type = buf.readUnsignedByte(); + buf.readUnsignedMedium(); // length + buf.skipBytes(BLOCK_LENGTH - 1 - 3); + + if (type == MSG_DEVICE_ID) { + + String imei = buf.readBytes(15).toString(StandardCharsets.US_ASCII); + if (getDeviceSession(channel, remoteAddress, imei) != null) { + + byte[] iv = new byte[BLOCK_LENGTH]; + buf.readBytes(iv); + IvParameterSpec ivSpec = new IvParameterSpec(iv); + + SecretKeySpec keySpec = new SecretKeySpec( + DatatypeConverter.parseHexBinary("000102030405060708090a0b0c0d0e0f"), "AES"); + + cipher = Cipher.getInstance("AES/CBC/NoPadding"); + cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); + + byte[] data = new byte[BLOCK_LENGTH]; + buf.readBytes(data); + cipher.update(data); + + } + + } else if (type == MSG_TRACK_RESPONSE) { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } + + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + byte[] data = new byte[buf.capacity() - BLOCK_LENGTH]; + buf.readBytes(data); + buf = ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, cipher.update(data)); + + buf.readUnsignedShort(); // index + buf.readUnsignedShort(); // reserved + + position.setValid(true); + + position.setTime(new Date(buf.readLong() * 1000)); + + position.setLatitude(buf.readFloat()); + position.setLongitude(buf.readFloat()); + position.setAltitude(buf.readFloat()); + position.setSpeed(UnitsConverter.knotsFromKph(buf.readFloat())); + position.setCourse(buf.readFloat()); + + return position; + + } + + sendResponse(channel); + + return null; + } + +} diff --git a/src/org/traccar/protocol/CityeasyProtocolEncoder.java b/src/org/traccar/protocol/CityeasyProtocolEncoder.java index 0c149c1c9..c800131d6 100644 --- a/src/org/traccar/protocol/CityeasyProtocolEncoder.java +++ b/src/org/traccar/protocol/CityeasyProtocolEncoder.java @@ -50,13 +50,13 @@ public class CityeasyProtocolEncoder extends BaseProtocolEncoder { case Command.TYPE_POSITION_SINGLE: return encodeContent(CityeasyProtocolDecoder.MSG_LOCATION_REQUEST, content); case Command.TYPE_POSITION_PERIODIC: - content.writeShort(((Number) command.getAttributes().get(Command.KEY_FREQUENCY)).intValue()); + content.writeShort(command.getInteger(Command.KEY_FREQUENCY)); return encodeContent(CityeasyProtocolDecoder.MSG_LOCATION_INTERVAL, content); case Command.TYPE_POSITION_STOP: content.writeShort(0); return encodeContent(CityeasyProtocolDecoder.MSG_LOCATION_INTERVAL, content); case Command.TYPE_SET_TIMEZONE: - int timezone = ((Number) command.getAttributes().get(Command.KEY_TIMEZONE)).intValue(); + int timezone = command.getInteger(Command.KEY_TIMEZONE); if (timezone < 0) { content.writeByte(1); } else { diff --git a/src/org/traccar/protocol/EelinkProtocolEncoder.java b/src/org/traccar/protocol/EelinkProtocolEncoder.java index b9d6b6f97..5a28733f7 100644 --- a/src/org/traccar/protocol/EelinkProtocolEncoder.java +++ b/src/org/traccar/protocol/EelinkProtocolEncoder.java @@ -47,7 +47,7 @@ public class EelinkProtocolEncoder extends BaseProtocolEncoder { switch (command.getType()) { case Command.TYPE_CUSTOM: - return encodeContent((String) command.getAttributes().get(Command.KEY_DATA)); + return encodeContent(command.getString(Command.KEY_DATA)); case Command.TYPE_ENGINE_STOP: return encodeContent("RELAY,1#"); case Command.TYPE_ENGINE_RESUME: diff --git a/src/org/traccar/protocol/Gps103Protocol.java b/src/org/traccar/protocol/Gps103Protocol.java index 82d42f1c9..e3beb34e5 100644 --- a/src/org/traccar/protocol/Gps103Protocol.java +++ b/src/org/traccar/protocol/Gps103Protocol.java @@ -47,7 +47,7 @@ public class Gps103Protocol extends BaseProtocol { serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { - pipeline.addLast("frameDecoder", new CharacterDelimiterFrameDecoder(1024, "\r\n", "\n", ";")); + pipeline.addLast("frameDecoder", new CharacterDelimiterFrameDecoder(2048, "\r\n", "\n", ";")); pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("stringDecoder", new StringDecoder()); pipeline.addLast("objectEncoder", new Gps103ProtocolEncoder()); diff --git a/src/org/traccar/protocol/Gt06ProtocolEncoder.java b/src/org/traccar/protocol/Gt06ProtocolEncoder.java index b6ec5a0d1..8e00522a7 100644 --- a/src/org/traccar/protocol/Gt06ProtocolEncoder.java +++ b/src/org/traccar/protocol/Gt06ProtocolEncoder.java @@ -59,7 +59,7 @@ public class Gt06ProtocolEncoder extends BaseProtocolEncoder { boolean alternative; Device device = Context.getIdentityManager().getDeviceById(command.getDeviceId()); if (device.getAttributes().containsKey("gt06.alternative")) { - alternative = Boolean.parseBoolean((String) device.getAttributes().get("gt06.alternative")); + alternative = device.getBoolean("gt06.alternative"); } else { alternative = Context.getConfig().getBoolean("gt06.alternative"); } diff --git a/src/org/traccar/protocol/Jt600ProtocolEncoder.java b/src/org/traccar/protocol/Jt600ProtocolEncoder.java index 3eb0566a4..0bf389460 100644 --- a/src/org/traccar/protocol/Jt600ProtocolEncoder.java +++ b/src/org/traccar/protocol/Jt600ProtocolEncoder.java @@ -29,7 +29,7 @@ public class Jt600ProtocolEncoder extends StringProtocolEncoder { case Command.TYPE_ENGINE_RESUME: return "(S07,1)"; case Command.TYPE_SET_TIMEZONE: - int offset = ((Number) command.getAttributes().get(Command.KEY_TIMEZONE)).intValue() / 60; + int offset = command.getInteger(Command.KEY_TIMEZONE) / 60; return "(S09,1," + offset + ")"; case Command.TYPE_REBOOT_DEVICE: return "(S17)"; diff --git a/src/org/traccar/protocol/MeiligaoProtocolEncoder.java b/src/org/traccar/protocol/MeiligaoProtocolEncoder.java index a91b33a60..268bae392 100644 --- a/src/org/traccar/protocol/MeiligaoProtocolEncoder.java +++ b/src/org/traccar/protocol/MeiligaoProtocolEncoder.java @@ -24,7 +24,6 @@ import org.traccar.model.Command; import javax.xml.bind.DatatypeConverter; import java.nio.charset.StandardCharsets; -import java.util.Map; public class MeiligaoProtocolEncoder extends BaseProtocolEncoder { @@ -62,13 +61,12 @@ public class MeiligaoProtocolEncoder extends BaseProtocolEncoder { protected Object encodeCommand(Command command) { ChannelBuffer content = ChannelBuffers.dynamicBuffer(); - Map<String, Object> attributes = command.getAttributes(); switch (command.getType()) { case Command.TYPE_POSITION_SINGLE: return encodeContent(command.getDeviceId(), MSG_TRACK_ON_DEMAND, content); case Command.TYPE_POSITION_PERIODIC: - content.writeShort(((Number) attributes.get(Command.KEY_FREQUENCY)).intValue() / 10); + content.writeShort(command.getInteger(Command.KEY_FREQUENCY) / 10); return encodeContent(command.getDeviceId(), MSG_TRACK_BY_INTERVAL, content); case Command.TYPE_ENGINE_STOP: content.writeByte(0x01); @@ -77,10 +75,10 @@ public class MeiligaoProtocolEncoder extends BaseProtocolEncoder { content.writeByte(0x00); return encodeContent(command.getDeviceId(), MSG_OUTPUT_CONTROL, content); case Command.TYPE_ALARM_GEOFENCE: - content.writeShort(((Number) attributes.get(Command.KEY_RADIUS)).intValue()); + content.writeShort(command.getInteger(Command.KEY_RADIUS)); return encodeContent(command.getDeviceId(), MSG_MOVEMENT_ALARM, content); case Command.TYPE_SET_TIMEZONE: - int offset = ((Number) attributes.get(Command.KEY_TIMEZONE)).intValue() / 60; + int offset = command.getInteger(Command.KEY_TIMEZONE) / 60; content.writeBytes(String.valueOf(offset).getBytes(StandardCharsets.US_ASCII)); return encodeContent(command.getDeviceId(), MSG_TIME_ZONE, content); case Command.TYPE_REBOOT_DEVICE: diff --git a/src/org/traccar/protocol/NoranProtocolEncoder.java b/src/org/traccar/protocol/NoranProtocolEncoder.java index 5bd069218..25b510a73 100644 --- a/src/org/traccar/protocol/NoranProtocolEncoder.java +++ b/src/org/traccar/protocol/NoranProtocolEncoder.java @@ -52,7 +52,7 @@ public class NoranProtocolEncoder extends BaseProtocolEncoder { case Command.TYPE_POSITION_SINGLE: return encodeContent("*KW,000,000,000000#"); case Command.TYPE_POSITION_PERIODIC: - int interval = ((Number) command.getAttributes().get(Command.KEY_FREQUENCY)).intValue(); + int interval = command.getInteger(Command.KEY_FREQUENCY); return encodeContent("*KW,000,002,000000," + interval + "#"); case Command.TYPE_POSITION_STOP: return encodeContent("*KW,000,002,000000,0#"); diff --git a/src/org/traccar/protocol/RuptelaProtocolEncoder.java b/src/org/traccar/protocol/RuptelaProtocolEncoder.java index c3b3a3619..0abfa18ce 100644 --- a/src/org/traccar/protocol/RuptelaProtocolEncoder.java +++ b/src/org/traccar/protocol/RuptelaProtocolEncoder.java @@ -43,7 +43,7 @@ public class RuptelaProtocolEncoder extends BaseProtocolEncoder { switch (command.getType()) { case Command.TYPE_CUSTOM: - return encodeContent((String) command.getAttributes().get(Command.KEY_DATA)); + return encodeContent(command.getString(Command.KEY_DATA)); default: Log.warning(new UnsupportedOperationException(command.getType())); break; diff --git a/src/org/traccar/protocol/T800xProtocolEncoder.java b/src/org/traccar/protocol/T800xProtocolEncoder.java index 7b02ed10b..6ed5dbccd 100644 --- a/src/org/traccar/protocol/T800xProtocolEncoder.java +++ b/src/org/traccar/protocol/T800xProtocolEncoder.java @@ -51,7 +51,7 @@ public class T800xProtocolEncoder extends BaseProtocolEncoder { switch (command.getType()) { case Command.TYPE_CUSTOM: - return encodeContent(command, (String) command.getAttributes().get(Command.KEY_DATA)); + return encodeContent(command, command.getString(Command.KEY_DATA)); default: Log.warning(new UnsupportedOperationException(command.getType())); break; diff --git a/src/org/traccar/protocol/TeltonikaProtocolEncoder.java b/src/org/traccar/protocol/TeltonikaProtocolEncoder.java index eb83477b8..fd6eae744 100644 --- a/src/org/traccar/protocol/TeltonikaProtocolEncoder.java +++ b/src/org/traccar/protocol/TeltonikaProtocolEncoder.java @@ -50,7 +50,7 @@ public class TeltonikaProtocolEncoder extends BaseProtocolEncoder { switch (command.getType()) { case Command.TYPE_CUSTOM: - return encodeContent((String) command.getAttributes().get(Command.KEY_DATA)); + return encodeContent(command.getString(Command.KEY_DATA)); default: Log.warning(new UnsupportedOperationException(command.getType())); break; diff --git a/src/org/traccar/protocol/Tk103ProtocolDecoder.java b/src/org/traccar/protocol/Tk103ProtocolDecoder.java index 9d04483c1..d3bc0efdc 100644 --- a/src/org/traccar/protocol/Tk103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tk103ProtocolDecoder.java @@ -113,9 +113,11 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { String type = sentence.substring(12, 16); if (type.equals("BP00") || type.equals("BP05")) { String content = sentence.substring(16); - getDeviceSession(channel, remoteAddress, content.substring(0, 15)); + if (content.length() >= 15) { + getDeviceSession(channel, remoteAddress, content.substring(0, 15)); + } if (type.equals("BP00")) { - channel.write("(" + id + "AP01" + content.substring(15) + ")"); + channel.write("(" + id + "AP01HSO)"); return null; } else if (type.equals("BP05")) { channel.write("(" + id + "AP05)"); diff --git a/src/org/traccar/protocol/WatchProtocolEncoder.java b/src/org/traccar/protocol/WatchProtocolEncoder.java index f15c0f0c0..0b6a83ce9 100644 --- a/src/org/traccar/protocol/WatchProtocolEncoder.java +++ b/src/org/traccar/protocol/WatchProtocolEncoder.java @@ -33,7 +33,7 @@ public class WatchProtocolEncoder extends StringProtocolEncoder { } private int getEnableFlag(Command command) { - if ((Boolean) command.getAttributes().get(Command.KEY_ENABLE)) { + if (command.getBoolean(Command.KEY_ENABLE)) { return 1; } else { return 0; @@ -51,7 +51,7 @@ public class WatchProtocolEncoder extends StringProtocolEncoder { } private String getBinaryData(Command command) { - byte[] data = DatatypeConverter.parseHexBinary((String) command.getAttributes().get(Command.KEY_DATA)); + byte[] data = DatatypeConverter.parseHexBinary(command.getString(Command.KEY_DATA)); int encodedLength = data.length; for (byte b : data) { diff --git a/src/org/traccar/reports/Events.java b/src/org/traccar/reports/Events.java index 9d82b97a5..33893aad0 100644 --- a/src/org/traccar/reports/Events.java +++ b/src/org/traccar/reports/Events.java @@ -28,6 +28,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; +import org.joda.time.DateTime; import org.jxls.area.Area; import org.jxls.builder.xls.XlsCommentAreaBuilder; import org.jxls.common.CellRef; @@ -68,13 +69,13 @@ public final class Events { public static void getExcel(OutputStream outputStream, long userId, Collection<Long> deviceIds, Collection<Long> groupIds, - Collection<String> types, Date from, Date to) throws SQLException, IOException { + Collection<String> types, DateTime from, DateTime to) throws SQLException, IOException { ArrayList<DeviceReport> devicesEvents = new ArrayList<>(); ArrayList<String> sheetNames = new ArrayList<>(); HashMap<Long, String> geofenceNames = new HashMap<>(); for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); - Collection<Event> events = Context.getDataManager().getEvents(deviceId, from, to); + Collection<Event> events = Context.getDataManager().getEvents(deviceId, from.toDate(), to.toDate()); boolean all = types.isEmpty() || types.contains(Event.ALL_EVENTS); for (Iterator<Event> iterator = events.iterator(); iterator.hasNext();) { Event event = iterator.next(); @@ -118,6 +119,7 @@ public final class Events { jxlsContext.putVar("to", to); jxlsContext.putVar("distanceUnit", ReportUtils.getDistanceUnit(userId)); jxlsContext.putVar("speedUnit", ReportUtils.getSpeedUnit(userId)); + jxlsContext.putVar("timezone", from.getZone()); jxlsContext.putVar("bracketsRegex", "[\\{\\}\"]"); Transformer transformer = TransformerFactory.createTransformer(inputStream, outputStream); List<Area> xlsAreas = new XlsCommentAreaBuilder(transformer).build(); diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index 7fcc767e9..032925a4a 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -63,18 +63,15 @@ public final class ReportUtils { double distance = 0.0; double firstOdometer = 0.0; double lastOdometer = 0.0; - if (firstPosition.getAttributes().containsKey(Position.KEY_ODOMETER)) { - firstOdometer = ((Number) firstPosition.getAttributes().get(Position.KEY_ODOMETER)).doubleValue(); - } - if (lastPosition.getAttributes().containsKey(Position.KEY_ODOMETER)) { - lastOdometer = ((Number) lastPosition.getAttributes().get(Position.KEY_ODOMETER)).doubleValue(); - } + firstOdometer = firstPosition.getDouble(Position.KEY_ODOMETER); + lastOdometer = lastPosition.getDouble(Position.KEY_ODOMETER); + if (useOdometer && (firstOdometer != 0.0 || lastOdometer != 0.0)) { distance = lastOdometer - firstOdometer; } else if (firstPosition.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE) && lastPosition.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE)) { - distance = ((Number) lastPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue() - - ((Number) firstPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue(); + distance = lastPosition.getDouble(Position.KEY_TOTAL_DISTANCE) + - firstPosition.getDouble(Position.KEY_TOTAL_DISTANCE); } return distance; } diff --git a/src/org/traccar/reports/Route.java b/src/org/traccar/reports/Route.java index b29e04b2e..468771a48 100644 --- a/src/org/traccar/reports/Route.java +++ b/src/org/traccar/reports/Route.java @@ -26,6 +26,7 @@ import java.util.Collection; import java.util.Date; import java.util.List; +import org.joda.time.DateTime; import org.jxls.area.Area; import org.jxls.builder.xls.XlsCommentAreaBuilder; import org.jxls.common.CellRef; @@ -56,12 +57,13 @@ public final class Route { public static void getExcel(OutputStream outputStream, long userId, Collection<Long> deviceIds, Collection<Long> groupIds, - Date from, Date to) throws SQLException, IOException { + DateTime from, DateTime to) throws SQLException, IOException { ArrayList<DeviceReport> devicesRoutes = new ArrayList<>(); ArrayList<String> sheetNames = new ArrayList<>(); for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); - Collection<Position> positions = Context.getDataManager().getPositions(deviceId, from, to); + Collection<Position> positions = Context.getDataManager() + .getPositions(deviceId, from.toDate(), to.toDate()); DeviceReport deviceRoutes = new DeviceReport(); Device device = Context.getIdentityManager().getDeviceById(deviceId); deviceRoutes.setDeviceName(device.getName()); @@ -85,6 +87,7 @@ public final class Route { jxlsContext.putVar("to", to); jxlsContext.putVar("distanceUnit", ReportUtils.getDistanceUnit(userId)); jxlsContext.putVar("speedUnit", ReportUtils.getSpeedUnit(userId)); + jxlsContext.putVar("timezone", from.getZone()); jxlsContext.putVar("bracketsRegex", "[\\{\\}\"]"); Transformer transformer = TransformerFactory.createTransformer(inputStream, outputStream); List<Area> xlsAreas = new XlsCommentAreaBuilder(transformer).build(); diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java index 95a3737a1..cacb4d1d6 100644 --- a/src/org/traccar/reports/Summary.java +++ b/src/org/traccar/reports/Summary.java @@ -25,6 +25,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Date; +import org.joda.time.DateTime; import org.jxls.transform.poi.PoiTransformer; import org.jxls.util.JxlsHelper; import org.traccar.Context; @@ -49,12 +50,8 @@ public final class Summary { if (firstPosition == null) { firstPosition = position; } - if (previousPosition != null - && position.getAttributes().get(Position.KEY_IGNITION) != null - && Boolean.parseBoolean(position.getAttributes().get(Position.KEY_IGNITION).toString()) - && previousPosition.getAttributes().get(Position.KEY_IGNITION) != null - && Boolean.parseBoolean(previousPosition.getAttributes() - .get(Position.KEY_IGNITION).toString())) { + if (previousPosition != null && position.getBoolean(Position.KEY_IGNITION) + && previousPosition.getBoolean(Position.KEY_IGNITION)) { result.addEngineHours(position.getFixTime().getTime() - previousPosition.getFixTime().getTime()); } @@ -63,7 +60,7 @@ public final class Summary { result.setMaxSpeed(position.getSpeed()); } boolean ignoreOdometer = Context.getDeviceManager() - .lookupConfigBoolean(deviceId, "report.ignoreOdometer", false); + .lookupAttributeBoolean(deviceId, "report.ignoreOdometer", false, true); result.setDistance(ReportUtils.calculateDistance(firstPosition, previousPosition, !ignoreOdometer)); result.setAverageSpeed(speedSum / positions.size()); } @@ -82,8 +79,8 @@ public final class Summary { public static void getExcel(OutputStream outputStream, long userId, Collection<Long> deviceIds, Collection<Long> groupIds, - Date from, Date to) throws SQLException, IOException { - Collection<SummaryReport> summaries = getObjects(userId, deviceIds, groupIds, from, to); + DateTime from, DateTime to) throws SQLException, IOException { + Collection<SummaryReport> summaries = getObjects(userId, deviceIds, groupIds, from.toDate(), to.toDate()); String templatePath = Context.getConfig().getString("report.templatesPath", "templates/export/"); try (InputStream inputStream = new FileInputStream(templatePath + "/summary.xlsx")) { @@ -93,6 +90,7 @@ public final class Summary { jxlsContext.putVar("to", to); jxlsContext.putVar("distanceUnit", ReportUtils.getDistanceUnit(userId)); jxlsContext.putVar("speedUnit", ReportUtils.getSpeedUnit(userId)); + jxlsContext.putVar("timezone", from.getZone()); JxlsHelper.getInstance().setUseFastFormulaProcessor(false) .processTemplate(inputStream, outputStream, jxlsContext); } diff --git a/src/org/traccar/reports/Trips.java b/src/org/traccar/reports/Trips.java index 91a080d45..d4e25f5e5 100644 --- a/src/org/traccar/reports/Trips.java +++ b/src/org/traccar/reports/Trips.java @@ -26,6 +26,7 @@ import java.util.Collection; import java.util.Date; import java.util.List; +import org.joda.time.DateTime; import org.jxls.area.Area; import org.jxls.builder.xls.XlsCommentAreaBuilder; import org.jxls.common.CellRef; @@ -79,7 +80,7 @@ public final class Trips { trip.setEndAddress(endTrip.getAddress()); boolean ignoreOdometer = Context.getDeviceManager() - .lookupConfigBoolean(deviceId, "report.ignoreOdometer", false); + .lookupAttributeBoolean(deviceId, "report.ignoreOdometer", false, true); trip.setDistance(ReportUtils.calculateDistance(startTrip, endTrip, !ignoreOdometer)); trip.setDuration(tripDuration); trip.setAverageSpeed(speedSum / (endIndex - startIndex)); @@ -175,12 +176,12 @@ public final class Trips { public static void getExcel(OutputStream outputStream, long userId, Collection<Long> deviceIds, Collection<Long> groupIds, - Date from, Date to) throws SQLException, IOException { + DateTime from, DateTime to) throws SQLException, IOException { ArrayList<DeviceReport> devicesTrips = new ArrayList<>(); ArrayList<String> sheetNames = new ArrayList<>(); for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); - Collection<TripReport> trips = detectTrips(deviceId, from, to); + Collection<TripReport> trips = detectTrips(deviceId, from.toDate(), to.toDate()); DeviceReport deviceTrips = new DeviceReport(); Device device = Context.getIdentityManager().getDeviceById(deviceId); deviceTrips.setDeviceName(device.getName()); @@ -204,6 +205,7 @@ public final class Trips { jxlsContext.putVar("to", to); jxlsContext.putVar("distanceUnit", ReportUtils.getDistanceUnit(userId)); jxlsContext.putVar("speedUnit", ReportUtils.getSpeedUnit(userId)); + jxlsContext.putVar("timezone", from.getZone()); Transformer transformer = TransformerFactory.createTransformer(inputStream, outputStream); List<Area> xlsAreas = new XlsCommentAreaBuilder(transformer).build(); for (Area xlsArea : xlsAreas) { |