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;
20 import java.util.Objects;
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;
39 * Handler for a LGW Sensor thing.
41 * @author Volker Bier - Initial contribution
44 public class LgwSensorHandler extends JeeLinkSensorHandler<LgwReading> {
45 private final Logger logger = LoggerFactory.getLogger(LgwSensorHandler.class);
46 private boolean hasHumidityChannel;
47 private boolean hasPressureChannel;
49 public LgwSensorHandler(Thing thing, String sensorType) {
50 super(thing, sensorType);
52 hasHumidityChannel = getThing().getChannel(HUMIDITY_CHANNEL) != null;
53 hasPressureChannel = getThing().getChannel(PRESSURE_CHANNEL) != null;
57 public Class<LgwReading> getReadingClass() {
58 return LgwReading.class;
62 public ReadingPublisher<LgwReading> createPublisher() {
63 return new ReadingPublisher<LgwReading>() {
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);
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));
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(),
82 .withLabel(Objects.requireNonNull(StringUtils.capitalize(HUMIDITY_CHANNEL)))
84 updateThing(thingBuilder.build());
86 hasHumidityChannel = true;
89 updateState(HUMIDITY_CHANNEL, new QuantityType<>(reading.getHumidity(), Units.PERCENT));
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(),
99 .withLabel(Objects.requireNonNull(StringUtils.capitalize(PRESSURE_CHANNEL)))
101 updateThing(thingBuilder.build());
103 hasPressureChannel = true;
106 updateState(PRESSURE_CHANNEL, new QuantityType<>(reading.getPressure(), HECTO(SIUnits.PASCAL)));
112 public void dispose() {