Search in sources :

Example 1 with GGGroup

use of software.amazon.awssdk.iot.discovery.model.GGGroup in project aws-iot-device-sdk-java-v2 by aws.

the class BasicDiscovery method getClientFromDiscovery.

private static MqttClientConnection getClientFromDiscovery(final DiscoveryClient discoveryClient) throws ExecutionException, InterruptedException {
    final CompletableFuture<DiscoverResponse> futureResponse = discoveryClient.discover(thingName);
    final DiscoverResponse response = futureResponse.get();
    if (response.getGGGroups() != null) {
        final Optional<GGGroup> groupOpt = response.getGGGroups().stream().findFirst();
        if (groupOpt.isPresent()) {
            final GGGroup group = groupOpt.get();
            final GGCore core = group.getCores().stream().findFirst().get();
            for (ConnectivityInfo connInfo : core.getConnectivity()) {
                final String dnsOrIp = connInfo.getHostAddress();
                final Integer port = connInfo.getPortNumber();
                System.out.println(String.format("Connecting to group ID %s, with thing arn %s, using endpoint %s:%d", group.getGGGroupId(), core.getThingArn(), dnsOrIp, port));
                final AwsIotMqttConnectionBuilder connectionBuilder = AwsIotMqttConnectionBuilder.newMtlsBuilderFromPath(certPath, keyPath).withClientId(thingName).withPort(port.shortValue()).withEndpoint(dnsOrIp).withConnectionEventCallbacks(new MqttClientConnectionEvents() {

                    @Override
                    public void onConnectionInterrupted(int errorCode) {
                        System.out.println("Connection interrupted: " + errorCode);
                    }

                    @Override
                    public void onConnectionResumed(boolean sessionPresent) {
                        System.out.println("Connection resumed!");
                    }
                });
                if (group.getCAs() != null) {
                    connectionBuilder.withCertificateAuthority(group.getCAs().get(0));
                }
                try (MqttClientConnection connection = connectionBuilder.build()) {
                    if (connection.connect().get()) {
                        System.out.println("Session resumed");
                    } else {
                        System.out.println("Started a clean session");
                    }
                    /* This lets the connection escape the try block without getting cleaned up */
                    connection.addRef();
                    return connection;
                } catch (Exception e) {
                    System.out.println(String.format("Connection failed with exception %s", e.toString()));
                }
            }
            throw new RuntimeException("ThingName " + thingName + " could not connect to the green grass core using any of the endpoint connectivity options");
        }
    }
    throw new RuntimeException("ThingName " + thingName + " does not have a Greengrass group/core configuration");
}
Also used : ConnectivityInfo(software.amazon.awssdk.iot.discovery.model.ConnectivityInfo) DiscoverResponse(software.amazon.awssdk.iot.discovery.model.DiscoverResponse) ExecutionException(java.util.concurrent.ExecutionException) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) GGCore(software.amazon.awssdk.iot.discovery.model.GGCore) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) AwsIotMqttConnectionBuilder(software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder) GGGroup(software.amazon.awssdk.iot.discovery.model.GGGroup)

Aggregations

ExecutionException (java.util.concurrent.ExecutionException)1 CrtRuntimeException (software.amazon.awssdk.crt.CrtRuntimeException)1 AwsIotMqttConnectionBuilder (software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder)1 ConnectivityInfo (software.amazon.awssdk.iot.discovery.model.ConnectivityInfo)1 DiscoverResponse (software.amazon.awssdk.iot.discovery.model.DiscoverResponse)1 GGCore (software.amazon.awssdk.iot.discovery.model.GGCore)1 GGGroup (software.amazon.awssdk.iot.discovery.model.GGGroup)1