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