aboutsummaryrefslogtreecommitdiff
path: root/src/org
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2012-05-06 08:56:39 +0000
committerAnton Tananaev <anton.tananaev@gmail.com>2012-05-06 08:56:39 +0000
commit131152712d834b39f44ae94ab5d2db04520d3125 (patch)
tree93a224f4cafba45d668153499a5d607382f46d9b /src/org
parent0218464141fc64f060699c6e98ed5d4f0814e139 (diff)
downloadtrackermap-server-131152712d834b39f44ae94ab5d2db04520d3125.tar.gz
trackermap-server-131152712d834b39f44ae94ab5d2db04520d3125.tar.bz2
trackermap-server-131152712d834b39f44ae94ab5d2db04520d3125.zip
Diffstat (limited to 'src/org')
-rw-r--r--src/org/traccar/Server.java14
-rw-r--r--src/org/traccar/helper/NamedParameterStatement.java13
-rw-r--r--src/org/traccar/http/WebServer.java53
-rw-r--r--src/org/traccar/model/DataManager.java4
-rw-r--r--src/org/traccar/model/DatabaseDataManager.java101
5 files changed, 147 insertions, 38 deletions
diff --git a/src/org/traccar/Server.java b/src/org/traccar/Server.java
index fda5e6ad3..654d32321 100644
--- a/src/org/traccar/Server.java
+++ b/src/org/traccar/Server.java
@@ -86,15 +86,19 @@ public class Server {
initAvl08Server(properties);
// Initialize web server
- Integer port = Integer.valueOf(properties.getProperty("http.port", "8082"));
- webServer = new WebServer(port, dataManager);
+ if (Boolean.valueOf(properties.getProperty("http.enable"))) {
+ Integer port = Integer.valueOf(properties.getProperty("http.port", "8082"));
+ webServer = new WebServer(port, dataManager);
+ }
}
/**
* Start
*/
public void start() {
- webServer.start();
+ if (webServer != null) {
+ webServer.start();
+ }
for (Object server: serverList) {
((TrackerServer) server).start();
}
@@ -107,7 +111,9 @@ public class Server {
for (Object server: serverList) {
((TrackerServer) server).stop();
}
- webServer.stop();
+ if (webServer != null) {
+ webServer.stop();
+ }
}
/**
diff --git a/src/org/traccar/helper/NamedParameterStatement.java b/src/org/traccar/helper/NamedParameterStatement.java
index c86217182..e898af2d3 100644
--- a/src/org/traccar/helper/NamedParameterStatement.java
+++ b/src/org/traccar/helper/NamedParameterStatement.java
@@ -30,12 +30,12 @@ public class NamedParameterStatement {
* Index mapping
*/
private final Map indexMap;
-
+
/**
* Query string
*/
private final String parsedQuery;
-
+
/**
* Database connection
*/
@@ -106,7 +106,7 @@ public class NamedParameterStatement {
return parsedQuery.toString();
}
-
+
public void prepare() throws SQLException {
try {
if (statement == null) {
@@ -134,6 +134,13 @@ public class NamedParameterStatement {
}
/**
+ * Return generated keys
+ */
+ public ResultSet getGeneratedKeys() throws SQLException {
+ return statement.getGeneratedKeys();
+ }
+
+ /**
* Immediately closes the statement
*/
public void close() throws SQLException {
diff --git a/src/org/traccar/http/WebServer.java b/src/org/traccar/http/WebServer.java
index d42f8fe2c..53cdb2966 100644
--- a/src/org/traccar/http/WebServer.java
+++ b/src/org/traccar/http/WebServer.java
@@ -20,6 +20,8 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -79,28 +81,65 @@ public class WebServer {
out.flush();
}
+ private Device parseDevice(String json) {
+
+ Pattern pattern = Pattern.compile("\\{\"id\":(\\d+),\"imei\":\"(.*)\"\\}");
+ Matcher parser = pattern.matcher(json);
+ if (parser.matches()) {
+ Device device = new Device();
+ device.setId(Long.valueOf(parser.group(1)));
+ device.setImei(parser.group(2));
+ return device;
+ }
+
+ return null;
+ }
+
+ // TODO: separate method into small parts
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();
+ String action = request.getParameter("action");
+ if (action == null) {
+ Iterator<Device> i = dataManager.getDevices().iterator();
+ out.print("{'success':true,'results':[");
+ while (i.hasNext()) {
+ Device device = i.next();
+ out.format("{'id':%d,'imei':'%s'}",
+ device.getId(),
+ device.getImei());
+ if (i.hasNext()) out.print(",");
+ }
+ } else if (action.equals("create")) {
+ Device device = parseDevice(request.getReader().readLine());
+ dataManager.addDevice(device);
+ out.print("{'success':true,'results':[");
+ out.format("{'id':%d,'imei':'%s'}",
+ device.getId(),
+ device.getImei());
+ } else if (action.equals("update")) {
+ Device device = parseDevice(request.getReader().readLine());
+ dataManager.updateDevice(device);
+ out.print("{'success':true,'results':[");
out.format("{'id':%d,'imei':'%s'}",
device.getId(),
device.getImei());
- if (i.hasNext()) out.print(",");
+ } else if (action.equals("destroy")) {
+ Device device = parseDevice(request.getReader().readLine());
+ dataManager.removeDevice(device);
+ out.print("{'success':true,'results':[");
}
} else if (target.equals("/positions.json")) {
+ out.print("{'success':true,'results':[");
String deviceId = request.getParameter("deviceId");
if (deviceId != null) {
Iterator<Position> i = dataManager.getPositions(Long.valueOf(deviceId)).iterator();
@@ -120,10 +159,10 @@ public class WebServer {
}
} catch (Exception error) {
+ out.print("{'success':false,'results':[");
System.out.println(error.getMessage());
}
- //" {'id': 1, 'imei': '123456789012345'} ]}");
out.print("]}");
out.flush();
}
@@ -138,7 +177,7 @@ public class WebServer {
{
if (target.equals("/") || target.equals("/index.html")) {
handleIndex(target, baseRequest, request, response);
- } else if (target.matches("/favicon.ico")) {
+ } else if (target.equals("/favicon.ico")) {
handleIcon(target, baseRequest, request, response);
} else if (target.matches("/.+\\.json")) {
handleData(target, baseRequest, request, response);
diff --git a/src/org/traccar/model/DataManager.java b/src/org/traccar/model/DataManager.java
index d0f290962..70cf63380 100644
--- a/src/org/traccar/model/DataManager.java
+++ b/src/org/traccar/model/DataManager.java
@@ -27,8 +27,8 @@ public interface DataManager {
*/
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;
+ public void updateDevice(Device device) throws Exception;
+ public void removeDevice(Device device) throws Exception;
public Device getDeviceByImei(String imei) throws Exception;
/**
diff --git a/src/org/traccar/model/DatabaseDataManager.java b/src/org/traccar/model/DatabaseDataManager.java
index 7d90ac194..c5492bc8a 100644
--- a/src/org/traccar/model/DatabaseDataManager.java
+++ b/src/org/traccar/model/DatabaseDataManager.java
@@ -35,16 +35,13 @@ public class DatabaseDataManager implements DataManager {
* Database statements
*/
private NamedParameterStatement queryGetDevices;
+ private NamedParameterStatement queryAddDevice;
+ private NamedParameterStatement queryUpdateDevice;
+ private NamedParameterStatement queryRemoveDevice;
+ private NamedParameterStatement queryGetPositions;
private NamedParameterStatement queryAddPosition;
/**
- * Devices cache
- */
- private Map devices;
- private Calendar devicesLastUpdate;
- private Long devicesRefreshDelay;
-
- /**
* Initialize database
*/
private void initDatabase(Properties properties)
@@ -70,9 +67,24 @@ public class DatabaseDataManager implements DataManager {
String password = properties.getProperty("database.password");
AdvancedConnection connection = new AdvancedConnection(url, user, password);
- String query = properties.getProperty("database.selectDevice");
+ // Load statements from configuration
+ String query;
+
+ query = properties.getProperty("database.selectDevice");
queryGetDevices = new NamedParameterStatement(connection, query);
+ query = properties.getProperty("database.insertDevice");
+ queryAddDevice = new NamedParameterStatement(connection, query);
+
+ query = properties.getProperty("database.updateDevice");
+ queryUpdateDevice = new NamedParameterStatement(connection, query);
+
+ query = properties.getProperty("database.deleteDevice");
+ queryRemoveDevice = new NamedParameterStatement(connection, query);
+
+ query = properties.getProperty("database.selectPosition");
+ queryGetPositions = new NamedParameterStatement(connection, query);
+
query = properties.getProperty("database.insertPosition");
queryAddPosition = new NamedParameterStatement(connection, query);
}
@@ -93,9 +105,46 @@ public class DatabaseDataManager implements DataManager {
return deviceList;
}
- public void addDevice(Device device) {} // TODO: implement
- public void addUpdate(Device device) {} // TODO: implement
- public void addRemove(Device device) {} // TODO: implement
+ public synchronized void addDevice(Device device) throws SQLException {
+
+ queryAddDevice.prepare();
+ queryAddDevice.setString("imei", device.getImei());
+ queryAddDevice.executeUpdate();
+
+ // Find generated id
+ ResultSet result = queryAddDevice.getGeneratedKeys();
+ if (result.next()) {
+ device.setId(result.getLong(1));
+ }
+
+ devices = null;
+ }
+
+ public synchronized void updateDevice(Device device) throws SQLException {
+
+ queryUpdateDevice.prepare();
+ queryUpdateDevice.setLong("id", device.getId());
+ queryUpdateDevice.setString("imei", device.getImei());
+ queryUpdateDevice.executeUpdate();
+
+ devices = null;
+ }
+
+ public synchronized void removeDevice(Device device) throws SQLException {
+
+ queryRemoveDevice.prepare();
+ queryRemoveDevice.setLong("id", device.getId());
+ queryRemoveDevice.executeUpdate();
+
+ devices = null;
+ }
+
+ /**
+ * Devices cache
+ */
+ private Map devices;
+ private Calendar devicesLastUpdate;
+ private Long devicesRefreshDelay;
public Device getDeviceByImei(String imei) throws SQLException {
@@ -110,20 +159,26 @@ public class DatabaseDataManager implements DataManager {
return (Device) devices.get(imei);
}
- public List<Position> getPositions(Long deviceId) { // TODO: implement
+ public synchronized List<Position> getPositions(Long deviceId) throws SQLException {
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);
+ queryGetPositions.prepare();
+ queryGetPositions.setLong("device_id", deviceId);
+ ResultSet result = queryGetPositions.executeQuery();
+ while (result.next()) {
+ // TODO: include other parameters
+ Position position = new Position();
+ position.setDeviceId(result.getLong("device_id"));
+ position.setTime(result.getTimestamp("time"));
+ position.setValid(result.getBoolean("valid"));
+ position.setLatitude(result.getDouble("latitude"));
+ position.setLongitude(result.getDouble("longitude"));
+ position.setSpeed(result.getDouble("speed"));
+ position.setCourse(result.getDouble("course"));
+ position.setPower(result.getDouble("power"));
+ positionList.add(position);
+ }
return positionList;
}
@@ -144,6 +199,8 @@ public class DatabaseDataManager implements DataManager {
queryAddPosition.setString("extended_info", position.getExtendedInfo());
queryAddPosition.executeUpdate();
+
+ // TODO: probably return row id
}
}