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;
}
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);
}
}
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;
}
Aggregations