]> git.basschouten.com Git - openhab-addons.git/blob
df9805986f86d5918d64332415da50c0737e99d5
[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.electroluxair.internal.handler;
14
15 import static org.openhab.binding.electroluxair.internal.ElectroluxAirBindingConstants.THING_TYPE_BRIDGE;
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.electroluxair.internal.ElectroluxAirBridgeConfiguration;
29 import org.openhab.binding.electroluxair.internal.api.ElectroluxDeltaAPI;
30 import org.openhab.binding.electroluxair.internal.discovery.ElectroluxAirDiscoveryService;
31 import org.openhab.binding.electroluxair.internal.dto.ElectroluxPureA9DTO;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusDetail;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.binding.BaseBridgeHandler;
38 import org.openhab.core.thing.binding.ThingHandlerService;
39 import org.openhab.core.types.Command;
40
41 import com.google.gson.Gson;
42
43 /**
44  * The {@link ElectroluxAirBridgeHandler} is responsible for handling commands, which are
45  * sent to one of the channels.
46  *
47  * @author Jan Gustafsson - Initial contribution
48  */
49 @NonNullByDefault
50 public class ElectroluxAirBridgeHandler extends BaseBridgeHandler {
51
52     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_BRIDGE);
53
54     private int refreshTimeInSeconds = 300;
55
56     private final Gson gson;
57     private final HttpClient httpClient;
58     private final Map<String, ElectroluxPureA9DTO> electroluxAirThings = new ConcurrentHashMap<>();
59
60     private @Nullable ElectroluxDeltaAPI api;
61     private @Nullable ScheduledFuture<?> refreshJob;
62
63     public ElectroluxAirBridgeHandler(Bridge bridge, HttpClient httpClient, Gson gson) {
64         super(bridge);
65         this.httpClient = httpClient;
66         this.gson = gson;
67     }
68
69     @Override
70     public void initialize() {
71         ElectroluxAirBridgeConfiguration config = getConfigAs(ElectroluxAirBridgeConfiguration.class);
72
73         ElectroluxDeltaAPI electroluxDeltaAPI = new ElectroluxDeltaAPI(config, gson, httpClient);
74         refreshTimeInSeconds = config.refresh;
75
76         if (config.username == null || config.password == null) {
77             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
78                     "Configuration of username, password is mandatory");
79         } else if (refreshTimeInSeconds < 0) {
80             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
81                     "Refresh time cannot be negative!");
82         } else {
83             try {
84                 this.api = electroluxDeltaAPI;
85                 scheduler.execute(() -> {
86                     updateStatus(ThingStatus.UNKNOWN);
87                     startAutomaticRefresh();
88
89                 });
90             } catch (RuntimeException e) {
91                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
92             }
93         }
94     }
95
96     public Map<String, ElectroluxPureA9DTO> getElectroluxAirThings() {
97         return electroluxAirThings;
98     }
99
100     @Override
101     public Collection<Class<? extends ThingHandlerService>> getServices() {
102         return Collections.singleton(ElectroluxAirDiscoveryService.class);
103     }
104
105     @Override
106     public void dispose() {
107         stopAutomaticRefresh();
108     }
109
110     public @Nullable ElectroluxDeltaAPI getElectroluxDeltaAPI() {
111         return api;
112     }
113
114     private boolean refreshAndUpdateStatus() {
115         if (api != null) {
116             if (api.refresh(electroluxAirThings)) {
117                 getThing().getThings().stream().forEach(thing -> {
118                     ElectroluxAirHandler handler = (ElectroluxAirHandler) thing.getHandler();
119                     if (handler != null) {
120                         handler.update();
121                     }
122                 });
123                 updateStatus(ThingStatus.ONLINE);
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         return;
151     }
152 }