Skip to content
Snippets Groups Projects
Commit 56dedeb5 authored by jkerdreu's avatar jkerdreu
Browse files

Added body<=>HashMap, make it easier for devive developper. Added example in Lamp.java

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Java/branches/0.7@2602 b32b6428-25c9-4566-ad07-03861ab6144f
parent ea906d91
Branches
No related tags found
No related merge requests found
......@@ -16,13 +16,13 @@ public abstract class Device {
private final static Logger LOGGER = Logger.getLogger(Device.class.getName());
// public xAAL data
protected String vendorID = "";
protected String productID = "";
protected String version = "";
protected String vendorID = null;
protected String productID = null;
protected String version = null;
protected URI url = null;
protected URI schema = null;
protected String info = "";
protected String hwID = "";
protected String info = null;
protected String hwID = null;
protected UUID groupID = null;
protected UUID address = null;
......
......@@ -137,7 +137,7 @@ public class Engine extends Thread {
for (Device key : map.keySet()) {
Message msg = factory.buildMSG(key, Arrays.asList(), MessageType.NOTIFY, "attributes_change",null);
LOGGER.info("AttributesChange : " + key + "=>" + map.get(key));
msg.setBody(map.get(key));
msg.setBodyAsMap(map.get(key));
queueMessage(msg);
}
}
......@@ -169,13 +169,13 @@ public class Engine extends Thread {
*/
public void handleGetDescription(Device dev, Message msg) throws Exception {
HashMap<String,Object> body = new HashMap<String,Object>();
body.put("vendor_id", dev.getVendorID());
body.put("product_id", dev.getProductID());
body.put("version", dev.getVersion());
body.put("url", dev.getUrl());
body.put("info", dev.getInfo());
body.put("hw_id", dev.getHwID());
body.put("schema", dev.getSchema());
if (dev.getVendorID()!=null) body.put("vendor_id", dev.getVendorID());
if (dev.getProductID()!=null) body.put("product_id", dev.getProductID());
if (dev.getVersion()!=null) body.put("version", dev.getVersion());
if (dev.getUrl()!=null) body.put("url", dev.getUrl());
if (dev.getInfo()!=null) body.put("info", dev.getInfo());
if (dev.getHwID()!=null) body.put("hw_id", dev.getHwID());
if (dev.getSchema()!=null) body.put("schema", dev.getSchema());
if (dev.getGroupID() != null) body.put("group_id", dev.getGroupID());
body.put("unsupportedMethods", dev.getUnsupportedMethods());
......@@ -183,7 +183,7 @@ public class Engine extends Thread {
body.put("unsupportedNotifications", dev.getUnsupportedNotifications());
Message reply = factory.buildMSG(dev, Arrays.asList(msg.getSource()), MessageType.REPLY, msg.getAction(), null);
reply.setBody(body);
reply.setBodyAsMap(body);
queueMessage(reply);
}
......@@ -200,7 +200,7 @@ public class Engine extends Thread {
body.put(attr.getName(), attr.getValue());
}
Message reply = factory.buildMSG(dev, Arrays.asList(msg.getSource()), MessageType.REPLY, msg.getAction(), null);
reply.setBody(body);
reply.setBodyAsMap(body);
queueMessage(reply);
}
......@@ -374,6 +374,7 @@ public class Engine extends Thread {
Thread.sleep(100);
} catch (InterruptedException ex) {
LOGGER.info(ex.toString());
LOGGER.info(ex.getStackTrace().toString());
}
}
}
......@@ -3,12 +3,17 @@ package org.imta.xaal.lib;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.UUID;
import com.upokecenter.cbor.CBORObject;
public class Lamp extends Device {
Attribute light = new Attribute("light", false);
Attribute brightness = new Attribute("brightness", 100);
public Lamp(String devType, UUID address) throws URISyntaxException{
super(devType, address);
......@@ -18,7 +23,7 @@ public class Lamp extends Device {
setHwID("6b2");
setSchema( new URI("http://foobar.com"));
setUrl(new URI("http://dev.foobar.org"));
registerAttributes(Arrays.asList(light));
registerAttributes(Arrays.asList(light,brightness));
setGroupID(UUID.randomUUID());
}
......@@ -38,12 +43,33 @@ public class Lamp extends Device {
light.setValue(false);
}
@Override
public Message handleRequest(Message msg) {
switch (msg.getAction()) {
case "toggle":
toggle();
break;
case "turn_on":
turn_on();
break;
case "turn_off":
turn_off();
break;
case "dim":
Integer target = msg.getBodyAsMap().get("target").AsInt32();
if (target<=0) turn_off();
else turn_on();
brightness.setValue(target);
}
/*
if (msg.getAction().equals("toggle")) toggle();
if (msg.getAction().equals("turn_on")) turn_on();;
if (msg.getAction().equals("turn_off")) turn_off();;
if (msg.getAction())
*/
return null;
}
......
......@@ -16,6 +16,7 @@ public class Message {
private MessageType msgType = null;
private String action;
private CBORObject body;
private HashMap<String,CBORObject> bodyAsMap = null;
Message() {
version = 7;
......@@ -75,11 +76,23 @@ public class Message {
return body;
}
public HashMap<String,CBORObject> getBodyAsMap() {
if (bodyAsMap!=null) return bodyAsMap;
HashMap<String,CBORObject> result = new HashMap<>();
HashMap<CBORObject,CBORObject> hash = body.ToObject(HashMap.class);
for (CBORObject key : hash.keySet()) {
String tmp = key.toString().replace("\"", "");
result.put(tmp, hash.get(key));
}
bodyAsMap = result;
return bodyAsMap;
}
public void setBody(CBORObject body) {
this.body = body;
}
public void setBody(HashMap<String,Object> body) {
public void setBodyAsMap(HashMap<String,Object> body) {
this.body = CBORObject.FromObject(body);
}
......
......@@ -22,6 +22,7 @@ import com.upokecenter.cbor.CBORObject;
public class MessageFactory {
//private final static Logger LOGGER = Logger.getLogger(Device.class.getName());
static Random random = new Random();
private byte[] key;
MessageFactory(byte[] key) {
......@@ -133,7 +134,7 @@ public class MessageFactory {
Message message = buildMSG(dev, targets, MessageType.NOTIFY, "alive", null);
HashMap<String,Object> body = new HashMap<String,Object>();
body.put("timeout", timeout);
message.setBody(body);
message.setBodyAsMap(body);
return message;
}
......@@ -177,11 +178,9 @@ public class MessageFactory {
public static long[] buildTimeStamp() {
// Java don't provide a microsecond accurate clock
// so we compute on millisecond and add a random seed
// runnning a new Random() is not the best stuff for entropy
var rand = new Random();
long now = new Date().getTime();
long ts1 = now / 1000;
long ts2 = now % 1000 * 1000 + rand.nextInt(999);
long ts2 = now % 1000 * 1000 + random.nextInt(999);
long[] r = {ts1, ts2};
return r;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment