use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.
the class ObserveUtil method createLwM2mObservation.
/**
* Create a LWM2M observation from a CoAP request.
*/
public static Observation createLwM2mObservation(Request request) {
String regId = null;
String lwm2mPath = null;
Map<String, String> context = null;
for (Entry<String, String> ctx : request.getUserContext().entrySet()) {
switch(ctx.getKey()) {
case CTX_REGID:
regId = ctx.getValue();
break;
case CTX_LWM2M_PATH:
lwm2mPath = ctx.getValue();
break;
case CTX_ENDPOINT:
break;
default:
if (context == null) {
context = new HashMap<>();
}
context.put(ctx.getKey(), ctx.getValue());
}
}
return new Observation(request.getToken().getBytes(), regId, new LwM2mPath(lwm2mPath), context);
}
use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.
the class InMemoryRegistrationStore method removeObservation.
@Override
public Observation removeObservation(String registrationId, byte[] observationId) {
try {
lock.writeLock().lock();
Token token = new Token(observationId);
Observation observation = build(unsafeGetObservation(token));
if (observation != null && registrationId.equals(observation.getRegistrationId())) {
unsafeRemoveObservation(token);
return observation;
}
return null;
} finally {
lock.writeLock().unlock();
}
}
use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.
the class InMemoryRegistrationStore 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 {
lock.writeLock().lock();
// cancel existing observations for the same path and registration id.
for (Observation obs : unsafeGetObservations(registrationId)) {
if (observation.getPath().equals(obs.getPath()) && !Arrays.equals(observation.getId(), obs.getId())) {
unsafeRemoveObservation(new Token(obs.getId()));
removed.add(obs);
}
}
} finally {
lock.writeLock().unlock();
}
return removed;
}
use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.
the class InMemoryRegistrationStore method unsafeRemoveAllObservations.
private Collection<Observation> unsafeRemoveAllObservations(String registrationId) {
Collection<Observation> removed = new ArrayList<>();
Set<Token> tokens = tokensByRegId.get(registrationId);
if (tokens != null) {
for (Token token : tokens) {
Observation observationRemoved = build(obsByToken.remove(token));
if (observationRemoved != null) {
removed.add(observationRemoved);
}
}
}
tokensByRegId.remove(registrationId);
return removed;
}
use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.
the class ObservationServiceImpl method getObservations.
private Set<Observation> getObservations(String registrationId, String resourcePath) {
if (registrationId == null || resourcePath == null)
return Collections.emptySet();
Set<Observation> result = new HashSet<>();
LwM2mPath lwPath = new LwM2mPath(resourcePath);
for (Observation obs : getObservations(registrationId)) {
if (lwPath.equals(obs.getPath())) {
result.add(obs);
}
}
return result;
}
Aggregations