use of org.eclipse.leshan.core.model.LwM2mModel in project leshan by eclipse.
the class CoapRequestBuilderTest method loadModel.
@BeforeClass
public static void loadModel() {
model = new LwM2mModel(ObjectLoader.loadDefault());
encoder = new DefaultLwM2mNodeEncoder();
}
use of org.eclipse.leshan.core.model.LwM2mModel in project leshan by eclipse.
the class ObserveTest method can_handle_error_on_notification.
@Test
public void can_handle_error_on_notification() throws InterruptedException {
TestObservationListener listener = new TestObservationListener();
helper.server.getObservationService().addListener(listener);
// observe device timezone
ObserveResponse observeResponse = helper.server.send(helper.getCurrentRegistration(), new ObserveRequest(3, 0, 15));
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/0/15", 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 a notification with unsupported content format *** //
byte[] payload = LwM2mNodeJsonEncoder.encode(LwM2mSingleResource.newStringResource(15, "Paris"), new LwM2mPath("/3/0/15"), new LwM2mModel(helper.createObjectModels()), new DefaultLwM2mValueConverter());
Response firstCoapResponse = (Response) observeResponse.getCoapResponse();
// 666 is not a supported //
sendNotification(getConnector(helper.client), payload, firstCoapResponse, 666);
// contentFormat.
// *** Hack End *** //
// verify result
listener.waitForNotification(2000);
assertTrue(listener.receivedNotify().get());
assertNotNull(listener.getError());
}
use of org.eclipse.leshan.core.model.LwM2mModel in project leshan by eclipse.
the class LwM2mNodeEncoderTest method loadModel.
@BeforeClass
public static void loadModel() {
model = new LwM2mModel(ObjectLoader.loadDefault());
encoder = new DefaultLwM2mNodeEncoder();
}
use of org.eclipse.leshan.core.model.LwM2mModel in project leshan by eclipse.
the class LwM2mNodeDecoderTest method loadModel.
@BeforeClass
public static void loadModel() {
// load default object
List<ObjectModel> objects = ObjectLoader.loadDefault();
// add object 65 from the LWM2M v1.0.1 specification (figure 28)
List<ResourceModel> resForObj65 = new ArrayList<>();
resForObj65.add(new ResourceModel(0, "res0", Operations.R, true, false, Type.OBJLNK, null, null, null));
resForObj65.add(new ResourceModel(1, "res1", Operations.R, false, false, Type.STRING, null, null, null));
resForObj65.add(new ResourceModel(2, "res2", Operations.R, false, false, Type.INTEGER, null, null, null));
objects.add(new ObjectModel(65, "object link tests 65", "", ObjectModel.DEFAULT_VERSION, true, false, resForObj65));
// add object 66 from the LWM2M v1.0.1 specification (figure 28)
List<ResourceModel> resForObj66 = new ArrayList<>();
resForObj66.add(new ResourceModel(0, "res0", Operations.R, true, false, Type.STRING, null, null, null));
resForObj66.add(new ResourceModel(1, "res1", Operations.R, false, false, Type.STRING, null, null, null));
resForObj66.add(new ResourceModel(2, "res2", Operations.R, false, false, Type.OBJLNK, null, null, null));
objects.add(new ObjectModel(66, "object link tests 66", "", ObjectModel.DEFAULT_VERSION, true, false, resForObj66));
model = new LwM2mModel(objects);
decoder = new DefaultLwM2mNodeDecoder();
}
use of org.eclipse.leshan.core.model.LwM2mModel in project leshan by eclipse.
the class CaliforniumLwM2mRequestSender method send.
@Override
public <T extends LwM2mResponse> void send(final Registration destination, final DownlinkRequest<T> request, long timeout, ResponseCallback<T> responseCallback, ErrorCallback errorCallback) {
// 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();
// Add CoAP request callback
MessageObserver obs = new AsyncRequestObserver<T>(coapRequest, responseCallback, errorCallback, 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(obs);
// Store pending request to cancel it on de-registration
addPendingRequest(destination.getId(), coapRequest);
// Send CoAP request asynchronously
Endpoint endpoint = getEndpointForClient(destination);
endpoint.sendRequest(coapRequest);
}
Aggregations