2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.onewire.internal.device;
15 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
16 import static org.openhab.core.library.unit.MetricPrefix.MILLI;
18 import javax.measure.quantity.Dimensionless;
19 import javax.measure.quantity.Temperature;
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;
41 * The {@link DS2438} class defines a DS2438 device
43 * @author Jan N. Klug - Initial contribution
46 public class DS2438 extends AbstractOwDevice {
47 private final Logger logger = LoggerFactory.getLogger(DS2438.class);
49 public enum LightSensorType {
55 public enum CurrentSensorType {
60 private LightSensorType lightSensorType = LightSensorType.ELABNET_V1;
61 private CurrentSensorType currentSensorType = CurrentSensorType.INTERNAL;
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");
69 public DS2438(SensorId sensorId, OwBaseThingHandler callback) {
70 super(sensorId, callback);
74 public void configureChannels() {
75 Thing thing = callback.getThing();
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));
83 humidityParameter = new OwserverDeviceParameter("/humidity");
91 public void refresh(OwserverBridgeHandler bridgeHandler, Boolean forcedRefresh) throws OwException {
93 logger.trace("refresh of sensor {} started", sensorId);
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);
103 if (enabledChannels.contains(CHANNEL_TEMPERATURE)) {
104 callback.postUpdate(CHANNEL_TEMPERATURE, temperature);
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);
113 if (enabledChannels.contains(CHANNEL_HUMIDITY)) {
114 callback.postUpdate(CHANNEL_HUMIDITY, humidity);
117 if (enabledChannels.contains(CHANNEL_ABSOLUTE_HUMIDITY)) {
118 callback.postUpdate(CHANNEL_ABSOLUTE_HUMIDITY,
119 Util.calculateAbsoluteHumidity(temperature, humidity));
122 if (enabledChannels.contains(CHANNEL_DEWPOINT)) {
123 callback.postUpdate(CHANNEL_DEWPOINT, Util.calculateDewpoint(temperature, humidity));
128 if (enabledChannels.contains(CHANNEL_VOLTAGE)) {
129 double measured = ((DecimalType) bridgeHandler.readDecimalType(sensorId, voltageParameter))
131 if (measured < 0 || measured > 10.0) {
132 // workaround bug in DS2438
135 State voltage = new QuantityType<>(measured, Units.VOLT);
137 logger.trace("read voltage {} from {}", voltage, sensorId);
138 callback.postUpdate(CHANNEL_VOLTAGE, voltage);
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);
149 callback.postUpdate(CHANNEL_CURRENT, current);
151 callback.postUpdate(CHANNEL_CURRENT, UnDefType.UNDEF);
154 State current = new QuantityType<>(
155 (DecimalType) bridgeHandler.readDecimalType(sensorId, currentParamater),
156 MILLI(Units.AMPERE));
157 callback.postUpdate(CHANNEL_CURRENT, current);
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);
167 if (enabledChannels.contains(CHANNEL_LIGHT)) {
168 switch (lightSensorType) {
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);
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);
187 double measured = ((DecimalType) bridgeHandler.readDecimalType(sensorId, voltageParameter))
189 if (measured <= 0 || measured > 10.0) {
190 // workaround bug in DS2438
191 light = new QuantityType<>(0, Units.LUX);
193 light = new QuantityType<>(Math.pow(10, (65 / 7.5) - (47 / 7.5) * (Vcc / measured)),
196 callback.postUpdate(CHANNEL_LIGHT, light);
203 * set the type of the attached light sensor
205 * @param lightSensorType
207 public void setLightSensorType(LightSensorType lightSensorType) {
208 this.lightSensorType = lightSensorType;
212 * set the type of the attached current sensor
214 * @param currentSensorType
216 public void setCurrentSensorType(CurrentSensorType currentSensorType) {
217 this.currentSensorType = currentSensorType;