From 6dfee3947ef96e1092b668ecfc7c58037355ffb8 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 20 May 2015 16:57:21 +1200 Subject: Implement position report loading --- src/org/traccar/database/DataManager.java | 17 +++++++------ src/org/traccar/http/BaseServlet.java | 6 ++++- src/org/traccar/http/JsonConverter.java | 8 ++++-- src/org/traccar/http/PositionServlet.java | 42 +++++++++++++++++++++++++++++++ src/org/traccar/http/WebServer.java | 1 + web/app/view/report/Report.js | 23 ++++++++++++----- web/app/view/report/ReportController.js | 29 ++++++++++++++++++++- 7 files changed, 109 insertions(+), 17 deletions(-) create mode 100644 src/org/traccar/http/PositionServlet.java diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 5e921c0ea..82e312c7e 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -25,13 +25,7 @@ import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.util.Calendar; -import java.util.Collection; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Properties; +import java.util.*; import javax.sql.DataSource; import org.traccar.helper.DriverDelegate; import org.traccar.model.Device; @@ -381,4 +375,13 @@ public class DataManager { .executeUpdate(); } + public Collection getPositions(long userId, long deviceId, Date from, Date to) throws SQLException { + return QueryBuilder.create(dataSource, + "SELECT * FROM position WHERE deviceId = :deviceId AND fixTime BETWEEN :from AND :to;") + .setLong("deviceId", deviceId) + .setDate("from", from) + .setDate("to", to) + .executeQuery(new Position()); + } + } diff --git a/src/org/traccar/http/BaseServlet.java b/src/org/traccar/http/BaseServlet.java index 122dbe70b..be4b41631 100644 --- a/src/org/traccar/http/BaseServlet.java +++ b/src/org/traccar/http/BaseServlet.java @@ -70,7 +70,11 @@ public abstract class BaseServlet extends HttpServlet { public void sendResponse(Writer writer, Exception error) throws IOException { JsonObjectBuilder result = Json.createObjectBuilder(); result.add("success", false); - result.add("error", error.getMessage()); + if (error.getMessage() != null) { + result.add("error", error.getMessage()); + } else { + result.add("error", error.getClass().getSimpleName()); + } writer.write(result.build().toString()); } diff --git a/src/org/traccar/http/JsonConverter.java b/src/org/traccar/http/JsonConverter.java index f7c5a5622..6cdba5492 100644 --- a/src/org/traccar/http/JsonConverter.java +++ b/src/org/traccar/http/JsonConverter.java @@ -34,8 +34,12 @@ import org.traccar.model.Factory; public class JsonConverter { - public static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ"); - + private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); + + public static Date parseDate(String value) throws ParseException { + return dateFormat.parse(value); + } + public static T objectFromJson(Reader reader, T prototype) throws ParseException { return objectFromJson(Json.createReader(reader).readObject(), prototype); } diff --git a/src/org/traccar/http/PositionServlet.java b/src/org/traccar/http/PositionServlet.java new file mode 100644 index 000000000..e6348ec54 --- /dev/null +++ b/src/org/traccar/http/PositionServlet.java @@ -0,0 +1,42 @@ +/* + * Copyright 2015 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. + * 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.http; + +import org.traccar.Context; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class PositionServlet extends BaseServlet { + + @Override + protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception { + if (command.equals("/get")) { + get(req, resp); + } + return true; + } + + private void get(HttpServletRequest req, HttpServletResponse resp) throws Exception { + sendResponse(resp.getWriter(), JsonConverter.arrayToJson( + Context.getDataManager().getPositions( + getUserId(req.getSession()), + Long.valueOf(req.getParameter("deviceId")), + JsonConverter.parseDate(req.getParameter("from")), + JsonConverter.parseDate(req.getParameter("to"))))); + } + +} diff --git a/src/org/traccar/http/WebServer.java b/src/org/traccar/http/WebServer.java index 268b53856..4a7b029e1 100644 --- a/src/org/traccar/http/WebServer.java +++ b/src/org/traccar/http/WebServer.java @@ -54,6 +54,7 @@ public class WebServer { servletHandler.setContextPath("/api"); servletHandler.addServlet(new ServletHolder(new AsyncServlet()), "/async/*"); servletHandler.addServlet(new ServletHolder(new DeviceServlet()), "/device/*"); + servletHandler.addServlet(new ServletHolder(new PositionServlet()), "/position/*"); servletHandler.addServlet(new ServletHolder(new MainServlet()), "/*"); ResourceHandler mobileResourceHandler = new ResourceHandler(); diff --git a/web/app/view/report/Report.js b/web/app/view/report/Report.js index d39513873..a8b191ef1 100644 --- a/web/app/view/report/Report.js +++ b/web/app/view/report/Report.js @@ -32,6 +32,7 @@ Ext.define('Traccar.view.report.Report', { html: strings.report_device }, { xtype: 'combobox', + reference: 'deviceField', store: 'Devices', valueField: 'id', displayField: 'name', @@ -41,22 +42,32 @@ Ext.define('Traccar.view.report.Report', { xtype: 'tbtext', html: strings.report_from }, { - xtype: 'datefield' + xtype: 'datefield', + reference: 'fromDateField', + value: new Date() }, { xtype: 'timefield', - maxWidth: styles.report_time + reference: 'fromTimeField', + maxWidth: styles.report_time, + value: new Date() }, '-', { xtype: 'tbtext', html: strings.report_to }, { - xtype: 'datefield' + xtype: 'datefield', + reference: 'toDateField', + value: new Date() }, { xtype: 'timefield', - maxWidth: styles.report_time + reference: 'toTimeField', + maxWidth: styles.report_time, + value: new Date() }, '-', { - text: strings.report_show + text: strings.report_show, + handler: 'onShowClick' }, { - text: strings.report_clear + text: strings.report_clear, + handler: 'onClearClick' }], columns: [ diff --git a/web/app/view/report/ReportController.js b/web/app/view/report/ReportController.js index 92fd1ba7e..48cfda74f 100644 --- a/web/app/view/report/ReportController.js +++ b/web/app/view/report/ReportController.js @@ -18,7 +18,34 @@ Ext.define('Traccar.view.report.ReportController', { extend: 'Ext.app.ViewController', alias: 'controller.report', - onSelectionChange: function(selected) { + onShowClick: function() { + + var deviceId = this.lookupReference('deviceField').getValue(); + var fromDate = this.lookupReference('fromDateField').getValue(); + var fromTime = this.lookupReference('fromTimeField').getValue(); + + var from = new Date( + fromDate.getYear(), fromDate.getMonth(), fromDate.getDay(), + fromTime.getHours(), fromTime.getMinutes(), fromTime.getSeconds(), fromTime.getMilliseconds()); + + var toDate = this.lookupReference('toDateField').getValue(); + var toTime = this.lookupReference('toTimeField').getValue(); + + var to = new Date( + toDate.getYear(), toDate.getMonth(), toDate.getDay(), + toTime.getHours(), toTime.getMinutes(), toTime.getSeconds(), toTime.getMilliseconds()); + + var store = Ext.getStore('Positions'); + store.load({ + params:{ + deviceId: deviceId, + from: from, + to: to + } + }); + }, + + onClearClick: function() { } }); -- cgit v1.2.3