]> git.basschouten.com Git - openhab-addons.git/blob
618751f41f7fa8e258735b7c085d53f4fee1a6a3
[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.Collections;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
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;
40
41 /**
42  * The {@link BasicThingHandler} is responsible for handling simple sensors
43  *
44  * @author Jan N. Klug - Initial contribution
45  */
46 @NonNullByDefault
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()));
53
54     private final Logger logger = LoggerFactory.getLogger(BasicThingHandler.class);
55
56     public BasicThingHandler(Thing thing, OwDynamicStateDescriptionProvider dynamicStateDescriptionProvider) {
57         super(thing, dynamicStateDescriptionProvider, SUPPORTED_SENSOR_TYPES);
58     }
59
60     @Override
61     public void initialize() {
62         if (!super.configureThingHandler()) {
63             return;
64         }
65
66         // add sensor
67         switch (sensorType) {
68             case DS18B20:
69             case DS18S20:
70             case DS1822:
71                 sensors.add(new DS18x20(sensorId, this));
72                 break;
73             case DS1420:
74             case DS2401:
75                 sensors.add(new DS2401(sensorId, this));
76                 break;
77             case DS2405:
78                 sensors.add(new DS2405(sensorId, this));
79                 break;
80             case DS2406:
81             case DS2413:
82                 sensors.add(new DS2406_DS2413(sensorId, this));
83                 break;
84             case DS2408:
85                 sensors.add(new DS2408(sensorId, this));
86                 break;
87             case DS2423:
88                 sensors.add(new DS2423(sensorId, this));
89                 break;
90             default:
91                 throw new IllegalArgumentException(
92                         "unsupported sensorType " + sensorType.name() + ", this should have been checked before!");
93         }
94
95         scheduler.execute(() -> {
96             configureThingChannels();
97         });
98     }
99
100     @Override
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,
110                                 command)) {
111                             logger.debug("writing to channel {} in thing {} not permitted (input channel)", channelUID,
112                                     this.thing.getUID());
113                         }
114                     } else {
115                         logger.warn("bridge handler not found");
116                     }
117                 } else {
118                     logger.warn("bridge not found");
119                 }
120             }
121         }
122         super.handleCommand(channelUID, command);
123     }
124 }