Search in sources :

Example 1 with Maintenance

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

use of org.traccar.model.Maintenance 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)

Example 3 with Maintenance

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

the class MaintenanceEventHandler method analyzePosition.

@Override
protected Map<Event, Position> analyzePosition(Position position) {
    if (identityManager.getById(position.getDeviceId()) == null || !identityManager.isLatestPosition(position)) {
        return null;
    }
    Position lastPosition = identityManager.getLastPosition(position.getDeviceId());
    if (lastPosition == null) {
        return null;
    }
    Map<Event, Position> events = new HashMap<>();
    for (long maintenanceId : maintenancesManager.getAllDeviceItems(position.getDeviceId())) {
        Maintenance maintenance = maintenancesManager.getById(maintenanceId);
        if (maintenance.getPeriod() != 0) {
            double oldValue = lastPosition.getDouble(maintenance.getType());
            double newValue = position.getDouble(maintenance.getType());
            if (oldValue != 0.0 && newValue != 0.0 && (long) ((oldValue - maintenance.getStart()) / maintenance.getPeriod()) < (long) ((newValue - maintenance.getStart()) / maintenance.getPeriod())) {
                Event event = new Event(Event.TYPE_MAINTENANCE, position);
                event.setMaintenanceId(maintenanceId);
                event.set(maintenance.getType(), newValue);
                events.put(event, position);
            }
        }
    }
    return events;
}
Also used : Position(org.traccar.model.Position) HashMap(java.util.HashMap) Event(org.traccar.model.Event) Maintenance(org.traccar.model.Maintenance)

Aggregations

HashMap (java.util.HashMap)3 Maintenance (org.traccar.model.Maintenance)3 Device (org.traccar.model.Device)2 Event (org.traccar.model.Event)2 Geofence (org.traccar.model.Geofence)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Group (org.traccar.model.Group)1 Position (org.traccar.model.Position)1 DeviceReport (org.traccar.reports.model.DeviceReport)1