]> git.basschouten.com Git - openhab-addons.git/blob
bb6cf9aa3cad75e327021a25392aacb680317785
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.jeelink.internal.ec3k;
14
15 import static org.openhab.binding.jeelink.internal.JeeLinkBindingConstants.*;
16
17 import java.math.BigDecimal;
18 import java.math.RoundingMode;
19
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;
32
33 /**
34  * Handler for an EC3000 sensor thing.
35  *
36  * @author Volker Bier - Initial contribution
37  */
38 public class Ec3kSensorHandler extends JeeLinkSensorHandler<Ec3kReading> {
39     private final Logger logger = LoggerFactory.getLogger(Ec3kSensorHandler.class);
40
41     public Ec3kSensorHandler(Thing thing, String sensorType) {
42         super(thing, sensorType);
43     }
44
45     @Override
46     public Class<Ec3kReading> getReadingClass() {
47         return Ec3kReading.class;
48     }
49
50     @Override
51     public ReadingPublisher<Ec3kReading> createPublisher() {
52         ReadingPublisher<Ec3kReading> publisher = new ReadingPublisher<Ec3kReading>() {
53             @Override
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);
58
59                     logger.debug(
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());
63
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()));
71                 }
72             }
73
74             @Override
75             public void dispose() {
76             }
77         };
78
79         BufferedSensorConfig cfg = getConfigAs(BufferedSensorConfig.class);
80         if (cfg.bufferSize > 1 && cfg.updateInterval > 0) {
81             publisher = new RollingAveragePublisher<Ec3kReading>(cfg.bufferSize, cfg.updateInterval, publisher,
82                     scheduler) {
83                 @Override
84                 public RollingReadingAverage<Ec3kReading> createRollingReadingAverage(int bufferSize) {
85                     return new Ec3kRollingReadingAverage(bufferSize);
86                 }
87             };
88         }
89
90         return publisher;
91     }
92 }