Search in sources :

Example 1 with LwM2mResourceInstance

use of org.eclipse.leshan.core.node.LwM2mResourceInstance in project thingsboard by thingsboard.

the class DefaultLwM2MAttributesService method onAttributesUpdate.

/**
 * #1.1 If two names have equal path => last time attribute
 * #2.1 if there is a difference in values between the current resource values and the shared attribute values
 * => send to client Request Update of value (new value from shared attribute)
 * and LwM2MClient.delayedRequests.add(path)
 * #2.1 if there is not a difference in values between the current resource values and the shared attribute values
 */
@Override
public void onAttributesUpdate(LwM2mClient lwM2MClient, List<TransportProtos.TsKvProto> tsKvProtos, boolean logFailedUpdateOfNonChangedValue) {
    log.trace("[{}] onAttributesUpdate [{}]", lwM2MClient.getEndpoint(), tsKvProtos);
    Map<String, TransportProtos.TsKvProto> attributesUpdate = new ConcurrentHashMap<>();
    tsKvProtos.forEach(tsKvProto -> {
        try {
            String pathIdVer = clientContext.getObjectIdByKeyNameFromProfile(lwM2MClient, tsKvProto.getKv().getKey());
            if (pathIdVer != null) {
                // #1.1
                if (lwM2MClient.getSharedAttributes().containsKey(pathIdVer)) {
                    if (tsKvProto.getTs() > lwM2MClient.getSharedAttributes().get(pathIdVer).getTs()) {
                        attributesUpdate.put(pathIdVer, tsKvProto);
                    }
                } else {
                    attributesUpdate.put(pathIdVer, tsKvProto);
                }
            }
        } catch (IllegalArgumentException e) {
            log.error("Failed update resource [" + lwM2MClient.getEndpoint() + "] onAttributesUpdate:", e);
            String logMsg = String.format("%s: Failed update resource onAttributesUpdate %s.", LOG_LWM2M_ERROR, e.getMessage());
            logService.log(lwM2MClient, logMsg);
        }
    });
    clientContext.update(lwM2MClient);
    // #2.1
    attributesUpdate.forEach((pathIdVer, tsKvProto) -> {
        ResourceModel resourceModel = lwM2MClient.getResourceModel(pathIdVer, modelProvider);
        Object newValProto = getValueFromKvProto(tsKvProto.getKv());
        Object oldResourceValue = this.getResourceValueFormatKv(lwM2MClient, pathIdVer);
        if (!resourceModel.multiple || !(newValProto instanceof JsonElement)) {
            this.pushUpdateToClientIfNeeded(lwM2MClient, oldResourceValue, newValProto, pathIdVer, tsKvProto, logFailedUpdateOfNonChangedValue);
        } else {
            try {
                pushUpdateMultiToClientIfNeeded(lwM2MClient, resourceModel, (JsonElement) newValProto, (Map<Integer, LwM2mResourceInstance>) oldResourceValue, pathIdVer, tsKvProto, logFailedUpdateOfNonChangedValue);
            } catch (Exception e) {
                log.error("Failed update resource [" + lwM2MClient.getEndpoint() + "] onAttributesUpdate:", e);
                String logMsg = String.format("%s: Failed update resource onAttributesUpdate %s.", LOG_LWM2M_ERROR, e.getMessage());
                logService.log(lwM2MClient, logMsg);
            }
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JsonElement(com.google.gson.JsonElement) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LwM2mResourceInstance(org.eclipse.leshan.core.node.LwM2mResourceInstance)

Example 2 with LwM2mResourceInstance

use of org.eclipse.leshan.core.node.LwM2mResourceInstance in project thingsboard by thingsboard.

the class DefaultLwM2mUplinkMsgHandler method onWriteCompositeResponseOk.

@Override
public void onWriteCompositeResponseOk(LwM2mClient client, WriteCompositeRequest request, int code) {
    log.trace("ReadCompositeResponse: [{}]", request.getNodes());
    request.getNodes().forEach((k, v) -> {
        if (v instanceof LwM2mSingleResource) {
            this.updateResourcesValue(client, (LwM2mResource) v, k.toString(), Mode.REPLACE, code);
        } else {
            LwM2mResourceInstance resourceInstance = (LwM2mResourceInstance) v;
            LwM2mMultipleResource multipleResource = new LwM2mMultipleResource(v.getId(), resourceInstance.getType(), resourceInstance);
            this.updateResourcesValue(client, multipleResource, k.toString(), Mode.REPLACE, code);
        }
    });
}
Also used : LwM2mMultipleResource(org.eclipse.leshan.core.node.LwM2mMultipleResource) LwM2mSingleResource(org.eclipse.leshan.core.node.LwM2mSingleResource) LwM2mResourceInstance(org.eclipse.leshan.core.node.LwM2mResourceInstance)

Aggregations

LwM2mResourceInstance (org.eclipse.leshan.core.node.LwM2mResourceInstance)2 JsonElement (com.google.gson.JsonElement)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ResourceModel (org.eclipse.leshan.core.model.ResourceModel)1 LwM2mMultipleResource (org.eclipse.leshan.core.node.LwM2mMultipleResource)1 LwM2mSingleResource (org.eclipse.leshan.core.node.LwM2mSingleResource)1