Search in sources :

Example 36 with LwM2mPath

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

the class LwM2mNodeDecoderTest method tlv_acl_object_multi_instance.

@Test
public void tlv_acl_object_multi_instance() throws Exception {
    LwM2mObject oObject = ((LwM2mObject) decoder.decode(ENCODED_ACL, ContentFormat.TLV, new LwM2mPath(2), model));
    assertAclInstances(oObject);
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) Test(org.junit.Test)

Example 37 with LwM2mPath

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

the class LwM2mNodeDecoderTest method json_timestamped_resources.

@Test
public void json_timestamped_resources() throws CodecException {
    // json content for instance 0 of device object
    StringBuilder b = new StringBuilder();
    b.append("{\"e\":[");
    b.append("{\"n\":\"\",\"v\":22.9,\"t\":-30},");
    b.append("{\"n\":\"\",\"v\":22.4,\"t\":-5},");
    b.append("{\"n\":\"\",\"v\":24.1,\"t\":-50}],");
    b.append("\"bt\":25462634}");
    List<TimestampedLwM2mNode> timestampedResources = decoder.decodeTimestampedData(b.toString().getBytes(), ContentFormat.JSON, new LwM2mPath(1024, 0, 1), model);
    assertEquals(3, timestampedResources.size());
    assertEquals(Long.valueOf(25462634L - 5), timestampedResources.get(0).getTimestamp());
    assertEquals(22.4d, ((LwM2mResource) timestampedResources.get(0).getNode()).getValue());
    assertEquals(Long.valueOf(25462634L - 30), timestampedResources.get(1).getTimestamp());
    assertEquals(22.9d, ((LwM2mResource) timestampedResources.get(1).getNode()).getValue());
    assertEquals(Long.valueOf(25462634 - 50), timestampedResources.get(2).getTimestamp());
    assertEquals(24.1d, ((LwM2mResource) timestampedResources.get(2).getNode()).getValue());
}
Also used : TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Test(org.junit.Test)

Example 38 with LwM2mPath

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

the class LwM2mNodeDecoderTest method tlv_multi_instance_with_obj_link.

@Test
public void tlv_multi_instance_with_obj_link() throws Exception {
    LwM2mObject oObject = ((LwM2mObject) decoder.decode(ENCODED_OBJ66, ContentFormat.TLV, new LwM2mPath(66), model));
    assertObj66Instance(oObject);
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) Test(org.junit.Test)

Example 39 with LwM2mPath

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

the class LwM2mNodeDecoderTest method json_empty_multi_resource.

@Test
public void json_empty_multi_resource() {
    // Completely empty
    StringBuilder b = new StringBuilder();
    b.append("{}");
    LwM2mResource resource = (LwM2mResource) decoder.decode(b.toString().getBytes(), ContentFormat.JSON, new LwM2mPath(3, 0, 6), model);
    assertNotNull(resource);
    assertTrue(resource instanceof LwM2mMultipleResource);
    assertEquals(6, resource.getId());
    assertTrue(resource.getValues().isEmpty());
    // with empty resource list
    b = new StringBuilder();
    b.append("{\"e\":[]}");
    resource = (LwM2mResource) decoder.decode(b.toString().getBytes(), ContentFormat.JSON, new LwM2mPath(3, 0, 6), model);
    assertNotNull(resource);
    assertTrue(resource instanceof LwM2mMultipleResource);
    assertEquals(6, resource.getId());
    assertTrue(resource.getValues().isEmpty());
    // with empty resources list and base name
    b = new StringBuilder();
    b.append("{\"bn\":\"3/0/6\", \"e\":[]}");
    resource = (LwM2mResource) decoder.decode(b.toString().getBytes(), ContentFormat.JSON, new LwM2mPath(3, 0, 6), model);
    assertNotNull(resource);
    assertTrue(resource instanceof LwM2mMultipleResource);
    assertEquals(6, resource.getId());
    assertTrue(resource.getValues().isEmpty());
}
Also used : LwM2mMultipleResource(org.eclipse.leshan.core.node.LwM2mMultipleResource) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) Test(org.junit.Test)

Example 40 with LwM2mPath

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

the class ObjectEnabler method doWrite.

@Override
protected BootstrapWriteResponse doWrite(ServerIdentity identity, BootstrapWriteRequest request) {
    LwM2mPath path = request.getPath();
    // Manage Object case
    if (path.isObject()) {
        for (LwM2mObjectInstance instanceNode : ((LwM2mObject) request.getNode()).getInstances().values()) {
            LwM2mInstanceEnabler instanceEnabler = instances.get(instanceNode.getId());
            if (instanceEnabler == null) {
                doCreate(new CreateRequest(path.getObjectId(), instanceNode));
            } else {
                doWrite(identity, new WriteRequest(Mode.REPLACE, path.getObjectId(), path.getObjectInstanceId(), instanceNode.getResources().values()));
            }
        }
        return BootstrapWriteResponse.success();
    }
    // Manage Instance case
    if (path.isObjectInstance()) {
        LwM2mObjectInstance instanceNode = (LwM2mObjectInstance) request.getNode();
        LwM2mInstanceEnabler instanceEnabler = instances.get(path.getObjectInstanceId());
        if (instanceEnabler == null) {
            doCreate(new CreateRequest(path.getObjectId(), instanceNode));
        } else {
            doWrite(identity, new WriteRequest(Mode.REPLACE, request.getContentFormat(), path.getObjectId(), path.getObjectInstanceId(), instanceNode.getResources().values()));
        }
        return BootstrapWriteResponse.success();
    }
    // Manage resource case
    LwM2mResource resource = (LwM2mResource) request.getNode();
    LwM2mInstanceEnabler instanceEnabler = instances.get(path.getObjectInstanceId());
    if (instanceEnabler == null) {
        doCreate(new CreateRequest(path.getObjectId(), new LwM2mObjectInstance(path.getObjectInstanceId(), resource)));
    } else {
        instanceEnabler.write(path.getResourceId(), resource);
    }
    return BootstrapWriteResponse.success();
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource)

Aggregations

LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)70 Test (org.junit.Test)51 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)25 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)13 Observation (org.eclipse.leshan.core.observation.Observation)11 ArrayList (java.util.ArrayList)10 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)10 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)9 Tlv (org.eclipse.leshan.tlv.Tlv)8 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)7 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)6 CodecException (org.eclipse.leshan.core.node.codec.CodecException)6 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)6 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)6 BootstrapWriteRequest (org.eclipse.leshan.core.request.BootstrapWriteRequest)5 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)5 HashMap (java.util.HashMap)4 Response (org.eclipse.californium.core.coap.Response)4 ResourceModel (org.eclipse.leshan.core.model.ResourceModel)4 DefaultLwM2mValueConverter (org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter)4