2 * Copyright (c) 2010-2021 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.danfossairunit.internal;
15 import static org.openhab.binding.danfossairunit.internal.DanfossAirUnitBindingConstants.*;
17 import java.io.IOException;
18 import java.net.InetAddress;
19 import java.net.UnknownHostException;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.thing.binding.BaseThingHandler;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.RefreshType;
32 import org.openhab.core.types.State;
33 import org.openhab.core.types.UnDefType;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link DanfossAirUnitHandler} is responsible for handling commands, which are
39 * sent to one of the channels.
41 * @author Ralf Duckstein - Initial contribution
42 * @author Robert Bach - heavy refactorings
43 * @author Jacob Laursen - Refactoring, bugfixes and enhancements
46 public class DanfossAirUnitHandler extends BaseThingHandler {
48 private static final int TCP_PORT = 30046;
49 private static final int POLLING_INTERVAL_SECONDS = 5;
50 private final Logger logger = LoggerFactory.getLogger(DanfossAirUnitHandler.class);
51 private @NonNullByDefault({}) DanfossAirUnitConfiguration config;
52 private @Nullable ValueCache valueCache;
53 private @Nullable ScheduledFuture<?> pollingJob;
54 private @Nullable DanfossAirUnitCommunicationController communicationController;
55 private @Nullable DanfossAirUnit airUnit;
57 public DanfossAirUnitHandler(Thing thing) {
62 public void handleCommand(ChannelUID channelUID, Command command) {
63 if (command instanceof RefreshType) {
67 DanfossAirUnit localAirUnit = this.airUnit;
68 if (localAirUnit != null) {
69 Channel channel = Channel.getByName(channelUID.getIdWithoutGroup());
70 DanfossAirUnitWriteAccessor writeAccessor = channel.getWriteAccessor();
71 if (writeAccessor != null) {
72 updateState(channelUID, writeAccessor.access(localAirUnit, command));
75 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.NONE,
76 "Air unit connection not initialized.");
79 } catch (IllegalArgumentException e) {
80 logger.debug("Ignoring unknown channel id: {}", channelUID.getIdWithoutGroup(), e);
81 } catch (IOException ioe) {
82 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, ioe.getMessage());
88 public void initialize() {
89 updateStatus(ThingStatus.UNKNOWN);
90 config = getConfigAs(DanfossAirUnitConfiguration.class);
91 valueCache = new ValueCache(config.updateUnchangedValuesEveryMillis);
93 var localCommunicationController = new DanfossAirUnitCommunicationController(
94 InetAddress.getByName(config.host), TCP_PORT);
95 this.communicationController = localCommunicationController;
96 var localAirUnit = new DanfossAirUnit(localCommunicationController);
97 this.airUnit = localAirUnit;
98 scheduler.execute(() -> {
100 thing.setProperty(PROPERTY_UNIT_NAME, localAirUnit.getUnitName());
101 thing.setProperty(PROPERTY_SERIAL, localAirUnit.getUnitSerialNumber());
103 updateStatus(ThingStatus.ONLINE);
104 } catch (IOException e) {
105 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
108 } catch (UnknownHostException e) {
109 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
110 "Unknown host: " + config.host);
115 private void updateAllChannels() {
116 DanfossAirUnit localAirUnit = this.airUnit;
117 if (localAirUnit == null) {
121 logger.debug("Updating DanfossHRV data '{}'", getThing().getUID());
123 for (Channel channel : Channel.values()) {
124 if (Thread.interrupted()) {
125 logger.debug("Polling thread interrupted...");
129 updateState(channel.getGroup().getGroupName(), channel.getChannelName(),
130 channel.getReadAccessor().access(localAirUnit));
131 if (getThing().getStatus() == ThingStatus.OFFLINE) {
132 updateStatus(ThingStatus.ONLINE);
134 } catch (UnexpectedResponseValueException e) {
135 updateState(channel.getGroup().getGroupName(), channel.getChannelName(), UnDefType.UNDEF);
137 "Cannot update channel {}: an unexpected or invalid response has been received from the air unit: {}",
138 channel.getChannelName(), e.getMessage());
139 if (getThing().getStatus() == ThingStatus.OFFLINE) {
140 updateStatus(ThingStatus.ONLINE);
142 } catch (IOException e) {
143 updateState(channel.getGroup().getGroupName(), channel.getChannelName(), UnDefType.UNDEF);
144 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
145 logger.debug("Cannot update channel {}: an error occurred retrieving the value: {}",
146 channel.getChannelName(), e.getMessage());
152 public void dispose() {
153 logger.debug("Disposing Danfoss HRV handler '{}'", getThing().getUID());
159 DanfossAirUnitCommunicationController localCommunicationController = this.communicationController;
160 if (localCommunicationController != null) {
161 localCommunicationController.disconnect();
163 this.communicationController = null;
166 private synchronized void startPolling() {
167 this.pollingJob = scheduler.scheduleWithFixedDelay(this::updateAllChannels, POLLING_INTERVAL_SECONDS,
168 config.refreshInterval, TimeUnit.SECONDS);
171 private synchronized void stopPolling() {
172 ScheduledFuture<?> localPollingJob = this.pollingJob;
173 if (localPollingJob != null) {
174 localPollingJob.cancel(true);
176 this.pollingJob = null;
179 private void updateState(String groupId, String channelId, State state) {
180 ValueCache cache = valueCache;
185 if (cache.updateValue(channelId, state)) {
186 updateState(new ChannelUID(thing.getUID(), groupId, channelId), state);