Search in sources :

Example 6 with Device

use of org.traccar.model.Device in project traccar by tananaev.

the class IgnitionEventHandler method analyzePosition.

@Override
protected Map<Event, Position> analyzePosition(Position position) {
    Device device = Context.getIdentityManager().getById(position.getDeviceId());
    if (device == null || !Context.getIdentityManager().isLatestPosition(position)) {
        return null;
    }
    Map<Event, Position> result = null;
    if (position.getAttributes().containsKey(Position.KEY_IGNITION)) {
        boolean ignition = position.getBoolean(Position.KEY_IGNITION);
        Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId());
        if (lastPosition != null && lastPosition.getAttributes().containsKey(Position.KEY_IGNITION)) {
            boolean oldIgnition = lastPosition.getBoolean(Position.KEY_IGNITION);
            if (ignition && !oldIgnition) {
                result = Collections.singletonMap(new Event(Event.TYPE_IGNITION_ON, position.getDeviceId(), position.getId()), position);
            } else if (!ignition && oldIgnition) {
                result = Collections.singletonMap(new Event(Event.TYPE_IGNITION_OFF, position.getDeviceId(), position.getId()), position);
            }
        }
    }
    return result;
}
Also used : Position(org.traccar.model.Position) Device(org.traccar.model.Device) Event(org.traccar.model.Event)

Example 7 with Device

use of org.traccar.model.Device in project traccar by tananaev.

the class TextMessageEventHandler method handleTextMessage.

public static void handleTextMessage(String phone, String message) {
    Device device = Context.getDeviceManager().getDeviceByPhone(phone);
    if (device != null && Context.getNotificationManager() != null) {
        Event event = new Event(Event.TYPE_TEXT_MESSAGE, device.getId());
        event.set("message", message);
        Context.getNotificationManager().updateEvent(event, null);
    }
}
Also used : Device(org.traccar.model.Device) Event(org.traccar.model.Event)

Example 8 with Device

use of org.traccar.model.Device in project traccar by tananaev.

the class EventForwarder method prepareJsonPayload.

protected String prepareJsonPayload(Event event, Position position, Set<Long> users) {
    Map<String, Object> data = new HashMap<>();
    data.put(KEY_EVENT, event);
    if (position != null) {
        data.put(KEY_POSITION, position);
    }
    Device device = Context.getIdentityManager().getById(event.getDeviceId());
    if (device != null) {
        data.put(KEY_DEVICE, device);
    }
    if (event.getGeofenceId() != 0) {
        Geofence geofence = Context.getGeofenceManager().getById(event.getGeofenceId());
        if (geofence != null) {
            data.put(KEY_GEOFENCE, geofence);
        }
    }
    data.put(KEY_USERS, Context.getUsersManager().getItems(users));
    try {
        return Context.getObjectMapper().writeValueAsString(data);
    } catch (JsonProcessingException e) {
        Log.warning(e);
        return null;
    }
}
Also used : HashMap(java.util.HashMap) Device(org.traccar.model.Device) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Geofence(org.traccar.model.Geofence)

Example 9 with Device

use of org.traccar.model.Device in project traccar by tananaev.

the class NotificationFormatter method prepareContext.

public static VelocityContext prepareContext(long userId, Event event, Position position) {
    User user = Context.getPermissionsManager().getUser(userId);
    Device device = Context.getIdentityManager().getById(event.getDeviceId());
    VelocityContext velocityContext = new VelocityContext();
    velocityContext.put("user", user);
    velocityContext.put("device", device);
    velocityContext.put("event", event);
    if (position != null) {
        velocityContext.put("position", position);
        velocityContext.put("speedUnit", ReportUtils.getSpeedUnit(userId));
        velocityContext.put("distanceUnit", ReportUtils.getDistanceUnit(userId));
        velocityContext.put("volumeUnit", ReportUtils.getVolumeUnit(userId));
    }
    if (event.getGeofenceId() != 0) {
        velocityContext.put("geofence", Context.getGeofenceManager().getById(event.getGeofenceId()));
    }
    String driverUniqueId = event.getString(Position.KEY_DRIVER_UNIQUE_ID);
    if (driverUniqueId != null) {
        velocityContext.put("driver", Context.getDriversManager().getDriverByUniqueId(driverUniqueId));
    }
    velocityContext.put("webUrl", Context.getVelocityEngine().getProperty("web.url"));
    velocityContext.put("dateTool", new DateTool());
    velocityContext.put("numberTool", new NumberTool());
    velocityContext.put("timezone", ReportUtils.getTimezone(userId));
    velocityContext.put("locale", Locale.getDefault());
    return velocityContext;
}
Also used : NumberTool(org.apache.velocity.tools.generic.NumberTool) User(org.traccar.model.User) DateTool(org.apache.velocity.tools.generic.DateTool) Device(org.traccar.model.Device) VelocityContext(org.apache.velocity.VelocityContext)

Example 10 with Device

use of org.traccar.model.Device in project traccar by tananaev.

the class ComputedAttributesHandler method prepareContext.

private MapContext prepareContext(Position position) {
    MapContext result = new MapContext();
    if (mapDeviceAttributes) {
        Device device = Context.getIdentityManager().getById(position.getDeviceId());
        if (device != null) {
            for (Object key : device.getAttributes().keySet()) {
                result.set((String) key, device.getAttributes().get(key));
            }
        }
    }
    Set<Method> methods = new HashSet<>(Arrays.asList(position.getClass().getMethods()));
    methods.removeAll(Arrays.asList(Object.class.getMethods()));
    for (Method method : methods) {
        if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
            String name = Character.toLowerCase(method.getName().charAt(3)) + method.getName().substring(4);
            try {
                if (!method.getReturnType().equals(Map.class)) {
                    result.set(name, method.invoke(position));
                } else {
                    for (Object key : ((Map) method.invoke(position)).keySet()) {
                        result.set((String) key, ((Map) method.invoke(position)).get(key));
                    }
                }
            } catch (IllegalAccessException | InvocationTargetException error) {
                Log.warning(error);
            }
        }
    }
    return result;
}
Also used : Device(org.traccar.model.Device) MapContext(org.apache.commons.jexl2.MapContext) Method(java.lang.reflect.Method) Map(java.util.Map) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashSet(java.util.HashSet)

Aggregations

Device (org.traccar.model.Device)33 Position (org.traccar.model.Position)10 Event (org.traccar.model.Event)9 Group (org.traccar.model.Group)7 SQLException (java.sql.SQLException)6 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 DeviceReport (org.traccar.reports.model.DeviceReport)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 HashSet (java.util.HashSet)3 Map (java.util.Map)2 DeviceState (org.traccar.model.DeviceState)2 Geofence (org.traccar.model.Geofence)2 Permission (org.traccar.model.Permission)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 InetSocketAddress (java.net.InetSocketAddress)1