RFC8428 Json Parsing at proxy

This commit is contained in:
giomba 2019-11-08 21:55:43 +01:00
parent 109322dba8
commit e41959fefa
7 changed files with 70 additions and 13 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
local/

View File

@ -189,7 +189,7 @@
<viewport>8.441705440311774 0.0 0.0 8.441705440311774 -46.28112185124378 27.204467518174773</viewport> <viewport>8.441705440311774 0.0 0.0 8.441705440311774 -46.28112185124378 27.204467518174773</viewport>
</plugin_config> </plugin_config>
<width>678</width> <width>678</width>
<z>2</z> <z>3</z>
<height>633</height> <height>633</height>
<location_x>1</location_x> <location_x>1</location_x>
<location_y>1</location_y> <location_y>1</location_y>
@ -202,7 +202,7 @@
<coloring /> <coloring />
</plugin_config> </plugin_config>
<width>1039</width> <width>1039</width>
<z>3</z> <z>1</z>
<height>475</height> <height>475</height>
<location_x>681</location_x> <location_x>681</location_x>
<location_y>160</location_y> <location_y>160</location_y>
@ -247,7 +247,7 @@
<bound>true</bound> <bound>true</bound>
</plugin_config> </plugin_config>
<width>362</width> <width>362</width>
<z>1</z> <z>2</z>
<height>116</height> <height>116</height>
<location_x>992</location_x> <location_x>992</location_x>
<location_y>662</location_y> <location_y>662</location_y>

View File

@ -6,14 +6,17 @@
#include "net/ip/uip-debug.h" #include "net/ip/uip-debug.h"
int precious_resource = 0; int precious_resource = 0;
char sensor_name[32]; /* for RFC8428 */
/* handlers for coap */ /* handlers for coap */
void res_get_handler(void* request, void* response, uint8_t* buffer, uint16_t preferred_size, int32_t* offset) { void res_get_handler(void* request, void* response, uint8_t* buffer, uint16_t preferred_size, int32_t* offset) {
static char payload[16]; static char payload[128];
sprintf(payload, "pr = %d", precious_resource); sprintf(payload, "[{\"n\":\"urn:it.unipi.ing.ce.netpp:%s\",\"v\":%d}]", sensor_name, precious_resource);
REST.set_header_content_type(response, REST.type.TEXT_PLAIN); REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
REST.set_response_payload(response, payload, strlen(payload)); REST.set_response_payload(response, payload, strlen(payload));
printf("[D] GET handled\n");
} }
void res_periodic_handler(); void res_periodic_handler();
@ -25,6 +28,7 @@ void res_periodic_handler() {
/* sample some sensor */ /* sample some sensor */
/* byte mysample = *(some_bar + some_offset); */ /* byte mysample = *(some_bar + some_offset); */
++precious_resource; ++precious_resource;
printf("sampled the precious resource: %d\n", precious_resource);
/* Notify subscribers of the new sample */ /* Notify subscribers of the new sample */
REST.notify_subscribers(&myresource); REST.notify_subscribers(&myresource);
@ -48,10 +52,15 @@ PROCESS_THREAD(main_process_name, ev, data) {
printf("\n"); printf("\n");
} }
} }
/* set sensor_name as mac address */
for (i = 0; i < sizeof(uip_lladdr.addr); ++i) {
sprintf(sensor_name + i, "%02x", (unsigned char)uip_lladdr.addr[i]);
}
/* enable CoAP engine */ /* enable CoAP engine */
rest_init_engine(); rest_init_engine();
rest_activate_resource(&myresource, "test/myresource"); rest_activate_resource(&myresource, "PreciousResource");
while (1) { while (1) {
PROCESS_WAIT_EVENT(); PROCESS_WAIT_EVENT();

View File

@ -11,6 +11,17 @@
<artifactId>californium-core</artifactId> <artifactId>californium-core</artifactId>
<version>2.0.0-M18</version> <version>2.0.0-M18</version>
</dependency> </dependency>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
</dependency>
</dependencies> </dependencies>

View File

@ -1,6 +1,10 @@
package netpp; package netpp;
import java.io.*;
import java.net.URI; import java.net.URI;
import java.util.Date;
import javax.json.*;
import org.eclipse.californium.core.*; import org.eclipse.californium.core.*;
public class Handler implements CoapHandler { public class Handler implements CoapHandler {
@ -9,13 +13,32 @@ public class Handler implements CoapHandler {
public Handler(URI uri) { public Handler(URI uri) {
this.uri = uri; this.uri = uri;
System.err.println("[D] new handler for " + this.uri);
} }
@Override @Override
public void onLoad(CoapResponse response) { public void onLoad(CoapResponse response) {
String content = response.getResponseText(); String content = response.getResponseText();
System.out.println("[D] resource: " + uri); // System.out.println("[D] resource: " + uri);
System.out.println(" new value: " + content); // System.out.println(" new value: " + content);
System.out.println("update" + new Date());
JsonArray jsonSenML = null;
try {
InputStream is = new ByteArrayInputStream(content.getBytes());
System.out.println("0");
JsonReader reader = Json.createReader(is);
System.err.println("1: " + content);
jsonSenML = reader.readArray();
System.out.println("2");
reader.close();
System.out.println("Java Json Object");
System.out.println(jsonSenML.getJsonObject(0).getString("n"));
System.out.println(jsonSenML.getJsonObject(0).getJsonNumber("v"));
} catch (Exception ex) {
System.err.println("[E] " + ex);
}
} }
@Override @Override

View File

@ -1,8 +1,10 @@
package netpp; package netpp;
import java.util.*;
public class Main { public class Main {
public final static String COOJA_MAGIC = "c30c"; public final static String COOJA_MAGIC = "c30c";
public final static String resource = "/test/myresource"; public final static String resource = "/PreciousResource";
public static void main(String[] args) { public static void main(String[] args) {
if (args.length < 2) { if (args.length < 2) {
@ -17,23 +19,33 @@ public class Main {
System.exit(1); System.exit(1);
} }
/* getting parameters from CLI */
final String prefix = args[0]; final String prefix = args[0];
final int n = Integer.parseInt(args[1]); final int n = Integer.parseInt(args[1]);
/* subscribe to every node specified */
System.err.println("[I] now subscribing to node [2; " + n + "] in " + prefix + "/64"); System.err.println("[I] now subscribing to node [2; " + n + "] in " + prefix + "/64");
/* subscribe to every node */ ArrayList<Node> nodeList = new ArrayList<Node>();
for (int i = 2; i <= n; i++) { // node 1 is the gateway, skip. And mind the <= for (int i = 2; i <= n; i++) { // node 1 is the gateway, skip. And mind the <=
String uri_string = "coap://[" + prefix + ":" + COOJA_MAGIC + "::" + Integer.toString(i, 16) + "]" + resource; String uri_string = "coap://[" + prefix + ":" + COOJA_MAGIC + "::" + Integer.toString(i, 16) + "]" + resource;
System.err.println("[I] subscribing to " + uri_string); System.err.println("[I] subscribing to " + uri_string);
Node node = new Node(uri_string); nodeList.add(new Node(uri_string));
} }
/* thread sleep */
try { try {
Thread.sleep(5 * 60 * 1000); // 5 min Thread.sleep(5 * 60 * 1000); // 5 min
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
System.err.println(ex); System.err.println(ex);
} }
/* shutting down everything */
for (Node node : nodeList) {
node.finalize();
}
} }
} }

View File

@ -9,13 +9,14 @@ public class Node {
private URI uri; private URI uri;
private CoapClient client; private CoapClient client;
private CoapObserveRelation relation; private CoapObserveRelation relation;
private Handler handler;
public Node(String uriString) { public Node(String uriString) {
try { try {
this.uri = new URI(uriString); this.uri = new URI(uriString);
this.client = new CoapClient(uri); this.client = new CoapClient(uri);
this.relation = client.observe(new Handler(this.uri)); this.handler = new Handler(this.uri);
this.relation = client.observe(handler);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
System.err.println("[F] Invalid URI: " + e.getMessage()); System.err.println("[F] Invalid URI: " + e.getMessage());
System.exit(1); System.exit(1);