Search in sources :

Example 6 with LwM2mResource

use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.

the class WriteTest method write_time_resource.

private void write_time_resource(ContentFormat format) throws InterruptedException {
    // write resource
    // second accuracy
    Date expectedvalue = new Date(946681000l);
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(format, TEST_OBJECT_ID, 0, TIME_RESOURCE_ID, expectedvalue));
    // verify result
    assertEquals(ResponseCode.CHANGED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    // read resource to check the value changed
    ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(TEST_OBJECT_ID, 0, TIME_RESOURCE_ID));
    LwM2mResource resource = (LwM2mResource) readResponse.getContent();
    assertEquals(expectedvalue, resource.getValue());
}
Also used : ReadResponse(org.eclipse.leshan.core.response.ReadResponse) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) Date(java.util.Date) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 7 with LwM2mResource

use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.

the class WriteTest method write_float_resource.

private void write_float_resource(ContentFormat format) throws InterruptedException {
    // write resource
    double expectedvalue = 999.99;
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(format, TEST_OBJECT_ID, 0, FLOAT_RESOURCE_ID, expectedvalue));
    // verify result
    assertEquals(ResponseCode.CHANGED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    // read resource to check the value changed
    ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(TEST_OBJECT_ID, 0, FLOAT_RESOURCE_ID));
    LwM2mResource resource = (LwM2mResource) readResponse.getContent();
    assertEquals(expectedvalue, resource.getValue());
}
Also used : ReadResponse(org.eclipse.leshan.core.response.ReadResponse) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 8 with LwM2mResource

use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.

the class WriteTest method can_write_object_instance.

public void can_write_object_instance(ContentFormat format) throws InterruptedException {
    // write device timezone and offset
    LwM2mResource utcOffset = LwM2mSingleResource.newStringResource(14, "+02");
    LwM2mResource timeZone = LwM2mSingleResource.newStringResource(15, "Europe/Paris");
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(Mode.REPLACE, format, 3, 0, utcOffset, timeZone));
    // verify result
    assertEquals(ResponseCode.CHANGED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    // read the timezone to check the value changed
    ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3, 0));
    LwM2mObjectInstance instance = (LwM2mObjectInstance) readResponse.getContent();
    assertEquals(utcOffset, instance.getResource(14));
    assertEquals(timeZone, instance.getResource(15));
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 9 with LwM2mResource

use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.

the class LwM2mNodeSerDes method jSerialize.

public static JsonObject jSerialize(LwM2mNode n) {
    JsonObject o = Json.object();
    o.add("id", n.getId());
    if (n instanceof LwM2mObject) {
        o.add("kind", "object");
        JsonObject instances = Json.object();
        for (LwM2mObjectInstance instance : ((LwM2mObject) n).getInstances().values()) {
            instances.add(String.valueOf(instance.getId()), jSerialize(instance));
        }
        o.add("instances", instances);
    } else if (n instanceof LwM2mObjectInstance) {
        o.add("kind", "instance");
        JsonObject resources = Json.object();
        for (LwM2mResource resource : ((LwM2mObjectInstance) n).getResources().values()) {
            resources.add(String.valueOf(resource.getId()), jSerialize(resource));
        }
        o.add("resources", resources);
    } else if (n instanceof LwM2mResource) {
        LwM2mResource r = (LwM2mResource) n;
        o.add("type", r.getType().toString());
        if (r.isMultiInstances()) {
            o.add("kind", "multipleResource");
            JsonObject values = Json.object();
            for (Entry<Integer, ?> value : r.getValues().entrySet()) {
                values.add(value.getKey().toString(), ValueSerDes.jSerialize(value.getValue(), r.getType()));
            }
            o.add("values", values);
        } else {
            o.add("kind", "singleResource");
            o.add("value", ValueSerDes.jSerialize(r.getValue(), r.getType()));
        }
    }
    return o;
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) JsonObject(com.eclipsesource.json.JsonObject) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource)

Example 10 with LwM2mResource

use of org.eclipse.leshan.core.node.LwM2mResource 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");
    }
}
Also used : JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) JsonArray(com.eclipsesource.json.JsonArray) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) Type(org.eclipse.leshan.core.model.ResourceModel.Type) Collection(java.util.Collection) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) JsonObject(com.eclipsesource.json.JsonObject) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) Member(com.eclipsesource.json.JsonObject.Member) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)33 Test (org.junit.Test)15 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)14 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)13 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)13 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)11 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)10 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)10 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)9 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)5 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)5 JsonObject (com.eclipsesource.json.JsonObject)4 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)4 JsonArray (com.eclipsesource.json.JsonArray)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 JsonValue (com.eclipsesource.json.JsonValue)2 SortedMap (java.util.SortedMap)2 TreeMap (java.util.TreeMap)2