From 21ce0d17ffa82e17d36c44e7c4c55f3592eba8ef Mon Sep 17 00:00:00 2001 From: giomba Date: Sat, 16 Nov 2019 15:57:40 +0100 Subject: [PATCH] Proxy answers with SenML RFC8428 compliant json messages --- proxy/src/netpp/Resource.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/proxy/src/netpp/Resource.java b/proxy/src/netpp/Resource.java index c670df8..b7668ed 100644 --- a/proxy/src/netpp/Resource.java +++ b/proxy/src/netpp/Resource.java @@ -1,5 +1,11 @@ package netpp; +import java.io.ByteArrayOutputStream; + +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonWriter; + import org.eclipse.californium.core.CoapResource; import org.eclipse.californium.core.server.resources.CoapExchange; @@ -15,7 +21,18 @@ public class Resource extends CoapResource { public void handleGET(CoapExchange exchange) { // TODO build the proper JSON object - exchange.respond("TODO: " + this.getName() + " : " + this.getValue()); + JsonArray senml = Json.createArrayBuilder() + .add( + Json.createObjectBuilder() + .add("n", this.getName()) + .add("v", this.getValue()) + .build() + ).build(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + JsonWriter writer = Json.createWriter(bos); + writer.writeArray(senml); + exchange.respond(bos.toString()); } public int getValue() {