Search in sources :

Example 26 with ThingBuilder

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

the class TACmiSchemaHandler method refreshData.

private void refreshData() {
    URI schemaApiPage = this.schemaApiPage;
    if (schemaApiPage == null) {
        return;
    }
    try {
        final ApiPageParser pp = parsePage(schemaApiPage, new ApiPageParser(this, entries, this.channelTypeProvider));
        final List<Channel> channels = pp.getChannels();
        if (pp.isConfigChanged() || channels.size() != this.getThing().getChannels().size()) {
            // we have to update our channels...
            final ThingBuilder thingBuilder = editThing();
            thingBuilder.withChannels(channels);
            updateThing(thingBuilder.build());
        }
        if (!this.online) {
            updateStatus(ThingStatus.ONLINE);
            this.online = true;
        }
    } catch (final InterruptedException e) {
        // binding shutdown is in progress
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE);
        this.online = false;
    } catch (final ParseException | RuntimeException e) {
        logger.debug("Error parsing API Scheme: {} ", e.getMessage(), e);
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR, "Error: " + e.getMessage());
        this.online = false;
    } catch (final TimeoutException | ExecutionException e) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Error: " + e.getMessage());
        this.online = false;
    }
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) Channel(org.openhab.core.thing.Channel) ParseException(org.attoparser.ParseException) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) TimeoutException(java.util.concurrent.TimeoutException)

Example 27 with ThingBuilder

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

the class BAE091xSensorThingHandler method configureThingChannels.

@Override
protected void configureThingChannels() {
    ThingUID thingUID = getThing().getUID();
    logger.debug("configuring sensors for {}", thingUID);
    BAE091xHandlerConfiguration configuration = getConfig().as(BAE091xHandlerConfiguration.class);
    Set<OwChannelConfig> wantedChannel = new HashSet<>();
    wantedChannel.addAll(SENSOR_TYPE_CHANNEL_MAP.getOrDefault(sensorType, Set.of()));
    // Pin1:
    switch(configuration.pin1) {
        case CONFIG_BAE_PIN_DISABLED:
            break;
        case CONFIG_BAE_PIN_COUNTER:
            wantedChannel.add(new OwChannelConfig(CHANNEL_COUNTER, CHANNEL_TYPE_UID_BAE_COUNTER));
            break;
        default:
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unknown configuration option for pin 1");
            return;
    }
    // Pin2:
    switch(configuration.pin2) {
        case CONFIG_BAE_PIN_DISABLED:
            break;
        case CONFIG_BAE_PIN_OUT:
            wantedChannel.add(new OwChannelConfig(CHANNEL_DIGITAL2, CHANNEL_TYPE_UID_BAE_DIGITAL_OUT, "Digital Out Pin 2"));
            break;
        case CONFIG_BAE_PIN_PWM:
            wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_DUTY3, CHANNEL_TYPE_UID_BAE_PWM_DUTY, "Duty Cycle PWM 3"));
            wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_FREQ1, CHANNEL_TYPE_UID_BAE_PWM_FREQUENCY, "Frequency PWM 1/3"));
            break;
        default:
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unknown configuration option for pin 2");
            return;
    }
    // Pin6:
    switch(configuration.pin6) {
        case CONFIG_BAE_PIN_DISABLED:
            break;
        case CONFIG_BAE_PIN_PIO:
            wantedChannel.add(new OwChannelConfig(CHANNEL_DIGITAL6, CHANNEL_TYPE_UID_BAE_PIO, "PIO Pin 6"));
            break;
        case CONFIG_BAE_PIN_PWM:
            wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_DUTY4, CHANNEL_TYPE_UID_BAE_PWM_DUTY, "Duty Cycle PWM 4"));
            wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_FREQ2, CHANNEL_TYPE_UID_BAE_PWM_FREQUENCY, "Frequency PWM 2/4"));
            break;
        default:
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unknown configuration option for pin 6");
            return;
    }
    // Pin7:
    switch(configuration.pin7) {
        case CONFIG_BAE_PIN_DISABLED:
            break;
        case CONFIG_BAE_PIN_ANALOG:
            wantedChannel.add(new OwChannelConfig(CHANNEL_VOLTAGE, CHANNEL_TYPE_UID_BAE_ANALOG, "Analog Input"));
            break;
        case CONFIG_BAE_PIN_OUT:
            wantedChannel.add(new OwChannelConfig(CHANNEL_DIGITAL7, CHANNEL_TYPE_UID_BAE_DIGITAL_OUT, "Digital Out Pin 7"));
            break;
        case CONFIG_BAE_PIN_PWM:
            wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_DUTY2, CHANNEL_TYPE_UID_BAE_PWM_DUTY, "Duty Cycle PWM 2"));
            wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_FREQ2, CHANNEL_TYPE_UID_BAE_PWM_FREQUENCY, "Frequency PWM 2/4"));
            break;
        default:
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unknown configuration option for pin 7");
            return;
    }
    // Pin8:
    switch(configuration.pin8) {
        case CONFIG_BAE_PIN_DISABLED:
            break;
        case CONFIG_BAE_PIN_IN:
            wantedChannel.add(new OwChannelConfig(CHANNEL_DIGITAL8, CHANNEL_TYPE_UID_BAE_DIN, "Digital In Pin 8"));
            break;
        case CONFIG_BAE_PIN_OUT:
            wantedChannel.add(new OwChannelConfig(CHANNEL_DIGITAL8, CHANNEL_TYPE_UID_BAE_DOUT, "Digital Out Pin 8"));
            break;
        case CONFIG_BAE_PIN_PWM:
            wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_DUTY1, CHANNEL_TYPE_UID_BAE_PWM_DUTY, "Duty Cycle PWM 1"));
            wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_FREQ1, CHANNEL_TYPE_UID_BAE_PWM_FREQUENCY, "Frequency PWM 1/3"));
            break;
        default:
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unknown configuration option for pin 8");
            return;
    }
    ThingBuilder thingBuilder = editThing();
    // remove unwanted channels
    Set<String> existingChannelIds = thing.getChannels().stream().map(channel -> channel.getUID().getId()).collect(Collectors.toSet());
    Set<String> wantedChannelIds = wantedChannel.stream().map(channelConfig -> channelConfig.channelId).collect(Collectors.toSet());
    existingChannelIds.stream().filter(channelId -> !wantedChannelIds.contains(channelId)).forEach(channelId -> removeChannelIfExisting(thingBuilder, channelId));
    // add or update wanted channels
    wantedChannel.stream().forEach(channelConfig -> {
        addChannelIfMissingAndEnable(thingBuilder, channelConfig);
    });
    updateThing(thingBuilder.build());
    try {
        sensors.get(0).configureChannels();
    } catch (OwException e) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
        return;
    }
    validConfig = true;
    updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE);
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) OnOffType(org.openhab.core.library.types.OnOffType) BAE0910(org.openhab.binding.onewire.internal.device.BAE0910) BAE091xHandlerConfiguration(org.openhab.binding.onewire.internal.config.BAE091xHandlerConfiguration) HashSet(java.util.HashSet) Thing(org.openhab.core.thing.Thing) Map(java.util.Map) OwException(org.openhab.binding.onewire.internal.OwException) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ChannelUID(org.openhab.core.thing.ChannelUID) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) OwChannelConfig(org.openhab.binding.onewire.internal.device.OwChannelConfig) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ThingStatus(org.openhab.core.thing.ThingStatus) Command(org.openhab.core.types.Command) Logger(org.slf4j.Logger) ThingUID(org.openhab.core.thing.ThingUID) OwBindingConstants(org.openhab.binding.onewire.internal.OwBindingConstants) Set(java.util.Set) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) Collectors(java.util.stream.Collectors) OwDynamicStateDescriptionProvider(org.openhab.binding.onewire.internal.OwDynamicStateDescriptionProvider) OwSensorType(org.openhab.binding.onewire.internal.device.OwSensorType) Collections(java.util.Collections) Bridge(org.openhab.core.thing.Bridge) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) OwChannelConfig(org.openhab.binding.onewire.internal.device.OwChannelConfig) ThingUID(org.openhab.core.thing.ThingUID) BAE091xHandlerConfiguration(org.openhab.binding.onewire.internal.config.BAE091xHandlerConfiguration) HashSet(java.util.HashSet) OwException(org.openhab.binding.onewire.internal.OwException)

Example 28 with ThingBuilder

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

the class PlugwiseHAApplianceHandler method addBatteryChannels.

protected synchronized void addBatteryChannels() {
    logger.debug("Battery operated appliance: {} detected: adding 'Battery level' and 'Battery low level' channels", thing.getLabel());
    ChannelUID channelUIDBatteryLevel = new ChannelUID(getThing().getUID(), APPLIANCE_BATTERYLEVEL_CHANNEL);
    ChannelUID channelUIDBatteryLevelLow = new ChannelUID(getThing().getUID(), APPLIANCE_BATTERYLEVELLOW_CHANNEL);
    boolean channelBatteryLevelExists = false;
    boolean channelBatteryLowExists = false;
    List<Channel> channels = getThing().getChannels();
    for (Channel channel : channels) {
        if (channel.getUID().equals(channelUIDBatteryLevel)) {
            channelBatteryLevelExists = true;
        } else if (channel.getUID().equals(channelUIDBatteryLevelLow)) {
            channelBatteryLowExists = true;
        }
        if (channelBatteryLevelExists && channelBatteryLowExists) {
            break;
        }
    }
    if (!channelBatteryLevelExists) {
        ThingBuilder thingBuilder = editThing();
        Channel channelBatteryLevel = ChannelBuilder.create(channelUIDBatteryLevel, "Number").withType(CHANNEL_TYPE_BATTERYLEVEL).withKind(ChannelKind.STATE).withLabel("Battery Level").withDescription("Represents the battery level as a percentage (0-100%)").build();
        thingBuilder.withChannel(channelBatteryLevel);
        updateThing(thingBuilder.build());
    }
    if (!channelBatteryLowExists) {
        ThingBuilder thingBuilder = editThing();
        Channel channelBatteryLow = ChannelBuilder.create(channelUIDBatteryLevelLow, "Switch").withType(CHANNEL_TYPE_BATTERYLEVELLOW).withKind(ChannelKind.STATE).withLabel("Battery Low Level").withDescription("Switches ON when battery level gets below threshold level").build();
        thingBuilder.withChannel(channelBatteryLow);
        updateThing(thingBuilder.build());
    }
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ChannelUID(org.openhab.core.thing.ChannelUID) Channel(org.openhab.core.thing.Channel)

Example 29 with ThingBuilder

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

the class TouchWandBSensorHandler method removeUnsupportedChannels.

void removeUnsupportedChannels(TouchWandBSensorUnitData unitData) {
    ArrayList<Channel> toBeRemovedChannels = new ArrayList<>(thing.getChannels());
    String sensorSubType = unitData.getIdData().getSubType();
    switch(sensorSubType) {
        case BSENSOR_SUBTYPE_DOORWINDOW:
            toBeRemovedChannels.remove(thing.getChannel(CHANNEL_DOORWINDOW));
            break;
        case BSENSOR_SUBTYPE_MOTION:
            toBeRemovedChannels.remove(thing.getChannel(CHANNEL_MOTION));
            break;
        case BSENSOR_SUBTYPE_SMOKE:
            Channel channel = thing.getChannel(CHANNEL_SMOKE);
            toBeRemovedChannels.remove(channel);
            break;
    }
    ThingBuilder thingBuilder = editThing();
    thingBuilder.withoutChannels(toBeRemovedChannels);
    updateThing(thingBuilder.build());
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) Channel(org.openhab.core.thing.Channel) ArrayList(java.util.ArrayList)

Example 30 with ThingBuilder

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

the class PLCDateTimeHandler method doInitialization.

@Override
protected void doInitialization() {
    Thing thing = getThing();
    logger.debug("Initialize LOGO! date/time handler.");
    config.set(getConfigAs(PLCDateTimeConfiguration.class));
    super.doInitialization();
    if (ThingStatus.OFFLINE != thing.getStatus()) {
        String block = config.get().getBlockType();
        String text = "Time".equalsIgnoreCase(block) ? "Time" : "Date";
        ThingBuilder tBuilder = editThing();
        String label = thing.getLabel();
        if (label == null) {
            Bridge bridge = getBridge();
            label = (bridge == null) || (bridge.getLabel() == null) ? "Siemens Logo!" : bridge.getLabel();
            label += (": " + text.toLowerCase() + " in/output");
        }
        tBuilder.withLabel(label);
        String name = config.get().getBlockName();
        String type = config.get().getChannelType();
        ChannelUID uid = new ChannelUID(thing.getUID(), "Time".equalsIgnoreCase(block) ? "time" : "date");
        ChannelBuilder cBuilder = ChannelBuilder.create(uid, type);
        cBuilder.withType(new ChannelTypeUID(BINDING_ID, type.toLowerCase()));
        cBuilder.withLabel(name);
        cBuilder.withDescription(text + " block parameter " + name);
        cBuilder.withProperties(Collections.singletonMap(BLOCK_PROPERTY, name));
        tBuilder.withChannel(cBuilder.build());
        cBuilder = ChannelBuilder.create(new ChannelUID(thing.getUID(), VALUE_CHANNEL), ANALOG_ITEM);
        cBuilder.withType(new ChannelTypeUID(BINDING_ID, ANALOG_ITEM.toLowerCase()));
        cBuilder.withLabel(name);
        cBuilder.withDescription(text + " block parameter " + name);
        cBuilder.withProperties(Collections.singletonMap(BLOCK_PROPERTY, name));
        tBuilder.withChannel(cBuilder.build());
        setOldValue(name, null);
        updateThing(tBuilder.build());
        updateStatus(ThingStatus.ONLINE);
    }
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ChannelUID(org.openhab.core.thing.ChannelUID) PLCDateTimeConfiguration(org.openhab.binding.plclogo.internal.config.PLCDateTimeConfiguration) ChannelBuilder(org.openhab.core.thing.binding.builder.ChannelBuilder) Thing(org.openhab.core.thing.Thing) Bridge(org.openhab.core.thing.Bridge)

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