Search in sources :

Example 1 with OwChannelConfig

use of org.openhab.binding.onewire.internal.device.OwChannelConfig 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 2 with OwChannelConfig

use of org.openhab.binding.onewire.internal.device.OwChannelConfig in project openhab-addons by openhab.

the class AdvancedMultisensorThingHandler method configureThingChannels.

@Override
protected void configureThingChannels() {
    Configuration configuration = getConfig();
    ThingBuilder thingBuilder = editThing();
    // delete unwanted channels
    Set<String> existingChannelIds = thing.getChannels().stream().map(channel -> channel.getUID().getId()).collect(Collectors.toSet());
    Set<String> wantedChannelIds = SENSOR_TYPE_CHANNEL_MAP.get(sensorType).stream().map(channelConfig -> channelConfig.channelId).collect(Collectors.toSet());
    wantedChannelIds.add(CHANNEL_TEMPERATURE);
    wantedChannelIds.add(CHANNEL_HUMIDITY);
    existingChannelIds.stream().filter(channelId -> !wantedChannelIds.contains(channelId)).forEach(channelId -> removeChannelIfExisting(thingBuilder, channelId));
    // add or update wanted channels
    SENSOR_TYPE_CHANNEL_MAP.get(sensorType).stream().forEach(channelConfig -> {
        addChannelIfMissingAndEnable(thingBuilder, channelConfig);
    });
    // temperature channel
    if (configuration.containsKey(CONFIG_TEMPERATURESENSOR) && configuration.get(CONFIG_TEMPERATURESENSOR).equals("DS18B20")) {
        addChannelIfMissingAndEnable(thingBuilder, new OwChannelConfig(CHANNEL_TEMPERATURE, CHANNEL_TYPE_UID_TEMPERATURE_POR_RES), 1);
    } else {
        addChannelIfMissingAndEnable(thingBuilder, new OwChannelConfig(CHANNEL_TEMPERATURE, CHANNEL_TYPE_UID_TEMPERATURE));
    }
    // humidity channel
    addChannelIfMissingAndEnable(thingBuilder, new OwChannelConfig(CHANNEL_HUMIDITY, CHANNEL_TYPE_UID_HUMIDITY), new Configuration(new HashMap<String, Object>() {

        private static final long serialVersionUID = 1L;

        {
            put(CONFIG_HUMIDITY, "/HIH4000/humidity");
        }
    }));
    // configure light channel
    if (sensorType == OwSensorType.AMS_S || sensorType == OwSensorType.BMS_S) {
        if (hwRevision <= 13) {
            ((DS2438) sensors.get(0)).setLightSensorType(LightSensorType.ELABNET_V1);
        } else {
            ((DS2438) sensors.get(0)).setLightSensorType(LightSensorType.ELABNET_V2);
        }
    }
    updateThing(thingBuilder.build());
    try {
        for (AbstractOwDevice sensor : sensors) {
            sensor.configureChannels();
        }
    } catch (OwException e) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
        return;
    }
    validConfig = true;
    updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE);
}
Also used : Arrays(java.util.Arrays) AbstractOwDevice(org.openhab.binding.onewire.internal.device.AbstractOwDevice) LoggerFactory(org.slf4j.LoggerFactory) DS18x20(org.openhab.binding.onewire.internal.device.DS18x20) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Thing(org.openhab.core.thing.Thing) DS2406_DS2413(org.openhab.binding.onewire.internal.device.DS2406_DS2413) Configuration(org.openhab.core.config.core.Configuration) DS2438Configuration(org.openhab.binding.onewire.internal.DS2438Configuration) Map(java.util.Map) OwException(org.openhab.binding.onewire.internal.OwException) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) AMSHandlerConfiguration(org.openhab.binding.onewire.internal.config.AMSHandlerConfiguration) OwChannelConfig(org.openhab.binding.onewire.internal.device.OwChannelConfig) SensorId(org.openhab.binding.onewire.internal.SensorId) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ThingStatus(org.openhab.core.thing.ThingStatus) Logger(org.slf4j.Logger) OwBindingConstants(org.openhab.binding.onewire.internal.OwBindingConstants) Set(java.util.Set) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) DS2438(org.openhab.binding.onewire.internal.device.DS2438) Collectors(java.util.stream.Collectors) LightSensorType(org.openhab.binding.onewire.internal.device.DS2438.LightSensorType) Stream(java.util.stream.Stream) OwDynamicStateDescriptionProvider(org.openhab.binding.onewire.internal.OwDynamicStateDescriptionProvider) OwSensorType(org.openhab.binding.onewire.internal.device.OwSensorType) Collections(java.util.Collections) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) OwChannelConfig(org.openhab.binding.onewire.internal.device.OwChannelConfig) Configuration(org.openhab.core.config.core.Configuration) DS2438Configuration(org.openhab.binding.onewire.internal.DS2438Configuration) AMSHandlerConfiguration(org.openhab.binding.onewire.internal.config.AMSHandlerConfiguration) DS2438(org.openhab.binding.onewire.internal.device.DS2438) HashMap(java.util.HashMap) AbstractOwDevice(org.openhab.binding.onewire.internal.device.AbstractOwDevice) OwException(org.openhab.binding.onewire.internal.OwException)

Aggregations

Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)2 OwBindingConstants (org.openhab.binding.onewire.internal.OwBindingConstants)2 OwDynamicStateDescriptionProvider (org.openhab.binding.onewire.internal.OwDynamicStateDescriptionProvider)2 OwException (org.openhab.binding.onewire.internal.OwException)2 OwChannelConfig (org.openhab.binding.onewire.internal.device.OwChannelConfig)2 OwSensorType (org.openhab.binding.onewire.internal.device.OwSensorType)2 Thing (org.openhab.core.thing.Thing)2 ThingStatus (org.openhab.core.thing.ThingStatus)2 ThingStatusDetail (org.openhab.core.thing.ThingStatusDetail)2 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)2 ThingBuilder (org.openhab.core.thing.binding.builder.ThingBuilder)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1