package org.iotbricks.hono.device.registry.client;

import static java.lang.Integer.parseInt;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import org.iotbricks.hono.device.registry.client.model.DeviceInformation;

import io.glutamate.util.concurrent.CloseableCompletionStage;
import io.vertx.core.Vertx;
import io.vertx.core.net.PemKeyCertOptions;

public class Main {

    public static void main(final String[] args) throws Exception {

        final String id = "my-id-1";

        final Map<String, Object> data = new HashMap<>();
        data.put("foo", "bar");
        data.put("answer", 42);

        final Vertx vertx = Vertx.vertx();

        final Path certsPath = Paths.get("/home/jreimann/git/hono/example/target/config/hono-demo-certs-jar");

        final PemKeyCertOptions keyCert = new PemKeyCertOptions();
        keyCert.addCertPath(certsPath.resolve("device-registry-cert.pem").toString());
        keyCert.addKeyPath(certsPath.resolve("device-registry-key.pem").toString());

        try (Client client = AmqpClient.newClient()
                .tenant("foo-bar-baz")
                .transport(transport -> {
                    transport
                            .hostname(args[0])
                            .port(parseInt(args[1]))
                            .port(31671)
                            .username("consumer@HONO")
                            .password("verysecret")
                            .protonClientOptions(options -> {
                                options
                                        .addEnabledSaslMechanism("PLAIN")
                                        .setSsl(true)
                                        .setKeyCertOptions(keyCert)
                                        .setSniServerName(args[1])
                                        .setHostnameVerificationAlgorithm("")
                                        .setTrustAll(true);
                            });
                })
                .build(vertx)) {

            try (CloseableCompletionStage<DeviceInformation> request = client.registerDevice(id, data)) {
                final DeviceInformation result = request.toCompletableFuture().get();
                System.out.println(result);
            }
        }
    }
}

@@ -41,24 +41,65 @@
 ry.client;%0A%0A
+import static io.vertx.core.Vertx.vertx;%0A
 import stati
@@ -308,16 +308,51 @@
 ation;%0A%0A
+import io.glutamate.lang.Resource;%0A
 import i
@@ -741,19 +741,34 @@
     
+try (
 final 
+Resource%3C
 Vertx
+%3E
  ver
@@ -776,24 +776,55 @@
 x = 
-Vertx.vertx();%0A%0A
+Resource.manage(vertx(), Vertx::close)) %7B%0A%0A    
     
@@ -935,24 +935,28 @@
 );%0A%0A        
+    
 final PemKey
@@ -998,24 +998,28 @@
 tOptions();%0A
+    
         keyC
@@ -1093,32 +1093,36 @@
 ing());%0A        
+    
 keyCert.addKeyPa
@@ -1191,21 +1191,31 @@
         
+    
 try (
+final 
 Client c
@@ -1261,16 +1261,20 @@
         
+    
 .tenant(
@@ -1304,16 +1304,20 @@
         
+    
 .transpo
@@ -1330,24 +1330,28 @@
 nsport -%3E %7B%0A
+    
             
@@ -1396,16 +1396,20 @@
         
+    
 .hostnam
@@ -1443,24 +1443,28 @@
             
+    
 .port(parseI
@@ -1500,24 +1500,28 @@
             
+    
 .port(31671)
@@ -1549,16 +1549,20 @@
         
+    
 .usernam
@@ -1568,24 +1568,27 @@
 me(%22
-consumer
+hono-client
 @HONO%22)%0A
     
@@ -1583,16 +1583,20 @@
 @HONO%22)%0A
+    
         
@@ -1630,12 +1630,8 @@
 rd(%22
-very
 secr
@@ -1635,16 +1635,78 @@
 ecret%22)%0A
+                                .container(%22test-client%22)%0A    
         
@@ -1791,16 +1791,20 @@
         
+    
 options%0A
@@ -1847,40 +1847,24 @@
     
-.addEnabledSaslMechanism(%22PLAIN%22
+    .setSsl(true
 )%0A  
@@ -1897,38 +1897,66 @@
             
-.setSsl(true)%0A
+    .addEnabledSaslMechanism(%22PLAIN%22)%0A    
             
@@ -2047,24 +2047,28 @@
             
+    
 .setSniServe
@@ -2082,16 +2082,20 @@
 rgs%5B1%5D)%0A
+    
         
@@ -2200,24 +2200,28 @@
             
+    
 .setTrustAll
@@ -2260,12 +2260,20 @@
     
-%7D);%0A
+    %7D);%0A    
     
@@ -2303,16 +2303,20 @@
         
+    
 .build(v
@@ -2319,22 +2319,32 @@
 ld(vertx
+.get()
 )) %7B%0A%0A
+    
         
@@ -2453,24 +2453,28 @@
             
+    
 final Device
@@ -2547,16 +2547,20 @@
         
+    
 System.o
@@ -2579,16 +2579,34 @@
 esult);%0A
+                %7D%0A
         

Manage vertx resource, use correct credentials