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.freeboxos.internal.handler;
15 import java.math.BigDecimal;
16 import java.time.ZonedDateTime;
17 import java.util.HashMap;
18 import java.util.Hashtable;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
23 import javax.measure.Unit;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
28 import org.openhab.binding.freeboxos.internal.api.rest.LanBrowserManager.Source;
29 import org.openhab.binding.freeboxos.internal.api.rest.MediaReceiverManager;
30 import org.openhab.binding.freeboxos.internal.api.rest.MediaReceiverManager.MediaType;
31 import org.openhab.binding.freeboxos.internal.api.rest.MediaReceiverManager.Receiver;
32 import org.openhab.binding.freeboxos.internal.api.rest.RestManager;
33 import org.openhab.binding.freeboxos.internal.config.ApiConsumerConfiguration;
34 import org.openhab.binding.freeboxos.internal.config.ClientConfiguration;
35 import org.openhab.core.audio.AudioSink;
36 import org.openhab.core.config.core.Configuration;
37 import org.openhab.core.library.types.DateTimeType;
38 import org.openhab.core.library.types.DecimalType;
39 import org.openhab.core.library.types.OnOffType;
40 import org.openhab.core.library.types.QuantityType;
41 import org.openhab.core.library.types.StringType;
42 import org.openhab.core.thing.Bridge;
43 import org.openhab.core.thing.ChannelUID;
44 import org.openhab.core.thing.Thing;
45 import org.openhab.core.thing.ThingStatus;
46 import org.openhab.core.thing.ThingStatusDetail;
47 import org.openhab.core.thing.ThingStatusInfo;
48 import org.openhab.core.thing.binding.BaseThingHandler;
49 import org.openhab.core.thing.binding.BridgeHandler;
50 import org.openhab.core.types.Command;
51 import org.openhab.core.types.RefreshType;
52 import org.openhab.core.types.State;
53 import org.openhab.core.types.UnDefType;
54 import org.osgi.framework.ServiceRegistration;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 import inet.ipaddr.IPAddress;
61 * The {@link ServerHandler} is a base abstract class for all devices made available by the FreeboxOs bridge
63 * @author Gaƫl L'hopital - Initial contribution
66 abstract class ApiConsumerHandler extends BaseThingHandler implements ApiConsumerIntf {
67 private final Logger logger = LoggerFactory.getLogger(ApiConsumerHandler.class);
68 private final Map<String, ScheduledFuture<?>> jobs = new HashMap<>();
70 private @Nullable ServiceRegistration<?> reg;
71 protected boolean statusDrivenByBridge = true;
73 ApiConsumerHandler(Thing thing) {
78 public void initialize() {
79 FreeboxOsHandler bridgeHandler = checkBridgeHandler();
80 if (bridgeHandler == null) {
84 Map<String, String> properties = editProperties();
85 if (properties.isEmpty()) {
87 initializeProperties(properties);
88 checkAirMediaCapabilities(properties);
89 updateProperties(properties);
90 } catch (FreeboxException e) {
91 logger.warn("Error getting thing {} properties: {}", thing.getUID(), e.getMessage());
95 boolean isAudioReceiver = Boolean.parseBoolean(properties.get(MediaType.AUDIO.name()));
96 if (isAudioReceiver) {
97 configureMediaSink(bridgeHandler, properties.getOrDefault(Source.UPNP.name(), ""));
103 private void configureMediaSink(FreeboxOsHandler bridgeHandler, String upnpName) {
105 Receiver receiver = getManager(MediaReceiverManager.class).getReceiver(upnpName);
106 if (receiver != null && reg == null) {
107 ApiConsumerConfiguration config = getConfig().as(ApiConsumerConfiguration.class);
108 String callbackURL = bridgeHandler.getCallbackURL();
109 if (!config.password.isEmpty() || !receiver.passwordProtected()) {
110 reg = bridgeHandler.getBundleContext().registerService(
111 AudioSink.class.getName(), new AirMediaSink(this, bridgeHandler.getAudioHTTPServer(),
112 callbackURL, receiver.name(), config.password, config.acceptAllMp3),
115 logger.info("A password needs to be configured to enable Air Media capability.");
118 } catch (FreeboxException e) {
119 logger.warn("Unable to retrieve Media Receivers: {}", e.getMessage());
123 public <T extends RestManager> T getManager(Class<T> clazz) throws FreeboxException {
124 FreeboxOsHandler handler = checkBridgeHandler();
125 if (handler != null) {
126 return handler.getManager(clazz);
128 throw new FreeboxException("Bridge handler not yet defined");
131 abstract void initializeProperties(Map<String, String> properties) throws FreeboxException;
134 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
135 if (checkBridgeHandler() != null) {
143 public void handleCommand(ChannelUID channelUID, Command command) {
144 if (command instanceof RefreshType || getThing().getStatus() != ThingStatus.ONLINE) {
148 if (checkBridgeHandler() == null || !internalHandleCommand(channelUID.getIdWithoutGroup(), command)) {
149 logger.debug("Unexpected command {} on channel {}", command, channelUID.getId());
151 } catch (FreeboxException e) {
152 logger.warn("Error handling command: {}", e.getMessage());
156 private void checkAirMediaCapabilities(Map<String, String> properties) throws FreeboxException {
157 String upnpName = properties.getOrDefault(Source.UPNP.name(), "");
158 Receiver receiver = getManager(MediaReceiverManager.class).getReceiver(upnpName);
159 if (receiver != null) {
160 receiver.capabilities().entrySet()
161 .forEach(entry -> properties.put(entry.getKey().name(), entry.getValue().toString()));
165 private @Nullable FreeboxOsHandler checkBridgeHandler() {
166 Bridge bridge = getBridge();
167 if (bridge != null) {
168 BridgeHandler handler = bridge.getHandler();
169 if (handler instanceof FreeboxOsHandler fbOsHandler) {
170 if (bridge.getStatus() == ThingStatus.ONLINE) {
171 if (statusDrivenByBridge) {
172 updateStatus(ThingStatus.ONLINE);
176 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
178 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_MISSING_ERROR);
181 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
187 public void dispose() {
189 ServiceRegistration<?> localReg = reg;
190 if (localReg != null) {
191 localReg.unregister();
196 private void startRefreshJob() {
197 removeJob("GlobalJob");
199 int refreshInterval = getConfigAs(ApiConsumerConfiguration.class).refreshInterval;
200 logger.debug("Scheduling state update every {} seconds for thing {}...", refreshInterval, getThing().getUID());
202 ThingStatusDetail detail = thing.getStatusInfo().getStatusDetail();
203 if (ThingStatusDetail.DUTY_CYCLE.equals(detail)) {
206 } catch (FreeboxException ignore) {
207 // An exception is normal if the box is rebooting then let's try again later...
208 addJob("Initialize", this::initialize, 10, TimeUnit.SECONDS);
213 addJob("GlobalJob", () -> {
216 } catch (FreeboxException e) {
217 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
219 }, 0, refreshInterval, TimeUnit.SECONDS);
222 private void removeJob(String name) {
223 ScheduledFuture<?> existing = jobs.get(name);
224 if (existing != null && !existing.isCancelled()) {
225 existing.cancel(true);
230 public void addJob(String name, Runnable command, long initialDelay, long delay, TimeUnit unit) {
232 jobs.put(name, scheduler.scheduleWithFixedDelay(command, initialDelay, delay, unit));
236 public void addJob(String name, Runnable command, long delay, TimeUnit unit) {
238 jobs.put(name, scheduler.schedule(command, delay, unit));
242 public void stopJobs() {
243 jobs.keySet().forEach(name -> removeJob(name));
247 protected boolean internalHandleCommand(String channelId, Command command) throws FreeboxException {
251 protected abstract void internalPoll() throws FreeboxException;
253 private void updateIfActive(String group, String channelId, State state) {
254 ChannelUID id = new ChannelUID(getThing().getUID(), group, channelId);
256 updateState(id, state);
260 protected void updateIfActive(String channelId, State state) {
261 ChannelUID id = new ChannelUID(getThing().getUID(), channelId);
263 updateState(id, state);
267 protected void updateChannelDateTimeState(String channelId, @Nullable ZonedDateTime timestamp) {
268 updateIfActive(channelId, timestamp == null ? UnDefType.NULL : new DateTimeType(timestamp));
271 protected void updateChannelDateTimeState(String group, String channelId, @Nullable ZonedDateTime timestamp) {
272 updateIfActive(group, channelId, timestamp == null ? UnDefType.NULL : new DateTimeType(timestamp));
275 protected void updateChannelOnOff(String group, String channelId, boolean value) {
276 updateIfActive(group, channelId, OnOffType.from(value));
279 protected void updateChannelOnOff(String channelId, boolean value) {
280 updateIfActive(channelId, OnOffType.from(value));
283 protected void updateChannelString(String group, String channelId, @Nullable String value) {
284 updateIfActive(group, channelId, value != null ? new StringType(value) : UnDefType.NULL);
287 protected void updateChannelString(String group, String channelId, @Nullable IPAddress value) {
288 updateIfActive(group, channelId, value != null ? new StringType(value.toCanonicalString()) : UnDefType.NULL);
291 protected void updateChannelString(String channelId, @Nullable String value) {
292 updateIfActive(channelId, value != null ? new StringType(value) : UnDefType.NULL);
295 protected void updateChannelString(String channelId, Enum<?> value) {
296 updateIfActive(channelId, new StringType(value.name()));
299 protected void updateChannelString(String group, String channelId, Enum<?> value) {
300 updateIfActive(group, channelId, new StringType(value.name()));
303 protected void updateChannelQuantity(String group, String channelId, double d, Unit<?> unit) {
304 updateChannelQuantity(group, channelId, new QuantityType<>(d, unit));
307 protected void updateChannelQuantity(String channelId, @Nullable QuantityType<?> quantity) {
308 updateIfActive(channelId, quantity != null ? quantity : UnDefType.NULL);
311 protected void updateChannelQuantity(String group, String channelId, @Nullable QuantityType<?> quantity) {
312 updateIfActive(group, channelId, quantity != null ? quantity : UnDefType.NULL);
315 protected void updateChannelDecimal(String group, String channelId, @Nullable Integer value) {
316 updateIfActive(group, channelId, value != null ? new DecimalType(value) : UnDefType.NULL);
319 protected void updateChannelQuantity(String group, String channelId, QuantityType<?> qtty, Unit<?> unit) {
320 updateChannelQuantity(group, channelId, qtty.toUnit(unit));
324 public void updateStatus(ThingStatus status, ThingStatusDetail statusDetail, @Nullable String description) {
325 super.updateStatus(status, statusDetail, description);
329 public Map<String, String> editProperties() {
330 return super.editProperties();
334 public void updateProperties(@Nullable Map<String, String> properties) {
335 super.updateProperties(properties);
339 public Configuration getConfig() {
340 return super.getConfig();
344 public int getClientId() {
345 return ((BigDecimal) getConfig().get(ClientConfiguration.ID)).intValue();