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