Example usage for io.vertx.core Future succeededFuture

List of usage examples for io.vertx.core Future succeededFuture

Introduction

In this page you can find the example usage for io.vertx.core Future succeededFuture.

Prototype

static <T> Future<T> succeededFuture(T result) 

Source Link

Document

Created a succeeded future with the specified result.

Usage

From source file:se.liquidbytes.jel.database.DatabaseConnectionVertxEBProxy.java

License:Apache License

public DatabaseConnection addUser(JsonObject document, Handler<AsyncResult<JsonObject>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }/*from  w w w.  j  a v a  2 s. c o  m*/
    JsonObject _json = new JsonObject();
    _json.put("document", document);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "addUser");
    _vertx.eventBus().<JsonObject>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            resultHandler.handle(Future.failedFuture(res.cause()));
        } else {
            resultHandler.handle(Future.succeededFuture(res.result().body()));
        }
    });
    return this;
}

From source file:se.liquidbytes.jel.database.DatabaseConnectionVertxEBProxy.java

License:Apache License

public DatabaseConnection updateUser(JsonObject document, Handler<AsyncResult<JsonObject>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }/*www.j  a v a2  s  .c om*/
    JsonObject _json = new JsonObject();
    _json.put("document", document);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "updateUser");
    _vertx.eventBus().<JsonObject>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            resultHandler.handle(Future.failedFuture(res.cause()));
        } else {
            resultHandler.handle(Future.succeededFuture(res.result().body()));
        }
    });
    return this;
}

From source file:se.liquidbytes.jel.database.DatabaseConnectionVertxEBProxy.java

License:Apache License

public DatabaseConnection removeUser(String id, Handler<AsyncResult<Void>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }//from  w  ww.j  a  v  a 2  s  . com
    JsonObject _json = new JsonObject();
    _json.put("id", id);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "removeUser");
    _vertx.eventBus().<Void>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            resultHandler.handle(Future.failedFuture(res.cause()));
        } else {
            resultHandler.handle(Future.succeededFuture(res.result().body()));
        }
    });
    return this;
}

From source file:se.liquidbytes.jel.database.DatabaseServiceVertxEBProxy.java

License:Apache License

public void getConnection(Handler<AsyncResult<DatabaseConnection>> handler) {
    if (closed) {
        handler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;/*from w ww . j  av a2  s  .  c  om*/
    }
    JsonObject _json = new JsonObject();
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "getConnection");
    _vertx.eventBus().<DatabaseConnection>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            handler.handle(Future.failedFuture(res.cause()));
        } else {
            String addr = res.result().headers().get("proxyaddr");
            handler.handle(
                    Future.succeededFuture(ProxyHelper.createProxy(DatabaseConnection.class, _vertx, addr)));
        }
    });
}

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

License:Apache License

public void listAdapterDevices(String adapterId, Handler<AsyncResult<JsonArray>> resultHandler) {
    DeliveryOptions options = new DeliveryOptions();
    options.addHeader("action", "listDevices");
    DeployedAdapter adapter = JelService.adapterManager().getAdapter(adapterId);

    if (adapter == null) {
        resultHandler/*from  w w w.  java2 s  .  c o  m*/
                .handle(Future.failedFuture(String.format("Adapter with id %s does not exist.", adapterId)));
    } else {
        // Send message to adapter to report back its devices.
        JelService.vertx().eventBus().send(String.format("%s.%s@%s:%d", AdapterEvents.EVENTBUS_ADAPTERS,
                adapter.config().getType(), adapter.config().getAddress(), adapter.config().getPort()), null,
                options, res -> {
                    if (res.succeeded()) {
                        JsonArray devices = new JsonArray();

                        JsonObject result = (JsonObject) res.result().body();
                        result.getJsonArray("result").forEach((d) -> {
                            JsonObject tmpDevice = (JsonObject) d;

                            // 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("name", tmpDevice.getString("name"))
                                    .put("type", tmpDevice.getString("type"))
                                    .put("hwId", tmpDevice.getString("hwId")));

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

                        resultHandler.handle(Future.succeededFuture(devices));
                    } else {
                        resultHandler.handle(Future.failedFuture(res.cause()));
                    }
                });
    }
}

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

License:Apache License

/**
 * Returns a list of all supported devices for a specific adapter.
 *
 * @param adapterId id for adapter to query.
 * @param resultHandler Promise will give the list of supported devices.
 *//* w  ww.  j  ava 2 s .com*/
public void listSupportedAdapterDevices(String adapterId, Handler<AsyncResult<JsonArray>> resultHandler) {
    DeliveryOptions options = new DeliveryOptions();
    options.addHeader("action", "listSupportedDevices");
    DeployedAdapter adapter = JelService.adapterManager().getAdapter(adapterId);

    if (adapter == null) {
        resultHandler
                .handle(Future.failedFuture(String.format("Adapter with id %s does not exist.", adapterId)));
    } else {
        JelService.vertx().eventBus().send(String.format("%s.%s@%s:%d", AdapterEvents.EVENTBUS_ADAPTERS,
                adapter.config().getType(), adapter.config().getAddress(), adapter.config().getPort()), null,
                options, res -> {
                    if (res.succeeded()) {
                        JsonObject result = (JsonObject) res.result().body();
                        resultHandler.handle(Future.succeededFuture(result.getJsonArray("result")));
                    } else {
                        resultHandler.handle(Future.failedFuture(res.cause()));
                    }
                });
    }
}

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.
 *//* w w  w.j  a  va2 s .  c  o 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();
    }
}

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

License:Apache License

/**
 * Retrieve the current value of an result using specified result id.
 *
 * @param deviceId id of existing result.
 * @param resultHandler/*from  w ww  . jav  a2s. c o m*/
 */
public void retrieveDeviceValue(String deviceId, Handler<AsyncResult<JsonObject>> resultHandler) {
    DeliveryOptions options = new DeliveryOptions();
    options.addHeader("action", "retrieveDeviceValue");

    JsonObject device = allDevices.get(deviceId);
    if (device == null) {
        resultHandler.handle(Future.failedFuture(String.format("Device with id %s does not exist.", deviceId)));
        return;
    }

    DeployedAdapter adapter = JelService.adapterManager().getAdapter(device.getString("adapterId"));

    if (adapter == null) {
        resultHandler.handle(Future.failedFuture(
                String.format("Adapter with id %s does not exist.", device.getString("adapterId"))));
        return;
    }

    // Send message to adapter to report back its devices.
    JelService.vertx().eventBus()
            .send(String.format("%s.%s@%s:%d", AdapterEvents.EVENTBUS_ADAPTERS, adapter.config().getType(),
                    adapter.config().getAddress(), adapter.config().getPort()),
                    new JsonObject().put("hwId", device.getString("hwId")), options, res -> {
                        if (res.succeeded()) {
                            JsonObject reading = ((JsonObject) res.result().body()).getJsonObject("result")
                                    .getJsonObject("reading");

                            String time = null, value = null;

                            if (reading != null) {
                                time = reading.getJsonObject("lastReading").getString("time");
                                value = reading.getJsonObject("lastReading").getString("value");
                            }

                            JsonObject result = new JsonObject().put("deviceId", device.getString("deviceId"))
                                    .put("time", time).put("value", value);

                            resultHandler.handle(Future.succeededFuture(result));
                        } else {
                            resultHandler.handle(Future.failedFuture(res.cause()));
                        }
                    });
}

From source file:se.liquidbytes.jel.system.impl.JelServiceImpl.java

License:Apache License

@Override
public void systemInformation(Handler<AsyncResult<JsonObject>> resultHandler) {
    try {//from   w  ww .  jav  a 2  s. com
        resultHandler.handle(Future.succeededFuture(SystemInfo.getSystemInformation()));
    } catch (Exception ex) {
        resultHandler.handle(Future.failedFuture(ex.getMessage()));
    }
}

From source file:se.liquidbytes.jel.system.impl.JelServiceImpl.java

License:Apache License

@Override
public void systemResources(Handler<AsyncResult<JsonObject>> resultHandler) {
    try {/* w  ww  .j a  v  a2s  .c  om*/
        resultHandler.handle(Future.succeededFuture(SystemInfo.getSystemResources()));
    } catch (Exception ex) {
        resultHandler.handle(Future.failedFuture(ex.getMessage()));
    }
}