Search in sources :

Example 16 with ThingBuilder

use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.

the class EventFilterHandler method updateChannelSet.

/**
 * Checks existing channels, adds missing and removes extraneous channels from the Thing.
 *
 * @param config The validated Configuration of the Thing.
 */
private void updateChannelSet(EventFilterConfiguration config) {
    final ThingHandlerCallback handlerCallback = getCallback();
    if (handlerCallback == null) {
        return;
    }
    final List<Channel> currentChannels = getThing().getChannels();
    final ThingBuilder thingBuilder = editThing();
    BigDecimal maxEvents = config.maxEvents;
    if (maxEvents == null || maxEvents.compareTo(BigDecimal.ZERO) < 1) {
        thingBuilder.withoutChannels(currentChannels);
        updateThing(thingBuilder.build());
        return;
    }
    generateExpectedChannelList(maxEvents.intValue());
    synchronized (resultChannels) {
        currentChannels.stream().filter((Channel current) -> {
            String currentGroupId = current.getUID().getGroupId();
            if (currentGroupId == null) {
                return true;
            }
            for (ResultChannelSet channelSet : resultChannels) {
                if (channelSet.resultGroup.getId().contentEquals(currentGroupId)) {
                    return false;
                }
            }
            return true;
        }).forEach((Channel toDelete) -> {
            thingBuilder.withoutChannel(toDelete.getUID());
        });
        resultChannels.stream().filter((ResultChannelSet current) -> {
            return (getThing().getChannelsOfGroup(current.resultGroup.toString()).size() == 0);
        }).forEach((ResultChannelSet current) -> {
            for (ChannelBuilder builder : handlerCallback.createChannelBuilders(current.resultGroup, GROUP_TYPE_UID)) {
                Channel currentChannel = builder.build();
                Channel existingChannel = getThing().getChannel(currentChannel.getUID());
                if (existingChannel == null) {
                    thingBuilder.withChannel(currentChannel);
                }
            }
        });
    }
    updateThing(thingBuilder.build());
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) Channel(org.openhab.core.thing.Channel) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ChannelBuilder(org.openhab.core.thing.binding.builder.ChannelBuilder) BigDecimal(java.math.BigDecimal)

Example 17 with ThingBuilder

use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.

the class ZoneTemperatureControlHandler method loadChannel.

private synchronized void loadChannel() {
    List<Channel> newChannelList = new ArrayList<>(1);
    if (currentChannelID != null) {
        newChannelList.add(ChannelBuilder.create(new ChannelUID(this.getThing().getUID(), currentChannelID), DsChannelTypeProvider.getItemType(currentChannelID)).withType(new ChannelTypeUID(BINDING_ID, currentChannelID)).build());
    }
    ThingBuilder thingBuilder = editThing();
    thingBuilder.withChannels(newChannelList);
    updateThing(thingBuilder.build());
    logger.debug("load channel: {} with item: {}", currentChannelID, DsChannelTypeProvider.getItemType(currentChannelID));
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ChannelUID(org.openhab.core.thing.ChannelUID) Channel(org.openhab.core.thing.Channel) ArrayList(java.util.ArrayList)

Example 18 with ThingBuilder

use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.

the class TrackerHandler method createBasicDistanceChannel.

/**
 * Create distance channel for measuring the distance between the tracker and the szstem.
 */
private void createBasicDistanceChannel() {
    @Nullable ThingHandlerCallback callback = getCallback();
    if (callback != null) {
        // find the system distance channel
        ChannelUID systemDistanceChannelUID = new ChannelUID(thing.getUID(), CHANNEL_DISTANCE_SYSTEM_ID);
        Channel systemDistance = thing.getChannel(CHANNEL_DISTANCE_SYSTEM_ID);
        ChannelBuilder channelBuilder = null;
        if (systemDistance != null) {
            if (!systemDistance.getConfiguration().get(CONFIG_REGION_CENTER_LOCATION).equals(sysLocation.toFullString())) {
                logger.trace("Existing distance channel for system. Changing system location config parameter: {}", sysLocation.toFullString());
                channelBuilder = callback.editChannel(thing, systemDistanceChannelUID);
                Configuration configToUpdate = systemDistance.getConfiguration();
                configToUpdate.put(CONFIG_REGION_CENTER_LOCATION, sysLocation.toFullString());
                channelBuilder.withConfiguration(configToUpdate);
            } else {
                logger.trace("Existing distance channel for system. No change.");
            }
        } else {
            logger.trace("Creating missing distance channel for system.");
            Configuration config = new Configuration();
            config.put(ConfigHelper.CONFIG_REGION_NAME, CHANNEL_DISTANCE_SYSTEM_NAME);
            config.put(CONFIG_REGION_CENTER_LOCATION, sysLocation.toFullString());
            config.put(ConfigHelper.CONFIG_REGION_RADIUS, CHANNEL_DISTANCE_SYSTEM_RADIUS);
            config.put(ConfigHelper.CONFIG_ACCURACY_THRESHOLD, 0);
            channelBuilder = callback.createChannelBuilder(systemDistanceChannelUID, CHANNEL_TYPE_DISTANCE).withLabel("System Distance").withConfiguration(config);
        }
        // update the thing with system distance channel
        if (channelBuilder != null) {
            List<Channel> channels = new ArrayList<>(thing.getChannels());
            if (systemDistance != null) {
                channels.remove(systemDistance);
            }
            channels.add(channelBuilder.build());
            ThingBuilder thingBuilder = editThing();
            thingBuilder.withChannels(channels);
            updateThing(thingBuilder.build());
            logger.debug("Distance channel created for system: {}", systemDistanceChannelUID);
        }
    }
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) Configuration(org.openhab.core.config.core.Configuration) ChannelUID(org.openhab.core.thing.ChannelUID) Channel(org.openhab.core.thing.Channel) ArrayList(java.util.ArrayList) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ChannelBuilder(org.openhab.core.thing.binding.builder.ChannelBuilder) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 19 with ThingBuilder

use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.

the class Ipx800v3Handler method discoverAttributes.

protected void discoverAttributes() {
    final Map<String, String> properties = new HashMap<>();
    properties.put(Thing.PROPERTY_VENDOR, "GCE Electronics");
    properties.put(Thing.PROPERTY_FIRMWARE_VERSION, statusFile.getElement(StatusEntry.VERSION));
    properties.put(Thing.PROPERTY_MAC_ADDRESS, statusFile.getElement(StatusEntry.CONFIG_MAC));
    updateProperties(properties);
    ThingBuilder thingBuilder = editThing();
    List<Channel> channels = new ArrayList<>(getThing().getChannels());
    PortDefinition.asStream().forEach(portDefinition -> {
        int nbElements = statusFile.getMaxNumberofNodeType(portDefinition);
        for (int i = 0; i < nbElements; i++) {
            createChannels(portDefinition, i, channels);
        }
    });
    thingBuilder.withChannels(channels);
    updateThing(thingBuilder.build());
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) HashMap(java.util.HashMap) Channel(org.openhab.core.thing.Channel) ArrayList(java.util.ArrayList)

Example 20 with ThingBuilder

use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.

the class LinuxInputHandler method delayedSetup.

@Override
boolean delayedSetup() throws IOException {
    ThingBuilder customizer = editThing();
    List<Channel> newChannels = new ArrayList<>();
    newChannels.add(keyChannel);
    EvdevDevice newDevice = new EvdevDevice(config.path);
    for (EvdevDevice.Key o : newDevice.enumerateKeys()) {
        String name = o.getName();
        Channel channel = ChannelBuilder.create(new ChannelUID(thing.getUID(), CHANNEL_GROUP_KEYPRESSES_ID, name), CoreItemFactory.CONTACT).withLabel(name).withType(CHANNEL_TYPE_KEY_PRESS).build();
        channels.put(o.getCode(), channel);
        newChannels.add(channel);
    }
    if (Objects.equals(defaultLabel, thing.getLabel())) {
        customizer.withLabel(newDevice.getName());
    }
    customizer.withChannels(newChannels);
    Map<String, String> props = getProperties(Objects.requireNonNull(newDevice));
    customizer.withProperties(props);
    updateThing(customizer.build());
    for (Channel channel : newChannels) {
        updateState(channel.getUID(), OpenClosedType.OPEN);
    }
    if (config.enable) {
        updateStatus(ThingStatus.ONLINE);
    }
    device = newDevice;
    return config.enable;
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) EvdevDevice(org.openhab.binding.linuxinput.internal.evdev4j.EvdevDevice) ChannelUID(org.openhab.core.thing.ChannelUID) Channel(org.openhab.core.thing.Channel) ArrayList(java.util.ArrayList)

Aggregations

ThingBuilder (org.openhab.core.thing.binding.builder.ThingBuilder)105 Channel (org.openhab.core.thing.Channel)71 ChannelUID (org.openhab.core.thing.ChannelUID)59 ChannelTypeUID (org.openhab.core.thing.type.ChannelTypeUID)38 ArrayList (java.util.ArrayList)37 Thing (org.openhab.core.thing.Thing)28 HashMap (java.util.HashMap)22 ChannelBuilder (org.openhab.core.thing.binding.builder.ChannelBuilder)19 Map (java.util.Map)18 Bridge (org.openhab.core.thing.Bridge)17 ThingStatus (org.openhab.core.thing.ThingStatus)17 ThingStatusDetail (org.openhab.core.thing.ThingStatusDetail)17 Logger (org.slf4j.Logger)17 LoggerFactory (org.slf4j.LoggerFactory)17 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)16 Nullable (org.eclipse.jdt.annotation.Nullable)15 List (java.util.List)14 Collectors (java.util.stream.Collectors)14 Configuration (org.openhab.core.config.core.Configuration)14 Command (org.openhab.core.types.Command)11