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); })); }
private Future<Void> processCommand(final Command command) { LOG.info("Command sent to device... [request will timeout in {} seconds]", requestTimeoutInSecs); final Future<CommandClient> commandClient = client.getOrCreateCommandClient(tenantId, deviceId); return commandClient .map(this::setRequestTimeOut) .compose(c -> { if (command.isOneWay()) { return c .sendOneWayCommand(command.getName(), command.getContentType(), Buffer.buffer(command.getPayload()), null) .map(ok -> c); } else { return c .sendCommand(command.getName(), command.getContentType(), Buffer.buffer(command.getPayload()), null) .map(this::printResponse) .map(ok -> c); } }) .map(this::closeCommandClient) .otherwise(error -> { LOG.error("Error sending command: {}", error.getMessage()); if (commandClient.succeeded()) { return closeCommandClient(commandClient.result()); } else { return null; } }); }
/** * Create a command client for the device for that a {@link TimeUntilDisconnectNotification} was received, if no such * command client is already active. * @param notification The notification that was received for the device. */ private Future<CommandClient> createCommandClientAndSendCommand(final TimeUntilDisconnectNotification notification) { return honoClient.getOrCreateCommandClient(notification.getTenantId(), notification.getDeviceId()) .map(commandClient -> { commandClient.setRequestTimeout(calculateCommandTimeout(notification)); // send the command upstream to the device if (SEND_ONE_WAY_COMMANDS) { sendOneWayCommandToAdapter(commandClient, notification); } else { sendCommandToAdapter(commandClient, notification); } return commandClient; }).otherwise(t -> { LOG.error("Could not create command client", t); return null; }); }