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.meater.internal.handler;
15 import static org.openhab.binding.meater.internal.MeaterBindingConstants.*;
17 import java.util.Collection;
18 import java.util.Collections;
21 import java.util.concurrent.ConcurrentHashMap;
22 import java.util.concurrent.ScheduledFuture;
23 import java.util.concurrent.TimeUnit;
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;
46 import com.google.gson.Gson;
49 * The {@link MeaterBridgeHandler} is responsible for handling commands, which are
50 * sent to one of the channels.
52 * @author Jan Gustafsson - Initial contribution
55 public class MeaterBridgeHandler extends BaseBridgeHandler {
57 private final Logger logger = LoggerFactory.getLogger(MeaterBridgeHandler.class);
59 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_BRIDGE);
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<>();
67 private int refreshTimeInSeconds = 300;
68 private @Nullable MeaterRestAPI api;
69 private @Nullable ScheduledFuture<?> refreshJob;
71 public MeaterBridgeHandler(Bridge bridge, HttpClient httpClient, Gson gson, TranslationProvider i18nProvider,
72 LocaleProvider localeProvider) {
74 this.httpClient = httpClient;
76 this.i18nProvider = i18nProvider;
77 this.localeProvider = localeProvider;
81 public void initialize() {
82 MeaterBridgeConfiguration config = getConfigAs(MeaterBridgeConfiguration.class);
84 api = new MeaterRestAPI(config, gson, httpClient, localeProvider);
85 refreshTimeInSeconds = config.refresh;
87 if (config.email.isBlank() || config.password.isBlank()) {
88 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
89 "@text/config.missing-username-password.description");
91 updateStatus(ThingStatus.UNKNOWN);
92 scheduler.execute(() -> {
93 startAutomaticRefresh();
98 public Map<String, MeaterProbeDTO.Device> getMeaterThings() {
99 return meaterProbeThings;
103 public Collection<Class<? extends ThingHandlerService>> getServices() {
104 return Collections.singleton(MeaterDiscoveryService.class);
108 public void dispose() {
109 stopAutomaticRefresh();
110 meaterProbeThings.clear();
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) {
126 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
132 private void startAutomaticRefresh() {
133 ScheduledFuture<?> refreshJob = this.refreshJob;
134 if (refreshJob == null || refreshJob.isCancelled()) {
135 this.refreshJob = scheduler.scheduleWithFixedDelay(this::refreshAndUpdateStatus, 0, refreshTimeInSeconds,
140 private void stopAutomaticRefresh() {
141 ScheduledFuture<?> refreshJob = this.refreshJob;
142 if (refreshJob != null) {
143 refreshJob.cancel(true);
144 this.refreshJob = null;
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();
159 public TranslationProvider getI18nProvider() {
163 public LocaleProvider getLocaleProvider() {
164 return localeProvider;