Search in sources :

Example 1 with RemoteopenhabItem

use of org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabItem in project openhab-addons by openhab.

the class RemoteopenhabBridgeHandler method removeChannels.

private void removeChannels(List<RemoteopenhabItem> items) {
    synchronized (updateThingLock) {
        int nbRemoved = 0;
        ThingBuilder thingBuilder = editThing();
        for (RemoteopenhabItem item : items) {
            Channel channel = getThing().getChannel(item.name);
            if (channel != null) {
                thingBuilder.withoutChannel(channel.getUID());
                nbRemoved++;
            }
        }
        if (nbRemoved > 0) {
            updateThing(thingBuilder.build());
            logger.debug("{} channels removed for the thing {} (from {} items)", nbRemoved, getThing().getUID(), items.size());
        }
    }
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) Channel(org.openhab.core.thing.Channel) RemoteopenhabItem(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabItem)

Example 2 with RemoteopenhabItem

use of org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabItem in project openhab-addons by openhab.

the class RemoteopenhabRestClient method onEvent.

private void onEvent(InboundSseEvent inboundEvent) {
    String name = inboundEvent.getName();
    String data = inboundEvent.readData();
    logger.trace("Received event name {} data {}", name, data);
    lastEventTimestamp = System.currentTimeMillis();
    if (!connected) {
        logger.debug("Connected to streaming events");
        connected = true;
        listeners.forEach(listener -> listener.onConnected());
    }
    if (!"message".equals(name)) {
        logger.debug("Received unhandled event with name '{}' and data '{}'", name, data);
        return;
    }
    try {
        RemoteopenhabEvent event = jsonParser.fromJson(data, RemoteopenhabEvent.class);
        String itemName;
        String thingUID;
        RemoteopenhabEventPayload payload;
        RemoteopenhabItem item;
        RemoteopenhabThing thing;
        switch(event.type) {
            case "ItemStateEvent":
                itemName = extractItemNameFromTopic(event.topic, event.type, "state");
                payload = jsonParser.fromJson(event.payload, RemoteopenhabEventPayload.class);
                itemsListeners.forEach(listener -> listener.onItemStateEvent(itemName, payload.type, payload.value, false));
                break;
            case "ItemStateChangedEvent":
                itemName = extractItemNameFromTopic(event.topic, event.type, "statechanged");
                payload = jsonParser.fromJson(event.payload, RemoteopenhabEventPayload.class);
                itemsListeners.forEach(listener -> listener.onItemStateEvent(itemName, payload.type, payload.value, true));
                break;
            case "GroupItemStateChangedEvent":
                itemName = extractItemNameFromTopic(event.topic, event.type, "statechanged");
                payload = jsonParser.fromJson(event.payload, RemoteopenhabEventPayload.class);
                itemsListeners.forEach(listener -> listener.onItemStateEvent(itemName, payload.type, payload.value, false));
                break;
            case "ItemAddedEvent":
                itemName = extractItemNameFromTopic(event.topic, event.type, "added");
                item = Objects.requireNonNull(jsonParser.fromJson(event.payload, RemoteopenhabItem.class));
                itemsListeners.forEach(listener -> listener.onItemAdded(item));
                break;
            case "ItemRemovedEvent":
                itemName = extractItemNameFromTopic(event.topic, event.type, "removed");
                item = Objects.requireNonNull(jsonParser.fromJson(event.payload, RemoteopenhabItem.class));
                itemsListeners.forEach(listener -> listener.onItemRemoved(item));
                break;
            case "ItemUpdatedEvent":
                itemName = extractItemNameFromTopic(event.topic, event.type, "updated");
                RemoteopenhabItem[] updItem = jsonParser.fromJson(event.payload, RemoteopenhabItem[].class);
                if (updItem.length == 2) {
                    itemsListeners.forEach(listener -> listener.onItemUpdated(updItem[0], updItem[1]));
                } else {
                    logger.debug("Invalid payload for event type {} for topic {}", event.type, event.topic);
                }
                break;
            case "ThingStatusInfoChangedEvent":
                thingUID = extractThingUIDFromTopic(event.topic, event.type, "statuschanged");
                RemoteopenhabStatusInfo[] updStatus = jsonParser.fromJson(event.payload, RemoteopenhabStatusInfo[].class);
                if (updStatus.length == 2) {
                    thingsListeners.forEach(listener -> listener.onThingStatusUpdated(thingUID, updStatus[0]));
                } else {
                    logger.debug("Invalid payload for event type {} for topic {}", event.type, event.topic);
                }
                break;
            case "ThingAddedEvent":
                thingUID = extractThingUIDFromTopic(event.topic, event.type, "added");
                thing = Objects.requireNonNull(jsonParser.fromJson(event.payload, RemoteopenhabThing.class));
                thingsListeners.forEach(listener -> listener.onThingAdded(thing));
                break;
            case "ThingRemovedEvent":
                thingUID = extractThingUIDFromTopic(event.topic, event.type, "removed");
                thing = Objects.requireNonNull(jsonParser.fromJson(event.payload, RemoteopenhabThing.class));
                thingsListeners.forEach(listener -> listener.onThingRemoved(thing));
                break;
            case "ChannelTriggeredEvent":
                RemoteopenhabChannelTriggerEvent triggerEvent = jsonParser.fromJson(event.payload, RemoteopenhabChannelTriggerEvent.class);
                thingsListeners.forEach(listener -> listener.onChannelTriggered(triggerEvent.channel, triggerEvent.event));
                break;
            case "ChannelDescriptionChangedEvent":
                RemoteopenhabStateDescription stateDescription = new RemoteopenhabStateDescription();
                RemoteopenhabCommandDescription commandDescription = new RemoteopenhabCommandDescription();
                RemoteopenhabChannelDescriptionChangedEvent descriptionChanged = Objects.requireNonNull(jsonParser.fromJson(event.payload, RemoteopenhabChannelDescriptionChangedEvent.class));
                switch(descriptionChanged.field) {
                    case "STATE_OPTIONS":
                        RemoteopenhabStateOptions stateOptions = Objects.requireNonNull(jsonParser.fromJson(descriptionChanged.value, RemoteopenhabStateOptions.class));
                        stateDescription.options = stateOptions.options;
                        break;
                    case "COMMAND_OPTIONS":
                        RemoteopenhabCommandOptions commandOptions = Objects.requireNonNull(jsonParser.fromJson(descriptionChanged.value, RemoteopenhabCommandOptions.class));
                        commandDescription.commandOptions = commandOptions.options;
                        break;
                    default:
                        break;
                }
                if (stateDescription.options != null || commandDescription.commandOptions != null) {
                    descriptionChanged.linkedItemNames.forEach(linkedItemName -> {
                        RemoteopenhabItem item1 = new RemoteopenhabItem();
                        item1.name = linkedItemName;
                        item1.stateDescription = stateDescription;
                        item1.commandDescription = commandDescription;
                        itemsListeners.forEach(listener -> listener.onItemOptionsUpdatedd(item1));
                    });
                }
                break;
            case "ItemStatePredictedEvent":
            case "ItemCommandEvent":
            case "ThingStatusInfoEvent":
            case "ThingUpdatedEvent":
                logger.trace("Ignored event type {} for topic {}", event.type, event.topic);
                break;
            default:
                logger.debug("Unexpected event type {} for topic {}", event.type, event.topic);
                break;
        }
    } catch (RemoteopenhabException | JsonSyntaxException e) {
        logger.debug("An exception occurred while processing the inbound '{}' event containg data: {}", name, data, e);
    }
}
Also used : RemoteopenhabCommandOptions(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabCommandOptions) RemoteopenhabException(org.openhab.binding.remoteopenhab.internal.exceptions.RemoteopenhabException) RemoteopenhabStateDescription(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabStateDescription) RemoteopenhabChannelTriggerEvent(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabChannelTriggerEvent) RemoteopenhabItem(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabItem) RemoteopenhabStatusInfo(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabStatusInfo) RemoteopenhabEventPayload(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabEventPayload) RemoteopenhabChannelDescriptionChangedEvent(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabChannelDescriptionChangedEvent) RemoteopenhabThing(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabThing) JsonSyntaxException(com.google.gson.JsonSyntaxException) RemoteopenhabCommandDescription(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabCommandDescription) RemoteopenhabEvent(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabEvent) RemoteopenhabStateOptions(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabStateOptions)

Example 3 with RemoteopenhabItem

use of org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabItem in project openhab-addons by openhab.

the class RemoteopenhabBridgeHandler method setDynamicOptions.

private void setDynamicOptions(List<RemoteopenhabItem> items) {
    for (RemoteopenhabItem item : items) {
        Channel channel = getThing().getChannel(item.name);
        if (channel == null) {
            continue;
        }
        RemoteopenhabStateDescription stateDescr = item.stateDescription;
        List<RemoteopenhabStateOption> stateOptions = stateDescr == null ? null : stateDescr.options;
        if (stateOptions != null && !stateOptions.isEmpty()) {
            List<StateOption> options = new ArrayList<>();
            for (RemoteopenhabStateOption option : stateOptions) {
                options.add(new StateOption(option.value, option.label));
            }
            stateDescriptionProvider.setStateOptions(channel.getUID(), options);
            logger.trace("{} state options set for the channel {}", options.size(), channel.getUID());
        }
        RemoteopenhabCommandDescription commandDescr = item.commandDescription;
        List<RemoteopenhabCommandOption> commandOptions = commandDescr == null ? null : commandDescr.commandOptions;
        if (commandOptions != null && !commandOptions.isEmpty()) {
            List<CommandOption> options = new ArrayList<>();
            for (RemoteopenhabCommandOption option : commandOptions) {
                options.add(new CommandOption(option.command, option.label));
            }
            commandDescriptionProvider.setCommandOptions(channel.getUID(), options);
            logger.trace("{} command options set for the channel {}", options.size(), channel.getUID());
        }
    }
}
Also used : CommandOption(org.openhab.core.types.CommandOption) RemoteopenhabCommandOption(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabCommandOption) Channel(org.openhab.core.thing.Channel) ArrayList(java.util.ArrayList) RemoteopenhabStateDescription(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabStateDescription) StateOption(org.openhab.core.types.StateOption) RemoteopenhabStateOption(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabStateOption) RemoteopenhabItem(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabItem) RemoteopenhabCommandDescription(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabCommandDescription) RemoteopenhabStateOption(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabStateOption) RemoteopenhabCommandOption(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabCommandOption)

Example 4 with RemoteopenhabItem

use of org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabItem in project openhab-addons by openhab.

the class RemoteopenhabBridgeHandler method createChannels.

private boolean createChannels(List<RemoteopenhabItem> items, boolean replace) {
    synchronized (updateThingLock) {
        try {
            int nbGroups = 0;
            int nbChannelTypesCreated = 0;
            List<Channel> channels = new ArrayList<>();
            for (RemoteopenhabItem item : items) {
                String itemType = item.type;
                boolean readOnly = false;
                if ("Group".equals(itemType)) {
                    if (item.groupType.isEmpty()) {
                        // Standard groups are ignored
                        nbGroups++;
                        continue;
                    } else {
                        itemType = item.groupType;
                    }
                } else {
                    if (item.stateDescription != null && item.stateDescription.readOnly) {
                        readOnly = true;
                    }
                }
                // Ignore pattern containing a transformation (detected by a parenthesis in the pattern)
                RemoteopenhabStateDescription stateDescription = item.stateDescription;
                String pattern = (stateDescription == null || stateDescription.pattern.contains("(")) ? "" : stateDescription.pattern;
                ChannelTypeUID channelTypeUID;
                ChannelType channelType = channelTypeProvider.getChannelType(itemType, readOnly, pattern);
                String label;
                String description;
                String defaultValue;
                if (channelType == null) {
                    channelTypeUID = channelTypeProvider.buildNewChannelTypeUID(itemType);
                    logger.trace("Create the channel type {} for item type {} ({} and with pattern {})", channelTypeUID, itemType, readOnly ? "read only" : "read write", pattern);
                    defaultValue = String.format("Remote %s Item", itemType);
                    label = i18nProvider.getText(bundle, "channel-type.label", defaultValue, localeProvider.getLocale(), itemType);
                    label = label != null && !label.isBlank() ? label : defaultValue;
                    description = i18nProvider.getText(bundle, "channel-type.description", defaultValue, localeProvider.getLocale(), itemType);
                    description = description != null && !description.isBlank() ? description : defaultValue;
                    StateDescriptionFragmentBuilder stateDescriptionBuilder = StateDescriptionFragmentBuilder.create().withReadOnly(readOnly);
                    if (!pattern.isEmpty()) {
                        stateDescriptionBuilder = stateDescriptionBuilder.withPattern(pattern);
                    }
                    channelType = ChannelTypeBuilder.state(channelTypeUID, label, itemType).withDescription(description).withStateDescriptionFragment(stateDescriptionBuilder.build()).withAutoUpdatePolicy(AutoUpdatePolicy.VETO).build();
                    channelTypeProvider.addChannelType(itemType, channelType);
                    nbChannelTypesCreated++;
                } else {
                    channelTypeUID = channelType.getUID();
                }
                ChannelUID channelUID = new ChannelUID(getThing().getUID(), item.name);
                logger.trace("Create the channel {} of type {}", channelUID, channelTypeUID);
                defaultValue = String.format("Item %s", item.name);
                label = i18nProvider.getText(bundle, "channel.label", defaultValue, localeProvider.getLocale(), item.name);
                label = label != null && !label.isBlank() ? label : defaultValue;
                description = i18nProvider.getText(bundle, "channel.description", defaultValue, localeProvider.getLocale(), item.name);
                description = description != null && !description.isBlank() ? description : defaultValue;
                channels.add(ChannelBuilder.create(channelUID, itemType).withType(channelTypeUID).withKind(ChannelKind.STATE).withLabel(label).withDescription(description).build());
            }
            ThingBuilder thingBuilder = editThing();
            if (replace) {
                thingBuilder.withChannels(channels);
                updateThing(thingBuilder.build());
                logger.debug("{} channels defined (with {} different channel types) for the thing {} (from {} items including {} groups)", channels.size(), nbChannelTypesCreated, getThing().getUID(), items.size(), nbGroups);
            } else if (!channels.isEmpty()) {
                int nbRemoved = 0;
                for (Channel channel : channels) {
                    if (getThing().getChannel(channel.getUID()) != null) {
                        thingBuilder.withoutChannel(channel.getUID());
                        nbRemoved++;
                    }
                }
                if (nbRemoved > 0) {
                    logger.debug("{} channels removed for the thing {} (from {} items)", nbRemoved, getThing().getUID(), items.size());
                }
                for (Channel channel : channels) {
                    thingBuilder.withChannel(channel);
                }
                updateThing(thingBuilder.build());
                if (nbGroups > 0) {
                    logger.debug("{} channels added for the thing {} (from {} items including {} groups)", channels.size(), getThing().getUID(), items.size(), nbGroups);
                } else {
                    logger.debug("{} channels added for the thing {} (from {} items)", channels.size(), getThing().getUID(), items.size());
                }
            }
            return true;
        } catch (IllegalArgumentException e) {
            logger.warn("An error occurred while creating the channels for the server {}: {}", getThing().getUID(), e.getMessage());
            return false;
        }
    }
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) Channel(org.openhab.core.thing.Channel) ArrayList(java.util.ArrayList) RemoteopenhabStateDescription(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabStateDescription) RemoteopenhabItem(org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabItem) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ChannelUID(org.openhab.core.thing.ChannelUID) StateDescriptionFragmentBuilder(org.openhab.core.types.StateDescriptionFragmentBuilder) ChannelType(org.openhab.core.thing.type.ChannelType)

Aggregations

RemoteopenhabItem (org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabItem)4 RemoteopenhabStateDescription (org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabStateDescription)3 Channel (org.openhab.core.thing.Channel)3 ArrayList (java.util.ArrayList)2 RemoteopenhabCommandDescription (org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabCommandDescription)2 ThingBuilder (org.openhab.core.thing.binding.builder.ThingBuilder)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 RemoteopenhabChannelDescriptionChangedEvent (org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabChannelDescriptionChangedEvent)1 RemoteopenhabChannelTriggerEvent (org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabChannelTriggerEvent)1 RemoteopenhabCommandOption (org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabCommandOption)1 RemoteopenhabCommandOptions (org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabCommandOptions)1 RemoteopenhabEvent (org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabEvent)1 RemoteopenhabEventPayload (org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabEventPayload)1 RemoteopenhabStateOption (org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabStateOption)1 RemoteopenhabStateOptions (org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabStateOptions)1 RemoteopenhabStatusInfo (org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabStatusInfo)1 RemoteopenhabThing (org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabThing)1 RemoteopenhabException (org.openhab.binding.remoteopenhab.internal.exceptions.RemoteopenhabException)1 ChannelUID (org.openhab.core.thing.ChannelUID)1 ChannelType (org.openhab.core.thing.type.ChannelType)1