This is an automated email from the ASF dual-hosted git repository. sruehl pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git
The following commit(s) were added to refs/heads/master by this push: new dd293ba cleanup fo azure example dd293ba is described below commit dd293bab023aa62f94096f0f2f4ae3446a87d9db Author: Sebastian Rühl <sru...@apache.org> AuthorDate: Thu Aug 23 16:21:26 2018 +0200 cleanup fo azure example --- .../azure/iothub/S7PlcToAzureIoTHubSample.java | 45 ++++++++++------------ 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/examples/azure/src/main/java/org/apache/plc4x/java/examples/azure/iothub/S7PlcToAzureIoTHubSample.java b/examples/azure/src/main/java/org/apache/plc4x/java/examples/azure/iothub/S7PlcToAzureIoTHubSample.java index ec046bc..e2129f4 100644 --- a/examples/azure/src/main/java/org/apache/plc4x/java/examples/azure/iothub/S7PlcToAzureIoTHubSample.java +++ b/examples/azure/src/main/java/org/apache/plc4x/java/examples/azure/iothub/S7PlcToAzureIoTHubSample.java @@ -28,20 +28,22 @@ import org.apache.plc4x.java.api.model.Address; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Optional; +import java.util.concurrent.TimeUnit; public class S7PlcToAzureIoTHubSample { - private static final Logger logger = LoggerFactory.getLogger(S7PlcToAzureIoTHubSample.class); + private static final Logger LOGGER = LoggerFactory.getLogger(S7PlcToAzureIoTHubSample.class); private static IotHubClientProtocol protocol = IotHubClientProtocol.MQTT; private static DeviceClient client; public static class Callback implements IotHubEventCallback { + private static final Logger LOGGER = LoggerFactory.getLogger(Callback.class); + @Override public void execute(IotHubStatusCode iotHubStatusCode, Object ctx) { - System.out.println("Received status: " + iotHubStatusCode.name()); + LOGGER.info("Received status: ", iotHubStatusCode); } } @@ -50,46 +52,39 @@ public class S7PlcToAzureIoTHubSample { * * @param args Expected: [plc4x connection string, plc4x address, IoT-Hub connection string]. */ - public static void main(String[] args) { - logger.info("Connecting"); + public static void main(String[] args) throws Exception { + LOGGER.info("Connecting"); try (PlcConnection plcConnection = new PlcDriverManager().getConnection(args[0])) { - logger.info("Connected"); + LOGGER.info("Connected"); client = new DeviceClient(args[2], protocol); client.open(); - Optional<PlcReader> reader = plcConnection.getReader(); - - if (reader.isPresent()) { - PlcReader plcReader = reader.get(); - Address outputs = plcConnection.parseAddress(args[1]); + PlcReader plcReader = plcConnection.getReader().orElseThrow(IllegalStateException::new); - while (true) { - // Simulate telemetry. - TypeSafePlcReadResponse<Byte> plcReadResponse = plcReader.read( - new TypeSafePlcReadRequest<>(Byte.class, outputs)).get(); + Address outputs = plcConnection.parseAddress(args[1]); - System.out.println("Outputs: " + Long.toBinaryString(plcReadResponse.getResponseItem() - .orElseThrow(() -> new IllegalStateException("No response available")) - .getValues().get(0).longValue())); + while (!Thread.currentThread().isInterrupted()) { + // Simulate telemetry. + TypeSafePlcReadResponse<Byte> plcReadResponse = plcReader.read( + new TypeSafePlcReadRequest<>(Byte.class, outputs)).get(); - plcReadResponse.getResponseItem().map(byt -> { - String result = Long.toBinaryString(byt.getValues().get(0).longValue()); + plcReadResponse.getResponseItems().stream() + .flatMap(readResponseItem -> readResponseItem.getValues().stream()) + .forEach(byteValue -> { + String result = Long.toBinaryString(byteValue.longValue()); + LOGGER.info("Outputs {}", result); Message msg = new Message("{ \"bits\" : \"" + result + "\"}"); // Send the message. Callback callback = new Callback(); client.sendEventAsync(msg, callback, new Object()); - return byt; } ); - Thread.sleep(1000); - } + TimeUnit.SECONDS.sleep(1); } - } catch (Exception e) { - e.printStackTrace(); } } }