2 * Copyright (c) 2010-2023 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.*;
17 import java.util.Collections;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.onewire.internal.OwDynamicStateDescriptionProvider;
24 import org.openhab.binding.onewire.internal.device.AbstractDigitalOwDevice;
25 import org.openhab.binding.onewire.internal.device.DS18x20;
26 import org.openhab.binding.onewire.internal.device.DS2401;
27 import org.openhab.binding.onewire.internal.device.DS2405;
28 import org.openhab.binding.onewire.internal.device.DS2406_DS2413;
29 import org.openhab.binding.onewire.internal.device.DS2408;
30 import org.openhab.binding.onewire.internal.device.DS2423;
31 import org.openhab.binding.onewire.internal.device.OwSensorType;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.thing.Bridge;
34 import org.openhab.core.thing.ChannelUID;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.types.Command;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * The {@link BasicThingHandler} is responsible for handling simple sensors
44 * @author Jan N. Klug - Initial contribution
47 public class BasicThingHandler extends OwBaseThingHandler {
48 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_BASIC);
49 public static final Set<OwSensorType> SUPPORTED_SENSOR_TYPES = Collections
50 .unmodifiableSet(Stream.of(OwSensorType.DS1420, OwSensorType.DS18B20, OwSensorType.DS18S20,
51 OwSensorType.DS1822, OwSensorType.DS2401, OwSensorType.DS2405, OwSensorType.DS2406,
52 OwSensorType.DS2408, OwSensorType.DS2413, OwSensorType.DS2423).collect(Collectors.toSet()));
54 private final Logger logger = LoggerFactory.getLogger(BasicThingHandler.class);
56 public BasicThingHandler(Thing thing, OwDynamicStateDescriptionProvider dynamicStateDescriptionProvider) {
57 super(thing, dynamicStateDescriptionProvider, SUPPORTED_SENSOR_TYPES);
61 public void initialize() {
62 if (!super.configureThingHandler()) {
71 sensors.add(new DS18x20(sensorId, this));
75 sensors.add(new DS2401(sensorId, this));
78 sensors.add(new DS2405(sensorId, this));
82 sensors.add(new DS2406_DS2413(sensorId, this));
85 sensors.add(new DS2408(sensorId, this));
88 sensors.add(new DS2423(sensorId, this));
91 throw new IllegalArgumentException(
92 "unsupported sensorType " + sensorType.name() + ", this should have been checked before!");
95 scheduler.execute(() -> {
96 configureThingChannels();
101 public void handleCommand(ChannelUID channelUID, Command command) {
102 if (command instanceof OnOffType) {
103 if (channelUID.getId().startsWith(CHANNEL_DIGITAL) && thing.getChannel(channelUID.getId()) != null) {
104 Integer ioChannel = Integer.valueOf(channelUID.getId().substring(channelUID.getId().length() - 1));
105 Bridge bridge = getBridge();
106 if (bridge != null) {
107 OwserverBridgeHandler bridgeHandler = (OwserverBridgeHandler) bridge.getHandler();
108 if (bridgeHandler != null) {
109 if (!((AbstractDigitalOwDevice) sensors.get(0)).writeChannel(bridgeHandler, ioChannel,
111 logger.debug("writing to channel {} in thing {} not permitted (input channel)", channelUID,
112 this.thing.getUID());
115 logger.warn("bridge handler not found");
118 logger.warn("bridge not found");
122 super.handleCommand(channelUID, command);