]> git.basschouten.com Git - openhab-addons.git/blob
cd9a4bd7a714573b8b07bd879ca497aaa5ff20f3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.draytonwiser.internal.handler;
14
15 import java.util.Collection;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
20
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;
44
45 /**
46  * The {@link HeatHubHandler} is responsible for handling commands, which are
47  * sent to one of the channels.
48  *
49  * @author Andrew Schofield - Initial contribution
50  * @author Hilbrand Bouwkamp - Moved api and helper code to separate classes
51  */
52 @NonNullByDefault
53 public class HeatHubHandler extends BaseBridgeHandler {
54
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);
58
59     private boolean updateProperties;
60     private @Nullable DraytonWiserRefreshListener discoveryService;
61     private @Nullable ScheduledFuture<?> refreshJob;
62
63     public HeatHubHandler(final Bridge thing, final HttpClient httpClient) {
64         super(thing);
65         api = new DraytonWiserApi(httpClient);
66     }
67
68     public DraytonWiserApi getApi() {
69         return api;
70     }
71
72     @Override
73     public Collection<Class<? extends ThingHandlerService>> getServices() {
74         return Set.of(DraytonWiserDiscoveryService.class);
75     }
76
77     public void setDiscoveryService(final DraytonWiserRefreshListener discoveryService) {
78         this.discoveryService = discoveryService;
79         refreshCache.invalidateValue();
80         refresh();
81     }
82
83     public void unsetDiscoveryService() {
84         discoveryService = null;
85     }
86
87     @Override
88     public void handleCommand(final ChannelUID channelUID, final Command command) {
89     }
90
91     @Override
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);
99     }
100
101     public void refresh() {
102         refreshCache.getValue();
103     }
104
105     private @Nullable Boolean actualRefresh() {
106         try {
107             if (ThingHandlerHelper.isHandlerInitialized(this)) {
108                 logger.debug("Refreshing devices");
109                 final DomainDTO domain = api.getDomain();
110
111                 if (domain == null) {
112                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
113                             "No data received");
114                 } else {
115                     if (getThing().getStatus() != ThingStatus.ONLINE) {
116                         updateStatus(ThingStatus.ONLINE);
117                     }
118                     final DraytonWiserDTO draytonWiserDTO = new DraytonWiserDTO(domain);
119
120                     updateProperties(draytonWiserDTO);
121                     notifyListeners(draytonWiserDTO);
122                 }
123                 logger.debug("Finished refreshing devices");
124             }
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());
128             return null;
129         }
130         return Boolean.TRUE;
131     }
132
133     @Override
134     public void childHandlerInitialized(final ThingHandler childHandler, final Thing childThing) {
135         refresh();
136     }
137
138     private void updateProperties(final DraytonWiserDTO draytonWiseDTO) {
139         if (updateProperties) {
140             final DeviceDTO device = draytonWiseDTO.getExtendedDeviceProperties(0);
141
142             if (device != null) {
143                 final Map<String, String> properties = editProperties();
144                 DraytonWiserPropertyHelper.setGeneralDeviceProperties(device, properties);
145                 updateProperties(properties);
146                 updateProperties = false;
147             }
148         }
149     }
150
151     @Override
152     public void dispose() {
153         final ScheduledFuture<?> future = refreshJob;
154
155         if (future != null) {
156             future.cancel(true);
157         }
158     }
159
160     private void notifyListeners(final DraytonWiserDTO domain) {
161         final DraytonWiserRefreshListener discoveryListener = discoveryService;
162
163         if (discoveryListener != null) {
164             discoveryListener.onRefresh(domain);
165         }
166         getThing().getThings().stream().map(Thing::getHandler)
167                 .filter(handler -> handler instanceof DraytonWiserRefreshListener)
168                 .map(DraytonWiserRefreshListener.class::cast).forEach(listener -> listener.onRefresh(domain));
169     }
170 }