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.revolt;
15 import static org.openhab.binding.jeelink.internal.JeeLinkBindingConstants.*;
17 import java.math.BigDecimal;
18 import java.math.RoundingMode;
20 import org.openhab.binding.jeelink.internal.JeeLinkSensorHandler;
21 import org.openhab.binding.jeelink.internal.ReadingPublisher;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.library.unit.Units;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Handler for a Revolt Energy Meter sensor thing.
33 * @author Volker Bier - Initial contribution
35 public class RevoltSensorHandler extends JeeLinkSensorHandler<RevoltReading> {
36 private final Logger logger = LoggerFactory.getLogger(RevoltSensorHandler.class);
38 public RevoltSensorHandler(Thing thing, String sensorType) {
39 super(thing, sensorType);
43 public Class<RevoltReading> getReadingClass() {
44 return RevoltReading.class;
48 public ReadingPublisher<RevoltReading> createPublisher() {
49 ReadingPublisher<RevoltReading> publisher = new ReadingPublisher<RevoltReading>() {
51 public void publish(RevoltReading reading) {
52 if (reading != null && getThing().getStatus() == ThingStatus.ONLINE) {
53 BigDecimal power = new BigDecimal(reading.getPower()).setScale(1, RoundingMode.HALF_UP);
54 BigDecimal powerFactor = new BigDecimal(reading.getPowerFactor()).setScale(2, RoundingMode.HALF_UP);
55 BigDecimal consumption = new BigDecimal(reading.getConsumption()).setScale(2, RoundingMode.HALF_UP);
56 BigDecimal current = new BigDecimal(reading.getCurrent()).setScale(2, RoundingMode.HALF_UP);
59 "updating states for thing {}: power={}, powerFactor={}, consumption={}, current={}, voltage={}, frequency={} ",
60 getThing().getUID().getId(), power, powerFactor, consumption, current, reading.getVoltage(),
61 reading.getFrequency());
63 updateState(CURRENT_POWER_CHANNEL, new QuantityType<>(power, Units.WATT));
64 updateState(POWER_FACTOR_CHANNEL, new DecimalType(powerFactor));
65 updateState(CONSUMPTION_CHANNEL, new QuantityType<>(consumption, Units.WATT_HOUR));
66 updateState(ELECTRIC_CURRENT_CHANNEL, new QuantityType<>(current, Units.AMPERE));
67 updateState(ELECTRIC_POTENTIAL_CHANNEL, new QuantityType<>(reading.getVoltage(), Units.VOLT));
68 updateState(FREQUENCY_CHANNEL, new QuantityType<>(reading.getFrequency(), Units.HERTZ));
73 public void dispose() {