Example usage for io.vertx.core Future failedFuture

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

Introduction

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

Prototype

static <T> Future<T> failedFuture(String failureMessage) 

Source Link

Document

Create a failed future with the specified failure message.

Usage

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

License:Apache License

public void retrieveAdapterDevice(String id, Handler<AsyncResult<JsonObject>> resultHandler) {
    resultHandler.handle(Future.failedFuture(new UnsupportedOperationException("Not supported yet.")));
}

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

License:Apache License

public void updateAdapterDevice(String id, JsonObject device, Handler<AsyncResult<JsonObject>> resultHandler) {
    resultHandler.handle(Future.failedFuture(new UnsupportedOperationException("Not supported yet.")));
}

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

License:Apache License

public void deleteAdapterDevice(String id, Handler<AsyncResult<Void>> resultHandler) {
    resultHandler.handle(Future.failedFuture(new UnsupportedOperationException("Not supported yet.")));
}

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 w  w . ja  v  a  2  s .c om
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.
 *///from  w w  w.j  a  v  a 2s. 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  w  w.ja  va 2s.com
 */
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.device.DeviceManager.java

License:Apache License

/**
 * Update the value of an existing result using specified id and value.
 *
 * @param deviceId id of existing result.
 * @param value value to set./*  w w w .  j a v  a2 s .  c  o m*/
 * @param resultHandler
 */
public void updateDeviceValue(String deviceId, String value, Handler<AsyncResult<Void>> resultHandler) {
    JsonObject device = allDevices.get(deviceId);

    if (device == null) {
        resultHandler
                .handle(Future.failedFuture(String.format("No device with that id(%s) exists.", deviceId)));
    } else {
        String adapterId = device.getString("adapterId");
        DeliveryOptions options = new DeliveryOptions();
        options.addHeader("action", "updateDeviceValue");
        DeployedAdapter adapter = JelService.adapterManager().getAdapter(adapterId);

        if (adapter == null) {
            resultHandler.handle(
                    Future.failedFuture(String.format("Adapter with id %s does not exist.", adapterId)));
        } else {
            JsonObject data = new JsonObject().put("hwId", device.getString("hwId")).put("value", value);

            logger.info("Setting new value('{}') on device with id: {} and hwId: {}.", value,
                    device.getString("deviceId"), device.getString("hwId"));

            JelService.vertx().eventBus().send(String.format("%s.%s@%s:%d", AdapterEvents.EVENTBUS_ADAPTERS,
                    adapter.config().getType(), adapter.config().getAddress(), adapter.config().getPort()),
                    data, options);

            resultHandler.handle(Future.succeededFuture());
        }
    }
}

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

License:Apache License

@Override
public void systemInformation(Handler<AsyncResult<JsonObject>> resultHandler) {
    try {//w ww .j  a v a 2 s . c o  m
        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 {//from  w  w  w.j a  v  a  2 s.  c  o  m
        resultHandler.handle(Future.succeededFuture(SystemInfo.getSystemResources()));
    } 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 listInstalledPlugins(Handler<AsyncResult<JsonArray>> resultHandler) {
    try {//from   w  ww  .  j  a  va  2  s .co m
        List<PluginDesc> plugins = JelService.pluginManager().getInstalledPlugins();

        JsonArray list = new JsonArray();
        plugins.stream().forEach((plugin) -> {
            list.add(plugin.toApi());
        });

        resultHandler.handle(Future.succeededFuture(list));
    } catch (Exception ex) {
        resultHandler.handle(Future.failedFuture(ex.getMessage()));
    }
}