Search in sources :

Example 1 with LwM2mPath

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

the class ObjectEnabler method doObserve.

@Override
protected ObserveResponse doObserve(final ServerIdentity identity, final ObserveRequest request) {
    final LwM2mPath path = request.getPath();
    // Manage Object case
    if (path.isObject()) {
        List<LwM2mObjectInstance> lwM2mObjectInstances = new ArrayList<>();
        for (Entry<Integer, LwM2mInstanceEnabler> entry : instances.entrySet()) {
            lwM2mObjectInstances.add(getLwM2mObjectInstance(entry.getKey(), entry.getValue(), identity, true));
        }
        return ObserveResponse.success(new LwM2mObject(getId(), lwM2mObjectInstances));
    }
    // Manage Instance case
    final LwM2mInstanceEnabler instance = instances.get(path.getObjectInstanceId());
    if (instance == null)
        return ObserveResponse.notFound();
    if (path.getResourceId() == null) {
        return ObserveResponse.success(getLwM2mObjectInstance(path.getObjectInstanceId(), instance, identity, true));
    }
    // Manage Resource case
    return instance.observe(path.getResourceId());
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ArrayList(java.util.ArrayList) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject)

Example 2 with LwM2mPath

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

the class ObjectEnabler method doExecute.

@Override
protected ExecuteResponse doExecute(ExecuteRequest request) {
    LwM2mPath path = request.getPath();
    LwM2mInstanceEnabler instance = instances.get(path.getObjectInstanceId());
    if (instance == null) {
        return ExecuteResponse.notFound();
    }
    return instance.execute(path.getResourceId(), request.getParameters());
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath)

Example 3 with LwM2mPath

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

the class ObjectEnabler method doWrite.

@Override
protected WriteResponse doWrite(ServerIdentity identity, WriteRequest request) {
    LwM2mPath path = request.getPath();
    // Manage Instance case
    LwM2mInstanceEnabler instance = instances.get(path.getObjectInstanceId());
    if (instance == null)
        return WriteResponse.notFound();
    if (path.isObjectInstance()) {
        // instance write
        Map<Integer, LwM2mResource> writeResources = ((LwM2mObjectInstance) request.getNode()).getResources();
        if (request.isReplaceRequest()) {
            // REPLACE
            // make them modifiable
            writeResources = new HashMap<>(writeResources);
            for (ResourceModel resourceModel : getObjectModel().resources.values()) {
                if (!identity.isLwm2mServer() || resourceModel.operations.isWritable()) {
                    LwM2mResource writeResource = writeResources.remove(resourceModel.id);
                    if (null != writeResource) {
                        instance.write(resourceModel.id, writeResource);
                    } else {
                        instance.reset(resourceModel.id);
                    }
                }
            }
        }
        // UPDATE and resources currently not in the model
        for (LwM2mResource resource : writeResources.values()) {
            instance.write(resource.getId(), resource);
        }
        return WriteResponse.success();
    }
    // Manage Resource case
    return instance.write(path.getResourceId(), (LwM2mResource) request.getNode());
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource)

Example 4 with LwM2mPath

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

the class ObjectEnabler method doCreate.

@Override
protected CreateResponse doCreate(CreateRequest request) {
    Integer instanceId = request.getInstanceId();
    if (instanceId == null) {
        // the client is in charge to generate the id of the new instance
        if (instances.isEmpty()) {
            instanceId = 0;
        } else {
            instanceId = Collections.max(instances.keySet()) + 1;
        }
    }
    LwM2mInstanceEnabler newInstance = instanceFactory.create(getObjectModel());
    for (LwM2mResource resource : request.getResources()) {
        newInstance.write(resource.getId(), resource);
    }
    instances.put(instanceId, newInstance);
    listenInstance(newInstance, instanceId);
    return CreateResponse.success(new LwM2mPath(request.getPath().getObjectId(), instanceId).toString());
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource)

Example 5 with LwM2mPath

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

the class ObjectResource method handlePUT.

@Override
public void handlePUT(CoapExchange coapExchange) {
    ServerIdentity identity = extractServerIdentity(coapExchange, bootstrapHandler);
    String URI = coapExchange.getRequestOptions().getUriPathString();
    // get Observe Spec
    ObserveSpec spec = null;
    if (coapExchange.advanced().getRequest().getOptions().getURIQueryCount() != 0) {
        List<String> uriQueries = coapExchange.advanced().getRequest().getOptions().getUriQuery();
        spec = ObserveSpec.parse(uriQueries);
    }
    // Manage Write Attributes Request
    if (spec != null) {
        WriteAttributesResponse response = nodeEnabler.writeAttributes(identity, new WriteAttributesRequest(URI, spec));
        if (response.getCode().isError()) {
            coapExchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
        } else {
            coapExchange.respond(toCoapResponseCode(response.getCode()));
        }
        return;
    } else // Manage Write and Bootstrap Write Request (replace)
    {
        LwM2mPath path = new LwM2mPath(URI);
        if (!coapExchange.getRequestOptions().hasContentFormat()) {
            coapExchange.respond(ResponseCode.BAD_REQUEST, "Content Format is mandatory");
            return;
        }
        ContentFormat contentFormat = ContentFormat.fromCode(coapExchange.getRequestOptions().getContentFormat());
        if (!decoder.isSupported(contentFormat)) {
            coapExchange.respond(ResponseCode.UNSUPPORTED_CONTENT_FORMAT);
            return;
        }
        LwM2mNode lwM2mNode;
        try {
            LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
            lwM2mNode = decoder.decode(coapExchange.getRequestPayload(), contentFormat, path, model);
            if (identity.isLwm2mBootstrapServer()) {
                BootstrapWriteResponse response = nodeEnabler.write(identity, new BootstrapWriteRequest(path, lwM2mNode, contentFormat));
                if (response.getCode().isError()) {
                    coapExchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
                } else {
                    coapExchange.respond(toCoapResponseCode(response.getCode()));
                }
            } else {
                WriteResponse response = nodeEnabler.write(identity, new WriteRequest(Mode.REPLACE, contentFormat, URI, lwM2mNode));
                if (response.getCode().isError()) {
                    coapExchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
                } else {
                    coapExchange.respond(toCoapResponseCode(response.getCode()));
                }
            }
            return;
        } catch (CodecException e) {
            LOG.warn("Unable to decode payload to write", e);
            coapExchange.respond(ResponseCode.BAD_REQUEST);
            return;
        }
    }
}
Also used : BootstrapWriteResponse(org.eclipse.leshan.core.response.BootstrapWriteResponse) ContentFormat(org.eclipse.leshan.core.request.ContentFormat) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) BootstrapWriteResponse(org.eclipse.leshan.core.response.BootstrapWriteResponse) WriteAttributesResponse(org.eclipse.leshan.core.response.WriteAttributesResponse) ObserveSpec(org.eclipse.leshan.ObserveSpec) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) ResourceUtil.extractServerIdentity(org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity) ServerIdentity(org.eclipse.leshan.client.request.ServerIdentity) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) CodecException(org.eclipse.leshan.core.node.codec.CodecException) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest)

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