use of org.eclipse.californium.core.CoapHandler in project thingsboard by thingsboard.
the class DeviceEmulator method start.
public void start() {
executor.submit(new Runnable() {
@Override
public void run() {
try {
sendObserveRequest(rpcClient);
while (!Thread.interrupted()) {
sendRequest(attributesClient, createAttributesRequest());
sendRequest(telemetryClient, createTelemetryRequest());
Thread.sleep(1000);
}
} catch (Exception e) {
log.error("Error occurred while sending COAP requests", e);
}
}
private void sendRequest(CoapClient client, JsonNode request) throws JsonProcessingException {
CoapResponse telemetryResponse = client.setTimeout(60000).post(mapper.writeValueAsString(request), MediaTypeRegistry.APPLICATION_JSON);
log.info("Response: {}, {}", telemetryResponse.getCode(), telemetryResponse.getResponseText());
}
private void sendObserveRequest(CoapClient client) throws JsonProcessingException {
client.observe(new CoapHandler() {
@Override
public void onLoad(CoapResponse coapResponse) {
log.info("Command: {}, {}", coapResponse.getCode(), coapResponse.getResponseText());
try {
JsonNode node = mapper.readTree(coapResponse.getResponseText());
int requestId = node.get("id").asInt();
String method = node.get("method").asText();
ObjectNode params = (ObjectNode) node.get("params");
ObjectNode response = mapper.createObjectNode();
response.put("id", requestId);
response.set("response", params);
log.info("Command Response: {}, {}", requestId, mapper.writeValueAsString(response));
CoapClient commandResponseClient = new CoapClient(getFeatureTokenUrl(host, port, token, FeatureType.RPC));
commandResponseClient.post(new CoapHandler() {
@Override
public void onLoad(CoapResponse response) {
log.info("Command Response Ack: {}, {}", response.getCode(), response.getResponseText());
}
@Override
public void onError() {
// Do nothing
}
}, mapper.writeValueAsString(response), MediaTypeRegistry.APPLICATION_JSON);
} catch (IOException e) {
log.error("Error occurred while processing COAP response", e);
}
}
@Override
public void onError() {
// Do nothing
}
});
}
});
}
use of org.eclipse.californium.core.CoapHandler in project thingsboard by thingsboard.
the class NoSecObserveClient method start.
public void start() {
executor.submit(() -> {
try {
Request request = Request.newGet();
request.setObserve();
observeRelation = coapClient.observe(request, new CoapHandler() {
@Override
public void onLoad(CoapResponse response) {
String responseText = response.getResponseText();
CoAP.ResponseCode code = response.getCode();
Integer observe = response.getOptions().getObserve();
log.info("CoAP Response received! " + "responseText: {}, " + "code: {}, " + "observe seq number: {}", responseText, code, observe);
latch.countDown();
}
@Override
public void onError() {
log.error("Ack error!");
latch.countDown();
}
});
} catch (Exception e) {
log.error("Error occurred while sending COAP requests: ");
}
});
try {
latch.await();
observeRelation.proactiveCancel();
} catch (InterruptedException e) {
log.error("Error occurred: ", e);
}
}
use of org.eclipse.californium.core.CoapHandler in project thingsboard by thingsboard.
the class AbstractCoapServerSideRpcIntegrationTest method processOnLoadResponse.
protected void processOnLoadResponse(CoapResponse response, CoapClient client, Integer observe, CountDownLatch latch) {
JsonNode responseJson = JacksonUtil.fromBytes(response.getPayload());
client.setURI(getRpcResponseFeatureTokenUrl(accessToken, responseJson.get("id").asInt()));
client.post(new CoapHandler() {
@Override
public void onLoad(CoapResponse response) {
log.warn("Command Response Ack: {}, {}", response.getCode(), response.getResponseText());
latch.countDown();
}
@Override
public void onError() {
log.warn("Command Response Ack Error, No connect");
}
}, DEVICE_RESPONSE, MediaTypeRegistry.APPLICATION_JSON);
}
use of org.eclipse.californium.core.CoapHandler in project hono by eclipse.
the class CoapTestBase method warmUp.
/**
* Triggers the establishment of a downstream sender
* for a tenant so that subsequent messages will be
* more likely to be forwarded.
*
* @param client The CoAP client to use for sending the request.
* @param request The request to send.
* @return A succeeded future.
*/
protected final Future<Void> warmUp(final CoapClient client, final Request request) {
logger.debug("sending request to trigger CoAP adapter's downstream message sender");
final Promise<Void> result = Promise.promise();
client.advanced(new CoapHandler() {
@Override
public void onLoad(final CoapResponse response) {
waitForWarmUp();
}
@Override
public void onError() {
waitForWarmUp();
}
private void waitForWarmUp() {
vertx.setTimer(1000, tid -> result.complete());
}
}, request);
return result.future();
}
use of org.eclipse.californium.core.CoapHandler in project thingsboard by thingsboard.
the class AbstractCoapServerSideRpcProtoIntegrationTest method processOnLoadResponse.
@Override
protected void processOnLoadResponse(CoapResponse response, CoapClient client, Integer observe, CountDownLatch latch) {
ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = getProtoTransportPayloadConfiguration();
ProtoFileElement rpcRequestProtoSchemaFile = protoTransportPayloadConfiguration.getTransportProtoSchema(RPC_REQUEST_PROTO_SCHEMA);
DynamicSchema rpcRequestProtoSchema = protoTransportPayloadConfiguration.getDynamicSchema(rpcRequestProtoSchemaFile, ProtoTransportPayloadConfiguration.RPC_REQUEST_PROTO_SCHEMA);
byte[] requestPayload = response.getPayload();
DynamicMessage.Builder rpcRequestMsg = rpcRequestProtoSchema.newMessageBuilder("RpcRequestMsg");
Descriptors.Descriptor rpcRequestMsgDescriptor = rpcRequestMsg.getDescriptorForType();
try {
DynamicMessage dynamicMessage = DynamicMessage.parseFrom(rpcRequestMsgDescriptor, requestPayload);
Descriptors.FieldDescriptor requestIdDescriptor = rpcRequestMsgDescriptor.findFieldByName("requestId");
int requestId = (int) dynamicMessage.getField(requestIdDescriptor);
ProtoFileElement rpcResponseProtoSchemaFile = protoTransportPayloadConfiguration.getTransportProtoSchema(DEVICE_RPC_RESPONSE_PROTO_SCHEMA);
DynamicSchema rpcResponseProtoSchema = protoTransportPayloadConfiguration.getDynamicSchema(rpcResponseProtoSchemaFile, ProtoTransportPayloadConfiguration.RPC_RESPONSE_PROTO_SCHEMA);
DynamicMessage.Builder rpcResponseBuilder = rpcResponseProtoSchema.newMessageBuilder("RpcResponseMsg");
Descriptors.Descriptor rpcResponseMsgDescriptor = rpcResponseBuilder.getDescriptorForType();
DynamicMessage rpcResponseMsg = rpcResponseBuilder.setField(rpcResponseMsgDescriptor.findFieldByName("payload"), DEVICE_RESPONSE).build();
client.setURI(getRpcResponseFeatureTokenUrl(accessToken, requestId));
client.post(new CoapHandler() {
@Override
public void onLoad(CoapResponse response) {
log.warn("Command Response Ack: {}", response.getCode());
latch.countDown();
}
@Override
public void onError() {
log.warn("Command Response Ack Error, No connect");
}
}, rpcResponseMsg.toByteArray(), MediaTypeRegistry.APPLICATION_JSON);
} catch (InvalidProtocolBufferException e) {
log.warn("Command Response Ack Error, Invalid response received: ", e);
}
}
Aggregations