]> git.basschouten.com Git - openhab-addons.git/blob
932f45535d918536d28c9ce390469fce91bc7a58
[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.meater.internal.handler;
14
15 import static org.openhab.binding.meater.internal.MeaterBindingConstants.*;
16
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.concurrent.ConcurrentHashMap;
22 import java.util.concurrent.ScheduledFuture;
23 import java.util.concurrent.TimeUnit;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.eclipse.jetty.client.HttpClient;
28 import org.openhab.binding.meater.internal.MeaterBridgeConfiguration;
29 import org.openhab.binding.meater.internal.api.MeaterRestAPI;
30 import org.openhab.binding.meater.internal.discovery.MeaterDiscoveryService;
31 import org.openhab.binding.meater.internal.dto.MeaterProbeDTO;
32 import org.openhab.core.i18n.LocaleProvider;
33 import org.openhab.core.i18n.TranslationProvider;
34 import org.openhab.core.thing.Bridge;
35 import org.openhab.core.thing.ChannelUID;
36 import org.openhab.core.thing.ThingStatus;
37 import org.openhab.core.thing.ThingStatusDetail;
38 import org.openhab.core.thing.ThingTypeUID;
39 import org.openhab.core.thing.binding.BaseBridgeHandler;
40 import org.openhab.core.thing.binding.ThingHandlerService;
41 import org.openhab.core.types.Command;
42 import org.openhab.core.types.RefreshType;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import com.google.gson.Gson;
47
48 /**
49  * The {@link MeaterBridgeHandler} is responsible for handling commands, which are
50  * sent to one of the channels.
51  *
52  * @author Jan Gustafsson - Initial contribution
53  */
54 @NonNullByDefault
55 public class MeaterBridgeHandler extends BaseBridgeHandler {
56
57     private final Logger logger = LoggerFactory.getLogger(MeaterBridgeHandler.class);
58
59     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_BRIDGE);
60
61     private final Gson gson;
62     private final HttpClient httpClient;
63     private final TranslationProvider i18nProvider;
64     private final LocaleProvider localeProvider;
65     private final Map<String, MeaterProbeDTO.Device> meaterProbeThings = new ConcurrentHashMap<>();
66
67     private int refreshTimeInSeconds = 300;
68     private @Nullable MeaterRestAPI api;
69     private @Nullable ScheduledFuture<?> refreshJob;
70
71     public MeaterBridgeHandler(Bridge bridge, HttpClient httpClient, Gson gson, TranslationProvider i18nProvider,
72             LocaleProvider localeProvider) {
73         super(bridge);
74         this.httpClient = httpClient;
75         this.gson = gson;
76         this.i18nProvider = i18nProvider;
77         this.localeProvider = localeProvider;
78     }
79
80     @Override
81     public void initialize() {
82         MeaterBridgeConfiguration config = getConfigAs(MeaterBridgeConfiguration.class);
83
84         api = new MeaterRestAPI(config, gson, httpClient, localeProvider);
85         refreshTimeInSeconds = config.refresh;
86
87         if (config.email.isBlank() || config.password.isBlank()) {
88             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
89                     "@text/config.missing-username-password.description");
90         } else {
91             updateStatus(ThingStatus.UNKNOWN);
92             scheduler.execute(() -> {
93                 startAutomaticRefresh();
94             });
95         }
96     }
97
98     public Map<String, MeaterProbeDTO.Device> getMeaterThings() {
99         return meaterProbeThings;
100     }
101
102     @Override
103     public Collection<Class<? extends ThingHandlerService>> getServices() {
104         return Collections.singleton(MeaterDiscoveryService.class);
105     }
106
107     @Override
108     public void dispose() {
109         stopAutomaticRefresh();
110         meaterProbeThings.clear();
111     }
112
113     private boolean refreshAndUpdateStatus() {
114         MeaterRestAPI localAPI = api;
115         if (localAPI != null) {
116             if (localAPI.refresh(meaterProbeThings)) {
117                 updateStatus(ThingStatus.ONLINE);
118                 getThing().getThings().stream().forEach(thing -> {
119                     MeaterHandler handler = (MeaterHandler) thing.getHandler();
120                     if (handler != null) {
121                         handler.update();
122                     }
123                 });
124                 return true;
125             } else {
126                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
127             }
128         }
129         return false;
130     }
131
132     private void startAutomaticRefresh() {
133         ScheduledFuture<?> refreshJob = this.refreshJob;
134         if (refreshJob == null || refreshJob.isCancelled()) {
135             this.refreshJob = scheduler.scheduleWithFixedDelay(this::refreshAndUpdateStatus, 0, refreshTimeInSeconds,
136                     TimeUnit.SECONDS);
137         }
138     }
139
140     private void stopAutomaticRefresh() {
141         ScheduledFuture<?> refreshJob = this.refreshJob;
142         if (refreshJob != null) {
143             refreshJob.cancel(true);
144             this.refreshJob = null;
145         }
146     }
147
148     @Override
149     public void handleCommand(ChannelUID channelUID, Command command) {
150         logger.debug("Command received: {}", command);
151         if (command instanceof RefreshType) {
152             if (channelUID.getId().equals(CHANNEL_STATUS)) {
153                 logger.debug("Refresh command on status channel {} will trigger instant refresh", channelUID);
154                 refreshAndUpdateStatus();
155             }
156         }
157     }
158
159     public TranslationProvider getI18nProvider() {
160         return i18nProvider;
161     }
162
163     public LocaleProvider getLocaleProvider() {
164         return localeProvider;
165     }
166 }