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.openhab.binding.jeelink.internal.JeeLinkSensorHandler;
22 import org.openhab.binding.jeelink.internal.ReadingPublisher;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.unit.SIUnits;
26 import org.openhab.core.library.unit.Units;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Handler for a TX22 Temperature/Humidity Sensor thing.
35 * @author Volker Bier - Initial contribution
37 public class Tx22SensorHandler extends JeeLinkSensorHandler<Tx22Reading> {
38 private final Logger logger = LoggerFactory.getLogger(Tx22SensorHandler.class);
40 public Tx22SensorHandler(Thing thing, String sensorType) {
41 super(thing, sensorType);
45 public Class<Tx22Reading> getReadingClass() {
46 return Tx22Reading.class;
50 public ReadingPublisher<Tx22Reading> createPublisher() {
51 return new ReadingPublisher<Tx22Reading>() {
53 public void publish(Tx22Reading reading) {
54 if (reading != null && getThing().getStatus() == ThingStatus.ONLINE) {
55 logger.debug("updating states for thing {} ({}): {}", getThing().getLabel(),
56 getThing().getUID().getId(), reading);
58 updateState(BATTERY_NEW_CHANNEL, reading.isBatteryNew() ? OnOffType.ON : OnOffType.OFF);
59 updateState(BATTERY_LOW_CHANNEL, reading.isBatteryLow() ? OnOffType.ON : OnOffType.OFF);
61 if (reading.hasTemperature()) {
62 BigDecimal temp = new BigDecimal(reading.getTemperature()).setScale(1, RoundingMode.HALF_UP);
63 updateState(TEMPERATURE_CHANNEL, new QuantityType<>(temp, SIUnits.CELSIUS));
65 if (reading.hasHumidity()) {
66 updateState(HUMIDITY_CHANNEL, new QuantityType<>(reading.getHumidity(), Units.PERCENT));
68 if (reading.hasRain()) {
69 updateState(RAIN_CHANNEL, new QuantityType<>(reading.getRain(), MILLI(SIUnits.METRE)));
71 if (reading.hasPressure()) {
72 updateState(PRESSURE_CHANNEL, new QuantityType<>(reading.getPressure(), HECTO(SIUnits.PASCAL)));
74 if (reading.hasWindDirection()) {
75 updateState(WIND_ANGLE_CHANNEL,
76 new QuantityType<>(reading.getWindDirection(), Units.DEGREE_ANGLE));
78 if (reading.hasWindSpeed()) {
79 updateState(WIND_STENGTH_CHANNEL,
80 new QuantityType<>(reading.getWindSpeed(), Units.METRE_PER_SECOND));
82 if (reading.hasWindGust()) {
83 updateState(GUST_STRENGTH_CHANNEL,
84 new QuantityType<>(reading.getWindGust(), Units.METRE_PER_SECOND));
90 public void dispose() {