Search in sources :

Example 1 with AttributeSet

use of org.eclipse.leshan.core.attributes.AttributeSet in project thingsboard by thingsboard.

the class DefaultLwM2mDownlinkMsgHandler method sendWriteAttributesRequest.

/**
 * Example # 1:
 * AttributeSet attributes = new AttributeSet(new Attribute(Attribute.MINIMUM_PERIOD, 10L),
 * new Attribute(Attribute.MAXIMUM_PERIOD, 100L));
 * WriteAttributesRequest requestTest = new WriteAttributesRequest(3, 0, 14, attributes);
 * sendSimpleRequest(client, requestTest, request.getTimeout(), callback);
 * <p>
 * Example # 2
 * Dimension and Object version are read only attributes.
 * addAttribute(attributes, DIMENSION, params.getDim(), dim -> dim >= 0 && dim <= 255);
 * addAttribute(attributes, OBJECT_VERSION, params.getVer(), StringUtils::isNotEmpty, Function.identity());
 */
@Override
public void sendWriteAttributesRequest(LwM2mClient client, TbLwM2MWriteAttributesRequest request, DownlinkRequestCallback<WriteAttributesRequest, WriteAttributesResponse> callback) {
    try {
        validateVersionedId(client, request);
        if (request.getAttributes() == null) {
            throw new IllegalArgumentException("Attributes to write are not specified!");
        }
        ObjectAttributes params = request.getAttributes();
        List<Attribute> attributes = new LinkedList<>();
        addAttribute(attributes, MAXIMUM_PERIOD, params.getPmax());
        addAttribute(attributes, MINIMUM_PERIOD, params.getPmin());
        addAttribute(attributes, GREATER_THAN, params.getGt());
        addAttribute(attributes, LESSER_THAN, params.getLt());
        addAttribute(attributes, STEP, params.getSt());
        AttributeSet attributeSet = new AttributeSet(attributes);
        sendSimpleRequest(client, new WriteAttributesRequest(request.getObjectId(), attributeSet), request.getTimeout(), callback);
    } catch (InvalidRequestException e) {
        callback.onValidationError(request.toString(), e.getMessage());
    }
}
Also used : Attribute(org.eclipse.leshan.core.attributes.Attribute) AttributeSet(org.eclipse.leshan.core.attributes.AttributeSet) ObjectAttributes(org.thingsboard.server.common.data.device.profile.lwm2m.ObjectAttributes) InvalidRequestException(org.eclipse.leshan.core.request.exception.InvalidRequestException) LinkedList(java.util.LinkedList) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest)

Aggregations

LinkedList (java.util.LinkedList)1 Attribute (org.eclipse.leshan.core.attributes.Attribute)1 AttributeSet (org.eclipse.leshan.core.attributes.AttributeSet)1 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)1 InvalidRequestException (org.eclipse.leshan.core.request.exception.InvalidRequestException)1 ObjectAttributes (org.thingsboard.server.common.data.device.profile.lwm2m.ObjectAttributes)1