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.ec3k;
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.binding.jeelink.internal.RollingAveragePublisher;
23 import org.openhab.binding.jeelink.internal.RollingReadingAverage;
24 import org.openhab.binding.jeelink.internal.config.BufferedSensorConfig;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingStatus;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Handler for an EC3000 sensor thing.
36 * @author Volker Bier - Initial contribution
38 public class Ec3kSensorHandler extends JeeLinkSensorHandler<Ec3kReading> {
39 private final Logger logger = LoggerFactory.getLogger(Ec3kSensorHandler.class);
41 public Ec3kSensorHandler(Thing thing, String sensorType) {
42 super(thing, sensorType);
46 public Class<Ec3kReading> getReadingClass() {
47 return Ec3kReading.class;
51 public ReadingPublisher<Ec3kReading> createPublisher() {
52 ReadingPublisher<Ec3kReading> publisher = new ReadingPublisher<Ec3kReading>() {
54 public void publish(Ec3kReading reading) {
55 if (reading != null && getThing().getStatus() == ThingStatus.ONLINE) {
56 BigDecimal currentWatt = new BigDecimal(reading.getCurrentWatt()).setScale(1, RoundingMode.HALF_UP);
57 BigDecimal maxWatt = new BigDecimal(reading.getMaxWatt()).setScale(1, RoundingMode.HALF_UP);
60 "updating states for thing {}: currWatt={} ({}), maxWatt={}, consumption={}, secondsOn={}, secondsTotal={}",
61 getThing().getUID().getId(), currentWatt, reading.getCurrentWatt(), maxWatt,
62 reading.getConsumptionTotal(), reading.getApplianceTime(), reading.getSensorTime());
64 updateState(CURRENT_POWER_CHANNEL, new QuantityType<>(currentWatt, Units.WATT));
65 updateState(MAX_POWER_CHANNEL, new QuantityType<>(maxWatt, Units.WATT));
66 updateState(CONSUMPTION_CHANNEL,
67 new QuantityType<>(reading.getConsumptionTotal(), Units.WATT_HOUR));
68 updateState(APPLIANCE_TIME_CHANNEL, new QuantityType<>(reading.getApplianceTime(), Units.HOUR));
69 updateState(SENSOR_TIME_CHANNEL, new QuantityType<>(reading.getSensorTime(), Units.HOUR));
70 updateState(RESETS_CHANNEL, new DecimalType(reading.getResets()));
75 public void dispose() {
79 BufferedSensorConfig cfg = getConfigAs(BufferedSensorConfig.class);
80 if (cfg.bufferSize > 1 && cfg.updateInterval > 0) {
81 publisher = new RollingAveragePublisher<Ec3kReading>(cfg.bufferSize, cfg.updateInterval, publisher,
84 public RollingReadingAverage<Ec3kReading> createRollingReadingAverage(int bufferSize) {
85 return new Ec3kRollingReadingAverage(bufferSize);