use of org.eclipse.leshan.core.node.LwM2mObjectInstance in project leshan by eclipse.
the class LwM2mNodeSerDes method deserialize.
public static LwM2mNode deserialize(JsonObject o) {
String kind = o.getString("kind", null);
int id = o.getInt("id", LwM2mObjectInstance.UNDEFINED);
switch(kind) {
case "object":
{
Collection<LwM2mObjectInstance> instances = new ArrayList<>();
JsonArray jInstances = (JsonArray) o.get("instances");
for (JsonValue jInstance : jInstances) {
LwM2mObjectInstance instance = (LwM2mObjectInstance) deserialize((JsonObject) jInstance);
instances.add(instance);
}
return new LwM2mObject(id, instances);
}
case "instance":
{
Collection<LwM2mResource> resources = new ArrayList<>();
JsonObject jResources = (JsonObject) o.get("resources");
for (Member jResource : jResources) {
LwM2mResource resource = (LwM2mResource) deserialize((JsonObject) jResource.getValue());
resources.add(resource);
}
return new LwM2mObjectInstance(id, resources);
}
case "singleResource":
{
String jType = o.getString("type", null);
if (jType == null)
throw new IllegalStateException("Invalid LwM2mNode missing type attribute");
Type type = Enum.valueOf(Type.class, jType);
Object value = ValueSerDes.deserialize(o.get("value"), type);
return LwM2mSingleResource.newResource(id, value, type);
}
case "multipleResource":
{
String jType = o.getString("type", null);
if (jType == null)
throw new IllegalStateException("Invalid LwM2mNode missing type attribute");
Type type = Enum.valueOf(Type.class, jType);
Map<Integer, Object> values = new HashMap<>();
JsonObject jValues = (JsonObject) o.get("values");
for (Member jValue : jValues) {
Integer valueId = Integer.valueOf(jValue.getName());
Object value = ValueSerDes.deserialize(jValue.getValue(), type);
values.put(valueId, value);
}
return LwM2mMultipleResource.newResource(id, values, type);
}
default:
throw new IllegalStateException("Invalid LwM2mNode missing kind attribute");
}
}
use of org.eclipse.leshan.core.node.LwM2mObjectInstance in project leshan by eclipse.
the class CreateTest method can_create_specific_instance_of_object_with_json.
@Test
public void can_create_specific_instance_of_object_with_json() throws InterruptedException {
// create ACL instance
LwM2mObjectInstance instance = new LwM2mObjectInstance(12, Arrays.<LwM2mResource>asList(LwM2mSingleResource.newIntegerResource(3, 123)));
CreateResponse response = helper.server.send(helper.getCurrentRegistration(), new CreateRequest(ContentFormat.JSON, 2, instance));
// verify result
assertEquals(ResponseCode.CREATED, response.getCode());
assertEquals("2/12", response.getLocation());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
use of org.eclipse.leshan.core.node.LwM2mObjectInstance in project leshan by eclipse.
the class CreateTest method can_create_specific_instance_of_object.
@Test
public void can_create_specific_instance_of_object() throws InterruptedException {
// create ACL instance
LwM2mObjectInstance instance = new LwM2mObjectInstance(12, Arrays.<LwM2mResource>asList(LwM2mSingleResource.newIntegerResource(3, 123)));
CreateResponse response = helper.server.send(helper.getCurrentRegistration(), new CreateRequest(2, instance));
// verify result
assertEquals(ResponseCode.CREATED, response.getCode());
assertEquals("2/12", response.getLocation());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
use of org.eclipse.leshan.core.node.LwM2mObjectInstance in project leshan by eclipse.
the class ReadTest method can_read_object.
@Test
public void can_read_object() throws InterruptedException {
// read device object
ReadResponse response = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3));
// verify result
assertEquals(CONTENT, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
LwM2mObject object = (LwM2mObject) response.getContent();
assertEquals(3, object.getId());
LwM2mObjectInstance instance = object.getInstance(0);
assertEquals(0, instance.getId());
}
use of org.eclipse.leshan.core.node.LwM2mObjectInstance in project leshan by eclipse.
the class DownlinkRequestSerDes method deserialize.
public static DownlinkRequest<?> deserialize(JsonObject o) {
String kind = o.getString("kind", null);
String path = o.getString("path", null);
switch(kind) {
case "observe":
{
int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
return new ObserveRequest(ContentFormat.fromCode(format), path);
}
case "delete":
return new DeleteRequest(path);
case "discover":
return new DiscoverRequest(path);
case "create":
{
int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
int instanceId = o.getInt("instanceId", LwM2mObjectInstance.UNDEFINED);
Collection<LwM2mResource> resources = new ArrayList<>();
JsonArray jResources = (JsonArray) o.get("resources");
for (JsonValue jResource : jResources) {
LwM2mResource resource = (LwM2mResource) LwM2mNodeSerDes.deserialize((JsonObject) jResource);
resources.add(resource);
}
return new CreateRequest(ContentFormat.fromCode(format), path, new LwM2mObjectInstance(instanceId, resources));
}
case "execute":
String parameters = o.getString("parameters", null);
return new ExecuteRequest(path, parameters);
case "writeAttributes":
{
String observeSpec = o.getString("observeSpec", null);
return new WriteAttributesRequest(path, ObserveSpec.parse(observeSpec));
}
case "write":
{
int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
Mode mode = o.getString("mode", "REPLACE").equals("REPLACE") ? Mode.REPLACE : Mode.UPDATE;
LwM2mNode node = LwM2mNodeSerDes.deserialize((JsonObject) o.get("node"));
return new WriteRequest(mode, ContentFormat.fromCode(format), path, node);
}
case "read":
{
int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
return new ReadRequest(ContentFormat.fromCode(format), path);
}
default:
throw new IllegalStateException("Invalid request missing kind attribute");
}
}
Aggregations