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.handler;
15 import static org.openhab.binding.nikohomecontrol.internal.NikoHomeControlBindingConstants.CHANNEL_POWER;
16 import static org.openhab.core.types.RefreshType.REFRESH;
18 import java.util.HashMap;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.nikohomecontrol.internal.protocol.NhcEnergyMeter;
24 import org.openhab.binding.nikohomecontrol.internal.protocol.NhcEnergyMeterEvent;
25 import org.openhab.binding.nikohomecontrol.internal.protocol.NikoHomeControlCommunication;
26 import org.openhab.binding.nikohomecontrol.internal.protocol.nhc2.NhcEnergyMeter2;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.unit.Units;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingStatusDetail;
34 import org.openhab.core.thing.binding.BaseThingHandler;
35 import org.openhab.core.types.Command;
36 import org.openhab.core.types.UnDefType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link NikoHomeControlEnergyMeterHandler} is responsible for handling commands, which are
42 * sent to one of the channels.
44 * @author Mark Herwege - Initial Contribution
47 public class NikoHomeControlEnergyMeterHandler extends BaseThingHandler implements NhcEnergyMeterEvent {
49 private final Logger logger = LoggerFactory.getLogger(NikoHomeControlEnergyMeterHandler.class);
51 private volatile @Nullable NhcEnergyMeter nhcEnergyMeter;
53 private String energyMeterId = "";
55 public NikoHomeControlEnergyMeterHandler(Thing thing) {
60 public void handleCommand(ChannelUID channelUID, Command command) {
61 NhcEnergyMeter nhcEnergyMeter = this.nhcEnergyMeter;
62 if (nhcEnergyMeter == null) {
63 logger.debug("energy meter with ID {} not initialized", energyMeterId);
67 if (REFRESH.equals(command)) {
68 energyMeterEvent(nhcEnergyMeter.getPower());
73 public void initialize() {
74 NikoHomeControlEnergyMeterConfig config = getConfig().as(NikoHomeControlEnergyMeterConfig.class);
76 energyMeterId = config.energyMeterId;
78 NikoHomeControlCommunication nhcComm = getCommunication();
79 if (nhcComm == null) {
80 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED,
81 "@text/offline.bridge-unitialized");
84 updateStatus(ThingStatus.UNKNOWN);
87 // We need to do this in a separate thread because we may have to wait for the
88 // communication to become active
89 scheduler.submit(() -> {
90 if (!nhcComm.communicationActive()) {
91 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
92 "@text/offline.communication-error");
96 NhcEnergyMeter nhcEnergyMeter = nhcComm.getEnergyMeters().get(energyMeterId);
97 if (nhcEnergyMeter == null) {
98 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
99 "@text/offline.configuration-error.energyMeterId");
103 nhcEnergyMeter.setEventHandler(this);
107 // Subscribing to power readings starts an intensive data flow, therefore only do it when there is an item
108 // linked to the channel
109 if (isLinked(CHANNEL_POWER)) {
110 nhcComm.startEnergyMeter(energyMeterId);
113 this.nhcEnergyMeter = nhcEnergyMeter;
115 logger.debug("energy meter intialized {}", energyMeterId);
117 Bridge bridge = getBridge();
118 if ((bridge != null) && (bridge.getStatus() == ThingStatus.ONLINE)) {
119 updateStatus(ThingStatus.ONLINE);
121 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
127 public void dispose() {
128 NikoHomeControlCommunication nhcComm = getCommunication();
130 if (nhcComm != null) {
131 nhcComm.stopEnergyMeter(energyMeterId);
135 private void updateProperties() {
136 Map<String, String> properties = new HashMap<>();
138 if (nhcEnergyMeter instanceof NhcEnergyMeter2) {
139 NhcEnergyMeter2 energyMeter = (NhcEnergyMeter2) nhcEnergyMeter;
140 properties.put("model", energyMeter.getModel());
141 properties.put("technology", energyMeter.getTechnology());
144 thing.setProperties(properties);
148 public void energyMeterEvent(@Nullable Integer power) {
150 updateState(CHANNEL_POWER, UnDefType.UNDEF);
152 updateState(CHANNEL_POWER, new QuantityType<>(power, Units.WATT));
154 updateStatus(ThingStatus.ONLINE);
158 public void energyMeterInitialized() {
159 Bridge bridge = getBridge();
160 if ((bridge != null) && (bridge.getStatus() == ThingStatus.ONLINE)) {
161 updateStatus(ThingStatus.ONLINE);
166 public void energyMeterRemoved() {
167 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
168 "@text/offline.configuration-error.energyMeterRemoved");
172 // Subscribing to power readings starts an intensive data flow, therefore only do it when there is an item linked to
174 public void channelLinked(ChannelUID channelUID) {
175 NikoHomeControlCommunication nhcComm = getCommunication();
176 if (nhcComm != null) {
177 // This can be expensive, therefore do it in a job.
178 scheduler.submit(() -> {
179 if (!nhcComm.communicationActive()) {
180 restartCommunication(nhcComm);
183 if (nhcComm.communicationActive()) {
184 nhcComm.startEnergyMeter(energyMeterId);
185 updateStatus(ThingStatus.ONLINE);
192 public void channelUnlinked(ChannelUID channelUID) {
193 NikoHomeControlCommunication nhcComm = getCommunication();
194 if (nhcComm != null) {
195 // This can be expensive, therefore do it in a job.
196 scheduler.submit(() -> {
197 if (!nhcComm.communicationActive()) {
198 restartCommunication(nhcComm);
201 if (nhcComm.communicationActive()) {
202 nhcComm.stopEnergyMeter(energyMeterId);
203 // as this is momentary power production/consumption, we set it UNDEF as we do not get readings
205 updateState(CHANNEL_POWER, UnDefType.UNDEF);
206 updateStatus(ThingStatus.ONLINE);
212 private void restartCommunication(NikoHomeControlCommunication nhcComm) {
213 // We lost connection but the connection object is there, so was correctly started.
214 // Try to restart communication.
215 nhcComm.restartCommunication();
216 // If still not active, take thing offline and return.
217 if (!nhcComm.communicationActive()) {
218 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
219 "@text/offline.communication-error");
222 // Also put the bridge back online
223 NikoHomeControlBridgeHandler nhcBridgeHandler = getBridgeHandler();
224 if (nhcBridgeHandler != null) {
225 nhcBridgeHandler.bridgeOnline();
227 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED,
228 "@text/offline.bridge-unitialized");
232 private @Nullable NikoHomeControlCommunication getCommunication() {
233 NikoHomeControlBridgeHandler nhcBridgeHandler = getBridgeHandler();
234 return nhcBridgeHandler != null ? nhcBridgeHandler.getCommunication() : null;
237 private @Nullable NikoHomeControlBridgeHandler getBridgeHandler() {
238 Bridge nhcBridge = getBridge();
239 return nhcBridge != null ? (NikoHomeControlBridgeHandler) nhcBridge.getHandler() : null;