Search in sources :

Example 11 with Response

use of org.eclipse.californium.core.coap.Response in project leshan by eclipse.

the class ObserveTest method can_observe_timestamped_object.

@Test
public void can_observe_timestamped_object() throws InterruptedException {
    TestObservationListener listener = new TestObservationListener();
    helper.server.getObservationService().addListener(listener);
    // observe device timezone
    ObserveResponse observeResponse = helper.server.send(helper.getCurrentRegistration(), new ObserveRequest(3));
    assertEquals(ResponseCode.CONTENT, observeResponse.getCode());
    assertNotNull(observeResponse.getCoapResponse());
    assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class)));
    // an observation response should have been sent
    Observation observation = observeResponse.getObservation();
    assertEquals("/3", observation.getPath().toString());
    assertEquals(helper.getCurrentRegistration().getId(), observation.getRegistrationId());
    Set<Observation> observations = helper.server.getObservationService().getObservations(helper.getCurrentRegistration());
    assertTrue("We should have only on observation", observations.size() == 1);
    assertTrue("New observation is not there", observations.contains(observation));
    // *** HACK send time-stamped notification as Leshan client does not support it *** //
    // create time-stamped nodes
    TimestampedLwM2mNode mostRecentNode = new TimestampedLwM2mNode(System.currentTimeMillis(), new LwM2mObject(3, new LwM2mObjectInstance(0, LwM2mSingleResource.newStringResource(15, "Paris"))));
    List<TimestampedLwM2mNode> timestampedNodes = new ArrayList<>();
    timestampedNodes.add(mostRecentNode);
    timestampedNodes.add(new TimestampedLwM2mNode(mostRecentNode.getTimestamp() - 2, new LwM2mObject(3, new LwM2mObjectInstance(0, LwM2mSingleResource.newStringResource(15, "Londres")))));
    byte[] payload = LwM2mNodeJsonEncoder.encodeTimestampedData(timestampedNodes, new LwM2mPath("/3"), new LwM2mModel(helper.createObjectModels()), new DefaultLwM2mValueConverter());
    Response firstCoapResponse = (Response) observeResponse.getCoapResponse();
    sendNotification(getConnector(helper.client), payload, firstCoapResponse, ContentFormat.JSON_CODE);
    // *** Hack End *** //
    // verify result
    listener.waitForNotification(2000);
    assertTrue(listener.receivedNotify().get());
    assertEquals(mostRecentNode.getNode(), listener.getResponse().getContent());
    assertEquals(timestampedNodes, listener.getResponse().getTimestampedLwM2mNode());
    assertNotNull(listener.getResponse().getCoapResponse());
    assertThat(listener.getResponse().getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : ArrayList(java.util.ArrayList) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) DefaultLwM2mValueConverter(org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) Test(org.junit.Test)

Example 12 with Response

use of org.eclipse.californium.core.coap.Response in project leshan by eclipse.

the class ObserveTest method sendNotification.

private void sendNotification(Connector connector, byte[] payload, Response firstCoapResponse, int contentFormat) {
    // create observe response
    Response response = new Response(org.eclipse.californium.core.coap.CoAP.ResponseCode.CONTENT);
    response.setType(Type.NON);
    response.setPayload(payload);
    response.setMID(firstCoapResponse.getMID() + 1);
    response.setToken(firstCoapResponse.getToken());
    OptionSet options = new OptionSet().setContentFormat(contentFormat).setObserve(firstCoapResponse.getOptions().getObserve() + 1);
    response.setOptions(options);
    EndpointContext context = new AddressEndpointContext(helper.server.getUnsecuredAddress().getAddress(), helper.server.getUnsecuredAddress().getPort());
    response.setDestinationContext(context);
    // serialize response
    UdpDataSerializer serializer = new UdpDataSerializer();
    RawData data = serializer.serializeResponse(response);
    // send it
    connector.send(data);
}
Also used : Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) RawData(org.eclipse.californium.elements.RawData) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) EndpointContext(org.eclipse.californium.elements.EndpointContext) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) OptionSet(org.eclipse.californium.core.coap.OptionSet) UdpDataSerializer(org.eclipse.californium.core.network.serialization.UdpDataSerializer)

Example 13 with Response

use of org.eclipse.californium.core.coap.Response in project leshan by eclipse.

the class CaliforniumLwM2mBootstrapRequestSender method send.

@Override
public <T extends LwM2mResponse> void send(final String endpointName, final Identity destination, final DownlinkRequest<T> request, final long timeout, ResponseCallback<T> responseCallback, ErrorCallback errorCallback) {
    // Create the CoAP request from LwM2m request
    CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(destination, model, encoder);
    request.accept(coapClientRequestBuilder);
    final Request coapRequest = coapClientRequestBuilder.getRequest();
    // Add CoAP request callback
    MessageObserver obs = new AsyncRequestObserver<T>(coapRequest, responseCallback, errorCallback, timeout) {

        @Override
        public T buildResponse(Response coapResponse) {
            // TODO we need to fix that by removing the Client dependency from LwM2MResponseBuilder or by creating a
            // LwM2mBootstrapResponseBuilder
            Registration registration = new Registration.Builder("fakeregistrationid", endpointName, destination, destination.isSecure() ? secureEndpoint.getAddress() : nonSecureEndpoint.getAddress()).build();
            // Build LwM2m response
            LwM2mResponseBuilder<T> lwm2mResponseBuilder = new LwM2mResponseBuilder<>(coapRequest, coapResponse, registration, model, null, decoder);
            request.accept(lwm2mResponseBuilder);
            return lwm2mResponseBuilder.getResponse();
        }
    };
    coapRequest.addMessageObserver(obs);
    // Send CoAP request asynchronously
    if (destination.isSecure())
        secureEndpoint.sendRequest(coapRequest);
    else
        nonSecureEndpoint.sendRequest(coapRequest);
}
Also used : Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) MessageObserver(org.eclipse.californium.core.coap.MessageObserver) Registration(org.eclipse.leshan.server.registration.Registration) Request(org.eclipse.californium.core.coap.Request) DownlinkRequest(org.eclipse.leshan.core.request.DownlinkRequest) AsyncRequestObserver(org.eclipse.leshan.core.californium.AsyncRequestObserver)

Example 14 with Response

use of org.eclipse.californium.core.coap.Response in project leshan by eclipse.

the class CaliforniumLwM2mRequestSender method send.

@Override
public <T extends LwM2mResponse> T send(final Registration destination, final DownlinkRequest<T> request, long timeout) throws InterruptedException {
    // Retrieve the objects definition
    final LwM2mModel model = modelProvider.getObjectModel(destination);
    // Create the CoAP request from LwM2m request
    CoapRequestBuilder coapRequestBuilder = new CoapRequestBuilder(destination.getIdentity(), destination.getRootPath(), destination.getId(), destination.getEndpoint(), model, encoder);
    request.accept(coapRequestBuilder);
    final Request coapRequest = coapRequestBuilder.getRequest();
    // Send CoAP request synchronously
    SyncRequestObserver<T> syncMessageObserver = new SyncRequestObserver<T>(coapRequest, timeout) {

        @Override
        public T buildResponse(Response coapResponse) {
            // Build LwM2m response
            LwM2mResponseBuilder<T> lwm2mResponseBuilder = new LwM2mResponseBuilder<>(coapRequest, coapResponse, destination, model, observationService, decoder);
            request.accept(lwm2mResponseBuilder);
            return lwm2mResponseBuilder.getResponse();
        }
    };
    coapRequest.addMessageObserver(syncMessageObserver);
    // Store pending request to cancel it on de-registration
    addPendingRequest(destination.getId(), coapRequest);
    // Send CoAP request asynchronously
    Endpoint endpoint = getEndpointForClient(destination);
    endpoint.sendRequest(coapRequest);
    // Wait for response, then return it
    return syncMessageObserver.waitForResponse();
}
Also used : Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) Endpoint(org.eclipse.californium.core.network.Endpoint) Request(org.eclipse.californium.core.coap.Request) DownlinkRequest(org.eclipse.leshan.core.request.DownlinkRequest) SyncRequestObserver(org.eclipse.leshan.core.californium.SyncRequestObserver) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel)

Example 15 with Response

use of org.eclipse.californium.core.coap.Response in project thingsboard by thingsboard.

the class JsonCoapAdaptor method convertToRuleEngineErrorResponse.

private Response convertToRuleEngineErrorResponse(CoapSessionCtx ctx, RuleEngineErrorMsg msg) {
    ResponseCode status = ResponseCode.INTERNAL_SERVER_ERROR;
    switch(msg.getError()) {
        case PLUGIN_TIMEOUT:
            status = ResponseCode.GATEWAY_TIMEOUT;
            break;
        default:
            if (msg.getInMsgType() == MsgType.TO_SERVER_RPC_REQUEST) {
                status = ResponseCode.BAD_REQUEST;
            }
            break;
    }
    Response response = new Response(status);
    response.setPayload(JsonConverter.toErrorJson(msg.getErrorMsg()).toString());
    return response;
}
Also used : Response(org.eclipse.californium.core.coap.Response) ResponseCode(org.eclipse.californium.core.coap.CoAP.ResponseCode)

Aggregations

Response (org.eclipse.californium.core.coap.Response)16 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)11 Request (org.eclipse.californium.core.coap.Request)7 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)6 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)6 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)6 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)5 Test (org.junit.Test)5 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)4 DefaultLwM2mValueConverter (org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter)4 Observation (org.eclipse.leshan.core.observation.Observation)4 DownlinkRequest (org.eclipse.leshan.core.request.DownlinkRequest)4 ArrayList (java.util.ArrayList)3 MessageObserver (org.eclipse.californium.core.coap.MessageObserver)3 AsyncRequestObserver (org.eclipse.leshan.core.californium.AsyncRequestObserver)3 SyncRequestObserver (org.eclipse.leshan.core.californium.SyncRequestObserver)3 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)3 Endpoint (org.eclipse.californium.core.network.Endpoint)2 AddressEndpointContext (org.eclipse.californium.elements.AddressEndpointContext)2 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)2