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();
78 * This method should be called when an object implementing the {@NhcEnergyMeterEvent} interface is initialized.
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;
93 public String getId() {
100 * @return energyMeter name
102 public String getName() {
109 * @param name meter name
111 public void setName(String name) {
116 * Get location name of meter.
118 * @return location energyMeter location
120 public @Nullable String getLocation() {
125 * Set location name of meter.
127 * @param location meter location name
129 public void setLocation(@Nullable String location) {
130 this.location = location;
134 * @return the power in W (positive for consumption, negative for production), return null if no reading received
137 public @Nullable Integer getPower() {
142 * @param power the power to set in W (positive for consumption, negative for production), null if an empty reading
145 public void setPower(@Nullable Integer power) {
147 NhcEnergyMeterEvent handler = eventHandler;
148 if (handler != null) {
149 logger.debug("update power channel for {} with {}", id, power);
150 handler.energyMeterEvent(power);