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.jeelink.internal.lacrosse;
15 import static org.openhab.binding.jeelink.internal.JeeLinkBindingConstants.*;
16 import static org.openhab.core.library.unit.MetricPrefix.*;
18 import java.math.BigDecimal;
19 import java.math.RoundingMode;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.jeelink.internal.JeeLinkSensorHandler;
23 import org.openhab.binding.jeelink.internal.ReadingPublisher;
24 import org.openhab.binding.jeelink.internal.util.StringUtils;
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;
38 * Handler for a LGW Sensor thing.
40 * @author Volker Bier - Initial contribution
43 public class LgwSensorHandler extends JeeLinkSensorHandler<LgwReading> {
44 private final Logger logger = LoggerFactory.getLogger(LgwSensorHandler.class);
45 private boolean hasHumidityChannel;
46 private boolean hasPressureChannel;
48 public LgwSensorHandler(Thing thing, String sensorType) {
49 super(thing, sensorType);
51 hasHumidityChannel = getThing().getChannel(HUMIDITY_CHANNEL) != null;
52 hasPressureChannel = getThing().getChannel(PRESSURE_CHANNEL) != null;
56 public Class<LgwReading> getReadingClass() {
57 return LgwReading.class;
61 public ReadingPublisher<LgwReading> createPublisher() {
62 return new ReadingPublisher<LgwReading>() {
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);
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));
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(),
81 .withLabel(StringUtils.capitalize(HUMIDITY_CHANNEL)).build());
82 updateThing(thingBuilder.build());
84 hasHumidityChannel = true;
87 updateState(HUMIDITY_CHANNEL, new QuantityType<>(reading.getHumidity(), Units.PERCENT));
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(),
97 .withLabel(StringUtils.capitalize(PRESSURE_CHANNEL)).build());
98 updateThing(thingBuilder.build());
100 hasPressureChannel = true;
103 updateState(PRESSURE_CHANNEL, new QuantityType<>(reading.getPressure(), HECTO(SIUnits.PASCAL)));
109 public void dispose() {