diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..51b4cfd
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+local/
diff --git a/cooja/simulation-00.csc b/cooja/simulation-00.csc
index 6e00014..ac45cf8 100644
--- a/cooja/simulation-00.csc
+++ b/cooja/simulation-00.csc
@@ -189,7 +189,7 @@
8.441705440311774 0.0 0.0 8.441705440311774 -46.28112185124378 27.204467518174773
678
- 2
+ 3
633
1
1
@@ -202,7 +202,7 @@
1039
- 3
+ 1
475
681
160
@@ -247,7 +247,7 @@
true
362
- 1
+ 2
116
992
662
diff --git a/oracle/main.c b/oracle/main.c
index a1bbd66..0152c44 100644
--- a/oracle/main.c
+++ b/oracle/main.c
@@ -6,14 +6,17 @@
#include "net/ip/uip-debug.h"
int precious_resource = 0;
+char sensor_name[32]; /* for RFC8428 */
/* handlers for coap */
void res_get_handler(void* request, void* response, uint8_t* buffer, uint16_t preferred_size, int32_t* offset) {
- static char payload[16];
- sprintf(payload, "pr = %d", precious_resource);
+ static char payload[128];
+ 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_response_payload(response, payload, strlen(payload));
+
+ printf("[D] GET handled\n");
}
void res_periodic_handler();
@@ -25,6 +28,7 @@ void res_periodic_handler() {
/* sample some sensor */
/* byte mysample = *(some_bar + some_offset); */
++precious_resource;
+ printf("sampled the precious resource: %d\n", precious_resource);
/* Notify subscribers of the new sample */
REST.notify_subscribers(&myresource);
@@ -48,10 +52,15 @@ PROCESS_THREAD(main_process_name, ev, data) {
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 */
rest_init_engine();
- rest_activate_resource(&myresource, "test/myresource");
+ rest_activate_resource(&myresource, "PreciousResource");
while (1) {
PROCESS_WAIT_EVENT();
diff --git a/proxy/pom.xml b/proxy/pom.xml
index 55d1473..d300a79 100644
--- a/proxy/pom.xml
+++ b/proxy/pom.xml
@@ -11,6 +11,17 @@
californium-core
2.0.0-M18
+
+ javax.json
+ javax.json-api
+ 1.1.4
+
+
+
+ org.glassfish
+ javax.json
+ 1.1
+
diff --git a/proxy/src/netpp/Handler.java b/proxy/src/netpp/Handler.java
index d36a863..bc5f719 100644
--- a/proxy/src/netpp/Handler.java
+++ b/proxy/src/netpp/Handler.java
@@ -1,6 +1,10 @@
package netpp;
+import java.io.*;
import java.net.URI;
+import java.util.Date;
+
+import javax.json.*;
import org.eclipse.californium.core.*;
public class Handler implements CoapHandler {
@@ -9,13 +13,32 @@ public class Handler implements CoapHandler {
public Handler(URI uri) {
this.uri = uri;
+ System.err.println("[D] new handler for " + this.uri);
}
@Override
public void onLoad(CoapResponse response) {
String content = response.getResponseText();
- System.out.println("[D] resource: " + uri);
- System.out.println(" new value: " + content);
+ // System.out.println("[D] resource: " + uri);
+ // 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
diff --git a/proxy/src/netpp/Main.java b/proxy/src/netpp/Main.java
index e44c230..70c00b3 100644
--- a/proxy/src/netpp/Main.java
+++ b/proxy/src/netpp/Main.java
@@ -1,8 +1,10 @@
package netpp;
+import java.util.*;
+
public class Main {
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) {
if (args.length < 2) {
@@ -17,23 +19,33 @@ public class Main {
System.exit(1);
}
+ /* getting parameters from CLI */
final String prefix = args[0];
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");
- /* subscribe to every node */
+ ArrayList nodeList = new ArrayList();
+
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;
System.err.println("[I] subscribing to " + uri_string);
- Node node = new Node(uri_string);
+ nodeList.add(new Node(uri_string));
}
+ /* thread sleep */
try {
Thread.sleep(5 * 60 * 1000); // 5 min
} catch (InterruptedException ex) {
System.err.println(ex);
}
+ /* shutting down everything */
+ for (Node node : nodeList) {
+ node.finalize();
+ }
+
}
}
diff --git a/proxy/src/netpp/Node.java b/proxy/src/netpp/Node.java
index ab37844..9ca34da 100644
--- a/proxy/src/netpp/Node.java
+++ b/proxy/src/netpp/Node.java
@@ -9,13 +9,14 @@ public class Node {
private URI uri;
private CoapClient client;
private CoapObserveRelation relation;
+ private Handler handler;
public Node(String uriString) {
try {
this.uri = new URI(uriString);
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) {
System.err.println("[F] Invalid URI: " + e.getMessage());
System.exit(1);