]> git.basschouten.com Git - openhab-addons.git/blob
60abd8dbebe78b0a76bf6787af83ed7595c07992
[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.device;
14
15 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
16
17 import javax.measure.quantity.Dimensionless;
18 import javax.measure.quantity.Temperature;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.onewire.internal.OwException;
22 import org.openhab.binding.onewire.internal.SensorId;
23 import org.openhab.binding.onewire.internal.Util;
24 import org.openhab.binding.onewire.internal.handler.OwBaseThingHandler;
25 import org.openhab.binding.onewire.internal.handler.OwserverBridgeHandler;
26 import org.openhab.binding.onewire.internal.owserver.OwserverDeviceParameter;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.unit.SIUnits;
30 import org.openhab.core.library.unit.Units;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * The {@link DS1923} class defines a DS1923 device
36  *
37  * @author Jan N. Klug - Initial contribution
38  * @author Michał Wójcik - Adapted to DS1923
39  */
40 @NonNullByDefault
41 public class DS1923 extends AbstractOwDevice {
42     private final Logger logger = LoggerFactory.getLogger(DS1923.class);
43     private final OwserverDeviceParameter temperatureParameter = new OwserverDeviceParameter("/temperature");
44     private final OwserverDeviceParameter humidityParameter = new OwserverDeviceParameter("/humidity");
45
46     public DS1923(SensorId sensorId, OwBaseThingHandler callback) {
47         super(sensorId, callback);
48     }
49
50     @Override
51     public void configureChannels() throws OwException {
52         isConfigured = true;
53     }
54
55     @Override
56     public void refresh(OwserverBridgeHandler bridgeHandler, Boolean forcedRefresh) throws OwException {
57         if (isConfigured) {
58             logger.trace("refresh of sensor {} started", sensorId);
59             if (enabledChannels.contains(CHANNEL_TEMPERATURE) || enabledChannels.contains(CHANNEL_HUMIDITY)
60                     || enabledChannels.contains(CHANNEL_ABSOLUTE_HUMIDITY)
61                     || enabledChannels.contains(CHANNEL_DEWPOINT)) {
62                 QuantityType<Temperature> temperature = new QuantityType<>(
63                         (DecimalType) bridgeHandler.readDecimalType(sensorId, temperatureParameter), SIUnits.CELSIUS);
64                 callback.postUpdate(CHANNEL_TEMPERATURE, temperature);
65
66                 if (enabledChannels.contains(CHANNEL_HUMIDITY) || enabledChannels.contains(CHANNEL_ABSOLUTE_HUMIDITY)
67                         || enabledChannels.contains(CHANNEL_DEWPOINT)) {
68                     QuantityType<Dimensionless> humidity = new QuantityType<>(
69                             (DecimalType) bridgeHandler.readDecimalType(sensorId, humidityParameter), Units.PERCENT);
70
71                     if (enabledChannels.contains(CHANNEL_HUMIDITY)) {
72                         callback.postUpdate(CHANNEL_HUMIDITY, humidity);
73                     }
74
75                     if (enabledChannels.contains(CHANNEL_ABSOLUTE_HUMIDITY)) {
76                         callback.postUpdate(CHANNEL_ABSOLUTE_HUMIDITY,
77                                 Util.calculateAbsoluteHumidity(temperature, humidity));
78                     }
79
80                     if (enabledChannels.contains(CHANNEL_DEWPOINT)) {
81                         callback.postUpdate(CHANNEL_DEWPOINT, Util.calculateDewpoint(temperature, humidity));
82                     }
83                 }
84             }
85         }
86     }
87 }