2 * Copyright (c) 2010-2022 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.nikohomecontrol.internal.protocol;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.nikohomecontrol.internal.protocol.nhc2.NhcEnergyMeter2;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * The {@link NhcEnergyMeter} class represents the energyMeters metering Niko Home Control communication object. It
24 * fields representing a Niko Home Control energyMeters meter and has methods to receive energyMeters usage information.
26 * implementation is {@link NhcEnergyMeter2}.
28 * @author Mark Herwege - Initial Contribution
31 public abstract class NhcEnergyMeter {
33 private final Logger logger = LoggerFactory.getLogger(NhcEnergyMeter.class);
35 protected NikoHomeControlCommunication nhcComm;
38 protected String name;
39 protected @Nullable String location;
40 // This can be null as long as we do not receive power readings
41 protected volatile @Nullable Integer power = null;
43 private @Nullable NhcEnergyMeterEvent eventHandler;
45 protected NhcEnergyMeter(String id, String name, @Nullable String location, NikoHomeControlCommunication nhcComm) {
48 this.location = location;
49 this.nhcComm = nhcComm;
53 * Update all values of the energyMeters meter without touching the energyMeters meter definition (id, name) and
54 * without changing the ThingHandler callback.
56 * @param power current power consumption/production in W (positive for consumption)
58 public void updateState(int power) {
59 NhcEnergyMeterEvent handler = eventHandler;
60 if (handler != null) {
61 logger.debug("update channel for {}", id);
62 handler.energyMeterEvent(power);
67 * Method called when energyMeters meter is removed from the Niko Home Control Controller.
69 public void energyMeterRemoved() {
70 logger.debug("action removed {}, {}", id, name);
71 NhcEnergyMeterEvent eventHandler = this.eventHandler;
72 if (eventHandler != null) {
73 eventHandler.energyMeterRemoved();
79 * This method should be called when an object implementing the {@NhcEnergyMeterEvent} interface is initialized.
80 * It keeps a record of the event handler in that object so it can be updated when the action receives an update
81 * from the Niko Home Control IP-interface.
85 public void setEventHandler(NhcEnergyMeterEvent eventHandler) {
86 this.eventHandler = eventHandler;
90 * This method should be called when an object implementing the {@NhcEnergyMeterEvent} interface is disposed.
91 * It resets the reference, so no updates go to the handler anymore.
94 public void unsetEventHandler() {
95 this.eventHandler = null;
103 public String getId() {
110 * @return energyMeter name
112 public String getName() {
119 * @param name meter name
121 public void setName(String name) {
126 * Get location name of meter.
128 * @return location energyMeter location
130 public @Nullable String getLocation() {
135 * Set location name of meter.
137 * @param location meter location name
139 public void setLocation(@Nullable String location) {
140 this.location = location;
144 * @return the power in W (positive for consumption, negative for production), return null if no reading received
147 public @Nullable Integer getPower() {
152 * @param power the power to set in W (positive for consumption, negative for production), null if an empty reading
155 public void setPower(@Nullable Integer power) {
157 NhcEnergyMeterEvent handler = eventHandler;
158 if (handler != null) {
159 logger.debug("update power channel for {} with {}", id, power);
160 handler.energyMeterEvent(power);