aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/org/traccar/database/JsonConvertable.java30
-rw-r--r--src/org/traccar/database/ObjectConverter.java24
-rw-r--r--src/org/traccar/database/ResultSetConverter.java6
-rw-r--r--src/org/traccar/model/Data.java28
-rw-r--r--src/org/traccar/model/Device.java31
-rw-r--r--src/org/traccar/model/Position.java44
-rw-r--r--src/org/traccar/model/User.java45
7 files changed, 177 insertions, 31 deletions
diff --git a/src/org/traccar/database/JsonConvertable.java b/src/org/traccar/database/JsonConvertable.java
new file mode 100644
index 000000000..37af9a617
--- /dev/null
+++ b/src/org/traccar/database/JsonConvertable.java
@@ -0,0 +1,30 @@
+/*
+ * 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.database;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import javax.json.JsonObject;
+
+public interface JsonConvertable {
+
+ public static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
+
+ public JsonObject toJson();
+
+ public void fromJson(JsonObject json) throws Exception;
+
+}
diff --git a/src/org/traccar/database/ObjectConverter.java b/src/org/traccar/database/ObjectConverter.java
index bd5b03064..4ab83faec 100644
--- a/src/org/traccar/database/ObjectConverter.java
+++ b/src/org/traccar/database/ObjectConverter.java
@@ -15,37 +15,19 @@
*/
package org.traccar.database;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
import java.util.Collection;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
-import javax.json.JsonObjectBuilder;
-import org.traccar.model.Position;
public class ObjectConverter {
- private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
-
- public static JsonArray convert(Collection<Position> positions) {
+ public static JsonArray convert(Collection<? extends JsonConvertable> collection) {
JsonArrayBuilder array = Json.createArrayBuilder();
- for (Position position : positions) {
- JsonObjectBuilder object = Json.createObjectBuilder();
-
- //object.add("id", position.getId());
- object.add("time", dateFormat.format(position.getFixTime()));
- object.add("valid", position.getValid());
- object.add("latitude", position.getLatitude());
- object.add("longitude", position.getLongitude());
- object.add("altitude", position.getAltitude());
- object.add("speed", position.getSpeed());
- object.add("course", position.getCourse());
- //object.add("other", position.getExtendedInfo());
-
- array.add(object.build());
+ for (JsonConvertable object : collection) {
+ array.add(object.toJson());
}
return array.build();
diff --git a/src/org/traccar/database/ResultSetConverter.java b/src/org/traccar/database/ResultSetConverter.java
index e4aec956c..92cb8fa4e 100644
--- a/src/org/traccar/database/ResultSetConverter.java
+++ b/src/org/traccar/database/ResultSetConverter.java
@@ -26,8 +26,6 @@ import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
public class ResultSetConverter {
-
- private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
public static JsonArray convert(ResultSet rs) throws SQLException {
@@ -66,10 +64,10 @@ public class ResultSetConverter {
obj.add(columnName, rs.getString(columnName));
break;
case java.sql.Types.DATE:
- obj.add(columnName, dateFormat.format(rs.getDate(columnName)));
+ obj.add(columnName, JsonConvertable.dateFormat.format(rs.getDate(columnName)));
break;
case java.sql.Types.TIMESTAMP:
- obj.add(columnName, dateFormat.format(rs.getTimestamp(columnName)));
+ obj.add(columnName, JsonConvertable.dateFormat.format(rs.getTimestamp(columnName)));
break;
default:
break;
diff --git a/src/org/traccar/model/Data.java b/src/org/traccar/model/Data.java
index ce23154c2..1cef813a3 100644
--- a/src/org/traccar/model/Data.java
+++ b/src/org/traccar/model/Data.java
@@ -15,9 +15,15 @@
*/
package org.traccar.model;
+import java.text.ParseException;
import java.util.Date;
+import javax.json.Json;
+import javax.json.JsonObject;
+import javax.json.JsonObjectBuilder;
+import org.traccar.database.JsonConvertable;
+import static org.traccar.database.JsonConvertable.dateFormat;
-public class Data {
+public class Data implements JsonConvertable {
private long id;
public long getId() { return id; }
@@ -39,4 +45,24 @@ public class Data {
public String getExtendedInfo() { return extendedInfo; }
public void setExtendedInfo(String extendedInfo) { this.extendedInfo = extendedInfo; }
+ @Override
+ public JsonObject toJson() {
+ JsonObjectBuilder json = Json.createObjectBuilder();
+ json.add("id", id);
+ json.add("deviceId", deviceId);
+ json.add("serverTime", dateFormat.format(serverTime));
+ json.add("deviceTime", dateFormat.format(deviceTime));
+ //json.add("extendedInfo", extendedInfo);
+ return json.build();
+ }
+
+ @Override
+ public void fromJson(JsonObject json) throws ParseException {
+ id = json.getJsonNumber("id").longValue();
+ deviceId = json.getJsonNumber("deviceId").longValue();
+ serverTime = dateFormat.parse(json.getString("serverTime"));
+ deviceTime = dateFormat.parse(json.getString("deviceTime"));
+ //extendedInfo = json.getString("extendedInfo");
+ }
+
}
diff --git a/src/org/traccar/model/Device.java b/src/org/traccar/model/Device.java
index 273faebcf..4e2f57690 100644
--- a/src/org/traccar/model/Device.java
+++ b/src/org/traccar/model/Device.java
@@ -15,9 +15,14 @@
*/
package org.traccar.model;
+import java.text.ParseException;
import java.util.Date;
+import javax.json.Json;
+import javax.json.JsonObject;
+import javax.json.JsonObjectBuilder;
+import org.traccar.database.JsonConvertable;
-public class Device {
+public class Device implements JsonConvertable {
private long id;
public long getId() { return id; }
@@ -39,4 +44,28 @@ public class Device {
private long dataId;
+ @Override
+ public JsonObject toJson() {
+ JsonObjectBuilder json = Json.createObjectBuilder();
+ json.add("id", id);
+ json.add("name", name);
+ json.add("uniqueId", uniqueId);
+ json.add("status", status);
+ json.add("lastUpdate", dateFormat.format(lastUpdate));
+ json.add("positionId", positionId);
+ json.add("dataId", dataId);
+ return json.build();
+ }
+
+ @Override
+ public void fromJson(JsonObject json) throws ParseException {
+ id = json.getJsonNumber("id").longValue();
+ name = json.getString("name");
+ uniqueId = json.getString("uniqueId");
+ status = json.getString("status");
+ lastUpdate = dateFormat.parse(json.getString("lastUpdate"));
+ positionId = json.getJsonNumber("positionId").longValue();
+ dataId = json.getJsonNumber("dataId").longValue();
+ }
+
}
diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java
index 14f758b4a..7f042c409 100644
--- a/src/org/traccar/model/Position.java
+++ b/src/org/traccar/model/Position.java
@@ -15,9 +15,15 @@
*/
package org.traccar.model;
+import java.text.ParseException;
import java.util.Date;
+import javax.json.Json;
+import javax.json.JsonObject;
+import javax.json.JsonObjectBuilder;
+import org.traccar.database.JsonConvertable;
+import static org.traccar.database.JsonConvertable.dateFormat;
-public class Position {
+public class Position implements JsonConvertable {
private long id;
public long getId() { return id; }
@@ -76,4 +82,40 @@ public class Position {
public String getExtendedInfo() { return extendedInfo; }
public void setExtendedInfo(String extendedInfo) { this.extendedInfo = extendedInfo; }
+ @Override
+ public JsonObject toJson() {
+ JsonObjectBuilder json = Json.createObjectBuilder();
+ json.add("id", id);
+ json.add("deviceId", deviceId);
+ json.add("serverTime", dateFormat.format(serverTime));
+ json.add("deviceTime", dateFormat.format(deviceTime));
+ json.add("fixTime", dateFormat.format(fixTime));
+ json.add("valid", valid);
+ json.add("latitude", latitude);
+ json.add("longitude", longitude);
+ json.add("altitude", altitude);
+ json.add("speed", speed);
+ json.add("course", course);
+ json.add("address", address);
+ //json.add("extendedInfo", extendedInfo);
+ return json.build();
+ }
+
+ @Override
+ public void fromJson(JsonObject json) throws ParseException {
+ id = json.getJsonNumber("id").longValue();
+ deviceId = json.getJsonNumber("deviceId").longValue();
+ serverTime = dateFormat.parse(json.getString("serverTime"));
+ deviceTime = dateFormat.parse(json.getString("deviceTime"));
+ fixTime = dateFormat.parse(json.getString("fixTime"));
+ valid = json.getBoolean("valid");
+ latitude = json.getJsonNumber("latitude").doubleValue();
+ longitude = json.getJsonNumber("longitude").doubleValue();
+ altitude = json.getJsonNumber("altitude").doubleValue();
+ speed = json.getJsonNumber("speed").doubleValue();
+ course = json.getJsonNumber("course").doubleValue();
+ address = json.getString("address");
+ //extendedInfo = json.getString("extendedInfo");
+ }
+
}
diff --git a/src/org/traccar/model/User.java b/src/org/traccar/model/User.java
index 93ce111ec..a3eaf5a10 100644
--- a/src/org/traccar/model/User.java
+++ b/src/org/traccar/model/User.java
@@ -15,7 +15,12 @@
*/
package org.traccar.model;
-public class User {
+import javax.json.Json;
+import javax.json.JsonObject;
+import javax.json.JsonObjectBuilder;
+import org.traccar.database.JsonConvertable;
+
+public class User implements JsonConvertable {
private long id;
@@ -31,14 +36,48 @@ public class User {
private String language;
- private String distance_unit;
+ private String distanceUnit;
- private String speed_unit;
+ private String speedUnit;
private double latitude;
private double longitude;
private int zoom;
+
+ @Override
+ public JsonObject toJson() {
+ JsonObjectBuilder json = Json.createObjectBuilder();
+ json.add("id", id);
+ json.add("email", email);
+ json.add("password", password);
+ json.add("readonly", readonly);
+ json.add("admin", admin);
+ json.add("map", map);
+ json.add("language", language);
+ json.add("distanceUnit", distanceUnit);
+ json.add("speedUnit", speedUnit);
+ json.add("latitude", latitude);
+ json.add("longitude", longitude);
+ json.add("zoom", zoom);
+ return json.build();
+ }
+
+ @Override
+ public void fromJson(JsonObject json) {
+ id = json.getJsonNumber("id").longValue();
+ email = json.getString("email");
+ password = json.getString("password");
+ readonly = json.getBoolean("readonly");
+ admin = json.getBoolean("admin");
+ map = json.getString("map");
+ language = json.getString("language");
+ distanceUnit = json.getString("distanceUnit");
+ speedUnit = json.getString("speedUnit");
+ latitude = json.getJsonNumber("latitude").doubleValue();
+ longitude = json.getJsonNumber("longitude").doubleValue();
+ zoom = json.getJsonNumber("zoom").intValue();
+ }
}