private void sendCommandAndReceiveResponse(final String tenantId, final String deviceId) {
client.getOrCreateCommandClient(tenantId, deviceId)
.map(this::setCommandTimeOut)
.compose(commandClient -> commandClient
.sendCommand(sampler.getCommand(), Buffer.buffer(sampler.getCommandPayload()))
.map(commandResponse -> {
final String commandResponseText = Optional.ofNullable(commandResponse.getPayload())
.orElse(Buffer.buffer()).toString();
LOG.debug("Command response from device [{}:{}] received [{}]", tenantId,
deviceId, commandResponseText);
synchronized (lock) {
successCount.incrementAndGet();
bytesReceived.addAndGet(sampler.getCommandPayload().getBytes().length);
bytesSent.addAndGet(commandResponseText.getBytes().length);
}
return commandResponse;
})
.map(x -> closeCommandClient(commandClient, tenantId, deviceId))
.recover(error -> {
if (triggerType.equals("device")
|| devicesReadyToReceiveCommands.contains(deviceId)) {
errorCount.incrementAndGet();
}
LOG.error("Error processing command [{}] to device [{}:{}]", error.getMessage(), tenantId,
deviceId);
closeCommandClient(commandClient, tenantId, deviceId);
return Future.failedFuture(error);
}));
}