aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/http
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-04-28 22:41:07 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-04-28 22:41:07 +1200
commita5f47492857a175548beff07cfb565129f687ae2 (patch)
treed5650b6f5db7de9296d8d00cab505b9f594112f6 /src/org/traccar/http
parentc14f99819add0971a82ced01bf6d54ed77aedb74 (diff)
downloadtrackermap-server-a5f47492857a175548beff07cfb565129f687ae2.tar.gz
trackermap-server-a5f47492857a175548beff07cfb565129f687ae2.tar.bz2
trackermap-server-a5f47492857a175548beff07cfb565129f687ae2.zip
Implement API to create devices
Diffstat (limited to 'src/org/traccar/http')
-rw-r--r--src/org/traccar/http/DeviceServlet.java29
-rw-r--r--src/org/traccar/http/MainServlet.java2
2 files changed, 30 insertions, 1 deletions
diff --git a/src/org/traccar/http/DeviceServlet.java b/src/org/traccar/http/DeviceServlet.java
index 260083e0e..5cda5708e 100644
--- a/src/org/traccar/http/DeviceServlet.java
+++ b/src/org/traccar/http/DeviceServlet.java
@@ -17,13 +17,18 @@ package org.traccar.http;
import java.io.IOException;
import java.sql.SQLException;
+import java.text.ParseException;
import javax.json.Json;
+import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
+import javax.json.JsonReader;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.traccar.Context;
+import org.traccar.helper.Log;
+import org.traccar.model.Device;
public class DeviceServlet extends HttpServlet {
@@ -65,6 +70,30 @@ public class DeviceServlet extends HttpServlet {
}
private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+
+ long userId = (Long) req.getSession().getAttribute(MainServlet.USER_ID);
+
+ JsonReader jsonReader = Json.createReader(req.getReader());
+ JsonObject jsonObject = jsonReader.readObject();
+ Device device = new Device();
+ try {
+ device.fromJson(jsonObject);
+ } catch (ParseException error) {
+ Log.warning(error);
+ }
+
+ JsonObjectBuilder result = Json.createObjectBuilder();
+
+ try {
+ result.add("success", true);
+ Context.getDataManager().addDevice(device);
+ Context.getDataManager().linkDevice(userId, device.getId());
+ } catch(SQLException error) {
+ result.add("success", false);
+ result.add("error", error.getMessage());
+ }
+
+ resp.getWriter().println(result.build().toString());
}
private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
diff --git a/src/org/traccar/http/MainServlet.java b/src/org/traccar/http/MainServlet.java
index 62345489e..10920accb 100644
--- a/src/org/traccar/http/MainServlet.java
+++ b/src/org/traccar/http/MainServlet.java
@@ -173,7 +173,7 @@ public class MainServlet extends HttpServlet {
JsonObjectBuilder result = Json.createObjectBuilder();
result.add("success", true);
- result.add("data", ObjectConverter.convert(positions.values()));
+ result.add("data", ObjectConverter.arrayToJson(positions.values()));
positions.clear();
try {