Search in sources :

Example 1 with LwM2m

use of org.eclipse.leshan.core.LwM2m in project thingsboard by thingsboard.

the class DefaultLwM2mDownlinkMsgHandler method sendCreateRequest.

public void sendCreateRequest(LwM2mClient client, TbLwM2MCreateRequest request, DownlinkRequestCallback<CreateRequest, CreateResponse> callback) {
    validateVersionedId(client, request);
    CreateRequest downlink = null;
    LwM2mPath resultIds = new LwM2mPath(request.getObjectId());
    ObjectModel objectModel = client.getObjectModel(request.getVersionedId(), modelProvider);
    // POST /{Object ID}/{Object Instance ID} && Resources is Mandatory
    if (objectModel != null) {
        if (objectModel.multiple) {
            // LwM2M CBOR, SenML CBOR, SenML JSON, or TLV (see [LwM2M-CORE])
            ContentFormat contentFormat = getWriteRequestContentFormat(client, request, modelProvider);
            if (resultIds.isObject() || resultIds.isObjectInstance()) {
                Collection<LwM2mResource> resources;
                if (resultIds.isObject()) {
                    if (request.getValue() != null) {
                        resources = client.getNewResourcesForInstance(request.getVersionedId(), request.getValue(), modelProvider, this.converter);
                        downlink = new CreateRequest(contentFormat, resultIds.getObjectId(), resources);
                    } else if (request.getNodes() != null && request.getNodes().size() > 0) {
                        Set<LwM2mObjectInstance> instances = ConcurrentHashMap.newKeySet();
                        request.getNodes().forEach((key, value) -> {
                            Collection<LwM2mResource> resourcesForInstance = client.getNewResourcesForInstance(request.getVersionedId(), value, modelProvider, this.converter);
                            LwM2mObjectInstance instance = new LwM2mObjectInstance(Integer.parseInt(key), resourcesForInstance);
                            instances.add(instance);
                        });
                        LwM2mObjectInstance[] instanceArrays = instances.toArray(new LwM2mObjectInstance[instances.size()]);
                        downlink = new CreateRequest(contentFormat, resultIds.getObjectId(), instanceArrays);
                    }
                } else {
                    resources = client.getNewResourcesForInstance(request.getVersionedId(), request.getValue(), modelProvider, this.converter);
                    LwM2mObjectInstance instance = new LwM2mObjectInstance(resultIds.getObjectInstanceId(), resources);
                    downlink = new CreateRequest(contentFormat, resultIds.getObjectId(), instance);
                }
            }
            if (downlink != null) {
                sendSimpleRequest(client, downlink, request.getTimeout(), callback);
            } else {
                callback.onValidationError(toString(request), "Path " + request.getVersionedId() + ". Object must be Multiple !");
            }
        } else {
            throw new IllegalArgumentException("Path " + request.getVersionedId() + ". Object must be Multiple !");
        }
    } else {
        callback.onValidationError(toString(request), "Resource " + request.getVersionedId() + " is not configured in the device profile!");
    }
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2MTransportServerConfig(org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig) LwM2MTelemetryLogService(org.thingsboard.server.transport.lwm2m.server.log.LwM2MTelemetryLogService) Arrays(java.util.Arrays) Hex(org.eclipse.leshan.core.util.Hex) GREATER_THAN(org.eclipse.leshan.core.attributes.Attribute.GREATER_THAN) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) Date(java.util.Date) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) ClientSleepingException(org.eclipse.leshan.core.request.exception.ClientSleepingException) RequiredArgsConstructor(lombok.RequiredArgsConstructor) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) ContentFormat(org.eclipse.leshan.core.request.ContentFormat) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) MINIMUM_PERIOD(org.eclipse.leshan.core.attributes.Attribute.MINIMUM_PERIOD) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) OPAQUE(org.eclipse.leshan.core.model.ResourceModel.Type.OPAQUE) LwM2mModelProvider(org.eclipse.leshan.server.model.LwM2mModelProvider) LwM2mTransportContext(org.thingsboard.server.transport.lwm2m.server.LwM2mTransportContext) PreDestroy(javax.annotation.PreDestroy) LwM2MExecutorAwareService(org.thingsboard.server.transport.lwm2m.server.common.LwM2MExecutorAwareService) RpcWriteCompositeRequest(org.thingsboard.server.transport.lwm2m.server.rpc.composite.RpcWriteCompositeRequest) Registration(org.eclipse.leshan.server.registration.Registration) Observation(org.eclipse.leshan.core.observation.Observation) TimeoutException(org.eclipse.leshan.core.request.exception.TimeoutException) AttributeSet(org.eclipse.leshan.core.attributes.AttributeSet) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) Map(java.util.Map) WriteCompositeResponse(org.eclipse.leshan.core.response.WriteCompositeResponse) LwM2mClientContext(org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext) OBJLNK(org.eclipse.leshan.core.model.ResourceModel.Type.OBJLNK) DownlinkRequest(org.eclipse.leshan.core.request.DownlinkRequest) Link(org.eclipse.leshan.core.link.Link) MAXIMUM_PERIOD(org.eclipse.leshan.core.attributes.Attribute.MAXIMUM_PERIOD) Predicate(java.util.function.Predicate) Collection(java.util.Collection) LESSER_THAN(org.eclipse.leshan.core.attributes.Attribute.LESSER_THAN) LwM2MTransportUtil.createModelsDefault(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.createModelsDefault) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) LwM2MTransportUtil.validateVersionedId(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.validateVersionedId) ExecuteResponse(org.eclipse.leshan.core.response.ExecuteResponse) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) ObjectLink(org.eclipse.leshan.core.node.ObjectLink) SimpleDownlinkRequest(org.eclipse.leshan.core.request.SimpleDownlinkRequest) PostConstruct(javax.annotation.PostConstruct) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) TbLwM2MReadCompositeRequest(org.thingsboard.server.transport.lwm2m.server.downlink.composite.TbLwM2MReadCompositeRequest) DeleteResponse(org.eclipse.leshan.core.response.DeleteResponse) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) LwM2mClient(org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient) ObjectAttributes(org.thingsboard.server.common.data.device.profile.lwm2m.ObjectAttributes) LwM2mVersionedModelProvider(org.thingsboard.server.transport.lwm2m.server.LwM2mVersionedModelProvider) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) TbLwM2mTransportComponent(org.thingsboard.server.queue.util.TbLwM2mTransportComponent) Function(java.util.function.Function) CreateResponse(org.eclipse.leshan.core.response.CreateResponse) ReadCompositeResponse(org.eclipse.leshan.core.response.ReadCompositeResponse) WriteAttributesResponse(org.eclipse.leshan.core.response.WriteAttributesResponse) LwM2MTransportUtil.convertMultiResourceValuesFromRpcBody(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.convertMultiResourceValuesFromRpcBody) Service(org.springframework.stereotype.Service) STEP(org.eclipse.leshan.core.attributes.Attribute.STEP) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) Attribute(org.eclipse.leshan.core.attributes.Attribute) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LinkedList(java.util.LinkedList) CompositeDownlinkRequest(org.eclipse.leshan.core.request.CompositeDownlinkRequest) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) LwM2MTransportUtil.getVerFromPathIdVerOrId(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.getVerFromPathIdVerOrId) SingleObservation(org.eclipse.leshan.core.observation.SingleObservation) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) WriteCompositeRequest(org.eclipse.leshan.core.request.WriteCompositeRequest) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) ReadCompositeRequest(org.eclipse.leshan.core.request.ReadCompositeRequest) LwM2mValueConverterImpl(org.thingsboard.server.transport.lwm2m.utils.LwM2mValueConverterImpl) LwM2MTransportUtil.fromVersionedIdToObjectId(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.fromVersionedIdToObjectId) InvalidRequestException(org.eclipse.leshan.core.request.exception.InvalidRequestException) LwM2m(org.eclipse.leshan.core.LwM2m) ObjectModel(org.eclipse.leshan.core.model.ObjectModel) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) ObjectModel(org.eclipse.leshan.core.model.ObjectModel) AttributeSet(org.eclipse.leshan.core.attributes.AttributeSet) Set(java.util.Set) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ContentFormat(org.eclipse.leshan.core.request.ContentFormat) Collection(java.util.Collection) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource)

Aggregations

Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 PostConstruct (javax.annotation.PostConstruct)1 PreDestroy (javax.annotation.PreDestroy)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Slf4j (lombok.extern.slf4j.Slf4j)1 LwM2m (org.eclipse.leshan.core.LwM2m)1 Attribute (org.eclipse.leshan.core.attributes.Attribute)1 GREATER_THAN (org.eclipse.leshan.core.attributes.Attribute.GREATER_THAN)1 LESSER_THAN (org.eclipse.leshan.core.attributes.Attribute.LESSER_THAN)1 MAXIMUM_PERIOD (org.eclipse.leshan.core.attributes.Attribute.MAXIMUM_PERIOD)1