Search in sources :

Example 1 with StopReport

use of org.traccar.reports.model.StopReport in project traccar by tananaev.

the class ReportUtils method calculateStop.

private static StopReport calculateStop(ArrayList<Position> positions, int startIndex, int endIndex) {
    Position startStop = positions.get(startIndex);
    Position endStop = positions.get(endIndex);
    StopReport stop = new StopReport();
    long deviceId = startStop.getDeviceId();
    stop.setDeviceId(deviceId);
    stop.setDeviceName(Context.getIdentityManager().getById(deviceId).getName());
    stop.setPositionId(startStop.getId());
    stop.setLatitude(startStop.getLatitude());
    stop.setLongitude(startStop.getLongitude());
    stop.setStartTime(startStop.getFixTime());
    String address = startStop.getAddress();
    if (address == null && Context.getGeocoder() != null && Context.getConfig().getBoolean("geocoder.onRequest")) {
        address = Context.getGeocoder().getAddress(stop.getLatitude(), stop.getLongitude(), null);
    }
    stop.setAddress(address);
    stop.setEndTime(endStop.getFixTime());
    long stopDuration = endStop.getFixTime().getTime() - startStop.getFixTime().getTime();
    stop.setDuration(stopDuration);
    stop.setSpentFuel(calculateFuel(startStop, endStop));
    long engineHours = 0;
    for (int i = startIndex + 1; i <= endIndex; i++) {
        if (positions.get(i).getBoolean(Position.KEY_IGNITION) && positions.get(i - 1).getBoolean(Position.KEY_IGNITION)) {
            engineHours += positions.get(i).getFixTime().getTime() - positions.get(i - 1).getFixTime().getTime();
        }
    }
    stop.setEngineHours(engineHours);
    return stop;
}
Also used : Position(org.traccar.model.Position) StopReport(org.traccar.reports.model.StopReport)

Example 2 with StopReport

use of org.traccar.reports.model.StopReport in project traccar by tananaev.

the class Stops method getExcel.

public static void getExcel(OutputStream outputStream, long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Date from, Date to) throws SQLException, IOException {
    ReportUtils.checkPeriodLimit(from, to);
    ArrayList<DeviceReport> devicesStops = new ArrayList<>();
    ArrayList<String> sheetNames = new ArrayList<>();
    for (long deviceId : ReportUtils.getDeviceList(deviceIds, groupIds)) {
        Context.getPermissionsManager().checkDevice(userId, deviceId);
        Collection<StopReport> stops = detectStops(deviceId, from, to);
        DeviceReport deviceStops = new DeviceReport();
        Device device = Context.getIdentityManager().getById(deviceId);
        deviceStops.setDeviceName(device.getName());
        sheetNames.add(WorkbookUtil.createSafeSheetName(deviceStops.getDeviceName()));
        if (device.getGroupId() != 0) {
            Group group = Context.getGroupsManager().getById(device.getGroupId());
            if (group != null) {
                deviceStops.setGroupName(group.getName());
            }
        }
        deviceStops.setObjects(stops);
        devicesStops.add(deviceStops);
    }
    String templatePath = Context.getConfig().getString("report.templatesPath", "templates/export/");
    try (InputStream inputStream = new FileInputStream(templatePath + "/stops.xlsx")) {
        org.jxls.common.Context jxlsContext = ReportUtils.initializeContext(userId);
        jxlsContext.putVar("devices", devicesStops);
        jxlsContext.putVar("sheetNames", sheetNames);
        jxlsContext.putVar("from", from);
        jxlsContext.putVar("to", to);
        ReportUtils.processTemplateWithSheets(inputStream, outputStream, jxlsContext);
    }
}
Also used : Group(org.traccar.model.Group) Device(org.traccar.model.Device) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) StopReport(org.traccar.reports.model.StopReport) FileInputStream(java.io.FileInputStream) DeviceReport(org.traccar.reports.model.DeviceReport)

Example 3 with StopReport

use of org.traccar.reports.model.StopReport in project traccar by tananaev.

the class ReportUtilsTest method testDetectStopsWithTripCut.

@Test
public void testDetectStopsWithTripCut() throws ParseException {
    Collection<Position> data = Arrays.asList(position("2016-01-01 00:00:00.000", 0, 0), position("2016-01-01 00:01:00.000", 0, 0), position("2016-01-01 00:02:00.000", 0, 0), position("2016-01-01 00:03:00.000", 0, 0), position("2016-01-01 00:04:00.000", 1, 0), position("2016-01-01 00:05:00.000", 2, 0));
    TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01);
    Collection<StopReport> result = ReportUtils.detectTripsAndStops(data, tripsConfig, false, StopReport.class);
    assertNotNull(result);
    assertFalse(result.isEmpty());
    StopReport itemStop = result.iterator().next();
    assertEquals(date("2016-01-01 00:00:00.000"), itemStop.getStartTime());
    assertEquals(date("2016-01-01 00:04:00.000"), itemStop.getEndTime());
    assertEquals(240000, itemStop.getDuration());
}
Also used : Position(org.traccar.model.Position) TripsConfig(org.traccar.reports.model.TripsConfig) StopReport(org.traccar.reports.model.StopReport) Test(org.junit.Test) BaseTest(org.traccar.BaseTest)

Example 4 with StopReport

use of org.traccar.reports.model.StopReport in project traccar by tananaev.

the class ReportUtilsTest method testDetectTripsSimple.

@Test
public void testDetectTripsSimple() throws ParseException {
    List<Position> data = Arrays.asList(position("2016-01-01 00:00:00.000", 0, 0), position("2016-01-01 00:01:00.000", 0, 0), position("2016-01-01 00:02:00.000", 10, 0), position("2016-01-01 00:03:00.000", 10, 1000), position("2016-01-01 00:04:00.000", 10, 2000), position("2016-01-01 00:05:00.000", 0, 3000), position("2016-01-01 00:06:00.000", 0, 3000), position("2016-01-01 00:07:00.000", 0, 3000));
    TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, 900000, false, false, 0.01);
    Collection<TripReport> trips = ReportUtils.detectTripsAndStops(data, tripsConfig, false, TripReport.class);
    assertNotNull(trips);
    assertFalse(trips.isEmpty());
    TripReport itemTrip = trips.iterator().next();
    assertEquals(date("2016-01-01 00:02:00.000"), itemTrip.getStartTime());
    assertEquals(date("2016-01-01 00:05:00.000"), itemTrip.getEndTime());
    assertEquals(180000, itemTrip.getDuration());
    assertEquals(10, itemTrip.getAverageSpeed(), 0.01);
    assertEquals(10, itemTrip.getMaxSpeed(), 0.01);
    assertEquals(3000, itemTrip.getDistance(), 0.01);
    Collection<StopReport> stops = ReportUtils.detectTripsAndStops(data, tripsConfig, false, StopReport.class);
    assertNotNull(stops);
    assertFalse(stops.isEmpty());
    Iterator<StopReport> iterator = stops.iterator();
    StopReport itemStop = iterator.next();
    assertEquals(date("2016-01-01 00:00:00.000"), itemStop.getStartTime());
    assertEquals(date("2016-01-01 00:02:00.000"), itemStop.getEndTime());
    assertEquals(120000, itemStop.getDuration());
    itemStop = iterator.next();
    assertEquals(date("2016-01-01 00:05:00.000"), itemStop.getStartTime());
    assertEquals(date("2016-01-01 00:07:00.000"), itemStop.getEndTime());
    assertEquals(120000, itemStop.getDuration());
}
Also used : TripReport(org.traccar.reports.model.TripReport) Position(org.traccar.model.Position) TripsConfig(org.traccar.reports.model.TripsConfig) StopReport(org.traccar.reports.model.StopReport) Test(org.junit.Test) BaseTest(org.traccar.BaseTest)

Example 5 with StopReport

use of org.traccar.reports.model.StopReport in project traccar by tananaev.

the class ReportUtilsTest method testDetectStopsStartedFromTrip.

@Test
public void testDetectStopsStartedFromTrip() throws ParseException {
    Collection<Position> data = Arrays.asList(position("2016-01-01 00:00:00.000", 2, 0), position("2016-01-01 00:01:00.000", 1, 0), position("2016-01-01 00:02:00.000", 0, 0), position("2016-01-01 00:03:00.000", 0, 0), position("2016-01-01 00:04:00.000", 0, 0), position("2016-01-01 00:05:00.000", 0, 0));
    TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01);
    Collection<StopReport> result = ReportUtils.detectTripsAndStops(data, tripsConfig, false, StopReport.class);
    assertNotNull(result);
    assertFalse(result.isEmpty());
    StopReport itemStop = result.iterator().next();
    assertEquals(date("2016-01-01 00:02:00.000"), itemStop.getStartTime());
    assertEquals(date("2016-01-01 00:05:00.000"), itemStop.getEndTime());
    assertEquals(180000, itemStop.getDuration());
}
Also used : Position(org.traccar.model.Position) TripsConfig(org.traccar.reports.model.TripsConfig) StopReport(org.traccar.reports.model.StopReport) Test(org.junit.Test) BaseTest(org.traccar.BaseTest)

Aggregations

StopReport (org.traccar.reports.model.StopReport)10 Position (org.traccar.model.Position)9 Test (org.junit.Test)8 BaseTest (org.traccar.BaseTest)8 TripsConfig (org.traccar.reports.model.TripsConfig)8 TripReport (org.traccar.reports.model.TripReport)4 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Device (org.traccar.model.Device)1 Group (org.traccar.model.Group)1 DeviceReport (org.traccar.reports.model.DeviceReport)1