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");
}
Aggregations