]> git.basschouten.com Git - openhab-addons.git/blob
3b549e03f1ae056b2a086b3c346891c17bc6fd0c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.SmartHomeUnits;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * The {@link DS1923} class defines an 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),
70                             SmartHomeUnits.PERCENT);
71
72                     if (enabledChannels.contains(CHANNEL_HUMIDITY)) {
73                         callback.postUpdate(CHANNEL_HUMIDITY, humidity);
74                     }
75
76                     if (enabledChannels.contains(CHANNEL_ABSOLUTE_HUMIDITY)) {
77                         callback.postUpdate(CHANNEL_ABSOLUTE_HUMIDITY,
78                                 Util.calculateAbsoluteHumidity(temperature, humidity));
79                     }
80
81                     if (enabledChannels.contains(CHANNEL_DEWPOINT)) {
82                         callback.postUpdate(CHANNEL_DEWPOINT, Util.calculateDewpoint(temperature, humidity));
83                     }
84                 }
85             }
86         }
87     }
88 }