Search in sources :

Example 1 with Attribute

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

the class LwM2MTransportUtil method createWriteAttributes.

private static Attribute[] createWriteAttributes(Object params, DefaultLwM2mUplinkMsgHandler serviceImpl, String target) {
    List<Attribute> attributeLists = new ArrayList<>();
    Map<String, Object> map = JacksonUtil.convertValue(params, new TypeReference<>() {
    });
    map.forEach((k, v) -> {
        if (StringUtils.trimToNull(v.toString()) != null) {
            Object attrValue = convertWriteAttributes(k, v, serviceImpl, target);
            if (attrValue != null) {
                Attribute attribute = createAttribute(k, attrValue);
                if (attribute != null) {
                    attributeLists.add(new Attribute(k, attrValue));
                }
            }
        }
    });
    return attributeLists.toArray(Attribute[]::new);
}
Also used : Attribute(org.eclipse.leshan.core.attributes.Attribute) ArrayList(java.util.ArrayList)

Example 2 with Attribute

use of org.eclipse.leshan.core.attributes.Attribute 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

Attribute (org.eclipse.leshan.core.attributes.Attribute)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)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