Search in sources :

Example 1 with SingleObservation

use of org.eclipse.leshan.core.observation.SingleObservation in project thingsboard by thingsboard.

the class DefaultLwM2mDownlinkMsgHandler method sendObserveRequest.

@Override
public void sendObserveRequest(LwM2mClient client, TbLwM2MObserveRequest request, DownlinkRequestCallback<ObserveRequest, ObserveResponse> callback) {
    try {
        validateVersionedId(client, request);
        LwM2mPath resultIds = new LwM2mPath(request.getObjectId());
        Set<Observation> observations = context.getServer().getObservationService().getObservations(client.getRegistration());
        // TODO: should be able to use CompositeObservation
        if (observations.stream().noneMatch(observation -> ((SingleObservation) observation).getPath().equals(resultIds))) {
            ObserveRequest downlink;
            ContentFormat contentFormat = getReadRequestContentFormat(client, request, modelProvider);
            if (resultIds.isResource()) {
                downlink = new ObserveRequest(contentFormat, resultIds.getObjectId(), resultIds.getObjectInstanceId(), resultIds.getResourceId());
            } else if (resultIds.isObjectInstance()) {
                downlink = new ObserveRequest(contentFormat, resultIds.getObjectId(), resultIds.getObjectInstanceId());
            } else {
                downlink = new ObserveRequest(contentFormat, resultIds.getObjectId());
            }
            log.info("[{}] Send observation: {}.", client.getEndpoint(), request.getVersionedId());
            sendSimpleRequest(client, downlink, request.getTimeout(), callback);
        } else {
            callback.onValidationError(resultIds.toString(), "Observation is already registered!");
        }
    } catch (InvalidRequestException e) {
        callback.onValidationError(request.toString(), e.getMessage());
    }
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ContentFormat(org.eclipse.leshan.core.request.ContentFormat) Observation(org.eclipse.leshan.core.observation.Observation) SingleObservation(org.eclipse.leshan.core.observation.SingleObservation) SingleObservation(org.eclipse.leshan.core.observation.SingleObservation) InvalidRequestException(org.eclipse.leshan.core.request.exception.InvalidRequestException) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest)

Example 2 with SingleObservation

use of org.eclipse.leshan.core.observation.SingleObservation in project thingsboard by thingsboard.

the class TbLwM2mRedisRegistrationStore method addObservation.

/* *************** Leshan Observation API **************** */
/*
     * The observation is not persisted here, it is done by the Californium layer (in the implementation of the
     * org.eclipse.californium.core.observe.ObservationStore#add method)
     */
@Override
public Collection<Observation> addObservation(String registrationId, Observation observation) {
    List<Observation> removed = new ArrayList<>();
    try (var connection = connectionFactory.getConnection()) {
        // fetch the client ep by registration ID index
        byte[] ep = connection.get(toRegIdKey(registrationId));
        if (ep == null) {
            return null;
        }
        Lock lock = null;
        String lockKey = toLockKey(ep);
        try {
            lock = redisLock.obtain(lockKey);
            lock.lock();
            // cancel existing observations for the same path and registration id.
            for (Observation obs : getObservations(connection, registrationId)) {
                // TODO: should be able to use CompositeObservation
                if (((SingleObservation) observation).getPath().equals(((SingleObservation) obs).getPath()) && !Arrays.equals(observation.getId(), obs.getId())) {
                    removed.add(obs);
                    unsafeRemoveObservation(connection, registrationId, obs.getId());
                }
            }
        } finally {
            if (lock != null) {
                lock.unlock();
            }
        }
    }
    return removed;
}
Also used : Observation(org.eclipse.leshan.core.observation.Observation) SingleObservation(org.eclipse.leshan.core.observation.SingleObservation) ArrayList(java.util.ArrayList) SingleObservation(org.eclipse.leshan.core.observation.SingleObservation) Lock(java.util.concurrent.locks.Lock)

Aggregations

Observation (org.eclipse.leshan.core.observation.Observation)2 SingleObservation (org.eclipse.leshan.core.observation.SingleObservation)2 ArrayList (java.util.ArrayList)1 Lock (java.util.concurrent.locks.Lock)1 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)1 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)1 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)1 InvalidRequestException (org.eclipse.leshan.core.request.exception.InvalidRequestException)1