2 * Copyright (c) 2010-2024 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.nibeuplink.internal.handler;
15 import java.util.HashSet;
18 import java.util.concurrent.Future;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.atomic.AtomicReference;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.jetty.client.HttpClient;
25 import org.openhab.binding.nibeuplink.internal.AtomicReferenceTrait;
26 import org.openhab.binding.nibeuplink.internal.NibeUplinkBindingConstants;
27 import org.openhab.binding.nibeuplink.internal.command.UpdateSetting;
28 import org.openhab.binding.nibeuplink.internal.config.NibeUplinkConfiguration;
29 import org.openhab.binding.nibeuplink.internal.connector.UplinkWebInterface;
30 import org.openhab.core.thing.Channel;
31 import org.openhab.core.thing.ChannelGroupUID;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusDetail;
36 import org.openhab.core.thing.ThingUID;
37 import org.openhab.core.thing.binding.BaseThingHandler;
38 import org.openhab.core.thing.type.ChannelTypeUID;
39 import org.openhab.core.types.Command;
40 import org.openhab.core.types.RefreshType;
41 import org.openhab.core.types.State;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * The {@link UplinkBaseHandler} is responsible for handling commands, which are
47 * sent to one of the channels.
49 * @author Alexander Friese - initial contribution
52 public abstract class UplinkBaseHandler extends BaseThingHandler implements NibeUplinkHandler, AtomicReferenceTrait {
53 private final Logger logger = LoggerFactory.getLogger(UplinkBaseHandler.class);
55 private final long POLLING_INITIAL_DELAY = 30;
56 private final long HOUSE_KEEPING_INITIAL_DELAY = 300;
58 private final Set<Channel> deadChannels = new HashSet<>(100);
59 private final Set<ChannelGroupUID> registeredGroups = new HashSet<>(10);
62 * Interface object for querying the NibeUplink web interface
64 private UplinkWebInterface webInterface;
67 * Schedule for polling
69 private final AtomicReference<@Nullable Future<?>> pollingJobReference;
72 * Schedule for periodic cleaning dead channel list
74 private final AtomicReference<@Nullable Future<?>> deadChannelHouseKeepingReference;
76 public UplinkBaseHandler(Thing thing, HttpClient httpClient) {
78 this.webInterface = new UplinkWebInterface(scheduler, this, httpClient);
79 this.pollingJobReference = new AtomicReference<>(null);
80 this.deadChannelHouseKeepingReference = new AtomicReference<>(null);
84 public void handleCommand(ChannelUID channelUID, Command command) {
85 if (!(command instanceof RefreshType)) {
86 logger.debug("command for {}: {}", channelUID.getIdWithoutGroup(), command.toString());
87 Channel channel = getSpecificChannel(channelUID.getIdWithoutGroup());
88 if (channel != null) {
89 ChannelTypeUID typeUID = channel.getChannelTypeUID();
90 if (typeUID != null && typeUID.getId().startsWith(NibeUplinkBindingConstants.RW_CHANNEL_PREFIX)) {
91 webInterface.enqueueCommand(new UpdateSetting(this, channel, command));
98 public void initialize() {
99 logger.debug("About to initialize NibeUplink");
100 NibeUplinkConfiguration config = getConfiguration();
101 logger.debug("NibeUplink initialized with configuration: {}", config);
103 registeredGroups.clear();
104 validateChannelsAndRegisterGroups();
107 webInterface.start();
108 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "waiting for web api login");
112 * initialize the channels out of the configuration
115 private void validateChannelsAndRegisterGroups() {
116 logger.debug("Validating {} channels", getThing().getChannels().size());
117 for (Channel channel : getThing().getChannels()) {
118 if (!ChannelUtil.isValidNibeChannel(channel)) {
119 logger.warn("Channel {} is not a valid Nibe channel ({})", channel.getUID().getIdWithoutGroup(),
121 deadChannels.add(channel);
123 logger.debug("Successfully validated channel {} ({})", channel.getUID().getIdWithoutGroup(),
125 String groupId = channel.getUID().getGroupId();
126 if (groupId != null) {
127 ThingUID thingUID = this.getThing().getUID();
128 if (registeredGroups.add(new ChannelGroupUID(thingUID, groupId))) {
129 logger.debug("Successfully registered channel-group '{}'", groupId);
139 private void startPolling() {
140 updateJobReference(pollingJobReference, scheduler.scheduleWithFixedDelay(new UplinkPolling(this),
141 POLLING_INITIAL_DELAY, getConfiguration().getPollingInterval(), TimeUnit.SECONDS));
142 updateJobReference(deadChannelHouseKeepingReference, scheduler.scheduleWithFixedDelay(deadChannels::clear,
143 HOUSE_KEEPING_INITIAL_DELAY, getConfiguration().getHouseKeepingInterval(), TimeUnit.SECONDS));
147 * Disposes the bridge.
150 public void dispose() {
151 logger.debug("Handler disposed.");
153 cancelJobReference(pollingJobReference);
154 cancelJobReference(deadChannelHouseKeepingReference);
156 // the webinterface also makes use of the scheduler and must stop it's jobs
157 webInterface.dispose();
161 public UplinkWebInterface getWebInterface() {
166 * will update all channels provided in the map
168 * @param values map containing the data updates
171 public void updateChannelStatus(Map<Channel, State> values) {
172 logger.debug("Handling channel update. ({} Channels)", values.size());
174 for (Channel channel : values.keySet()) {
175 if (getChannels().contains(channel)) {
176 State value = values.get(channel);
178 logger.debug("Channel is to be updated: {}: {}", channel.getUID().getAsString(), value);
179 updateState(channel.getUID(), value);
182 logger.debug("Could not identify channel: {} for model {}", channel.getUID().getAsString(),
183 getThing().getThingTypeUID().getAsString());
189 public Set<Channel> getDeadChannels() {
194 public void setStatusInfo(ThingStatus status, ThingStatusDetail statusDetail, @Nullable String description) {
195 super.updateStatus(status, statusDetail, description);
199 public NibeUplinkConfiguration getConfiguration() {
200 return this.getConfigAs(NibeUplinkConfiguration.class);
203 public Set<ChannelGroupUID> getRegisteredGroups() {
204 return registeredGroups;