Example usage for io.vertx.core.json JsonObject getJsonArray

List of usage examples for io.vertx.core.json JsonObject getJsonArray

Introduction

In this page you can find the example usage for io.vertx.core.json JsonObject getJsonArray.

Prototype

public JsonArray getJsonArray(String key) 

Source Link

Document

Get the JsonArray value with the specified key

Usage

From source file:se.liquidbytes.jel.system.device.DeviceManager.java

License:Apache License

/**
 * Returns a list of all available devices for all adapters.
 *
 * @param resultHandler Promise will give the list of devices or a error if one has occured.
 *///from   w w  w  . j a  v  a  2  s  . co m
public void listAllDevices(Handler<AsyncResult<JsonArray>> resultHandler) {
    DeliveryOptions options = new DeliveryOptions();
    options.addHeader("action", "listDevices");
    List<DeployedAdapter> adapters = JelService.adapterManager().getAdapters();

    if (adapters.isEmpty()) {
        resultHandler.handle(Future.succeededFuture(new JsonArray()));
    } else {
        Promise promise = JelService.promiseFactory().create();
        adapters.stream().forEach((_item) -> {
            // Send message to all adapters to report back their devices.
            promise.then((context, onResult) -> {
                JelService.vertx().eventBus().send(String.format("%s.%s@%s:%d", AdapterEvents.EVENTBUS_ADAPTERS,
                        _item.config().getType(), _item.config().getAddress(), _item.config().getPort()), null,
                        options, res -> {
                            if (res.succeeded()) {
                                // All adapters fills in turn a json-array named "adapterDevices".
                                JsonArray adapterDevices = context.getJsonArray("adapterDevices");

                                // If we are the first adapter to report back, create the array.
                                if (adapterDevices == null) {
                                    adapterDevices = new JsonArray();
                                }

                                JsonObject result = (JsonObject) res.result().body();

                                adapterDevices.add(new JsonObject()
                                        .put("adapterId", result.getString("adapterId"))
                                        .put("devices", new JsonArray().addAll(result.getJsonArray("result"))));

                                context.put("adapterDevices", adapterDevices);
                                onResult.accept(true);
                            } else {
                                context.put("errorMessage", res.cause().toString());
                                onResult.accept(false);
                            }
                        });
            });
        });

        promise.done((onSuccess) -> {
            // When we are done, all adapters devices should be here.
            JsonArray adapterDevices = onSuccess.getJsonArray("adapterDevices");
            JsonArray devices = new JsonArray();

            adapterDevices.forEach((adl) -> {

                JsonObject adapterDeviceList = (JsonObject) adl;
                String adapterId = adapterDeviceList.getString("adapterId");

                adapterDeviceList.getJsonArray("devices").forEach((ad) -> {
                    JsonObject tmpDevice = (JsonObject) ad;

                    devices.add(new JsonObject().put("adapterId", adapterId)
                            .put("deviceId", generateDeviceId(adapterId, tmpDevice.getString("hwId")))
                            .put("type", tmpDevice.getString("type")).put("name", tmpDevice.getString("name")));

                    // Sync found devices with allDevices-collection in case we are out of sync and have missed an event!
                    addToDeviceCollections(new JsonObject().put("adapterId", adapterId)
                            .put("hwId", tmpDevice.getString("hwId")).put("type", tmpDevice.getString("type"))
                            .put("name", tmpDevice.getString("name")));
                });
            });

            resultHandler.handle(Future.succeededFuture(devices));
        }).except((onError) -> {
            resultHandler.handle(Future.failedFuture(onError.getString("errorMessage")));
        }).eval();
    }
}