]> git.basschouten.com Git - openhab-addons.git/blob
1ee3d3cef390bedbe61a9b177c7e0b9f37994914
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.onewire.internal.handler;
14
15 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
16
17 import java.util.Set;
18
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;
37
38 /**
39  * The {@link BasicThingHandler} is responsible for handling simple sensors
40  *
41  * @author Jan N. Klug - Initial contribution
42  */
43 @NonNullByDefault
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);
49
50     private final Logger logger = LoggerFactory.getLogger(BasicThingHandler.class);
51
52     public BasicThingHandler(Thing thing, OwDynamicStateDescriptionProvider dynamicStateDescriptionProvider) {
53         super(thing, dynamicStateDescriptionProvider, SUPPORTED_SENSOR_TYPES);
54     }
55
56     @Override
57     public void initialize() {
58         if (!super.configureThingHandler()) {
59             return;
60         }
61
62         // add sensor
63         switch (sensorType) {
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!");
72         }
73
74         scheduler.execute(this::configureThingChannels);
75     }
76
77     @Override
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();
83                 if (bridge != null) {
84                     OwserverBridgeHandler bridgeHandler = (OwserverBridgeHandler) bridge.getHandler();
85                     if (bridgeHandler != null) {
86                         if (!((AbstractDigitalOwDevice) sensors.get(0)).writeChannel(bridgeHandler, ioChannel,
87                                 command)) {
88                             logger.debug("writing to channel {} in thing {} not permitted (input channel)", channelUID,
89                                     this.thing.getUID());
90                         }
91                     } else {
92                         logger.warn("bridge handler not found");
93                     }
94                 } else {
95                     logger.warn("bridge not found");
96                 }
97             }
98         }
99         super.handleCommand(channelUID, command);
100     }
101 }