Search in sources :

Example 1 with ResponseCode

use of org.eclipse.leshan.ResponseCode in project leshan by eclipse.

the class ResponseSerDes method deserialize.

public static LwM2mResponse deserialize(JsonObject o) {
    String sCode = o.getString("code", null);
    if (sCode == null)
        throw new IllegalStateException("Invalid response missing code attribute");
    ResponseCode code = ResponseCode.fromName(sCode);
    String errorMessage = o.getString("errorMessage", null);
    String kind = o.getString("kind", null);
    switch(kind) {
        case "observe":
            {
                // TODO ser Observation
                LwM2mNode content = LwM2mNodeSerDes.deserialize((JsonObject) o.get("content"));
                return new ObserveResponse(code, content, null, null, errorMessage);
            }
        case "delete":
            return new DeleteResponse(code, errorMessage);
        case "discover":
            String objectLinks = o.getString("objectLinks", "");
            return new DiscoverResponse(code, Link.parse(objectLinks.getBytes()), errorMessage);
        case "create":
            {
                String location = o.getString("location", null);
                return new CreateResponse(code, location, errorMessage);
            }
        case "execute":
            return new ExecuteResponse(code, errorMessage);
        case "writeAttributes":
            {
                return new WriteAttributesResponse(code, errorMessage);
            }
        case "write":
            {
                return new WriteResponse(code, errorMessage);
            }
        case "read":
            {
                LwM2mNode content = LwM2mNodeSerDes.deserialize((JsonObject) o.get("content"));
                return new ReadResponse(code, content, errorMessage);
            }
        default:
            throw new IllegalStateException("Invalid response missing kind attribute");
    }
}
Also used : ResponseCode(org.eclipse.leshan.ResponseCode) CreateResponse(org.eclipse.leshan.core.response.CreateResponse) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) JsonObject(com.eclipsesource.json.JsonObject) WriteAttributesResponse(org.eclipse.leshan.core.response.WriteAttributesResponse) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) ExecuteResponse(org.eclipse.leshan.core.response.ExecuteResponse) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) DeleteResponse(org.eclipse.leshan.core.response.DeleteResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse)

Aggregations

JsonObject (com.eclipsesource.json.JsonObject)1 ResponseCode (org.eclipse.leshan.ResponseCode)1 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)1 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)1 DeleteResponse (org.eclipse.leshan.core.response.DeleteResponse)1 DiscoverResponse (org.eclipse.leshan.core.response.DiscoverResponse)1 ExecuteResponse (org.eclipse.leshan.core.response.ExecuteResponse)1 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)1 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)1 WriteAttributesResponse (org.eclipse.leshan.core.response.WriteAttributesResponse)1 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)1