From c613143105d9f7c898dd3fb236d683b76cf8a42c Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 17 Oct 2016 14:02:23 +0500 Subject: - Implement export reports to excel - Migrate csv export to /api/positions --- src/org/traccar/api/resource/PositionResource.java | 24 +++++- src/org/traccar/api/resource/ReportResource.java | 70 +++++++++-------- src/org/traccar/reports/Events.java | 87 ++++++++++++++++++++-- src/org/traccar/reports/Route.java | 62 +++++++++++++-- src/org/traccar/reports/Summary.java | 29 ++++++-- src/org/traccar/reports/Trips.java | 68 +++++++++++++++-- src/org/traccar/reports/model/DeviceReport.java | 49 ++++++++++++ src/org/traccar/reports/model/TripReport.java | 48 ++++++++++++ 8 files changed, 377 insertions(+), 60 deletions(-) create mode 100644 src/org/traccar/reports/model/DeviceReport.java (limited to 'src/org') diff --git a/src/org/traccar/api/resource/PositionResource.java b/src/org/traccar/api/resource/PositionResource.java index e00e06e7a..92b62c0a6 100644 --- a/src/org/traccar/api/resource/PositionResource.java +++ b/src/org/traccar/api/resource/PositionResource.java @@ -18,6 +18,7 @@ package org.traccar.api.resource; import org.traccar.Context; import org.traccar.api.BaseResource; import org.traccar.model.Position; +import org.traccar.web.CsvBuilder; import org.traccar.web.JsonConverter; import javax.ws.rs.Consumes; @@ -25,17 +26,23 @@ import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + import java.sql.SQLException; import java.util.Collection; @Path("positions") -@Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public class PositionResource extends BaseResource { + public static final String TEXT_CSV = "text/csv"; + public static final String CONTENT_DISPOSITION_VALUE_CSV = "attachment; filename=positions.csv"; + @GET - public Collection get( + @Produces(MediaType.APPLICATION_JSON) + public Collection getJson( @QueryParam("deviceId") long deviceId, @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { if (deviceId == 0) { @@ -47,4 +54,17 @@ public class PositionResource extends BaseResource { } } + @GET + @Produces(TEXT_CSV) + public Response getCsv( + @QueryParam("deviceId") long deviceId, @QueryParam("from") String from, @QueryParam("to") String to) + throws SQLException { + Context.getPermissionsManager().checkDevice(getUserId(), deviceId); + CsvBuilder csv = new CsvBuilder(); + csv.addHeaderLine(new Position()); + csv.addArray(Context.getDataManager().getPositions( + deviceId, JsonConverter.parseDate(from), JsonConverter.parseDate(to))); + return Response.ok(csv.build()).header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_CSV).build(); + } + } diff --git a/src/org/traccar/api/resource/ReportResource.java b/src/org/traccar/api/resource/ReportResource.java index 0dd0452ff..709eef377 100644 --- a/src/org/traccar/api/resource/ReportResource.java +++ b/src/org/traccar/api/resource/ReportResource.java @@ -1,5 +1,7 @@ package org.traccar.api.resource; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.sql.SQLException; import java.util.List; @@ -23,8 +25,8 @@ import org.traccar.web.JsonConverter; @Consumes(MediaType.APPLICATION_JSON) public class ReportResource extends BaseResource { - public static final String TEXT_CSV = "text/csv"; - public static final String CONTENT_DISPOSITION_VALUE = "attachment; filename=report.csv"; + private static final String XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; + private static final String CONTENT_DISPOSITION_VALUE_XLSX = "attachment; filename=report.xlsx"; @Path("route") @GET @@ -38,14 +40,16 @@ public class ReportResource extends BaseResource { @Path("route") @GET - @Produces(TEXT_CSV) - public Response getRouteCsv( + @Produces(XLSX) + public Response getRouteExcel( @QueryParam("deviceId") final List deviceIds, @QueryParam("groupId") final List groupIds, - @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { - return Response.ok(Route.getCsv(getUserId(), deviceIds, groupIds, - JsonConverter.parseDate(from), JsonConverter.parseDate(to))) - .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE) - .build(); + @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException, IOException { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + Route.getExcel(stream, getUserId(), deviceIds, groupIds, + JsonConverter.parseDate(from), JsonConverter.parseDate(to)); + + return Response.ok(stream.toByteArray()) + .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build(); } @Path("events") @@ -61,15 +65,17 @@ public class ReportResource extends BaseResource { @Path("events") @GET - @Produces(TEXT_CSV) - public Response getEventsCsv( + @Produces(XLSX) + public Response getEventsExcel( @QueryParam("deviceId") final List deviceIds, @QueryParam("groupId") final List groupIds, @QueryParam("type") final List types, - @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { - return Response.ok(Events.getCsv(getUserId(), deviceIds, groupIds, - types, JsonConverter.parseDate(from), JsonConverter.parseDate(to))) - .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE) - .build(); + @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException, IOException { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + Events.getExcel(stream, getUserId(), deviceIds, groupIds, types, + JsonConverter.parseDate(from), JsonConverter.parseDate(to)); + + return Response.ok(stream.toByteArray()) + .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build(); } @Path("summary") @@ -84,14 +90,16 @@ public class ReportResource extends BaseResource { @Path("summary") @GET - @Produces(TEXT_CSV) - public Response getSummaryCsv( + @Produces(XLSX) + public Response getSummaryExcel( @QueryParam("deviceId") final List deviceIds, @QueryParam("groupId") final List groupIds, - @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { - return Response.ok(Summary.getCsv(getUserId(), deviceIds, groupIds, - JsonConverter.parseDate(from), JsonConverter.parseDate(to))) - .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE) - .build(); + @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException, IOException { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + Summary.getExcel(stream, getUserId(), deviceIds, groupIds, + JsonConverter.parseDate(from), JsonConverter.parseDate(to)); + + return Response.ok(stream.toByteArray()) + .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build(); } @Path("trips") @@ -106,14 +114,16 @@ public class ReportResource extends BaseResource { @Path("trips") @GET - @Produces(TEXT_CSV) - public Response getTripsCsv( + @Produces(XLSX) + public Response getTripsExcel( @QueryParam("deviceId") final List deviceIds, @QueryParam("groupId") final List groupIds, - @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { - return Response.ok(Trips.getCsv(getUserId(), deviceIds, groupIds, - JsonConverter.parseDate(from), JsonConverter.parseDate(to))) - .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE) - .build(); + @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException, IOException { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + Trips.getExcel(stream, getUserId(), deviceIds, groupIds, + JsonConverter.parseDate(from), JsonConverter.parseDate(to)); + + return Response.ok(stream.toByteArray()) + .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build(); } } diff --git a/src/org/traccar/reports/Events.java b/src/org/traccar/reports/Events.java index ed7e9e411..9f58e1bf4 100644 --- a/src/org/traccar/reports/Events.java +++ b/src/org/traccar/reports/Events.java @@ -16,16 +16,36 @@ */ package org.traccar.reports; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Collection; +import java.util.Comparator; import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.SortedSet; +import java.util.TreeSet; import javax.json.Json; import javax.json.JsonArrayBuilder; +import org.jxls.area.Area; +import org.jxls.builder.xls.XlsCommentAreaBuilder; +import org.jxls.common.CellRef; +import org.jxls.formula.StandardFormulaProcessor; +import org.jxls.transform.Transformer; +import org.jxls.transform.poi.PoiTransformer; +import org.jxls.util.TransformerFactory; import org.traccar.Context; +import org.traccar.model.Device; import org.traccar.model.Event; -import org.traccar.web.CsvBuilder; +import org.traccar.model.Geofence; +import org.traccar.model.Group; +import org.traccar.reports.model.DeviceReport; import org.traccar.web.JsonConverter; public final class Events { @@ -47,16 +67,69 @@ public final class Events { return json.build().toString(); } - public static String getCsv(long userId, Collection deviceIds, Collection groupIds, - Collection types, Date from, Date to) throws SQLException { - CsvBuilder csv = new CsvBuilder(); - csv.addHeaderLine(new Event()); + public static void getExcel(OutputStream outputStream, + long userId, Collection deviceIds, Collection groupIds, + Collection types, Date from, Date to) throws SQLException, IOException { + ArrayList devicesEvents = new ArrayList<>(); + ArrayList sheetNames = new ArrayList<>(); + HashMap geofenceNames = new HashMap<>(); for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); + SortedSet eventReports = new TreeSet(new Comparator() { + @Override + public int compare(Event e1, Event e2) { + return e1.getServerTime().compareTo(e2.getServerTime()); + } + }); for (String type : types) { - csv.addArray(Context.getDataManager().getEvents(deviceId, type, from, to)); + Collection events = Context.getDataManager().getEvents(deviceId, type, from, to); + for (Event event : events) { + long geofenceId = event.getGeofenceId(); + if (geofenceId != 0) { + Geofence geofence = Context.getGeofenceManager().getGeofence(geofenceId); + if (geofence != null) { + geofenceNames.put(geofenceId, geofence.getName()); + } else { + geofenceNames.put(geofenceId, Long.toString(geofenceId)); + } + } + eventReports.add(event); + } + } + if (!eventReports.isEmpty()) { + DeviceReport deviceEvents = new DeviceReport(); + Device device = Context.getIdentityManager().getDeviceById(deviceId); + deviceEvents.setDeviceName(device.getName()); + sheetNames.add(deviceEvents.getDeviceName()); + if (device.getGroupId() != 0) { + Group group = Context.getDeviceManager().getGroupById(device.getGroupId()); + if (group != null) { + deviceEvents.setGroupName(group.getName()); + } + } + deviceEvents.setObjects(eventReports); + devicesEvents.add(deviceEvents); + } + } + String templatePath = Context.getConfig().getString("report.events.template.excel", + "templates/export/events.xlsx"); + try (InputStream inputStream = new FileInputStream(templatePath)) { + org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext(); + jxlsContext.putVar("devices", devicesEvents); + jxlsContext.putVar("sheetNames", sheetNames); + jxlsContext.putVar("geofenceNames", geofenceNames); + jxlsContext.putVar("from", from); + jxlsContext.putVar("to", to); + jxlsContext.putVar("bracketsRegex", "[\\{\\}\"]"); + Transformer transformer = TransformerFactory.createTransformer(inputStream, outputStream); + List xlsAreas = new XlsCommentAreaBuilder(transformer).build(); + for (Area xlsArea : xlsAreas) { + xlsArea.applyAt(new CellRef(xlsArea.getStartCellRef().getCellName()), jxlsContext); + xlsArea.setFormulaProcessor(new StandardFormulaProcessor()); + xlsArea.processFormulas(); } + transformer.deleteSheet(xlsAreas.get(0).getStartCellRef().getSheetName()); + transformer.write(); } - return csv.build(); } } diff --git a/src/org/traccar/reports/Route.java b/src/org/traccar/reports/Route.java index 45b81cd8c..1bb7b564a 100644 --- a/src/org/traccar/reports/Route.java +++ b/src/org/traccar/reports/Route.java @@ -16,16 +16,31 @@ */ package org.traccar.reports; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Collection; import java.util.Date; +import java.util.List; import javax.json.Json; import javax.json.JsonArrayBuilder; +import org.jxls.area.Area; +import org.jxls.builder.xls.XlsCommentAreaBuilder; +import org.jxls.common.CellRef; +import org.jxls.formula.StandardFormulaProcessor; +import org.jxls.transform.Transformer; +import org.jxls.transform.poi.PoiTransformer; +import org.jxls.util.TransformerFactory; import org.traccar.Context; +import org.traccar.model.Device; +import org.traccar.model.Group; import org.traccar.model.Position; -import org.traccar.web.CsvBuilder; +import org.traccar.reports.model.DeviceReport; import org.traccar.web.JsonConverter; public final class Route { @@ -45,14 +60,47 @@ public final class Route { return json.build().toString(); } - public static String getCsv(long userId, Collection deviceIds, Collection groupIds, - Date from, Date to) throws SQLException { - CsvBuilder csv = new CsvBuilder(); - csv.addHeaderLine(new Position()); + public static void getExcel(OutputStream outputStream, + long userId, Collection deviceIds, Collection groupIds, + Date from, Date to) throws SQLException, IOException { + ArrayList devicesRoutes = new ArrayList<>(); + ArrayList sheetNames = new ArrayList<>(); for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); - csv.addArray(Context.getDataManager().getPositions(deviceId, from, to)); + Collection positions = Context.getDataManager().getPositions(deviceId, from, to); + if (positions != null && !positions.isEmpty()) { + DeviceReport deviceRoutes = new DeviceReport(); + Device device = Context.getIdentityManager().getDeviceById(deviceId); + deviceRoutes.setDeviceName(device.getName()); + sheetNames.add(deviceRoutes.getDeviceName()); + if (device.getGroupId() != 0) { + Group group = Context.getDeviceManager().getGroupById(device.getGroupId()); + if (group != null) { + deviceRoutes.setGroupName(group.getName()); + } + } + deviceRoutes.setObjects(positions); + devicesRoutes.add(deviceRoutes); + } + } + String templatePath = Context.getConfig().getString("report.route.template.excel", + "templates/export/route.xlsx"); + try (InputStream inputStream = new FileInputStream(templatePath)) { + org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext(); + jxlsContext.putVar("devices", devicesRoutes); + jxlsContext.putVar("sheetNames", sheetNames); + jxlsContext.putVar("from", from); + jxlsContext.putVar("to", to); + jxlsContext.putVar("bracketsRegex", "[\\{\\}\"]"); + Transformer transformer = TransformerFactory.createTransformer(inputStream, outputStream); + List xlsAreas = new XlsCommentAreaBuilder(transformer).build(); + for (Area xlsArea : xlsAreas) { + xlsArea.applyAt(new CellRef(xlsArea.getStartCellRef().getCellName()), jxlsContext); + xlsArea.setFormulaProcessor(new StandardFormulaProcessor()); + xlsArea.processFormulas(); + } + transformer.deleteSheet(xlsAreas.get(0).getStartCellRef().getSheetName()); + transformer.write(); } - return csv.build(); } } diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java index d4171f644..e7f41bcd6 100644 --- a/src/org/traccar/reports/Summary.java +++ b/src/org/traccar/reports/Summary.java @@ -16,17 +16,23 @@ */ package org.traccar.reports; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Collection; import java.util.Date; import javax.json.Json; import javax.json.JsonArrayBuilder; +import org.jxls.transform.poi.PoiTransformer; +import org.jxls.util.JxlsHelper; import org.traccar.Context; import org.traccar.model.Position; import org.traccar.reports.model.SummaryReport; -import org.traccar.web.CsvBuilder; import org.traccar.web.JsonConverter; public final class Summary { @@ -78,14 +84,23 @@ public final class Summary { return json.build().toString(); } - public static String getCsv(long userId, Collection deviceIds, Collection groupIds, - Date from, Date to) throws SQLException { - CsvBuilder csv = new CsvBuilder(); - csv.addHeaderLine(new SummaryReport()); + public static void getExcel(OutputStream outputStream, + long userId, Collection deviceIds, Collection groupIds, + Date from, Date to) throws SQLException, IOException { + ArrayList summaries = new ArrayList<>(); for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); - csv.addLine(calculateSummaryResult(deviceId, from, to)); + summaries.add(calculateSummaryResult(deviceId, from, to)); + } + String templatePath = Context.getConfig().getString("report.summary.template.excel", + "templates/export/summary.xlsx"); + try (InputStream inputStream = new FileInputStream(templatePath)) { + org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext(); + jxlsContext.putVar("summaries", summaries); + jxlsContext.putVar("from", from); + jxlsContext.putVar("to", to); + JxlsHelper.getInstance().setUseFastFormulaProcessor(false) + .processTemplate(inputStream, outputStream, jxlsContext); } - return csv.build(); } } diff --git a/src/org/traccar/reports/Trips.java b/src/org/traccar/reports/Trips.java index f0a10edbd..c912dc651 100644 --- a/src/org/traccar/reports/Trips.java +++ b/src/org/traccar/reports/Trips.java @@ -16,18 +16,32 @@ */ package org.traccar.reports; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; import java.util.Date; +import java.util.List; import javax.json.Json; import javax.json.JsonArrayBuilder; +import org.jxls.area.Area; +import org.jxls.builder.xls.XlsCommentAreaBuilder; +import org.jxls.common.CellRef; +import org.jxls.formula.StandardFormulaProcessor; +import org.jxls.transform.Transformer; +import org.jxls.transform.poi.PoiTransformer; +import org.jxls.util.TransformerFactory; import org.traccar.Context; +import org.traccar.model.Device; +import org.traccar.model.Group; import org.traccar.model.Position; +import org.traccar.reports.model.DeviceReport; import org.traccar.reports.model.TripReport; -import org.traccar.web.CsvBuilder; import org.traccar.web.JsonConverter; public final class Trips { @@ -50,16 +64,24 @@ public final class Trips { } TripReport trip = new TripReport(); + long tripDuration = endTrip.getFixTime().getTime() - positions.get(startIndex).getFixTime().getTime(); long deviceId = startTrip.getDeviceId(); trip.setDeviceId(deviceId); trip.setDeviceName(Context.getIdentityManager().getDeviceById(deviceId).getName()); + trip.setStartPositionId(startTrip.getId()); + trip.setStartLat(startTrip.getLatitude()); + trip.setStartLon(startTrip.getLongitude()); trip.setStartTime(startTrip.getFixTime()); trip.setStartAddress(startTrip.getAddress()); + trip.setEndPositionId(endTrip.getId()); + trip.setEndLat(endTrip.getLatitude()); + trip.setEndLon(endTrip.getLongitude()); trip.setEndTime(endTrip.getFixTime()); trip.setEndAddress(endTrip.getAddress()); + boolean ignoreOdometer = Context.getDeviceManager() .lookupConfigBoolean(deviceId, "report.ignoreOdometer", false); trip.setDistance(ReportUtils.calculateDistance(startTrip, endTrip, !ignoreOdometer)); @@ -157,14 +179,46 @@ public final class Trips { return json.build().toString(); } - public static String getCsv(long userId, Collection deviceIds, Collection groupIds, - Date from, Date to) throws SQLException { - CsvBuilder csv = new CsvBuilder(); - csv.addHeaderLine(new TripReport()); + public static void getExcel(OutputStream outputStream, + long userId, Collection deviceIds, Collection groupIds, + Date from, Date to) throws SQLException, IOException { + ArrayList devicesTrips = new ArrayList<>(); + ArrayList sheetNames = new ArrayList<>(); for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); - csv.addArray(detectTrips(deviceId, from, to)); + Collection trips = detectTrips(deviceId, from, to); + if (!trips.isEmpty()) { + DeviceReport deviceTrips = new DeviceReport(); + Device device = Context.getIdentityManager().getDeviceById(deviceId); + deviceTrips.setDeviceName(device.getName()); + sheetNames.add(deviceTrips.getDeviceName()); + if (device.getGroupId() != 0) { + Group group = Context.getDeviceManager().getGroupById(device.getGroupId()); + if (group != null) { + deviceTrips.setGroupName(group.getName()); + } + } + deviceTrips.setObjects(detectTrips(deviceId, from, to)); + devicesTrips.add(deviceTrips); + } + } + String templatePath = Context.getConfig().getString("report.trips.template.excel", + "templates/export/trips.xlsx"); + try (InputStream inputStream = new FileInputStream(templatePath)) { + org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext(); + jxlsContext.putVar("devices", devicesTrips); + jxlsContext.putVar("sheetNames", sheetNames); + jxlsContext.putVar("from", from); + jxlsContext.putVar("to", to); + Transformer transformer = TransformerFactory.createTransformer(inputStream, outputStream); + List xlsAreas = new XlsCommentAreaBuilder(transformer).build(); + for (Area xlsArea : xlsAreas) { + xlsArea.applyAt(new CellRef(xlsArea.getStartCellRef().getCellName()), jxlsContext); + xlsArea.setFormulaProcessor(new StandardFormulaProcessor()); + xlsArea.processFormulas(); + } + transformer.deleteSheet(xlsAreas.get(0).getStartCellRef().getSheetName()); + transformer.write(); } - return csv.build(); } } diff --git a/src/org/traccar/reports/model/DeviceReport.java b/src/org/traccar/reports/model/DeviceReport.java new file mode 100644 index 000000000..c072de231 --- /dev/null +++ b/src/org/traccar/reports/model/DeviceReport.java @@ -0,0 +1,49 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2016 Andrey Kunitsyn (abyss@fox5.ru) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.reports.model; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public class DeviceReport { + + private String deviceName; + public String getDeviceName() { + return deviceName; + } + public void setDeviceName(String deviceName) { + this.deviceName = deviceName; + } + + private String groupName = ""; + public String getGroupName() { + return groupName; + } + public void setGroupName(String groupName) { + this.groupName = groupName; + } + + private List objects; + public Collection getObjects() { + return objects; + } + public void setObjects(Collection objects) { + this.objects = new ArrayList<>(objects); + } + +} diff --git a/src/org/traccar/reports/model/TripReport.java b/src/org/traccar/reports/model/TripReport.java index 3a77b02ca..d726195cb 100644 --- a/src/org/traccar/reports/model/TripReport.java +++ b/src/org/traccar/reports/model/TripReport.java @@ -1,3 +1,19 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2016 Andrey Kunitsyn (abyss@fox5.ru) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.reports.model; import java.util.Date; @@ -20,6 +36,38 @@ public class TripReport extends BaseReport { this.endPositionId = endPositionId; } + private double startLat; + public double getStartLat() { + return startLat; + } + public void setStartLat(double startLat) { + this.startLat = startLat; + } + + private double startLon; + public double getStartLon() { + return startLon; + } + public void setStartLon(double startLon) { + this.startLon = startLon; + } + + private double endLat; + public double getEndLat() { + return endLat; + } + public void setEndLat(double endLat) { + this.endLat = endLat; + } + + private double endLon; + public double getEndLon() { + return endLon; + } + public void setEndLon(double endLon) { + this.endLon = endLon; + } + private Date startTime; public Date getStartTime() { if (startTime != null) { -- cgit v1.2.3 From 17f5a899a08b992507b574eeb4047b1f6bfa8629 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 19 Oct 2016 18:35:28 +0500 Subject: - Filter events on server side - Use common path for templates - Filter events about geofences that user do not have access (minor security fix) - Removed unused function - Visual templates improvements --- debug.xml | 2 +- src/org/traccar/api/resource/EventResource.java | 13 ++---- src/org/traccar/database/DataManager.java | 11 +---- src/org/traccar/model/Event.java | 2 + src/org/traccar/reports/Events.java | 52 ++++++++++++------------ src/org/traccar/reports/Route.java | 6 +-- src/org/traccar/reports/Summary.java | 6 +-- src/org/traccar/reports/Trips.java | 6 +-- swagger.json | 42 ------------------- templates/export/events.xlsx | Bin 14120 -> 11830 bytes templates/export/route.xlsx | Bin 14505 -> 12398 bytes templates/export/summary.xlsx | Bin 14413 -> 11896 bytes templates/export/trips.xlsx | Bin 14666 -> 12811 bytes 13 files changed, 43 insertions(+), 97 deletions(-) (limited to 'src/org') diff --git a/debug.xml b/debug.xml index e3ee64221..3afe59253 100644 --- a/debug.xml +++ b/debug.xml @@ -242,7 +242,7 @@ - SELECT * FROM events WHERE deviceId = :deviceId AND type LIKE :type AND serverTime BETWEEN :from AND :to ORDER BY serverTime DESC; + SELECT * FROM events WHERE deviceId = :deviceId AND serverTime BETWEEN :from AND :to ORDER BY serverTime diff --git a/src/org/traccar/api/resource/EventResource.java b/src/org/traccar/api/resource/EventResource.java index 74a748ea5..c0a8f968d 100644 --- a/src/org/traccar/api/resource/EventResource.java +++ b/src/org/traccar/api/resource/EventResource.java @@ -1,14 +1,12 @@ package org.traccar.api.resource; import java.sql.SQLException; -import java.util.Collection; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import org.traccar.Context; @@ -26,14 +24,9 @@ public class EventResource extends BaseResource { public Event get(@PathParam("id") long id) throws SQLException { Event event = Context.getDataManager().getEvent(id); Context.getPermissionsManager().checkDevice(getUserId(), event.getDeviceId()); + if (event.getGeofenceId() != 0) { + Context.getPermissionsManager().checkGeofence(getUserId(), event.getGeofenceId()); + } return event; } - - @GET - public Collection get( - @QueryParam("deviceId") long deviceId, @QueryParam("type") String type, - @QueryParam("interval") int interval) throws SQLException { - Context.getPermissionsManager().checkDevice(getUserId(), deviceId); - return Context.getDataManager().getLastEvents(deviceId, type, interval); - } } diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 58d16d7f5..e98e1429c 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -20,7 +20,6 @@ import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; import java.sql.SQLException; -import java.util.Calendar; import java.util.Collection; import java.util.Date; @@ -332,22 +331,14 @@ public class DataManager { .executeUpdate()); } - public Collection getEvents(long deviceId, String type, Date from, Date to) throws SQLException { + public Collection getEvents(long deviceId, Date from, Date to) throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectEvents")) .setLong("deviceId", deviceId) - .setString("type", type) .setDate("from", from) .setDate("to", to) .executeQuery(Event.class); } - public Collection getLastEvents(long deviceId, String type, int interval) throws SQLException { - Calendar calendar = Calendar.getInstance(); - calendar.add(Calendar.SECOND, -interval); - Date from = calendar.getTime(); - return getEvents(deviceId, type, from, new Date()); - } - public Collection getGeofences() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectGeofencesAll")) .executeQuery(Geofence.class); diff --git a/src/org/traccar/model/Event.java b/src/org/traccar/model/Event.java index c3c8b5320..5b4b0e089 100644 --- a/src/org/traccar/model/Event.java +++ b/src/org/traccar/model/Event.java @@ -35,6 +35,8 @@ public class Event extends Message { public Event() { } + public static final String ALL_EVENTS = "allEvents"; + public static final String TYPE_COMMAND_RESULT = "commandResult"; public static final String TYPE_DEVICE_ONLINE = "deviceOnline"; diff --git a/src/org/traccar/reports/Events.java b/src/org/traccar/reports/Events.java index 9f58e1bf4..37bf7b227 100644 --- a/src/org/traccar/reports/Events.java +++ b/src/org/traccar/reports/Events.java @@ -23,12 +23,10 @@ import java.io.OutputStream; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; -import java.util.Comparator; import java.util.Date; import java.util.HashMap; +import java.util.Iterator; import java.util.List; -import java.util.SortedSet; -import java.util.TreeSet; import javax.json.Json; import javax.json.JsonArrayBuilder; @@ -58,9 +56,14 @@ public final class Events { JsonArrayBuilder json = Json.createArrayBuilder(); for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); - for (String type : types) { - for (Event event : Context.getDataManager().getEvents(deviceId, type, from, to)) { - json.add(JsonConverter.objectToJson(event)); + Collection events = Context.getDataManager().getEvents(deviceId, from, to); + boolean all = types.isEmpty() || types.contains(Event.ALL_EVENTS); + for (Event event : events) { + if (all || types.contains(event.getType())) { + long geofenceId = event.getGeofenceId(); + if (geofenceId == 0 || Context.getGeofenceManager().checkGeofence(userId, geofenceId)) { + json.add(JsonConverter.objectToJson(event)); + } } } } @@ -75,28 +78,27 @@ public final class Events { HashMap geofenceNames = new HashMap<>(); for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); - SortedSet eventReports = new TreeSet(new Comparator() { - @Override - public int compare(Event e1, Event e2) { - return e1.getServerTime().compareTo(e2.getServerTime()); - } - }); - for (String type : types) { - Collection events = Context.getDataManager().getEvents(deviceId, type, from, to); - for (Event event : events) { + Collection events = Context.getDataManager().getEvents(deviceId, from, to); + boolean all = types.isEmpty() || types.contains(Event.ALL_EVENTS); + for (Iterator iterator = events.iterator(); iterator.hasNext();) { + Event event = iterator.next(); + if (all || types.contains(event.getType())) { long geofenceId = event.getGeofenceId(); if (geofenceId != 0) { - Geofence geofence = Context.getGeofenceManager().getGeofence(geofenceId); - if (geofence != null) { - geofenceNames.put(geofenceId, geofence.getName()); + if (Context.getGeofenceManager().checkGeofence(userId, geofenceId)) { + Geofence geofence = Context.getGeofenceManager().getGeofence(geofenceId); + if (geofence != null) { + geofenceNames.put(geofenceId, geofence.getName()); + } } else { - geofenceNames.put(geofenceId, Long.toString(geofenceId)); + iterator.remove(); } } - eventReports.add(event); + } else { + iterator.remove(); } } - if (!eventReports.isEmpty()) { + if (!events.isEmpty()) { DeviceReport deviceEvents = new DeviceReport(); Device device = Context.getIdentityManager().getDeviceById(deviceId); deviceEvents.setDeviceName(device.getName()); @@ -107,13 +109,13 @@ public final class Events { deviceEvents.setGroupName(group.getName()); } } - deviceEvents.setObjects(eventReports); + deviceEvents.setObjects(events); devicesEvents.add(deviceEvents); } } - String templatePath = Context.getConfig().getString("report.events.template.excel", - "templates/export/events.xlsx"); - try (InputStream inputStream = new FileInputStream(templatePath)) { + String templatePath = Context.getConfig().getString("report.templatesPath", + "templates/export/"); + try (InputStream inputStream = new FileInputStream(templatePath + "/events.xlsx")) { org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext(); jxlsContext.putVar("devices", devicesEvents); jxlsContext.putVar("sheetNames", sheetNames); diff --git a/src/org/traccar/reports/Route.java b/src/org/traccar/reports/Route.java index 1bb7b564a..7cefe04a3 100644 --- a/src/org/traccar/reports/Route.java +++ b/src/org/traccar/reports/Route.java @@ -83,9 +83,9 @@ public final class Route { devicesRoutes.add(deviceRoutes); } } - String templatePath = Context.getConfig().getString("report.route.template.excel", - "templates/export/route.xlsx"); - try (InputStream inputStream = new FileInputStream(templatePath)) { + String templatePath = Context.getConfig().getString("report.templatesPath", + "templates/export/"); + try (InputStream inputStream = new FileInputStream(templatePath + "/route.xlsx")) { org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext(); jxlsContext.putVar("devices", devicesRoutes); jxlsContext.putVar("sheetNames", sheetNames); diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java index e7f41bcd6..14f8b7839 100644 --- a/src/org/traccar/reports/Summary.java +++ b/src/org/traccar/reports/Summary.java @@ -92,9 +92,9 @@ public final class Summary { Context.getPermissionsManager().checkDevice(userId, deviceId); summaries.add(calculateSummaryResult(deviceId, from, to)); } - String templatePath = Context.getConfig().getString("report.summary.template.excel", - "templates/export/summary.xlsx"); - try (InputStream inputStream = new FileInputStream(templatePath)) { + String templatePath = Context.getConfig().getString("report.templatesPath", + "templates/export/"); + try (InputStream inputStream = new FileInputStream(templatePath + "/summary.xlsx")) { org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext(); jxlsContext.putVar("summaries", summaries); jxlsContext.putVar("from", from); diff --git a/src/org/traccar/reports/Trips.java b/src/org/traccar/reports/Trips.java index c912dc651..27863d877 100644 --- a/src/org/traccar/reports/Trips.java +++ b/src/org/traccar/reports/Trips.java @@ -202,9 +202,9 @@ public final class Trips { devicesTrips.add(deviceTrips); } } - String templatePath = Context.getConfig().getString("report.trips.template.excel", - "templates/export/trips.xlsx"); - try (InputStream inputStream = new FileInputStream(templatePath)) { + String templatePath = Context.getConfig().getString("report.templatesPath", + "templates/export/"); + try (InputStream inputStream = new FileInputStream(templatePath + "/trips.xlsx")) { org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext(); jxlsContext.putVar("devices", devicesTrips); jxlsContext.putVar("sheetNames", sheetNames); diff --git a/swagger.json b/swagger.json index cd44c953a..0e19bf5a8 100644 --- a/swagger.json +++ b/swagger.json @@ -1040,48 +1040,6 @@ } } }, - "/events": { - "get": { - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "parameters": [ - { - "name": "deviceId", - "in": "query", - "required": true, - "type": "integer" - }, - { - "name": "type", - "in": "query", - "required": true, - "type": "string" - }, - { - "name": "interval", - "in": "query", - "required": true, - "type": "integer" - } - ], - "responses": { - "200": { - "description": "OK", - "headers": {}, - "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/Event" - } - } - } - } - } - }, "/events/{id}": { "get": { "consumes": [ diff --git a/templates/export/events.xlsx b/templates/export/events.xlsx index 007265496..99a3f7437 100644 Binary files a/templates/export/events.xlsx and b/templates/export/events.xlsx differ diff --git a/templates/export/route.xlsx b/templates/export/route.xlsx index c22e37268..6f74cfb45 100644 Binary files a/templates/export/route.xlsx and b/templates/export/route.xlsx differ diff --git a/templates/export/summary.xlsx b/templates/export/summary.xlsx index 6e22343ba..a975c54fe 100644 Binary files a/templates/export/summary.xlsx and b/templates/export/summary.xlsx differ diff --git a/templates/export/trips.xlsx b/templates/export/trips.xlsx index 136291181..8e13d5ff8 100644 Binary files a/templates/export/trips.xlsx and b/templates/export/trips.xlsx differ -- cgit v1.2.3 From 0601225489057f9b2310226c59119186464a6b06 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 20 Oct 2016 03:38:54 +1300 Subject: Don't cache user password (fix #2455) --- src/org/traccar/model/User.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/model/User.java b/src/org/traccar/model/User.java index 59951718e..860c91629 100644 --- a/src/org/traccar/model/User.java +++ b/src/org/traccar/model/User.java @@ -140,14 +140,11 @@ public class User extends Extensible { this.coordinateFormat = coordinateFormat; } - private String password; - public String getPassword() { - return password; + return null; } public void setPassword(String password) { - this.password = password; if (password != null && !password.isEmpty()) { Hashing.HashingResult hashingResult = Hashing.createHash(password); hashedPassword = hashingResult.getHash(); -- cgit v1.2.3 From 23e11715cc6f5e94002123293a1108a48044316e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 20 Oct 2016 08:00:39 +1300 Subject: Support Ruptela extended format (fix #2460) --- .../traccar/protocol/RuptelaProtocolDecoder.java | 32 +++++++++++++++------- .../protocol/RuptelaProtocolDecoderTest.java | 6 ++++ 2 files changed, 28 insertions(+), 10 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/protocol/RuptelaProtocolDecoder.java b/src/org/traccar/protocol/RuptelaProtocolDecoder.java index 224c027f3..84cc0e101 100644 --- a/src/org/traccar/protocol/RuptelaProtocolDecoder.java +++ b/src/org/traccar/protocol/RuptelaProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2014 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2013 - 2016 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,6 +35,7 @@ public class RuptelaProtocolDecoder extends BaseProtocolDecoder { } public static final int MSG_RECORDS = 1; + public static final int MSG_EXTENDED_RECORDS = 68; public static final int MSG_SMS_VIA_GPRS = 108; @Override @@ -53,7 +54,7 @@ public class RuptelaProtocolDecoder extends BaseProtocolDecoder { int type = buf.readUnsignedByte(); - if (type == MSG_RECORDS) { + if (type == MSG_RECORDS || type == MSG_EXTENDED_RECORDS) { List positions = new LinkedList<>(); buf.readUnsignedByte(); // records left @@ -67,45 +68,56 @@ public class RuptelaProtocolDecoder extends BaseProtocolDecoder { position.setTime(new Date(buf.readUnsignedInt() * 1000)); buf.readUnsignedByte(); // timestamp extension + if (type == MSG_EXTENDED_RECORDS) { + buf.readUnsignedByte(); // record extension + } + buf.readUnsignedByte(); // priority (reserved) + position.setValid(true); position.setLongitude(buf.readInt() / 10000000.0); position.setLatitude(buf.readInt() / 10000000.0); position.setAltitude(buf.readUnsignedShort() / 10.0); position.setCourse(buf.readUnsignedShort() / 100.0); - int satellites = buf.readUnsignedByte(); - position.set(Position.KEY_SATELLITES, satellites); - position.setValid(satellites >= 3); + position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort())); position.set(Position.KEY_HDOP, buf.readUnsignedByte() / 10.0); - buf.readUnsignedByte(); + if (type == MSG_EXTENDED_RECORDS) { + buf.readUnsignedShort(); // event + } else { + buf.readUnsignedByte(); // event + } // Read 1 byte data int cnt = buf.readUnsignedByte(); for (int j = 0; j < cnt; j++) { - position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedByte()); + int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte(); + position.set(Position.PREFIX_IO + id, buf.readUnsignedByte()); } // Read 2 byte data cnt = buf.readUnsignedByte(); for (int j = 0; j < cnt; j++) { - position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedShort()); + int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte(); + position.set(Position.PREFIX_IO + id, buf.readUnsignedShort()); } // Read 4 byte data cnt = buf.readUnsignedByte(); for (int j = 0; j < cnt; j++) { - position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedInt()); + int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte(); + position.set(Position.PREFIX_IO + id, buf.readUnsignedInt()); } // Read 8 byte data cnt = buf.readUnsignedByte(); for (int j = 0; j < cnt; j++) { - position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readLong()); + int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte(); + position.set(Position.PREFIX_IO + id, buf.readLong()); } positions.add(position); diff --git a/test/org/traccar/protocol/RuptelaProtocolDecoderTest.java b/test/org/traccar/protocol/RuptelaProtocolDecoderTest.java index 902752811..cc457efca 100644 --- a/test/org/traccar/protocol/RuptelaProtocolDecoderTest.java +++ b/test/org/traccar/protocol/RuptelaProtocolDecoderTest.java @@ -9,6 +9,12 @@ public class RuptelaProtocolDecoderTest extends ProtocolTest { public void testDecode() throws Exception { RuptelaProtocolDecoder decoder = new RuptelaProtocolDecoder(new RuptelaProtocol()); + + verifyPositions(decoder, binary( + "01a4000315bc70f9b69244000458068f4a0030000d11398a1c0c19fd056524040b000c0a00090c0005010031f40032fd0033f200ce47002400002500001c010199000195010196010086000900aa0000001e0ff000d3ffff0043ffff01930000019200000194000002220000022300000200300000000200af000e872401008e000000000000000058068f4a0031000d11398a1c0c19fd056524040b000c0a00090400870000880000a90000820010008b0002021e0000021f0000021d0000021c0000022400000225000000890000008505f00220000002210000008300000084000002260000022700000228000003008a00000000008d00000000008c000000000058068f4a0032000d11398a1c0c19fd056524040b000c0a000905019f01005800001b1f00ad0000cfb10b02290000022a0000022b0000022c0000022d00000012000000130000001d367400c52f8000740055023e0502060097000000000096000058520041007746cb00d0000003f1005c0007c21b0072001864880058068f4a0033000d11398a1c0c19fd056524040b000c0a000900000001008e0000000000000000e815")); + + verifyPositions(decoder, binary( + "033d000315bc70f9b69244000858068f3b0030010d11354e1c0c17a5055d54560c00000900050c0005010031f30032fb0033f300ce00002400002500001c010199000195010196010086000900aa0000001e0ff300d3ffff0043ffff01930000019200000194000002220000022300000200300000000000af000e872401008e000000000000000058068f3b0031010d11354e1c0c17a5055d54560c00000900050400870000880000a90000820010008b0000021e0000021f0000021d0000021c0000022400000225000000890000008500000220000002210000008300000084000002260000022700000228000003008a00000000008d00000000008c000000000058068f3b0032010d11354e1c0c17a5055d54560c000009000505019f01005800001b1f00ad0000cfac0b02290000022a0000022b0000022c0000022d00000012000000130000001d31b100c5000000740000023e0502060097000000000096000058520041007746be00d0000003f1005c0007c2150072001864880058068f3b0033010d11354e1c0c17a5055d54560c000009000500000001008e000000000000000058068f3b0130000d11354e1c0c17a5055d54560d00000900070c0005010031f30032fb0033f300ce00002400002500001c010199000195010196010086000900aa0000001e0ff300d3ffff0043ffff01930000019200000194000002220000022300000200300000000000af000e872401008e000000000000000058068f3b0131000d11354e1c0c17a5055d54560d00000900070400870000880000a90000820010008b0000021e0000021f0000021d0000021c0000022400000225000000890000008500000220000002210000008300000084000002260000022700000228000003008a00000000008d00000000008c000000000058068f3b0132000d11354e1c0c17a5055d54560d000009000705019f01005800001b1f00ad0000cfac0b02290000022a0000022b0000022c0000022d00000012000000130000001d31ae00c5000000740000023e0502060097000000000096000058520041007746be00d0000003f1005c0007c2150072001864880058068f3b0133000d11354e1c0c17a5055d54560d000009000700000001008e0000000000000000084d")); verifyPositions(decoder, binary( "0050000310f5615f419c0100015613d8ed0000fff5b37a035af37801e700000900000d07071b0c020003001c01202cad000500064302a81d33e61e100116317cd3ffff174ad60241000077fa960000f232003c2e")); -- cgit v1.2.3 From f64840b9e85d4869c92e9f20a2f5043522ad4008 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 20 Oct 2016 16:32:24 +1300 Subject: Use heuristics for H02 message length --- src/org/traccar/protocol/H02FrameDecoder.java | 16 +++++++++++++--- src/org/traccar/protocol/H02Protocol.java | 2 +- test/org/traccar/protocol/H02FrameDecoderTest.java | 6 +++++- 3 files changed, 19 insertions(+), 5 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/protocol/H02FrameDecoder.java b/src/org/traccar/protocol/H02FrameDecoder.java index feba8033d..2a005e760 100644 --- a/src/org/traccar/protocol/H02FrameDecoder.java +++ b/src/org/traccar/protocol/H02FrameDecoder.java @@ -22,6 +22,9 @@ import org.jboss.netty.handler.codec.frame.FrameDecoder; public class H02FrameDecoder extends FrameDecoder { + private static final int MESSAGE_SHORT = 32; + private static final int MESSAGE_LONG = 45; + private int messageLength; public H02FrameDecoder(int messageLength) { @@ -49,10 +52,17 @@ public class H02FrameDecoder extends FrameDecoder { return buf.readBytes(index + 1 - buf.readerIndex()); } - } else if (marker == '$' && buf.readableBytes() >= messageLength) { + } else if (marker == '$') { - // Return binary message - return buf.readBytes(messageLength); + if (messageLength > 0 && buf.readableBytes() >= messageLength) { + return buf.readBytes(messageLength); + } else if (buf.readableBytes() >= MESSAGE_SHORT) { + if (buf.getUnsignedByte(buf.readerIndex() + MESSAGE_SHORT - 1) == 0) { + return buf.readBytes(MESSAGE_SHORT); + } else if (buf.readableBytes() >= MESSAGE_LONG) { + return buf.readBytes(MESSAGE_LONG); + } + } } diff --git a/src/org/traccar/protocol/H02Protocol.java b/src/org/traccar/protocol/H02Protocol.java index 06ac2a6fa..089721aed 100644 --- a/src/org/traccar/protocol/H02Protocol.java +++ b/src/org/traccar/protocol/H02Protocol.java @@ -43,7 +43,7 @@ public class H02Protocol extends BaseProtocol { serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { - int messageLength = Context.getConfig().getInteger(getName() + ".messageLength", 32); + int messageLength = Context.getConfig().getInteger(getName() + ".messageLength"); pipeline.addLast("frameDecoder", new H02FrameDecoder(messageLength)); pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("objectEncoder", new H02ProtocolEncoder()); diff --git a/test/org/traccar/protocol/H02FrameDecoderTest.java b/test/org/traccar/protocol/H02FrameDecoderTest.java index a8417341a..3e12bb446 100644 --- a/test/org/traccar/protocol/H02FrameDecoderTest.java +++ b/test/org/traccar/protocol/H02FrameDecoderTest.java @@ -9,7 +9,11 @@ public class H02FrameDecoderTest extends ProtocolTest { @Test public void testDecode() throws Exception { - H02FrameDecoder decoder = new H02FrameDecoder(32); + H02FrameDecoder decoder = new H02FrameDecoder(0); + + Assert.assertEquals( + binary("24410600082621532131081504419390060740418306000000fffffbfdff0015060000002c02dc0c000000001f"), + decoder.decode(null, null, binary("24410600082621532131081504419390060740418306000000fffffbfdff0015060000002c02dc0c000000001f"))); Assert.assertEquals( binary("2a48512c3335333538383036303031353536382c56312c3139333530352c412c3830392e303031302c532c333435342e383939372c572c302e30302c302e30302c3239313031332c65666666666266662c3030303264342c3030303030622c3030353338352c3030353261612c323523"), -- cgit v1.2.3 From 40d7ff2f9505150c85e87332fac9f67725beba8e Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 20 Oct 2016 09:55:49 +0500 Subject: - Fix export empty reports - Cleanup templates metadata --- src/org/traccar/reports/Events.java | 22 ++++++++++------------ src/org/traccar/reports/Route.java | 22 ++++++++++------------ src/org/traccar/reports/Trips.java | 22 ++++++++++------------ templates/export/events.xlsx | Bin 11830 -> 11211 bytes templates/export/route.xlsx | Bin 12398 -> 11707 bytes templates/export/summary.xlsx | Bin 11896 -> 11209 bytes templates/export/trips.xlsx | Bin 12811 -> 12124 bytes 7 files changed, 30 insertions(+), 36 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/reports/Events.java b/src/org/traccar/reports/Events.java index 37bf7b227..77d995f56 100644 --- a/src/org/traccar/reports/Events.java +++ b/src/org/traccar/reports/Events.java @@ -98,20 +98,18 @@ public final class Events { iterator.remove(); } } - if (!events.isEmpty()) { - DeviceReport deviceEvents = new DeviceReport(); - Device device = Context.getIdentityManager().getDeviceById(deviceId); - deviceEvents.setDeviceName(device.getName()); - sheetNames.add(deviceEvents.getDeviceName()); - if (device.getGroupId() != 0) { - Group group = Context.getDeviceManager().getGroupById(device.getGroupId()); - if (group != null) { - deviceEvents.setGroupName(group.getName()); - } + DeviceReport deviceEvents = new DeviceReport(); + Device device = Context.getIdentityManager().getDeviceById(deviceId); + deviceEvents.setDeviceName(device.getName()); + sheetNames.add(deviceEvents.getDeviceName()); + if (device.getGroupId() != 0) { + Group group = Context.getDeviceManager().getGroupById(device.getGroupId()); + if (group != null) { + deviceEvents.setGroupName(group.getName()); } - deviceEvents.setObjects(events); - devicesEvents.add(deviceEvents); } + deviceEvents.setObjects(events); + devicesEvents.add(deviceEvents); } String templatePath = Context.getConfig().getString("report.templatesPath", "templates/export/"); diff --git a/src/org/traccar/reports/Route.java b/src/org/traccar/reports/Route.java index 7cefe04a3..6992c89cc 100644 --- a/src/org/traccar/reports/Route.java +++ b/src/org/traccar/reports/Route.java @@ -68,20 +68,18 @@ public final class Route { for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); Collection positions = Context.getDataManager().getPositions(deviceId, from, to); - if (positions != null && !positions.isEmpty()) { - DeviceReport deviceRoutes = new DeviceReport(); - Device device = Context.getIdentityManager().getDeviceById(deviceId); - deviceRoutes.setDeviceName(device.getName()); - sheetNames.add(deviceRoutes.getDeviceName()); - if (device.getGroupId() != 0) { - Group group = Context.getDeviceManager().getGroupById(device.getGroupId()); - if (group != null) { - deviceRoutes.setGroupName(group.getName()); - } + DeviceReport deviceRoutes = new DeviceReport(); + Device device = Context.getIdentityManager().getDeviceById(deviceId); + deviceRoutes.setDeviceName(device.getName()); + sheetNames.add(deviceRoutes.getDeviceName()); + if (device.getGroupId() != 0) { + Group group = Context.getDeviceManager().getGroupById(device.getGroupId()); + if (group != null) { + deviceRoutes.setGroupName(group.getName()); } - deviceRoutes.setObjects(positions); - devicesRoutes.add(deviceRoutes); } + deviceRoutes.setObjects(positions); + devicesRoutes.add(deviceRoutes); } String templatePath = Context.getConfig().getString("report.templatesPath", "templates/export/"); diff --git a/src/org/traccar/reports/Trips.java b/src/org/traccar/reports/Trips.java index 27863d877..705644849 100644 --- a/src/org/traccar/reports/Trips.java +++ b/src/org/traccar/reports/Trips.java @@ -187,20 +187,18 @@ public final class Trips { for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); Collection trips = detectTrips(deviceId, from, to); - if (!trips.isEmpty()) { - DeviceReport deviceTrips = new DeviceReport(); - Device device = Context.getIdentityManager().getDeviceById(deviceId); - deviceTrips.setDeviceName(device.getName()); - sheetNames.add(deviceTrips.getDeviceName()); - if (device.getGroupId() != 0) { - Group group = Context.getDeviceManager().getGroupById(device.getGroupId()); - if (group != null) { - deviceTrips.setGroupName(group.getName()); - } + DeviceReport deviceTrips = new DeviceReport(); + Device device = Context.getIdentityManager().getDeviceById(deviceId); + deviceTrips.setDeviceName(device.getName()); + sheetNames.add(deviceTrips.getDeviceName()); + if (device.getGroupId() != 0) { + Group group = Context.getDeviceManager().getGroupById(device.getGroupId()); + if (group != null) { + deviceTrips.setGroupName(group.getName()); } - deviceTrips.setObjects(detectTrips(deviceId, from, to)); - devicesTrips.add(deviceTrips); } + deviceTrips.setObjects(trips); + devicesTrips.add(deviceTrips); } String templatePath = Context.getConfig().getString("report.templatesPath", "templates/export/"); diff --git a/templates/export/events.xlsx b/templates/export/events.xlsx index 99a3f7437..4f9569324 100644 Binary files a/templates/export/events.xlsx and b/templates/export/events.xlsx differ diff --git a/templates/export/route.xlsx b/templates/export/route.xlsx index 6f74cfb45..efd659e64 100644 Binary files a/templates/export/route.xlsx and b/templates/export/route.xlsx differ diff --git a/templates/export/summary.xlsx b/templates/export/summary.xlsx index a975c54fe..a989e0d19 100644 Binary files a/templates/export/summary.xlsx and b/templates/export/summary.xlsx differ diff --git a/templates/export/trips.xlsx b/templates/export/trips.xlsx index 8e13d5ff8..8e214a8a3 100644 Binary files a/templates/export/trips.xlsx and b/templates/export/trips.xlsx differ -- cgit v1.2.3