]> git.basschouten.com Git - openhab-addons.git/blob
37fdb3b7bc3afbc929a3d622c2c66d0bed7bc943
[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 import static org.openhab.core.library.unit.MetricPrefix.MILLI;
17
18 import javax.measure.quantity.Dimensionless;
19 import javax.measure.quantity.Temperature;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.onewire.internal.OwException;
23 import org.openhab.binding.onewire.internal.SensorId;
24 import org.openhab.binding.onewire.internal.Util;
25 import org.openhab.binding.onewire.internal.handler.OwBaseThingHandler;
26 import org.openhab.binding.onewire.internal.handler.OwserverBridgeHandler;
27 import org.openhab.binding.onewire.internal.owserver.OwserverDeviceParameter;
28 import org.openhab.core.config.core.Configuration;
29 import org.openhab.core.library.types.DecimalType;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.library.unit.SIUnits;
32 import org.openhab.core.library.unit.Units;
33 import org.openhab.core.thing.Channel;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.types.State;
36 import org.openhab.core.types.UnDefType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * The {@link DS2438} class defines a DS2438 device
42  *
43  * @author Jan N. Klug - Initial contribution
44  */
45 @NonNullByDefault
46 public class DS2438 extends AbstractOwDevice {
47     private final Logger logger = LoggerFactory.getLogger(DS2438.class);
48
49     public enum LightSensorType {
50         ELABNET_V1,
51         ELABNET_V2,
52         IBUTTONLINK
53     }
54
55     public enum CurrentSensorType {
56         INTERNAL,
57         IBUTTONLINK
58     }
59
60     private LightSensorType lightSensorType = LightSensorType.ELABNET_V1;
61     private CurrentSensorType currentSensorType = CurrentSensorType.INTERNAL;
62
63     private final OwserverDeviceParameter temperatureParameter = new OwserverDeviceParameter("/temperature");
64     private OwserverDeviceParameter humidityParameter = new OwserverDeviceParameter("/humidity");
65     private final OwserverDeviceParameter voltageParameter = new OwserverDeviceParameter("/VAD");
66     private final OwserverDeviceParameter currentParamater = new OwserverDeviceParameter("/vis");
67     private final OwserverDeviceParameter supplyVoltageParameter = new OwserverDeviceParameter("/VDD");
68
69     public DS2438(SensorId sensorId, OwBaseThingHandler callback) {
70         super(sensorId, callback);
71     }
72
73     @Override
74     public void configureChannels() {
75         Thing thing = callback.getThing();
76
77         Channel humidityChannel = thing.getChannel(CHANNEL_HUMIDITY);
78         if (humidityChannel != null) {
79             Configuration channelConfiguration = humidityChannel.getConfiguration();
80             if (channelConfiguration.get(CONFIG_HUMIDITY) != null) {
81                 humidityParameter = new OwserverDeviceParameter((String) channelConfiguration.get(CONFIG_HUMIDITY));
82             } else {
83                 humidityParameter = new OwserverDeviceParameter("/humidity");
84             }
85         }
86
87         isConfigured = true;
88     }
89
90     @Override
91     public void refresh(OwserverBridgeHandler bridgeHandler, Boolean forcedRefresh) throws OwException {
92         if (isConfigured) {
93             logger.trace("refresh of sensor {} started", sensorId);
94             double Vcc = 5.0;
95
96             if (enabledChannels.contains(CHANNEL_TEMPERATURE) || enabledChannels.contains(CHANNEL_HUMIDITY)
97                     || enabledChannels.contains(CHANNEL_ABSOLUTE_HUMIDITY)
98                     || enabledChannels.contains(CHANNEL_DEWPOINT)) {
99                 QuantityType<Temperature> temperature = new QuantityType<>(
100                         (DecimalType) bridgeHandler.readDecimalType(sensorId, temperatureParameter), SIUnits.CELSIUS);
101                 logger.trace("read temperature {} from {}", temperature, sensorId);
102
103                 if (enabledChannels.contains(CHANNEL_TEMPERATURE)) {
104                     callback.postUpdate(CHANNEL_TEMPERATURE, temperature);
105                 }
106
107                 if (enabledChannels.contains(CHANNEL_HUMIDITY) || enabledChannels.contains(CHANNEL_ABSOLUTE_HUMIDITY)
108                         || enabledChannels.contains(CHANNEL_DEWPOINT)) {
109                     QuantityType<Dimensionless> humidity = new QuantityType<>(
110                             (DecimalType) bridgeHandler.readDecimalType(sensorId, humidityParameter), Units.PERCENT);
111                     logger.trace("read humidity {} from {}", humidity, sensorId);
112
113                     if (enabledChannels.contains(CHANNEL_HUMIDITY)) {
114                         callback.postUpdate(CHANNEL_HUMIDITY, humidity);
115                     }
116
117                     if (enabledChannels.contains(CHANNEL_ABSOLUTE_HUMIDITY)) {
118                         callback.postUpdate(CHANNEL_ABSOLUTE_HUMIDITY,
119                                 Util.calculateAbsoluteHumidity(temperature, humidity));
120                     }
121
122                     if (enabledChannels.contains(CHANNEL_DEWPOINT)) {
123                         callback.postUpdate(CHANNEL_DEWPOINT, Util.calculateDewpoint(temperature, humidity));
124                     }
125                 }
126             }
127
128             if (enabledChannels.contains(CHANNEL_VOLTAGE)) {
129                 double measured = ((DecimalType) bridgeHandler.readDecimalType(sensorId, voltageParameter))
130                         .doubleValue();
131                 if (measured < 0 || measured > 10.0) {
132                     // workaround bug in DS2438
133                     measured = 0.0;
134                 }
135                 State voltage = new QuantityType<>(measured, Units.VOLT);
136
137                 logger.trace("read voltage {} from {}", voltage, sensorId);
138                 callback.postUpdate(CHANNEL_VOLTAGE, voltage);
139             }
140
141             if (enabledChannels.contains(CHANNEL_CURRENT)) {
142                 if (currentSensorType == CurrentSensorType.IBUTTONLINK) {
143                     State current = bridgeHandler.readDecimalType(sensorId, voltageParameter);
144                     if (current instanceof DecimalType decimalCommand) {
145                         double currentDouble = decimalCommand.doubleValue();
146                         if (currentDouble >= 0.1 || currentDouble <= 3.78) {
147                             current = new QuantityType<>(currentDouble * 5.163 + 0.483, Units.AMPERE);
148                         }
149                         callback.postUpdate(CHANNEL_CURRENT, current);
150                     } else {
151                         callback.postUpdate(CHANNEL_CURRENT, UnDefType.UNDEF);
152                     }
153                 } else {
154                     State current = new QuantityType<>(
155                             (DecimalType) bridgeHandler.readDecimalType(sensorId, currentParamater),
156                             MILLI(Units.AMPERE));
157                     callback.postUpdate(CHANNEL_CURRENT, current);
158                 }
159             }
160
161             if (enabledChannels.contains(CHANNEL_SUPPLYVOLTAGE)) {
162                 Vcc = ((DecimalType) bridgeHandler.readDecimalType(sensorId, supplyVoltageParameter)).doubleValue();
163                 State supplyVoltage = new QuantityType<>(Vcc, Units.VOLT);
164                 callback.postUpdate(CHANNEL_SUPPLYVOLTAGE, supplyVoltage);
165             }
166
167             if (enabledChannels.contains(CHANNEL_LIGHT)) {
168                 switch (lightSensorType) {
169                     case ELABNET_V2:
170                         State light = bridgeHandler.readDecimalType(sensorId, currentParamater);
171                         if (light instanceof DecimalType decimalCommand) {
172                             light = new QuantityType<>(
173                                     Math.round(Math.pow(10, decimalCommand.doubleValue() / 47 * 1000)), Units.LUX);
174                             callback.postUpdate(CHANNEL_LIGHT, light);
175                         }
176                         break;
177                     case ELABNET_V1:
178                         light = bridgeHandler.readDecimalType(sensorId, currentParamater);
179                         if (light instanceof DecimalType decimalCommand) {
180                             light = new QuantityType<>(Math.round(Math.exp(
181                                     1.059 * Math.log(1000000 * decimalCommand.doubleValue() / (4096 * 390)) + 4.518)
182                                     * 20000), Units.LUX);
183                             callback.postUpdate(CHANNEL_LIGHT, light);
184                         }
185                         break;
186                     case IBUTTONLINK:
187                         double measured = ((DecimalType) bridgeHandler.readDecimalType(sensorId, voltageParameter))
188                                 .doubleValue();
189                         if (measured <= 0 || measured > 10.0) {
190                             // workaround bug in DS2438
191                             light = new QuantityType<>(0, Units.LUX);
192                         } else {
193                             light = new QuantityType<>(Math.pow(10, (65 / 7.5) - (47 / 7.5) * (Vcc / measured)),
194                                     Units.LUX);
195                         }
196                         callback.postUpdate(CHANNEL_LIGHT, light);
197                 }
198             }
199         }
200     }
201
202     /**
203      * set the type of the attached light sensor
204      *
205      * @param lightSensorType
206      */
207     public void setLightSensorType(LightSensorType lightSensorType) {
208         this.lightSensorType = lightSensorType;
209     }
210
211     /**
212      * set the type of the attached current sensor
213      *
214      * @param currentSensorType
215      */
216     public void setCurrentSensorType(CurrentSensorType currentSensorType) {
217         this.currentSensorType = currentSensorType;
218     }
219 }