Skip to content

Commit

Permalink
server-demo: Use Timestamp for Time value of REST API.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Oct 28, 2021
1 parent 9bd4844 commit f98f949
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
Expand Down Expand Up @@ -104,7 +103,6 @@ public ClientServlet(LeshanServer server) {
module.addSerializer(LwM2mNode.class, new JacksonLwM2mNodeSerializer());
module.addDeserializer(LwM2mNode.class, new JacksonLwM2mNodeDeserializer());
mapper.registerModule(module);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.eclipse.leshan.server.demo.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
Expand Down Expand Up @@ -278,7 +277,6 @@ public EventServlet(LeshanServer server, int securePort) {
module.addSerializer(Registration.class, new JacksonRegistrationSerializer(server.getPresenceService()));
module.addSerializer(LwM2mNode.class, new JacksonLwM2mNodeSerializer());
mapper.registerModule(module);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"));
this.mapper = mapper;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.eclipse.leshan.core.model.ResourceModel;
import org.eclipse.leshan.core.model.ResourceModel.Type;
import org.eclipse.leshan.core.node.LwM2mMultipleResource;
Expand All @@ -35,7 +32,6 @@
import org.eclipse.leshan.core.node.LwM2mResource;
import org.eclipse.leshan.core.node.LwM2mResourceInstance;
import org.eclipse.leshan.core.node.LwM2mSingleResource;
import org.eclipse.leshan.core.node.codec.CodecException;
import org.eclipse.leshan.core.util.Hex;

import com.fasterxml.jackson.core.JsonParseException;
Expand Down Expand Up @@ -150,13 +146,7 @@ private Object deserializeValue(JsonNode val, ResourceModel.Type type) {
case FLOAT:
return val.asDouble();
case TIME:
try {
DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
XMLGregorianCalendar cal = datatypeFactory.newXMLGregorianCalendar(val.asText());
return cal.toGregorianCalendar().getTime();
} catch (DatatypeConfigurationException | IllegalArgumentException e) {
throw new CodecException("Unable to convert string (%s) to date", val.asText(), e);
}
return new Date(val.asLong());
case OPAQUE:
return Hex.decodeHex((val.asText()).toCharArray());
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<!-----------------------------------------------------------------------------
* Copyright (c) 2021 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
----------------------------------------------------------------------------->
<template>
<v-row>
<v-col>
<v-text-field
single-line
:label="resourcedef.type"
suffix="seconds"
v-model="dateValue"
:rules="[(v) => !v || isValidTimestamp(v) || 'Invalid timestamp']"
hint="Timestamp in seconds"
clearable
/>
</v-col>
</v-row>
</template>
<script>
import { isInteger } from "../../../js/utils.js";
/**
* An input for TIME single value LWM2M node ("Single Instance Resource" or "Resource Instance")
*/
export default {
props: {
value: null, // the input value for this LWM2M Node (v-model) : timestamp in milliseconds
resourcedef: Object, // the model of the resource
},
data() {
return {
localValue: null, // timestamp in seconds
};
},
watch: {
value(v) {
if (this.isValidTimestamp(v)) {
this.localValue = Math.floor(parseInt(v) / 1000);
} else {
this.localValue = v;
}
},
},
methods: {
isValidTimestamp(v) {
return isInteger(v);
},
},
computed: {
dateValue: {
get() {
return this.localValue;
},
set(v) {
this.$emit("input", this.isValidTimestamp(v) ? v * 1000 : v);
},
},
},
};
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
:value="value"
@input="$emit('input', convertValue($event))"
/>
<date-time-value-input
v-else-if="resourcedef.type == 'time'"
:resourcedef="resourcedef"
:value="value"
@input="$emit('input', convertValue($event))"
/>
<v-text-field
v-else
:label="resourcedef.type"
Expand All @@ -34,13 +40,14 @@
</template>
<script>
import BooleanValueInput from "./BooleanValueInput.vue";
import DateTimeValueInput from './DateTimeValueInput.vue';
import OpaqueValueInput from "./OpaqueValueInput.vue";
/**
* An input for single value LWM2M node ("Single Instance Resource" or "Resource Instance")
*/
export default {
components: { BooleanValueInput, OpaqueValueInput },
components: { BooleanValueInput, OpaqueValueInput, DateTimeValueInput },
props: {
value: null, // the input value for this LWM2M Node (v-model)
resourcedef: Object, // the model of the resource
Expand Down
27 changes: 27 additions & 0 deletions leshan-server-demo/webapp/src/js/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*******************************************************************************
* Copyright (c) 2021 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*******************************************************************************/

/**
* @param {*} value the value to check
* @returns true if the value is a integer
*/
function isInteger(value) {
if (typeof value === "number") {
return value === Math.round(value);
} else if (typeof value === "string" && value.trim() !== "") {
return value === String(Math.round(Number(value)));
}
return false;
}

export { isInteger };
7 changes: 5 additions & 2 deletions leshan-server-demo/webapp/src/js/valueutils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2021 Sierra Wireless and others.
*
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
Expand All @@ -19,6 +19,9 @@
function valueToString(value, type) {
if (type == "objlnk") {
return value.objectId + ":" + value.objectInstanceId;
} else if (type == "time") {
let date = new Date(value);
return date.toDateString()+ " - " + date.toLocaleTimeString() + " (" + date.getTime()/1000 + "s)";
} else {
return String(value);
}
Expand Down

0 comments on commit f98f949

Please sign in to comment.