Search in sources :

Example 6 with LwM2mObjectInstance

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

the class LwM2mNodeDeserializer method deserialize.

@Override
public LwM2mNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    if (json == null) {
        return null;
    }
    LwM2mNode node;
    if (json.isJsonObject()) {
        JsonObject object = (JsonObject) json;
        if (!object.has("id")) {
            throw new JsonParseException("Missing id");
        }
        int id = object.get("id").getAsInt();
        if (object.has("instances")) {
            JsonArray array = object.get("instances").getAsJsonArray();
            LwM2mObjectInstance[] instances = new LwM2mObjectInstance[array.size()];
            for (int i = 0; i < array.size(); i++) {
                instances[i] = context.deserialize(array.get(i), LwM2mNode.class);
            }
            node = new LwM2mObject(id, instances);
        } else if (object.has("resources")) {
            JsonArray array = object.get("resources").getAsJsonArray();
            LwM2mResource[] resources = new LwM2mResource[array.size()];
            for (int i = 0; i < array.size(); i++) {
                resources[i] = context.deserialize(array.get(i), LwM2mNode.class);
            }
            node = new LwM2mObjectInstance(id, resources);
        } else if (object.has("value")) {
            // single value resource
            JsonPrimitive val = object.get("value").getAsJsonPrimitive();
            org.eclipse.leshan.core.model.ResourceModel.Type expectedType = getTypeFor(val);
            node = LwM2mSingleResource.newResource(id, deserializeValue(val, expectedType), expectedType);
        } else if (object.has("values")) {
            // multi-instances resource
            Map<Integer, Object> values = new HashMap<>();
            org.eclipse.leshan.core.model.ResourceModel.Type expectedType = null;
            for (Entry<String, JsonElement> entry : object.get("values").getAsJsonObject().entrySet()) {
                JsonPrimitive pval = entry.getValue().getAsJsonPrimitive();
                expectedType = getTypeFor(pval);
                values.put(Integer.valueOf(entry.getKey()), deserializeValue(pval, expectedType));
            }
            // use string by default;
            if (expectedType == null)
                expectedType = org.eclipse.leshan.core.model.ResourceModel.Type.STRING;
            node = LwM2mMultipleResource.newResource(id, values, expectedType);
        } else {
            throw new JsonParseException("Invalid node element");
        }
    } else {
        throw new JsonParseException("Invalid node element");
    }
    return node;
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) JsonParseException(com.google.gson.JsonParseException) JsonArray(com.google.gson.JsonArray) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) Type(java.lang.reflect.Type) Entry(java.util.Map.Entry) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with LwM2mObjectInstance

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

the class WriteTest method can_write_replacing_object_instance.

@Test
public void can_write_replacing_object_instance() throws InterruptedException {
    // setup server object
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(1, 0, 3, 60));
    // verify result
    assertEquals(ResponseCode.CHANGED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    // write server object
    LwM2mResource lifetime = LwM2mSingleResource.newIntegerResource(1, 120);
    LwM2mResource defaultMinPeriod = LwM2mSingleResource.newIntegerResource(2, 10);
    LwM2mResource notificationStoring = LwM2mSingleResource.newBooleanResource(6, false);
    LwM2mResource binding = LwM2mSingleResource.newStringResource(7, "U");
    response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(Mode.REPLACE, 1, 0, lifetime, defaultMinPeriod, notificationStoring, binding));
    // verify result
    assertEquals(ResponseCode.CHANGED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    // read the values to check the value changed
    ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(1, 0));
    LwM2mObjectInstance instance = (LwM2mObjectInstance) readResponse.getContent();
    assertEquals(lifetime, instance.getResource(1));
    assertEquals(defaultMinPeriod, instance.getResource(2));
    assertEquals(notificationStoring, instance.getResource(6));
    assertEquals(binding, instance.getResource(7));
    // removed not contained optional writable resource
    assertNull(instance.getResource(3));
}
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) Test(org.junit.Test)

Example 8 with LwM2mObjectInstance

use of org.eclipse.leshan.core.node.LwM2mObjectInstance 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 LwM2mObjectInstance

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

the class CoapRequestBuilderTest method build_create_request__with_instance_id.

@Test
public void build_create_request__with_instance_id() throws Exception {
    Registration reg = newRegistration();
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    CreateRequest request = new CreateRequest(12, new LwM2mObjectInstance(26, LwM2mSingleResource.newStringResource(0, "value")));
    builder.visit(request);
    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.POST, coapRequest.getCode());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals("coap://127.0.0.1:12354/12", coapRequest.getURI());
    assertEquals(ContentFormat.TLV.getCode(), coapRequest.getOptions().getContentFormat());
    assertNotNull(coapRequest.getPayload());
    // assert it is encoded as array of instance TLV
    Tlv[] tlvs = TlvDecoder.decode(ByteBuffer.wrap(coapRequest.getPayload()));
    assertEquals(TlvType.OBJECT_INSTANCE, tlvs[0].getType());
    assertEquals(26, tlvs[0].getIdentifier());
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) Registration(org.eclipse.leshan.server.registration.Registration) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) Request(org.eclipse.californium.core.coap.Request) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) Tlv(org.eclipse.leshan.tlv.Tlv) Test(org.junit.Test)

Example 10 with LwM2mObjectInstance

use of org.eclipse.leshan.core.node.LwM2mObjectInstance 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)

Aggregations

LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)49 Test (org.junit.Test)30 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)25 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)13 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)13 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)11 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)10 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)9 ArrayList (java.util.ArrayList)8 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)8 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)6 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)6 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)5 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)5 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)4 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)4 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)4 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)4 JsonObject (com.eclipsesource.json.JsonObject)3 Collection (java.util.Collection)3