Search in sources :

Example 1 with Geofence

use of org.traccar.model.Geofence 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 2 with Geofence

use of org.traccar.model.Geofence in project traccar by traccar.

the class Events method getExcel.

public static void getExcel(OutputStream outputStream, long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Collection<String> types, Date from, Date to) throws SQLException, IOException {
    ReportUtils.checkPeriodLimit(from, to);
    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);
        boolean all = types.isEmpty() || types.contains(Event.ALL_EVENTS);
        for (Iterator<Event> iterator = events.iterator(); iterator.hasNext(); ) {
            Event event = iterator.next();
            if (all || types.contains(event.getType())) {
                long geofenceId = event.getGeofenceId();
                if (geofenceId != 0) {
                    if (Context.getGeofenceManager().checkItemPermission(userId, geofenceId)) {
                        Geofence geofence = Context.getGeofenceManager().getById(geofenceId);
                        if (geofence != null) {
                            geofenceNames.put(geofenceId, geofence.getName());
                        }
                    } else {
                        iterator.remove();
                    }
                }
            } else {
                iterator.remove();
            }
        }
        DeviceReport deviceEvents = new DeviceReport();
        Device device = Context.getIdentityManager().getById(deviceId);
        deviceEvents.setDeviceName(device.getName());
        sheetNames.add(WorkbookUtil.createSafeSheetName(deviceEvents.getDeviceName()));
        if (device.getGroupId() != 0) {
            Group group = Context.getGroupsManager().getById(device.getGroupId());
            if (group != null) {
                deviceEvents.setGroupName(group.getName());
            }
        }
        deviceEvents.setObjects(events);
        devicesEvents.add(deviceEvents);
    }
    String templatePath = Context.getConfig().getString("report.templatesPath", "templates/export/");
    try (InputStream inputStream = new FileInputStream(templatePath + "/events.xlsx")) {
        org.jxls.common.Context jxlsContext = ReportUtils.initializeContext(userId);
        jxlsContext.putVar("devices", devicesEvents);
        jxlsContext.putVar("sheetNames", sheetNames);
        jxlsContext.putVar("geofenceNames", geofenceNames);
        jxlsContext.putVar("from", from);
        jxlsContext.putVar("to", to);
        ReportUtils.processTemplateWithSheets(inputStream, outputStream, jxlsContext);
    }
}
Also used : Group(org.traccar.model.Group) HashMap(java.util.HashMap) Device(org.traccar.model.Device) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) Event(org.traccar.model.Event) DeviceReport(org.traccar.reports.model.DeviceReport) Geofence(org.traccar.model.Geofence)

Example 3 with Geofence

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

the class OverspeedEventHandler method analyzePosition.

@Override
protected Map<Event, Position> analyzePosition(Position position) {
    long deviceId = position.getDeviceId();
    Device device = deviceManager.getById(deviceId);
    if (device == null) {
        return null;
    }
    if (!deviceManager.isLatestPosition(position) || !position.getValid()) {
        return null;
    }
    double speedLimit = deviceManager.lookupAttributeDouble(deviceId, ATTRIBUTE_SPEED_LIMIT, 0, true, false);
    double positionSpeedLimit = position.getDouble(Position.KEY_SPEED_LIMIT);
    if (positionSpeedLimit > 0) {
        speedLimit = positionSpeedLimit;
    }
    double geofenceSpeedLimit = 0;
    long overspeedGeofenceId = 0;
    if (geofenceManager != null && device.getGeofenceIds() != null) {
        for (long geofenceId : device.getGeofenceIds()) {
            Geofence geofence = geofenceManager.getById(geofenceId);
            if (geofence != null) {
                double currentSpeedLimit = geofence.getDouble(ATTRIBUTE_SPEED_LIMIT);
                if (currentSpeedLimit > 0 && geofenceSpeedLimit == 0 || preferLowest && currentSpeedLimit < geofenceSpeedLimit || !preferLowest && currentSpeedLimit > geofenceSpeedLimit) {
                    geofenceSpeedLimit = currentSpeedLimit;
                    overspeedGeofenceId = geofenceId;
                }
            }
        }
    }
    if (geofenceSpeedLimit > 0) {
        speedLimit = geofenceSpeedLimit;
    }
    if (speedLimit == 0) {
        return null;
    }
    Map<Event, Position> result = null;
    DeviceState deviceState = deviceManager.getDeviceState(deviceId);
    if (deviceState.getOverspeedState() == null) {
        deviceState.setOverspeedState(position.getSpeed() > speedLimit);
        deviceState.setOverspeedGeofenceId(position.getSpeed() > speedLimit ? overspeedGeofenceId : 0);
    } else {
        result = updateOverspeedState(deviceState, position, speedLimit, overspeedGeofenceId);
    }
    deviceManager.setDeviceState(deviceId, deviceState);
    return result;
}
Also used : DeviceState(org.traccar.model.DeviceState) Position(org.traccar.model.Position) Device(org.traccar.model.Device) Event(org.traccar.model.Event) Geofence(org.traccar.model.Geofence)

Example 4 with Geofence

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

the class EventForwarder method preparePayload.

protected Map<String, Object> preparePayload(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);
        }
    }
    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));
    return data;
}
Also used : HashMap(java.util.HashMap) Device(org.traccar.model.Device) Maintenance(org.traccar.model.Maintenance) Geofence(org.traccar.model.Geofence)

Example 5 with Geofence

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

the class Events method getExcel.

public static void getExcel(OutputStream outputStream, long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Collection<String> types, Date from, Date to) throws StorageException, IOException {
    ReportUtils.checkPeriodLimit(from, to);
    ArrayList<DeviceReport> devicesEvents = new ArrayList<>();
    ArrayList<String> sheetNames = new ArrayList<>();
    HashMap<Long, String> geofenceNames = new HashMap<>();
    HashMap<Long, String> maintenanceNames = new HashMap<>();
    for (long deviceId : ReportUtils.getDeviceList(deviceIds, groupIds)) {
        Context.getPermissionsManager().checkDevice(userId, deviceId);
        Collection<Event> events = Context.getDataManager().getEvents(deviceId, from, to);
        boolean all = types.isEmpty() || types.contains(Event.ALL_EVENTS);
        for (Iterator<Event> iterator = events.iterator(); iterator.hasNext(); ) {
            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);
                        if (geofence != null) {
                            geofenceNames.put(geofenceId, geofence.getName());
                        }
                    } 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();
            }
        }
        DeviceReport deviceEvents = new DeviceReport();
        Device device = Context.getIdentityManager().getById(deviceId);
        deviceEvents.setDeviceName(device.getName());
        sheetNames.add(WorkbookUtil.createSafeSheetName(deviceEvents.getDeviceName()));
        if (device.getGroupId() != 0) {
            Group group = Context.getGroupsManager().getById(device.getGroupId());
            if (group != null) {
                deviceEvents.setGroupName(group.getName());
            }
        }
        deviceEvents.setObjects(events);
        devicesEvents.add(deviceEvents);
    }
    String templatePath = Context.getConfig().getString("report.templatesPath", "templates/export/");
    try (InputStream inputStream = new FileInputStream(templatePath + "/events.xlsx")) {
        org.jxls.common.Context jxlsContext = ReportUtils.initializeContext(userId);
        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);
    }
}
Also used : Group(org.traccar.model.Group) HashMap(java.util.HashMap) Device(org.traccar.model.Device) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Maintenance(org.traccar.model.Maintenance) FileInputStream(java.io.FileInputStream) Event(org.traccar.model.Event) DeviceReport(org.traccar.reports.model.DeviceReport) Geofence(org.traccar.model.Geofence)

Aggregations

Device (org.traccar.model.Device)6 Geofence (org.traccar.model.Geofence)6 HashMap (java.util.HashMap)5 Event (org.traccar.model.Event)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 Group (org.traccar.model.Group)2 Maintenance (org.traccar.model.Maintenance)2 DeviceReport (org.traccar.reports.model.DeviceReport)2 DeviceState (org.traccar.model.DeviceState)1 Position (org.traccar.model.Position)1