private Void closeCommandClient(final CommandClient commandClient) { LOG.trace("Close command client to device [{}:{}]", tenantId, deviceId); commandClient.close(closeHandler -> { }); return null; }
private CommandClient setCommandTimeOut(final CommandClient commandClient) { commandClient.setRequestTimeout(commandTimeoutInMs); return commandClient; }
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; } }); }
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); })); }
commandClient.sendOneWayCommand(command, commandBuffer).map(statusResult -> { if (LOG.isDebugEnabled()) { LOG.debug("Successfully sent one-way command payload: [{}] and received status [{}].", commandBuffer.toString(), statusResult);
commandClient.sendCommand(command, "application/json", commandBuffer, buildCommandProperties()).map(result -> { if (LOG.isDebugEnabled()) { LOG.debug("Successfully sent command payload: [{}].", commandBuffer.toString());
private Void closeCommandClient(final CommandClient commandClient, final String tenantId, final String deviceId) { commandClient .close(closeHandler -> LOG.debug("CommandClient to device [{}:{}] is closed.", tenantId, deviceId)); return null; }
private CommandClient setRequestTimeOut(final CommandClient commandClient) { commandClient.setRequestTimeout(TimeUnit.SECONDS.toMillis(requestTimeoutInSecs)); return commandClient; }
private void closeCommandClient(final CommandClient commandClient, final TimeUntilDisconnectNotification ttdNotification) { // do not close the command client if device stays connected commandClient.close(v -> { if (LOG.isDebugEnabled()) { LOG.trace("Closed commandClient for [{}].", ttdNotification.getTenantAndDeviceId()); } }); }
/** * 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; }); }