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.*;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.onewire.internal.OwDynamicStateDescriptionProvider;
21 import org.openhab.binding.onewire.internal.device.AbstractDigitalOwDevice;
22 import org.openhab.binding.onewire.internal.device.DS18x20;
23 import org.openhab.binding.onewire.internal.device.DS2401;
24 import org.openhab.binding.onewire.internal.device.DS2405;
25 import org.openhab.binding.onewire.internal.device.DS2406_DS2413;
26 import org.openhab.binding.onewire.internal.device.DS2408;
27 import org.openhab.binding.onewire.internal.device.DS2423;
28 import org.openhab.binding.onewire.internal.device.OwSensorType;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.types.Command;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The {@link BasicThingHandler} is responsible for handling simple sensors
41 * @author Jan N. Klug - Initial contribution
44 public class BasicThingHandler extends OwBaseThingHandler {
45 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_BASIC);
46 public static final Set<OwSensorType> SUPPORTED_SENSOR_TYPES = Set.of(OwSensorType.DS1420, OwSensorType.DS18B20,
47 OwSensorType.DS18S20, OwSensorType.DS1822, OwSensorType.DS2401, OwSensorType.DS2405, OwSensorType.DS2406,
48 OwSensorType.DS2408, OwSensorType.DS2413, OwSensorType.DS2423);
50 private final Logger logger = LoggerFactory.getLogger(BasicThingHandler.class);
52 public BasicThingHandler(Thing thing, OwDynamicStateDescriptionProvider dynamicStateDescriptionProvider) {
53 super(thing, dynamicStateDescriptionProvider, SUPPORTED_SENSOR_TYPES);
57 public void initialize() {
58 if (!super.configureThingHandler()) {
64 case DS18B20, DS18S20, DS1822 -> sensors.add(new DS18x20(sensorId, this));
65 case DS1420, DS2401 -> sensors.add(new DS2401(sensorId, this));
66 case DS2405 -> sensors.add(new DS2405(sensorId, this));
67 case DS2406, DS2413 -> sensors.add(new DS2406_DS2413(sensorId, this));
68 case DS2408 -> sensors.add(new DS2408(sensorId, this));
69 case DS2423 -> sensors.add(new DS2423(sensorId, this));
70 default -> throw new IllegalArgumentException(
71 "unsupported sensorType " + sensorType.name() + ", this should have been checked before!");
74 scheduler.execute(this::configureThingChannels);
78 public void handleCommand(ChannelUID channelUID, Command command) {
79 if (command instanceof OnOffType) {
80 if (channelUID.getId().startsWith(CHANNEL_DIGITAL) && thing.getChannel(channelUID.getId()) != null) {
81 Integer ioChannel = Integer.valueOf(channelUID.getId().substring(channelUID.getId().length() - 1));
82 Bridge bridge = getBridge();
84 OwserverBridgeHandler bridgeHandler = (OwserverBridgeHandler) bridge.getHandler();
85 if (bridgeHandler != null) {
86 if (!((AbstractDigitalOwDevice) sensors.get(0)).writeChannel(bridgeHandler, ioChannel,
88 logger.debug("writing to channel {} in thing {} not permitted (input channel)", channelUID,
92 logger.warn("bridge handler not found");
95 logger.warn("bridge not found");
99 super.handleCommand(channelUID, command);