Search in sources :

Example 21 with Observation

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);
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation)

Example 22 with Observation

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();
    }
}
Also used : Observation(org.eclipse.leshan.core.observation.Observation) Token(org.eclipse.californium.core.coap.Token)

Example 23 with Observation

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;
}
Also used : Observation(org.eclipse.leshan.core.observation.Observation) ArrayList(java.util.ArrayList) Token(org.eclipse.californium.core.coap.Token)

Example 24 with Observation

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;
}
Also used : Observation(org.eclipse.leshan.core.observation.Observation) ArrayList(java.util.ArrayList) Token(org.eclipse.californium.core.coap.Token)

Example 25 with Observation

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;
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) HashSet(java.util.HashSet)

Aggregations

Observation (org.eclipse.leshan.core.observation.Observation)32 Test (org.junit.Test)12 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)11 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)10 Registration (org.eclipse.leshan.server.registration.Registration)10 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)9 ArrayList (java.util.ArrayList)7 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)7 Token (org.eclipse.californium.core.coap.Token)6 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)6 Jedis (redis.clients.jedis.Jedis)6 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)5 Response (org.eclipse.californium.core.coap.Response)4 DefaultLwM2mValueConverter (org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter)4 Deregistration (org.eclipse.leshan.server.registration.Deregistration)4 UpdatedRegistration (org.eclipse.leshan.server.registration.UpdatedRegistration)4 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)3 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)3 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)3 RegistrationListener (org.eclipse.leshan.server.registration.RegistrationListener)3