aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/http/DeviceServlet.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/http/DeviceServlet.java')
-rw-r--r--src/org/traccar/http/DeviceServlet.java29
1 files changed, 29 insertions, 0 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 {