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());
}
}
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;
}
Aggregations