]> git.basschouten.com Git - openhab-addons.git/blob
8833c09446376446feefca4a1e3f75878be6aafd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.CHANNEL_DIGITAL;
16 import static org.openhab.binding.onewire.internal.OwBindingConstants.THING_TYPE_BASIC;
17
18 import java.util.Collections;
19 import java.util.Set;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
22
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;
34
35 /**
36  * The {@link BasicThingHandler} is responsible for handling simple sensors
37  *
38  * @author Jan N. Klug - Initial contribution
39  */
40 @NonNullByDefault
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()));
47
48     private final Logger logger = LoggerFactory.getLogger(BasicThingHandler.class);
49
50     public BasicThingHandler(Thing thing, OwDynamicStateDescriptionProvider dynamicStateDescriptionProvider) {
51         super(thing, dynamicStateDescriptionProvider, SUPPORTED_SENSOR_TYPES);
52     }
53
54     @Override
55     public void initialize() {
56         if (!super.configureThingHandler()) {
57             return;
58         }
59
60         // add sensor
61         switch (sensorType) {
62             case DS18B20:
63             case DS18S20:
64             case DS1822:
65                 sensors.add(new DS18x20(sensorId, this));
66                 break;
67             case DS1420:
68             case DS2401:
69                 sensors.add(new DS2401(sensorId, this));
70                 break;
71             case DS2405:
72                 sensors.add(new DS2405(sensorId, this));
73                 break;
74             case DS2406:
75             case DS2413:
76                 sensors.add(new DS2406_DS2413(sensorId, this));
77                 break;
78             case DS2408:
79                 sensors.add(new DS2408(sensorId, this));
80                 break;
81             case DS2423:
82                 sensors.add(new DS2423(sensorId, this));
83                 break;
84             default:
85                 throw new IllegalArgumentException(
86                         "unsupported sensorType " + sensorType.name() + ", this should have been checked before!");
87         }
88
89         scheduler.execute(() -> {
90             configureThingChannels();
91         });
92     }
93
94     @Override
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,
104                                 command)) {
105                             logger.debug("writing to channel {} in thing {} not permitted (input channel)", channelUID,
106                                     this.thing.getUID());
107                         }
108                     } else {
109                         logger.warn("bridge handler not found");
110                     }
111                 } else {
112                     logger.warn("bridge not found");
113                 }
114             }
115         }
116         super.handleCommand(channelUID, command);
117     }
118 }