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.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
23 * contains all fields representing a Niko Home Control energyMeters meter and has methods to receive energyMeters usage
24 * information. A specific implementation is {@link NhcEnergyMeter2}.
26 * @author Mark Herwege - Initial Contribution
29 public abstract class NhcEnergyMeter {
31 private final Logger logger = LoggerFactory.getLogger(NhcEnergyMeter.class);
33 protected NikoHomeControlCommunication nhcComm;
36 protected String name;
37 protected @Nullable String location;
38 // This can be null as long as we do not receive power readings
39 protected volatile @Nullable Integer power = null;
41 private @Nullable NhcEnergyMeterEvent eventHandler;
43 protected NhcEnergyMeter(String id, String name, @Nullable String location, NikoHomeControlCommunication nhcComm) {
46 this.location = location;
47 this.nhcComm = nhcComm;
51 * Update all values of the energyMeters meter without touching the energyMeters meter definition (id, name) and
52 * without changing the ThingHandler callback.
54 * @param power current power consumption/production in W (positive for consumption)
56 public void updateState(int power) {
57 NhcEnergyMeterEvent handler = eventHandler;
58 if (handler != null) {
59 logger.debug("update channel for {}", id);
60 handler.energyMeterEvent(power);
65 * Method called when energyMeters meter is removed from the Niko Home Control Controller.
67 public void energyMeterRemoved() {
68 logger.debug("action removed {}, {}", id, name);
69 NhcEnergyMeterEvent eventHandler = this.eventHandler;
70 if (eventHandler != null) {
71 eventHandler.energyMeterRemoved();
77 * This method should be called when an object implementing the {@link NhcEnergyMeterEvent} interface is
79 * It keeps a record of the event handler in that object so it can be updated when the action receives an update
80 * from the Niko Home Control IP-interface.
84 public void setEventHandler(NhcEnergyMeterEvent eventHandler) {
85 this.eventHandler = eventHandler;
89 * This method should be called when an object implementing the {@link NhcEnergyMeterEvent} interface is disposed.
90 * It resets the reference, so no updates go to the handler anymore.
93 public void unsetEventHandler() {
94 this.eventHandler = null;
102 public String getId() {
109 * @return energyMeter name
111 public String getName() {
118 * @param name meter name
120 public void setName(String name) {
125 * Get location name of meter.
127 * @return location energyMeter location
129 public @Nullable String getLocation() {
134 * Set location name of meter.
136 * @param location meter location name
138 public void setLocation(@Nullable String location) {
139 this.location = location;
143 * @return the power in W (positive for consumption, negative for production), return null if no reading received
146 public @Nullable Integer getPower() {
151 * @param power the power to set in W (positive for consumption, negative for production), null if an empty reading
154 public void setPower(@Nullable Integer power) {
156 NhcEnergyMeterEvent handler = eventHandler;
157 if (handler != null) {
158 logger.debug("update power channel for {} with {}", id, power);
159 handler.energyMeterEvent(power);