Example usage for io.vertx.core.eventbus EventBus publish

List of usage examples for io.vertx.core.eventbus EventBus publish

Introduction

In this page you can find the example usage for io.vertx.core.eventbus EventBus publish.

Prototype

@Fluent
EventBus publish(String address, @Nullable Object message, DeliveryOptions options);

Source Link

Document

Like #publish(String,Object) but specifying options that can be used to configure the delivery.

Usage

From source file:se.liquidbytes.jel.owfs.OwfsAdapter.java

License:Apache License

/**
 * Scan for available devices on 1-wire bus. And updates our internal list of available devices. Note this is a blocking call!
 *
 *///  w  ww  .  ja v a2 s .c  o  m
private void scanAvailableDevices() {
    try {
        String hwId, deviceType, deviceFamily, isPowered;
        Set<String> foundDeviceIds = new HashSet<>();
        EventBus eb = vertx.eventBus();
        JsonObject device, childDevice, broadcastDevice;

        Instant startExecutionTime = Instant.now();
        logger.debug("Scanning for available devices on Owserver at {}:{} with adapter id \"{}\".", this.host,
                this.port, this.getId());
        List<String> owDevices = owserverConnection.listDirectory(true);
        logger.debug("Found {} devices on Owserver at {}:{} with adapter id \"{}\".", owDevices.size(),
                this.host, this.port, this.getId());

        for (String owDevice : owDevices) {
            hwId = owserverConnection.read(owDevice + "/id");
            deviceType = owserverConnection.read(owDevice + "/type");
            deviceFamily = owserverConnection.read(owDevice + "/family");

            foundDeviceIds.add(hwId);

            device = deviceLookup.get(hwId);
            // Device not found in existing list, this must be a newly added device. Add it to the collection and broadcast its existence.
            if (device == null) {
                if (DeviceDatabase.getDeviceTypeInfo(deviceType) != null) {
                    JsonObject typeInfo = DeviceDatabase.getDeviceTypeInfo(deviceType);

                    // For devices that need to be setup in a special state to be usable, run their init commands when added to list of available devices.
                    if (typeInfo.containsKey("initCommands")) {
                        for (Iterator it = typeInfo.getJsonArray("initCommands").iterator(); it.hasNext();) {
                            JsonObject command = (JsonObject) it.next();
                            String path = owDevice + command.getString("path");
                            logger.debug(
                                    "Running initcommand (path '{}', value '{}') for device '{}' on Owserver at {}:{} with adapter id \"{}\".",
                                    path, command.getString("value"), hwId, this.host, this.port, this.getId());

                            owserverConnection.write(path, command.getString("value"));
                        }
                    }

                    try {
                        isPowered = owserverConnection.read(owDevice + "/power");
                        if (isPowered != null && isPowered.equals("0")) {
                            logger.warn(
                                    "Device '{}' of type '{}' on Owserver at {}:{} with adapter id \"{}\" is running on parasitic power, this will slow down the 1-wire network and is less reliable than a powered device.",
                                    hwId, deviceType, this.host, this.port, this.getId());
                        }
                    } catch (OwServerConnectionException ex) {
                        // Ignore. Devices that don't support the power-property will throw an error, so we just ignore this.
                    }

                    device = new JsonObject();
                    device.put("hwId", hwId);
                    device.put("type", deviceType);
                    device.put("name", typeInfo.getString("name"));
                    device.put("family", deviceFamily);
                    device.put("path", owDevice);
                    device.put("typeInfo", typeInfo);

                    deviceLookup.put(hwId, device);
                    logger.info(
                            "New device found during scan of Owserver at {}:{} with adapter id \"{}\". Device hwId: {}, type: {}, family: {}.",
                            this.host, this.port, this.getId(), hwId, deviceType, deviceFamily);

                    // For devices that supports it.
                    //setupAlarmHandler(device);
                    broadcastDevice = new JsonObject().put("adapterId", this.getId()).put("port", this.port)
                            .put("host", this.host).put("hwId", hwId).put("type", deviceType)
                            .put("name", typeInfo.getString("name"));
                    eb.publish(AdapterEvents.EVENTBUS_ADAPTERS, broadcastDevice,
                            new DeliveryOptions().addHeader("action", AdapterEvents.EVENT_DEVICES_ADDED));

                    // Check if this device is an container for other "child-devices". In that case, add all the children too, their Id will be <parent_childnumber>.
                    if (typeInfo.containsKey("childDevices")) {
                        for (Iterator it = typeInfo.getJsonArray("childDevices").iterator(); it.hasNext();) {
                            JsonObject childType = (JsonObject) it.next();
                            String childId = String.format("%s%s%s", hwId, CHILDSEPARATOR,
                                    childType.getString("idSuffix"));

                            childDevice = new JsonObject();
                            childDevice.put("hwId", childId);
                            childDevice.put("type", deviceType);
                            childDevice.put("name", String.format("%s-%s", typeInfo.getString("name"),
                                    childType.getString("name")));

                            deviceLookup.put(childId, childDevice);
                            logger.info(
                                    "New childdevice for device {} found during scan of Owserver at {}:{} with adapter id \"{}\". Device hwId: {}, type: {}, family: {}.",
                                    hwId, this.host, this.port, this.getId(), childId, deviceType,
                                    deviceFamily);

                            broadcastDevice = new JsonObject().put("adapterId", this.getId())
                                    .put("port", this.port).put("host", this.host).put("hwId", childId)
                                    .put("type", deviceType).put("name", childDevice.getString("name"));
                            eb.publish(AdapterEvents.EVENTBUS_ADAPTERS, broadcastDevice, new DeliveryOptions()
                                    .addHeader("action", AdapterEvents.EVENT_DEVICES_ADDED));
                        }
                    }
                } else {
                    logger.info(
                            "Found unsupported devicetype for device with hwId '{}' on Owserver at {}:{} with adapter id \"{}\". Device will be ignored! Please notify developers and provide: type={}, family={}.",
                            hwId, this.host, this.port, this.getId(), deviceType, deviceFamily);
                }
            }
        }

        // Remove all devices in device list that was no longer found during this scan. They has been disconnected from the 1-wire bus.
        Set<String> tempSet = new HashSet(deviceLookup.keySet());
        tempSet.removeAll(foundDeviceIds);

        for (String removeId : tempSet) {
            // Check that it's not a childdevice, we are not interested in them right now.
            if (!removeId.contains(CHILDSEPARATOR)) {
                // If device has children, remove these first.
                List<String> childDevicesId = tempSet.stream()
                        .filter(d -> d.startsWith(removeId + CHILDSEPARATOR)).collect(Collectors.toList());
                for (String childDeviceId : childDevicesId) {
                    removeDeviceFromLookup(childDeviceId, eb);
                }
                // Then remove device.
                removeDeviceFromLookup(removeId, eb);
            }
        }

        logger.debug("Scanning bus for devices took {}ms on Owserver at {}:{} with adapter id \"{}\".",
                Duration.between(startExecutionTime, Instant.now()).toMillis(), this.host, this.port,
                this.getId());
    } catch (OwServerConnectionException ex) {
        logger.error(
                "Error while trying to scan Owserver at {}:{} with adapter id \"{}\" for available devices.",
                this.host, this.port, this.getId(), ex);
    }
}

From source file:se.liquidbytes.jel.owfs.OwfsAdapter.java

License:Apache License

/**
 * Remove a device from deviceLookup. Also signal it's departure on the bus
 *
 * @param removeId remmoved device hwId//  ww  w . j a v a  2s . c o m
 * @param eb eventbus instance
 */
private void removeDeviceFromLookup(String removeId, EventBus eb) {
    deviceLookup.remove(removeId);
    logger.info(
            "Device with hwId: {}, not found after last scan of Owserver at {}:{} with adapter id \"{}\". Device has been removed from bus.",
            removeId, this.host, this.port, this.getId());

    JsonObject broadcastDevice = new JsonObject().put("adapterId", this.getId()).put("port", this.port)
            .put("host", this.host).put("hwId", removeId);

    eb.publish(AdapterEvents.EVENTBUS_ADAPTERS, broadcastDevice,
            new DeliveryOptions().addHeader("action", AdapterEvents.EVENT_DEVICES_REMOVED));
}