]> git.basschouten.com Git - openhab-addons.git/blob
7de65e62a6c599f862616c93ef42994feae1c09b
[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.Temperature;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.onewire.internal.OwException;
21 import org.openhab.binding.onewire.internal.SensorId;
22 import org.openhab.binding.onewire.internal.handler.OwBaseThingHandler;
23 import org.openhab.binding.onewire.internal.handler.OwserverBridgeHandler;
24 import org.openhab.binding.onewire.internal.owserver.OwserverDeviceParameter;
25 import org.openhab.core.config.core.Configuration;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.unit.SIUnits;
29 import org.openhab.core.thing.Channel;
30 import org.openhab.core.thing.Thing;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * The {@link DS18x20} class defines a DS18x20 or DS1822 device
36  *
37  * @author Jan N. Klug - Initial contribution
38  */
39 @NonNullByDefault
40 public class DS18x20 extends AbstractOwDevice {
41     private final Logger logger = LoggerFactory.getLogger(DS18x20.class);
42
43     private OwserverDeviceParameter temperatureParameter = new OwserverDeviceParameter("/temperature");
44
45     private boolean ignorePOR = false;
46
47     public DS18x20(SensorId sensorId, OwBaseThingHandler callback) {
48         super(sensorId, callback);
49     }
50
51     @Override
52     public void configureChannels() throws OwException {
53         Thing thing = callback.getThing();
54         Channel temperatureChannel = thing.getChannel(CHANNEL_TEMPERATURE);
55
56         if (temperatureChannel != null) {
57             Configuration channelConfiguration = temperatureChannel.getConfiguration();
58             if (channelConfiguration.containsKey(CONFIG_RESOLUTION)) {
59                 temperatureParameter = new OwserverDeviceParameter(
60                         "/temperature" + (String) channelConfiguration.get(CONFIG_RESOLUTION));
61             } else {
62                 temperatureParameter = new OwserverDeviceParameter("/temperature");
63             }
64             if (channelConfiguration.containsKey(CONFIG_IGNORE_POR)) {
65                 ignorePOR = (Boolean) channelConfiguration.get(CONFIG_IGNORE_POR);
66             } else {
67                 ignorePOR = false;
68             }
69         } else {
70             throw new OwException(CHANNEL_TEMPERATURE + " not found");
71         }
72
73         isConfigured = true;
74     }
75
76     @Override
77     public void refresh(OwserverBridgeHandler bridgeHandler, Boolean forcedRefresh) throws OwException {
78         if (isConfigured && enabledChannels.contains(CHANNEL_TEMPERATURE)) {
79             logger.trace("refresh of sensor {} started", sensorId);
80             QuantityType<Temperature> temperature = new QuantityType<>(
81                     (DecimalType) bridgeHandler.readDecimalType(sensorId, temperatureParameter), SIUnits.CELSIUS);
82             logger.trace("read temperature {} from {}", temperature, sensorId);
83             if (ignorePOR && (Double.compare(temperature.doubleValue(), 85.0) == 0)) {
84                 logger.trace("ignored POR value from sensor {}", sensorId);
85             } else {
86                 callback.postUpdate(CHANNEL_TEMPERATURE, temperature);
87             }
88         }
89     }
90 }