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.draytonwiser.internal.handler;
15 import java.util.Collection;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.openhab.binding.draytonwiser.internal.DraytonWiserRefreshListener;
25 import org.openhab.binding.draytonwiser.internal.api.DraytonWiserApi;
26 import org.openhab.binding.draytonwiser.internal.api.DraytonWiserApiException;
27 import org.openhab.binding.draytonwiser.internal.discovery.DraytonWiserDiscoveryService;
28 import org.openhab.binding.draytonwiser.internal.model.DeviceDTO;
29 import org.openhab.binding.draytonwiser.internal.model.DomainDTO;
30 import org.openhab.binding.draytonwiser.internal.model.DraytonWiserDTO;
31 import org.openhab.core.cache.ExpiringCache;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingStatus;
36 import org.openhab.core.thing.ThingStatusDetail;
37 import org.openhab.core.thing.binding.BaseBridgeHandler;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.thing.binding.ThingHandlerService;
40 import org.openhab.core.thing.util.ThingHandlerHelper;
41 import org.openhab.core.types.Command;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * The {@link HeatHubHandler} is responsible for handling commands, which are
47 * sent to one of the channels.
49 * @author Andrew Schofield - Initial contribution
50 * @author Hilbrand Bouwkamp - Moved api and helper code to separate classes
53 public class HeatHubHandler extends BaseBridgeHandler {
55 private final Logger logger = LoggerFactory.getLogger(HeatHubHandler.class);
56 private final DraytonWiserApi api;
57 private final ExpiringCache<Boolean> refreshCache = new ExpiringCache<>(3, this::actualRefresh);
59 private boolean updateProperties;
60 private @Nullable DraytonWiserRefreshListener discoveryService;
61 private @Nullable ScheduledFuture<?> refreshJob;
63 public HeatHubHandler(final Bridge thing, final HttpClient httpClient) {
65 api = new DraytonWiserApi(httpClient);
68 public DraytonWiserApi getApi() {
73 public Collection<Class<? extends ThingHandlerService>> getServices() {
74 return Set.of(DraytonWiserDiscoveryService.class);
77 public void setDiscoveryService(final DraytonWiserRefreshListener discoveryService) {
78 this.discoveryService = discoveryService;
79 refreshCache.invalidateValue();
83 public void unsetDiscoveryService() {
84 discoveryService = null;
88 public void handleCommand(final ChannelUID channelUID, final Command command) {
92 public void initialize() {
93 logger.debug("Initializing Drayton Wiser Heat Hub handler");
94 final HeatHubConfiguration configuration = getConfigAs(HeatHubConfiguration.class);
95 api.setConfiguration(configuration);
96 updateProperties = true;
97 refreshJob = scheduler.scheduleWithFixedDelay(this::refresh, 0, configuration.refresh, TimeUnit.SECONDS);
98 updateStatus(ThingStatus.UNKNOWN);
101 public void refresh() {
102 refreshCache.getValue();
105 private @Nullable Boolean actualRefresh() {
107 if (ThingHandlerHelper.isHandlerInitialized(this)) {
108 logger.debug("Refreshing devices");
109 final DomainDTO domain = api.getDomain();
111 if (domain == null) {
112 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
115 if (getThing().getStatus() != ThingStatus.ONLINE) {
116 updateStatus(ThingStatus.ONLINE);
118 final DraytonWiserDTO draytonWiserDTO = new DraytonWiserDTO(domain);
120 updateProperties(draytonWiserDTO);
121 notifyListeners(draytonWiserDTO);
123 logger.debug("Finished refreshing devices");
125 } catch (final RuntimeException | DraytonWiserApiException e) {
126 logger.debug("Exception occurred during execution: {}", e.getMessage(), e);
127 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
134 public void childHandlerInitialized(final ThingHandler childHandler, final Thing childThing) {
138 private void updateProperties(final DraytonWiserDTO draytonWiseDTO) {
139 if (updateProperties) {
140 final DeviceDTO device = draytonWiseDTO.getExtendedDeviceProperties(0);
142 if (device != null) {
143 final Map<String, String> properties = editProperties();
144 DraytonWiserPropertyHelper.setGeneralDeviceProperties(device, properties);
145 updateProperties(properties);
146 updateProperties = false;
152 public void dispose() {
153 final ScheduledFuture<?> future = refreshJob;
155 if (future != null) {
160 private void notifyListeners(final DraytonWiserDTO domain) {
161 final DraytonWiserRefreshListener discoveryListener = discoveryService;
163 if (discoveryListener != null) {
164 discoveryListener.onRefresh(domain);
166 getThing().getThings().stream().map(Thing::getHandler)
167 .filter(handler -> handler instanceof DraytonWiserRefreshListener)
168 .map(DraytonWiserRefreshListener.class::cast).forEach(listener -> listener.onRefresh(domain));