]> git.basschouten.com Git - openhab-addons.git/blob
a3885304ee93093c6df5cef141c01715ab9f51d8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.jeelink.internal.lacrosse;
14
15 import static org.openhab.binding.jeelink.internal.JeeLinkBindingConstants.*;
16 import static org.openhab.core.library.unit.MetricPrefix.*;
17
18 import java.math.BigDecimal;
19 import java.math.RoundingMode;
20 import java.util.Objects;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.jeelink.internal.JeeLinkSensorHandler;
24 import org.openhab.binding.jeelink.internal.ReadingPublisher;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.unit.SIUnits;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.binding.builder.ChannelBuilder;
32 import org.openhab.core.thing.binding.builder.ThingBuilder;
33 import org.openhab.core.thing.type.ChannelTypeUID;
34 import org.openhab.core.util.StringUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Handler for a LGW Sensor thing.
40  *
41  * @author Volker Bier - Initial contribution
42  */
43 @NonNullByDefault
44 public class LgwSensorHandler extends JeeLinkSensorHandler<LgwReading> {
45     private final Logger logger = LoggerFactory.getLogger(LgwSensorHandler.class);
46     private boolean hasHumidityChannel;
47     private boolean hasPressureChannel;
48
49     public LgwSensorHandler(Thing thing, String sensorType) {
50         super(thing, sensorType);
51
52         hasHumidityChannel = getThing().getChannel(HUMIDITY_CHANNEL) != null;
53         hasPressureChannel = getThing().getChannel(PRESSURE_CHANNEL) != null;
54     }
55
56     @Override
57     public Class<LgwReading> getReadingClass() {
58         return LgwReading.class;
59     }
60
61     @Override
62     public ReadingPublisher<LgwReading> createPublisher() {
63         return new ReadingPublisher<LgwReading>() {
64             @Override
65             public void publish(LgwReading reading) {
66                 if (reading != null && getThing().getStatus() == ThingStatus.ONLINE) {
67                     logger.debug("updating states for thing {} ({}): {}", getThing().getLabel(),
68                             getThing().getUID().getId(), reading);
69
70                     if (reading.hasTemperature()) {
71                         BigDecimal temp = new BigDecimal(reading.getTemperature()).setScale(1, RoundingMode.HALF_UP);
72                         updateState(TEMPERATURE_CHANNEL, new QuantityType<>(temp, SIUnits.CELSIUS));
73                     }
74
75                     if (reading.hasHumidity()) {
76                         if (!hasHumidityChannel) {
77                             ThingBuilder thingBuilder = editThing();
78                             thingBuilder.withChannel(ChannelBuilder
79                                     .create(new ChannelUID(getThing().getUID(), HUMIDITY_CHANNEL), "Number:Humidity")
80                                     .withType(new ChannelTypeUID(getThing().getThingTypeUID().getBindingId(),
81                                             HUMIDITY_CHANNEL))
82                                     .withLabel(Objects.requireNonNull(StringUtils.capitalize(HUMIDITY_CHANNEL)))
83                                     .build());
84                             updateThing(thingBuilder.build());
85
86                             hasHumidityChannel = true;
87                         }
88
89                         updateState(HUMIDITY_CHANNEL, new QuantityType<>(reading.getHumidity(), Units.PERCENT));
90                     }
91
92                     if (reading.hasPressure()) {
93                         if (!hasPressureChannel) {
94                             ThingBuilder thingBuilder = editThing();
95                             thingBuilder.withChannel(ChannelBuilder
96                                     .create(new ChannelUID(getThing().getUID(), PRESSURE_CHANNEL), "Number:Pressure")
97                                     .withType(new ChannelTypeUID(getThing().getThingTypeUID().getBindingId(),
98                                             PRESSURE_CHANNEL))
99                                     .withLabel(Objects.requireNonNull(StringUtils.capitalize(PRESSURE_CHANNEL)))
100                                     .build());
101                             updateThing(thingBuilder.build());
102
103                             hasPressureChannel = true;
104                         }
105
106                         updateState(PRESSURE_CHANNEL, new QuantityType<>(reading.getPressure(), HECTO(SIUnits.PASCAL)));
107                     }
108                 }
109             }
110
111             @Override
112             public void dispose() {
113             }
114         };
115     }
116 }