aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2012-04-29 07:26:44 +0000
committerAnton Tananaev <anton.tananaev@gmail.com>2012-04-29 07:26:44 +0000
commit933639c9ae85204011b40540584bc2c2058e4a52 (patch)
tree3f6eaeff9e245888fc26349d95d4b572e2aefd7f /src
parent3291206a50732601dedec5aa4982a8d6db87e16e (diff)
downloadtraccar-server-933639c9ae85204011b40540584bc2c2058e4a52.tar.gz
traccar-server-933639c9ae85204011b40540584bc2c2058e4a52.tar.bz2
traccar-server-933639c9ae85204011b40540584bc2c2058e4a52.zip
Diffstat (limited to 'src')
-rw-r--r--src/index.html213
-rw-r--r--src/org/traccar/Server.java2
-rw-r--r--src/org/traccar/http/WebServer.java112
-rw-r--r--src/org/traccar/model/DataManager.java4
-rw-r--r--src/org/traccar/model/DatabaseDataManager.java27
5 files changed, 347 insertions, 11 deletions
diff --git a/src/index.html b/src/index.html
new file mode 100644
index 000000000..1b782db82
--- /dev/null
+++ b/src/index.html
@@ -0,0 +1,213 @@
+<!doctype html>
+<html>
+<head>
+<title>Traccar Manager</title>
+<link rel="stylesheet" type="text/css" href="http://cdn.sencha.io/ext-4.1.0-gpl/resources/css/ext-all.css" />
+<script type="text/javascript" src="http://cdn.sencha.io/ext-4.1.0-gpl/ext-all.js"></script>
+
+<!-- WARNING: integrate to page -->
+<!-- <script type="text/javascript" src="https://raw.github.com/VinylFox/ExtJS.ux.GMapPanel/master/src/GMapPanel3.js"></script> -->
+
+<script type="text/javascript">
+Ext.onReady(function() {
+
+ Ext.define('Device', {
+ extend: 'Ext.data.Model',
+ fields: [
+ {name: 'id', type: 'int'},
+ {name: 'imei',type: 'string'}
+ ]
+ });
+
+ Ext.define('Position', {
+ extend: 'Ext.data.Model',
+ fields: [
+ {name: 'device_id', type: 'int'},
+ {name: 'time', type: 'date'},
+ {name: 'valid', type: 'boolean'},
+ {name: 'latitude', type: 'float'},
+ {name: 'longitude', type: 'float'},
+ {name: 'speed', type: 'float'},
+ {name: 'course', type: 'float'},
+ {name: 'power', type: 'float'}
+ ]
+ });
+
+ var devicesUrl = 'devices.json';
+ var positionsUrl = 'positions.json';
+
+ var devices = Ext.create('Ext.data.Store', {
+ id: 'devices',
+ model: 'Device',
+ fields: ['id', 'imei'],
+ proxy: {
+ type: 'ajax',
+ url: devicesUrl,
+ reader: {
+ type: 'json',
+ root: 'results'
+ }
+ }
+ });
+
+ var positions = Ext.create('Ext.data.Store', {
+ id: 'positions',
+ model: 'Position',
+ fields: [
+ 'device_id',
+ 'time',
+ 'valid',
+ 'latitude',
+ 'longitude',
+ 'speed',
+ 'course',
+ 'power'
+ ],
+ proxy: {
+ type: 'ajax',
+ url: positionsUrl,
+ reader: {
+ type: 'json',
+ root: 'results'
+ }
+ }
+ });
+
+ console.log(positions);
+
+ var devicesPanel = Ext.create('Ext.grid.Panel', {
+ title: 'Devices',
+ region: 'west',
+ split: true,
+ width: 300,
+ margins: {top: 5, bottom: 0, right: 0, left: 5},
+
+ sortableColumns: false,
+ enableColumnHide: false,
+
+ store: devices,
+ tbar: [
+ {
+ id: 'device_update',
+ text: 'Update',
+ handler : function() {
+ devices.load();
+ }
+ },
+ {
+ id: 'device_add',
+ text: 'Add',
+ handler : function() {
+ Ext.Msg.prompt('Add', 'Device IMEI:', function() {
+ });
+ }
+ },
+ {
+ id: 'device_remove',
+ text: 'Remove',
+ disabled: true,
+ handler : function() {
+ Ext.Msg.confirm('Remove', 'Are you sure to remove item?', function() {
+ });
+ }
+ },
+ {
+ id: 'device_edit',
+ text: 'Edit',
+ disabled: true,
+ handler : function() {
+ Ext.Msg.prompt('Edit', 'Device IMEI:', function() {
+ }, this, false, devicesPanel.getSelectionModel().getLastSelected().get('imei'));
+ }
+ }
+ ],
+ columns: [
+ {header: 'Id', dataIndex: 'id'},
+ {header: 'IMEI', dataIndex: 'imei', flex: 1}
+ ],
+ listeners: {
+ selectionchange: function(sender, selected, eOpts) {
+ if (selected.length != 0) {
+ Ext.getCmp('device_remove').enable();
+ Ext.getCmp('device_edit').enable();
+
+ positions.getProxy().url = positionsUrl + '?deviceId=' +
+ devicesPanel.getSelectionModel().getLastSelected().get('id');
+ positions.load();
+ Ext.getCmp('position_update').enable();
+ } else {
+ Ext.getCmp('position_update').disable();
+ positions.getProxy().url = positionsUrl;
+ positions.load();
+
+ Ext.getCmp('device_edit').disable();
+ Ext.getCmp('device_remove').disable();
+ }
+ }
+ }
+ });
+
+ var positionsPanel = Ext.create('Ext.grid.Panel', {
+ title: 'Positions',
+ region: 'south',
+ split: true,
+ height: 300,
+ margins: {top: 0, bottom: 5, right: 5, left: 5},
+
+ sortableColumns: false,
+ enableColumnHide: false,
+
+ store: positions,
+ tbar: [
+ {
+ id: 'position_update',
+ text: 'Update',
+ disabled: true,
+ handler : function() {
+ positions.load();
+ }
+ }
+ ],
+ columns: [
+ {header: 'Device Id', dataIndex: 'device_id'},
+ {
+ header: 'Time',
+ dataIndex: 'time',
+ flex: 1,
+ renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s')
+ },
+ {header: 'Valid', dataIndex: 'valid'},
+ {header: 'Latitude', dataIndex: 'latitude'},
+ {header: 'Longitude', dataIndex: 'longitude'},
+ {header: 'Speed', dataIndex: 'speed'},
+ {header: 'Course', dataIndex: 'course'},
+ {header: 'Power', dataIndex: 'power'}
+ ]
+ });
+
+ /*var map = Ext.create('Ext.ux.GMapPanel', {
+ id: 'gmap',
+ setCenter: {lat: 0, lng: 0}
+ });*/
+
+ var mapPanel = Ext.create('Ext.panel.Panel', {
+ title: 'Map',
+ region: 'center',
+ margins: {top: 5, bottom: 0, right: 5, left: 0}/*,
+
+ layout: 'fit',
+ items: [map]*/
+ });
+
+ Ext.create('Ext.container.Viewport', {
+ renderTo: Ext.getBody(),
+ layout: 'border',
+ items: [devicesPanel, positionsPanel, mapPanel]
+ });
+
+ devices.load();
+});
+</script>
+</head>
+<body></body>
+</html>
diff --git a/src/org/traccar/Server.java b/src/org/traccar/Server.java
index 1c11f07b0..fda5e6ad3 100644
--- a/src/org/traccar/Server.java
+++ b/src/org/traccar/Server.java
@@ -87,7 +87,7 @@ public class Server {
// Initialize web server
Integer port = Integer.valueOf(properties.getProperty("http.port", "8082"));
- webServer = new WebServer(port);
+ webServer = new WebServer(port, dataManager);
}
/**
diff --git a/src/org/traccar/http/WebServer.java b/src/org/traccar/http/WebServer.java
index 9b3b78413..a99f6e11b 100644
--- a/src/org/traccar/http/WebServer.java
+++ b/src/org/traccar/http/WebServer.java
@@ -15,23 +15,130 @@
*/
package org.traccar.http;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.util.Iterator;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.handler.AbstractHandler;
+import org.traccar.model.DataManager;
+import org.traccar.model.Device;
+import org.traccar.model.Position;
/**
- * Integrated http server
+ * Integrated HTTP server
*/
public class WebServer {
private Server server;
- public WebServer(Integer port) {
+ public class WebHandler extends AbstractHandler {
+
+ private DataManager dataManager;
+
+ public WebHandler(DataManager dataManager) {
+ this.dataManager = dataManager;
+ }
+
+ public void handleIndex(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response)
+ throws IOException, ServletException {
+
+ response.setContentType("text/html");
+
+ InputStream in = this.getClass().getClassLoader().getResourceAsStream("index.html");
+ OutputStream out = response.getOutputStream();
+
+ byte[] buffer = new byte[1024]; // Magic number
+ int count;
+ while ((count = in.read(buffer)) != -1) {
+ out.write(buffer, 0, count);
+ }
+ out.flush();
+ }
+
+ public void handleData(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response)
+ throws IOException, ServletException {
+
+ response.setContentType("application/json");
+
+ PrintWriter out = response.getWriter();
+ out.print("{'success':true,'results':[");
+
+ try {
+ if (target.equals("/devices.json")) {
+
+ Iterator<Device> i = dataManager.getDevices().iterator();
+ while (i.hasNext()) {
+ Device device = i.next();
+ out.format("{'id':%d,'imei':'%s'}",
+ device.getId(),
+ device.getImei());
+ if (i.hasNext()) out.print(",");
+ }
+
+ } else if (target.equals("/positions.json")) {
+
+ String deviceId = request.getParameter("deviceId");
+ if (deviceId != null) {
+ Iterator<Position> i = dataManager.getPositions(Long.valueOf(deviceId)).iterator();
+ while (i.hasNext()) {
+ Position position = i.next();
+ out.format("{'device_id':%d,'time':'%tF %tT','valid':%b,'latitude':%f,'longitude':%f,'speed':%f,'course':%f}",
+ position.getDeviceId(),
+ position.getTime(),
+ position.getTime(),
+ position.getValid(),
+ position.getLatitude(),
+ position.getLongitude(),
+ position.getSpeed(),
+ position.getCourse());
+ if (i.hasNext()) out.print(",");
+ }
+ }
+
+ }
+ } catch (Exception error) {
+ System.out.println(error.getMessage());
+ }
+
+ //" {'id': 1, 'imei': '123456789012345'} ]}");
+ out.print("]}");
+ out.flush();
+ }
+
+ public void handleOther(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response)
+ throws IOException, ServletException {
+ response.sendRedirect(response.encodeRedirectURL("/"));
+ }
+
+ public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response)
+ throws IOException, ServletException
+ {
+ if (target.equals("/") || target.equals("/index.html")) {
+ handleIndex(target, baseRequest, request, response);
+ } else if (target.matches("/.+\\.json")) {
+ handleData(target, baseRequest, request, response);
+ } else {
+ handleOther(target, baseRequest, request, response);
+ }
+ }
+ }
+
+ public WebServer(Integer port, DataManager dataManager) {
server = new Server(port);
+ server.setHandler(new WebHandler(dataManager));
}
public void start() {
try {
server.start();
} catch (Exception error) {
+ System.out.println(error.getMessage());
}
}
@@ -39,6 +146,7 @@ public class WebServer {
try {
server.stop();
} catch (Exception error) {
+ System.out.println(error.getMessage());
}
}
diff --git a/src/org/traccar/model/DataManager.java b/src/org/traccar/model/DataManager.java
index a7dbd6719..d0f290962 100644
--- a/src/org/traccar/model/DataManager.java
+++ b/src/org/traccar/model/DataManager.java
@@ -25,7 +25,7 @@ public interface DataManager {
/**
* Manage devices
*/
- public List getDevices() throws Exception;
+ public List<Device> getDevices() throws Exception;
public void addDevice(Device device) throws Exception;
public void addUpdate(Device device) throws Exception;
public void addRemove(Device device) throws Exception;
@@ -34,7 +34,7 @@ public interface DataManager {
/**
* Manage positions
*/
- public List getPositions(Long deviceId) throws Exception;
+ public List<Position> getPositions(Long deviceId) throws Exception;
public void addPosition(Position position) throws Exception;
}
diff --git a/src/org/traccar/model/DatabaseDataManager.java b/src/org/traccar/model/DatabaseDataManager.java
index 8e12f177c..7d90ac194 100644
--- a/src/org/traccar/model/DatabaseDataManager.java
+++ b/src/org/traccar/model/DatabaseDataManager.java
@@ -77,9 +77,9 @@ public class DatabaseDataManager implements DataManager {
queryAddPosition = new NamedParameterStatement(connection, query);
}
- public synchronized List getDevices() throws SQLException {
+ public synchronized List<Device> getDevices() throws SQLException {
- List deviceList = new LinkedList();
+ List<Device> deviceList = new LinkedList();
queryGetDevices.prepare();
ResultSet result = queryGetDevices.executeQuery();
@@ -101,9 +101,8 @@ public class DatabaseDataManager implements DataManager {
if ((devices == null) || (Calendar.getInstance().getTimeInMillis() - devicesLastUpdate.getTimeInMillis() > devicesRefreshDelay)) {
devices = new HashMap();
- List deviceList = getDevices();
- for (Object device: deviceList) {
- devices.put(((Device) device).getImei(), device);
+ for (Device device: getDevices()) {
+ devices.put(device.getImei(), device);
}
devicesLastUpdate = Calendar.getInstance();
}
@@ -111,7 +110,23 @@ public class DatabaseDataManager implements DataManager {
return (Device) devices.get(imei);
}
- public List getPositions(Long deviceId) { return null; } // TODO: implement
+ public List<Position> getPositions(Long deviceId) { // TODO: implement
+
+ List<Position> positionList = new LinkedList();
+
+ Position p = new Position();
+ p.setDeviceId(new Long(1));
+ p.setTime(new Date());
+ p.setValid(true);
+ p.setLatitude(1.0);
+ p.setLongitude(1.0);
+ p.setSpeed(1.0);
+ p.setCourse(1.0);
+
+ positionList.add(p);
+
+ return positionList;
+ }
public synchronized void addPosition(Position position) throws SQLException {