Search in sources :

Example 11 with ThingBuilder

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

the class GenericBluetoothHandler method updateThingChannels.

private void updateThingChannels() {
    List<Channel> channels = // 
    device.getServices().stream().flatMap(// 
    service -> service.getCharacteristics().stream()).flatMap(characteristic -> {
        logger.trace("{} processing characteristic {}", address, characteristic.getUuid());
        CharacteristicHandler handler = getCharacteristicHandler(characteristic);
        List<Channel> chans = handler.buildChannels();
        List<ChannelUID> chanUids = chans.stream().map(Channel::getUID).collect(Collectors.toList());
        for (ChannelUID channel : chanUids) {
            channelHandlers.put(channel, handler);
        }
        handlerToChannels.put(handler, chanUids);
        return chans.stream();
    }).collect(Collectors.toList());
    ThingBuilder builder = editThing();
    boolean changed = false;
    for (Channel channel : channels) {
        logger.trace("{} attempting to add channel {}", address, channel.getLabel());
        // we only want to add each channel, not replace all of them
        if (getThing().getChannel(channel.getUID()) == null) {
            changed = true;
            builder.withChannel(channel);
        }
    }
    if (changed) {
        updateThing(builder.build());
    }
}
Also used : Field(org.sputnikdev.bluetooth.gattparser.spec.Field) ConnectionState(org.openhab.binding.bluetooth.BluetoothDevice.ConnectionState) ScheduledFuture(java.util.concurrent.ScheduledFuture) StringType(org.openhab.core.library.types.StringType) GattResponse(org.sputnikdev.bluetooth.gattparser.GattResponse) LoggerFactory(org.slf4j.LoggerFactory) Characteristic(org.sputnikdev.bluetooth.gattparser.spec.Characteristic) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HexUtils(org.openhab.core.util.HexUtils) Thing(org.openhab.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) GattRequest(org.sputnikdev.bluetooth.gattparser.GattRequest) ChannelUID(org.openhab.core.thing.ChannelUID) BluetoothGattParserFactory(org.sputnikdev.bluetooth.gattparser.BluetoothGattParserFactory) BluetoothBindingConstants(org.openhab.binding.bluetooth.BluetoothBindingConstants) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ThingStatus(org.openhab.core.thing.ThingStatus) Command(org.openhab.core.types.Command) Logger(org.slf4j.Logger) ChannelBuilder(org.openhab.core.thing.binding.builder.ChannelBuilder) State(org.openhab.core.types.State) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RefreshType(org.openhab.core.types.RefreshType) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) BluetoothCharacteristic(org.openhab.binding.bluetooth.BluetoothCharacteristic) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Channel(org.openhab.core.thing.Channel) List(java.util.List) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) BluetoothGattParser(org.sputnikdev.bluetooth.gattparser.BluetoothGattParser) FieldHolder(org.sputnikdev.bluetooth.gattparser.FieldHolder) ConnectedBluetoothHandler(org.openhab.binding.bluetooth.ConnectedBluetoothHandler) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ChannelUID(org.openhab.core.thing.ChannelUID) Channel(org.openhab.core.thing.Channel) ArrayList(java.util.ArrayList) List(java.util.List)

Example 12 with ThingBuilder

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

the class BeaconBluetoothHandler method initialize.

@Override
public void initialize() {
    try {
        address = new BluetoothAddress(getConfig().get(BluetoothBindingConstants.CONFIGURATION_ADDRESS).toString());
    } catch (IllegalArgumentException e) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getLocalizedMessage());
        return;
    }
    Bridge bridge = getBridge();
    if (bridge == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Not associated with any bridge");
        return;
    }
    BridgeHandler bridgeHandler = bridge.getHandler();
    if (!(bridgeHandler instanceof BluetoothAdapter)) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Associated with an unsupported bridge");
        return;
    }
    adapter = (BluetoothAdapter) bridgeHandler;
    try {
        deviceLock.lock();
        device = adapter.getDevice(address);
        device.addListener(this);
    } finally {
        deviceLock.unlock();
    }
    ThingBuilder builder = editThing();
    boolean changed = false;
    for (Channel channel : createDynamicChannels()) {
        // we only want to add each channel, not replace all of them
        if (getThing().getChannel(channel.getUID()) == null) {
            builder.withChannel(channel);
            changed = true;
        }
    }
    if (changed) {
        updateThing(builder.build());
    }
    updateStatus(ThingStatus.UNKNOWN);
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) BridgeHandler(org.openhab.core.thing.binding.BridgeHandler) Channel(org.openhab.core.thing.Channel) Bridge(org.openhab.core.thing.Bridge)

Example 13 with ThingBuilder

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

the class EnOceanBaseThingHandler method updateChannels.

protected void updateChannels() {
    @NonNull List<@NonNull Channel> channelList = new LinkedList<>(this.getThing().getChannels());
    Collection<EEPType> eeps = getEEPTypes();
    if (eeps == null) {
        return;
    }
    // First remove channels which are no longer supported by current selected eeps of thing
    AtomicBoolean channelListChanged = new AtomicBoolean(channelList.removeIf(channel -> !eeps.stream().anyMatch(eep -> eep.isChannelSupported(channel))));
    // Next create supported channels of each selected eep
    eeps.stream().flatMap(eep -> eep.GetSupportedChannels().keySet().stream().map(id -> new SimpleEntry<>(id, eep))).forEach(entry -> {
        String channelId = entry.getKey();
        EnOceanChannelDescription cd = entry.getValue().GetSupportedChannels().get(channelId);
        if (cd == null) {
            return;
        }
        // if we do not need to auto create channel => skip
        if (!cd.autoCreate) {
            return;
        }
        // if we already created a channel with the same type and id => skip
        if (channelList.stream().anyMatch(channel -> cd.channelTypeUID.equals(channel.getChannelTypeUID()) && channelId.equals(channel.getUID().getId()))) {
            return;
        }
        // create channel and add it to the channelList
        Channel channel = ChannelBuilder.create(new ChannelUID(this.getThing().getUID(), channelId), cd.acceptedItemType).withConfiguration(entry.getValue().getChannelConfig(channelId)).withType(cd.channelTypeUID).withKind(cd.isStateChannel ? ChannelKind.STATE : ChannelKind.TRIGGER).withLabel(cd.label).build();
        channelList.add(channel);
        channelListChanged.set(true);
        if (!cd.isStateChannel) {
            lastEvents.putIfAbsent(channelId, "");
        }
    });
    if (channelListChanged.get()) {
        ThingBuilder thingBuilder = editThing();
        thingBuilder.withChannels(channelList);
        updateThing(thingBuilder.build());
    }
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Thing(org.openhab.core.thing.Thing) EnOceanBindingConstants(org.openhab.binding.enocean.internal.EnOceanBindingConstants) ChannelKind(org.openhab.core.thing.type.ChannelKind) EnOceanChannelDescription(org.openhab.binding.enocean.internal.EnOceanChannelDescription) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ChannelUID(org.openhab.core.thing.ChannelUID) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) LinkedList(java.util.LinkedList) SimpleEntry(java.util.AbstractMap.SimpleEntry) Hashtable(java.util.Hashtable) ThingStatus(org.openhab.core.thing.ThingStatus) Logger(org.slf4j.Logger) ConfigStatusMessage(org.openhab.core.config.core.status.ConfigStatusMessage) ThingHandler(org.openhab.core.thing.binding.ThingHandler) ChannelBuilder(org.openhab.core.thing.binding.builder.ChannelBuilder) ItemChannelLinkRegistry(org.openhab.core.thing.link.ItemChannelLinkRegistry) Collection(java.util.Collection) State(org.openhab.core.types.State) UnDefType(org.openhab.core.types.UnDefType) Set(java.util.Set) EnOceanBaseConfig(org.openhab.binding.enocean.internal.config.EnOceanBaseConfig) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) Item(org.openhab.core.items.Item) Channel(org.openhab.core.thing.Channel) List(java.util.List) ConfigStatusThingHandler(org.openhab.core.thing.binding.ConfigStatusThingHandler) EEPType(org.openhab.binding.enocean.internal.eep.EEPType) NonNull(org.eclipse.jdt.annotation.NonNull) Bridge(org.openhab.core.thing.Bridge) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) Channel(org.openhab.core.thing.Channel) EnOceanChannelDescription(org.openhab.binding.enocean.internal.EnOceanChannelDescription) LinkedList(java.util.LinkedList) EEPType(org.openhab.binding.enocean.internal.eep.EEPType) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ChannelUID(org.openhab.core.thing.ChannelUID) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 14 with ThingBuilder

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

the class HarmonyDeviceHandler method updateButtonPressChannel.

/**
 * Updates the buttonPress channel with the available buttons as option states.
 */
private void updateButtonPressChannel(@Nullable HarmonyConfig harmonyConfig) {
    ChannelTypeUID channelTypeUID = new ChannelTypeUID(getThing().getUID().getAsString() + ":" + CHANNEL_BUTTON_PRESS);
    if (harmonyConfig == null) {
        logger.debug("Cannot update {} when HarmonyConfig is null", channelTypeUID);
        return;
    }
    logger.debug("Updating {}", channelTypeUID);
    List<StateOption> states = getButtonStateOptions(harmonyConfig);
    ChannelType channelType = ChannelTypeBuilder.state(channelTypeUID, "Send Button Press", "String").withDescription("Send a button press to device " + getThing().getLabel()).withStateDescriptionFragment(StateDescriptionFragmentBuilder.create().withOptions(states).build()).build();
    factory.addChannelType(channelType);
    Channel channel = ChannelBuilder.create(new ChannelUID(getThing().getUID(), CHANNEL_BUTTON_PRESS), "String").withType(channelTypeUID).build();
    // replace existing buttonPress with updated one
    List<Channel> newChannels = new ArrayList<>();
    for (Channel c : getThing().getChannels()) {
        if (!c.getUID().equals(channel.getUID())) {
            newChannels.add(c);
        }
    }
    newChannels.add(channel);
    ThingBuilder thingBuilder = editThing();
    thingBuilder.withChannels(newChannels);
    updateThing(thingBuilder.build());
}
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) ChannelType(org.openhab.core.thing.type.ChannelType) StateOption(org.openhab.core.types.StateOption)

Example 15 with ThingBuilder

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

the class GuntamaticHandler method parseAndInit.

private void parseAndInit(String html) {
    String[] daqdesc = html.split("\\n");
    List<Channel> channelList = new ArrayList<>();
    // make sure that static channels are present
    for (String channelID : staticChannelIDs) {
        Channel channel = thing.getChannel(channelID);
        if (channel == null) {
            logger.warn("Static Channel '{}' is not present: remove and re-add Thing", channelID);
        } else {
            channelList.add(channel);
        }
    }
    // add dynamic channels, based on data provided by Guntamatic Heating System
    for (int i = 0; i < daqdesc.length; i++) {
        String[] param = daqdesc[i].split(";");
        String label = param[0].replace("C02", "CO2");
        if (!"reserved".equals(label)) {
            String channel = toLowerCamelCase(replaceUmlaut(label));
            label = label.substring(0, 1).toUpperCase() + label.substring(1);
            String unitStr = ((param.length == 1) || param[1].isBlank()) ? "" : param[1].trim();
            Unit<?> unit = guessUnit(unitStr);
            boolean channelInitialized = channels.containsValue(channel);
            if (!channelInitialized) {
                String itemType;
                String pattern;
                String type = types.get(i);
                if (type == null) {
                    type = "";
                }
                if ("boolean".equals(type)) {
                    itemType = CoreItemFactory.SWITCH;
                    pattern = "";
                } else if ("integer".equals(type)) {
                    itemType = guessItemType(unit);
                    pattern = "%d";
                    if (unit != null) {
                        pattern += " %unit%";
                    }
                } else if ("float".equals(type)) {
                    itemType = guessItemType(unit);
                    pattern = "%.2f";
                    if (unit != null) {
                        pattern += " %unit%";
                    }
                } else if ("string".equals(type)) {
                    itemType = CoreItemFactory.STRING;
                    pattern = "%s";
                } else {
                    if (unitStr.isBlank()) {
                        itemType = CoreItemFactory.STRING;
                        pattern = "%s";
                    } else {
                        itemType = guessItemType(unit);
                        pattern = "%.2f";
                        if (unit != null) {
                            pattern += " %unit%";
                        }
                    }
                }
                ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, channel);
                guntamaticChannelTypeProvider.addChannelType(channelTypeUID, channel, itemType, "Guntamatic " + label, false, pattern);
                Channel newChannel = ChannelBuilder.create(new ChannelUID(thing.getUID(), channel), itemType).withType(channelTypeUID).withKind(ChannelKind.STATE).withLabel(label).build();
                channelList.add(newChannel);
                channels.put(i, channel);
                if (unit != null) {
                    units.put(i, unit);
                }
                logger.debug("Supported Channel: Idx: '{}', Name: '{}'/'{}', Type: '{}'/'{}', Unit: '{}', Pattern '{}' ", String.format("%03d", i), label, channel, type, itemType, unitStr, pattern);
            }
        }
    }
    ThingBuilder thingBuilder = editThing();
    thingBuilder.withChannels(channelList);
    updateThing(thingBuilder.build());
    channelsInitialized = true;
}
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)

Aggregations

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