Search in sources :

Example 6 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 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)

Example 7 with Geofence

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

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 8 with Geofence

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

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)

Aggregations

Device (org.traccar.model.Device)8 Geofence (org.traccar.model.Geofence)8 HashMap (java.util.HashMap)6 Event (org.traccar.model.Event)4 Maintenance (org.traccar.model.Maintenance)4 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 Position (org.traccar.model.Position)2 DeviceReport (org.traccar.reports.model.DeviceReport)2 DeviceState (org.traccar.model.DeviceState)1 DeviceState (org.traccar.session.DeviceState)1