]> git.basschouten.com Git - openhab-addons.git/blob
21fc44a37bc823aa68398e4b796f7f3cbd54cef0
[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.Map;
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.DS2438Configuration;
25 import org.openhab.binding.onewire.internal.OwDynamicStateDescriptionProvider;
26 import org.openhab.binding.onewire.internal.OwException;
27 import org.openhab.binding.onewire.internal.config.MstxHandlerConfiguration;
28 import org.openhab.binding.onewire.internal.device.DS1923;
29 import org.openhab.binding.onewire.internal.device.DS2438;
30 import org.openhab.binding.onewire.internal.device.DS2438.CurrentSensorType;
31 import org.openhab.binding.onewire.internal.device.DS2438.LightSensorType;
32 import org.openhab.binding.onewire.internal.device.OwSensorType;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * The {@link BasicMultisensorThingHandler} is responsible for handling DS2438/DS1923 based multisensors (single
40  * sensors)
41  *
42  * @author Jan N. Klug - Initial contribution
43  */
44 @NonNullByDefault
45 public class BasicMultisensorThingHandler extends OwBaseThingHandler {
46     public Logger logger = LoggerFactory.getLogger(BasicMultisensorThingHandler.class);
47     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_MS_TX);
48     public static final Set<OwSensorType> SUPPORTED_SENSOR_TYPES = Collections
49             .unmodifiableSet(Stream.of(OwSensorType.MS_TH, OwSensorType.MS_TC, OwSensorType.MS_TL, OwSensorType.MS_TV,
50                     OwSensorType.DS1923, OwSensorType.DS2438).collect(Collectors.toSet()));
51
52     public BasicMultisensorThingHandler(Thing thing,
53             OwDynamicStateDescriptionProvider dynamicStateDescriptionProvider) {
54         super(thing, dynamicStateDescriptionProvider, SUPPORTED_SENSOR_TYPES);
55     }
56
57     @Override
58     public void initialize() {
59         if (!super.configureThingHandler()) {
60             return;
61         }
62
63         MstxHandlerConfiguration configuration = getConfig().as(MstxHandlerConfiguration.class);
64         if (configuration.manualsensor != null && sensorType != configuration.manualsensor) {
65             logger.debug("sensorType override for thing {}: old={}, new={}", thing.getUID(), sensorType,
66                     configuration.manualsensor);
67             sensorType = configuration.manualsensor;
68         }
69
70         // add sensors
71         if (sensorType == OwSensorType.DS1923) {
72             sensors.add(new DS1923(sensorId, this));
73         } else {
74             sensors.add(new DS2438(sensorId, this));
75         }
76
77         scheduler.execute(() -> {
78             configureThingChannels();
79         });
80     }
81
82     @Override
83     protected void configureThingChannels() {
84         switch (sensorType) {
85             case DS2438:
86                 ((DS2438) sensors.get(0)).setCurrentSensorType(CurrentSensorType.INTERNAL);
87                 break;
88             case MS_TC:
89                 ((DS2438) sensors.get(0)).setCurrentSensorType(CurrentSensorType.IBUTTONLINK);
90                 break;
91             case MS_TL:
92                 ((DS2438) sensors.get(0)).setLightSensorType(LightSensorType.IBUTTONLINK);
93                 break;
94             default:
95         }
96
97         super.configureThingChannels();
98     }
99
100     @Override
101     public void updateSensorProperties(OwserverBridgeHandler bridgeHandler) throws OwException {
102         Map<String, String> properties = editProperties();
103         sensorType = bridgeHandler.getType(sensorId);
104
105         if (sensorType == OwSensorType.DS1923) {
106             properties.put(PROPERTY_MODELID, sensorType.toString());
107             properties.put(PROPERTY_VENDOR, "Dallas/Maxim");
108         } else {
109             DS2438Configuration ds2438configuration = new DS2438Configuration(bridgeHandler, sensorId);
110
111             sensorType = ds2438configuration.getSensorSubType();
112             properties.put(PROPERTY_MODELID, sensorType.toString());
113
114             String vendor = ds2438configuration.getVendor();
115             properties.put(PROPERTY_VENDOR, vendor);
116         }
117
118         updateProperties(properties);
119     }
120 }