Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Fix for Elemental Jackson deserialiser and added new attribute types and an epoch seconds jackson serialiser/deserialiser

Rich Turnerc71eee3b98f5
diff --git a/container/src/main/java/org/openremote/container/json/ElementalJsonModule.java b/container/src/main/java/org/openremote/container/json/ElementalJsonModule.java
index 342fd19a9..a4caa975d 100644
--- a/container/src/main/java/org/openremote/container/json/ElementalJsonModule.java
+++ b/container/src/main/java/org/openremote/container/json/ElementalJsonModule.java
@@ -42,7 +42,7 @@ public class ElementalJsonModule extends SimpleModule {
@Override
public T deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException, JsonProcessingException {
- return (T) Json.parse(jsonParser.getCodec().readTree(jsonParser).toString());
+ return (T) Json.instance().parse(jsonParser.getCodec().readTree(jsonParser).toString());
}
}
diff --git a/container/src/main/java/org/openremote/container/json/EpochSecondsDeserializer.java b/container/src/main/java/org/openremote/container/json/EpochSecondsDeserializer.java
new file mode 100644
index 000000000..bd3e542a6
--- /dev/null
+++ b/container/src/main/java/org/openremote/container/json/EpochSecondsDeserializer.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2016, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.container.json;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
+
+import java.io.IOException;
+import java.util.Date;
+
+public class EpochSecondsDeserializer extends JsonDeserializer<Date> {
+ @Override
+ public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
+ String timestamp = p.getText().trim();
+ try {
+ return new Date(Long.valueOf(timestamp + "000"));
+ } catch (NumberFormatException e) {
+ return null;
+ }
+ }
+}
diff --git a/container/src/main/java/org/openremote/container/json/EpochSecondsSerializer.java b/container/src/main/java/org/openremote/container/json/EpochSecondsSerializer.java
new file mode 100644
index 000000000..4f7c69705
--- /dev/null
+++ b/container/src/main/java/org/openremote/container/json/EpochSecondsSerializer.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2016, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.container.json;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+
+import java.io.IOException;
+import java.util.Date;
+
+public class EpochSecondsSerializer extends JsonSerializer<Date> {
+ @Override
+ public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
+ gen.writeNumber(Math.round(((double)value.getTime()) / 1000));
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/AttributeType.java b/model/src/main/java/org/openremote/model/AttributeType.java
index e52741687..6deb13890 100644
--- a/model/src/main/java/org/openremote/model/AttributeType.java
+++ b/model/src/main/java/org/openremote/model/AttributeType.java
@@ -37,7 +37,13 @@ public enum AttributeType {
TIMESTAMP("Timestamp", JsonType.STRING), // Unix timestamp
DATE("Date", JsonType.STRING), // ISO 8601, e.g. "2012-04-23T18:25:43.511Z"
COLOR("Color", JsonType.OBJECT), // RGB integers
- TEMPERATURE("Temperature", JsonType.NUMBER);
+ TEMPERATURE("Temperature", JsonType.NUMBER),
+ CO2("CO2", JsonType.NUMBER),
+ HUMIDITY("Humidity", JsonType.NUMBER),
+ POWER("Power", JsonType.NUMBER),
+ CHARGE("Charge", JsonType.NUMBER),
+ ENERGY("Energy", JsonType.NUMBER),
+ WATER("Water", JsonType.NUMBER);
private String value;
private JsonType jsonType;