2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.onewire.internal.handler;
15 import static org.openhab.binding.onewire.internal.OwBindingConstants.CHANNEL_DIGITAL;
16 import static org.openhab.binding.onewire.internal.OwBindingConstants.THING_TYPE_BASIC;
18 import java.util.Collections;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.binding.onewire.internal.OwDynamicStateDescriptionProvider;
25 import org.openhab.binding.onewire.internal.device.*;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.thing.Bridge;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.types.Command;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link BasicThingHandler} is responsible for handling simple sensors
38 * @author Jan N. Klug - Initial contribution
41 public class BasicThingHandler extends OwBaseThingHandler {
42 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_BASIC);
43 public static final Set<OwSensorType> SUPPORTED_SENSOR_TYPES = Collections
44 .unmodifiableSet(Stream.of(OwSensorType.DS1420, OwSensorType.DS18B20, OwSensorType.DS18S20,
45 OwSensorType.DS1822, OwSensorType.DS2401, OwSensorType.DS2405, OwSensorType.DS2406,
46 OwSensorType.DS2408, OwSensorType.DS2413, OwSensorType.DS2423).collect(Collectors.toSet()));
48 private final Logger logger = LoggerFactory.getLogger(BasicThingHandler.class);
50 public BasicThingHandler(Thing thing, OwDynamicStateDescriptionProvider dynamicStateDescriptionProvider) {
51 super(thing, dynamicStateDescriptionProvider, SUPPORTED_SENSOR_TYPES);
55 public void initialize() {
56 if (!super.configureThingHandler()) {
65 sensors.add(new DS18x20(sensorId, this));
69 sensors.add(new DS2401(sensorId, this));
72 sensors.add(new DS2405(sensorId, this));
76 sensors.add(new DS2406_DS2413(sensorId, this));
79 sensors.add(new DS2408(sensorId, this));
82 sensors.add(new DS2423(sensorId, this));
85 throw new IllegalArgumentException(
86 "unsupported sensorType " + sensorType.name() + ", this should have been checked before!");
89 scheduler.execute(() -> {
90 configureThingChannels();
95 public void handleCommand(ChannelUID channelUID, Command command) {
96 if (command instanceof OnOffType) {
97 if (channelUID.getId().startsWith(CHANNEL_DIGITAL) && thing.getChannel(channelUID.getId()) != null) {
98 Integer ioChannel = Integer.valueOf(channelUID.getId().substring(channelUID.getId().length() - 1));
99 Bridge bridge = getBridge();
100 if (bridge != null) {
101 OwserverBridgeHandler bridgeHandler = (OwserverBridgeHandler) bridge.getHandler();
102 if (bridgeHandler != null) {
103 if (!((AbstractDigitalOwDevice) sensors.get(0)).writeChannel(bridgeHandler, ioChannel,
105 logger.debug("writing to channel {} in thing {} not permitted (input channel)", channelUID,
106 this.thing.getUID());
109 logger.warn("bridge handler not found");
112 logger.warn("bridge not found");
116 super.handleCommand(channelUID, command);