Search in sources :

Example 1 with Observation

use of org.eclipse.californium.core.observe.Observation in project leshan by eclipse.

the class ObservationSerDes method deserialize.

public static Observation deserialize(byte[] data) {
    JsonObject v = (JsonObject) Json.parse(new String(data));
    EndpointContext endpointContext = EndpointContextSerDes.deserialize(v.get("peer").asObject());
    byte[] req = Hex.decodeHex(v.getString("request", null).toCharArray());
    RawData rawData = RawData.outbound(req, endpointContext, null, false);
    Request request = (Request) parser.parseMessage(rawData);
    request.setDestinationContext(endpointContext);
    JsonValue ctxValue = v.get("context");
    if (ctxValue != null) {
        Map<String, String> context = new HashMap<>();
        JsonObject ctxObject = (JsonObject) ctxValue;
        for (String name : ctxObject.names()) {
            context.put(name, ctxObject.getString(name, null));
        }
        request.setUserContext(context);
    }
    return new Observation(request, endpointContext);
}
Also used : RawData(org.eclipse.californium.elements.RawData) EndpointContext(org.eclipse.californium.elements.EndpointContext) HashMap(java.util.HashMap) Request(org.eclipse.californium.core.coap.Request) JsonValue(com.eclipsesource.json.JsonValue) Observation(org.eclipse.californium.core.observe.Observation) JsonObject(com.eclipsesource.json.JsonObject)

Aggregations

JsonObject (com.eclipsesource.json.JsonObject)1 JsonValue (com.eclipsesource.json.JsonValue)1 HashMap (java.util.HashMap)1 Request (org.eclipse.californium.core.coap.Request)1 Observation (org.eclipse.californium.core.observe.Observation)1 EndpointContext (org.eclipse.californium.elements.EndpointContext)1 RawData (org.eclipse.californium.elements.RawData)1