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;
59 import inet.ipaddr.MACAddressString;
60 import inet.ipaddr.mac.MACAddress;
63 * The {@link ServerHandler} is a base abstract class for all devices made available by the FreeboxOs bridge
65 * @author Gaƫl L'hopital - Initial contribution
68 abstract class ApiConsumerHandler extends BaseThingHandler implements ApiConsumerIntf {
69 private final Logger logger = LoggerFactory.getLogger(ApiConsumerHandler.class);
70 private final Map<String, ScheduledFuture<?>> jobs = new HashMap<>();
72 private @Nullable ServiceRegistration<?> reg;
74 ApiConsumerHandler(Thing thing) {
79 public void initialize() {
80 FreeboxOsHandler bridgeHandler = checkBridgeHandler();
81 if (bridgeHandler == null) {
85 Map<String, String> properties = editProperties();
86 if (properties.isEmpty()) {
88 initializeProperties(properties);
89 checkAirMediaCapabilities(properties);
90 updateProperties(properties);
91 } catch (FreeboxException e) {
92 logger.warn("Error getting thing {} properties: {}", thing.getUID(), e.getMessage());
96 boolean isAudioReceiver = Boolean.parseBoolean(properties.get(MediaType.AUDIO.name()));
97 if (isAudioReceiver) {
98 configureMediaSink(bridgeHandler, properties.getOrDefault(Source.UPNP.name(), ""));
104 private void configureMediaSink(FreeboxOsHandler bridgeHandler, String upnpName) {
106 Receiver receiver = getManager(MediaReceiverManager.class).getReceiver(upnpName);
107 if (receiver != null && reg == null) {
108 ApiConsumerConfiguration config = getConfig().as(ApiConsumerConfiguration.class);
109 String callbackURL = bridgeHandler.getCallbackURL();
110 if (!config.password.isEmpty() || !receiver.passwordProtected()) {
111 reg = bridgeHandler.getBundleContext().registerService(
112 AudioSink.class.getName(), new AirMediaSink(this, bridgeHandler.getAudioHTTPServer(),
113 callbackURL, receiver.name(), config.password, config.acceptAllMp3),
116 logger.info("A password needs to be configured to enable Air Media capability.");
119 } catch (FreeboxException e) {
120 logger.warn("Unable to retrieve Media Receivers: {}", e.getMessage());
124 public <T extends RestManager> T getManager(Class<T> clazz) throws FreeboxException {
125 FreeboxOsHandler handler = checkBridgeHandler();
126 if (handler != null) {
127 return handler.getManager(clazz);
129 throw new FreeboxException("Bridge handler not yet defined");
132 abstract void initializeProperties(Map<String, String> properties) throws FreeboxException;
135 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
136 if (checkBridgeHandler() != null) {
144 public void handleCommand(ChannelUID channelUID, Command command) {
145 if (command instanceof RefreshType || getThing().getStatus() != ThingStatus.ONLINE) {
149 if (checkBridgeHandler() == null || !internalHandleCommand(channelUID.getIdWithoutGroup(), command)) {
150 logger.debug("Unexpected command {} on channel {}", command, channelUID.getId());
152 } catch (FreeboxException e) {
153 logger.warn("Error handling command: {}", e.getMessage());
157 private void checkAirMediaCapabilities(Map<String, String> properties) throws FreeboxException {
158 String upnpName = properties.getOrDefault(Source.UPNP.name(), "");
159 Receiver receiver = getManager(MediaReceiverManager.class).getReceiver(upnpName);
160 if (receiver != null) {
161 receiver.capabilities().entrySet()
162 .forEach(entry -> properties.put(entry.getKey().name(), entry.getValue().toString()));
166 private @Nullable FreeboxOsHandler checkBridgeHandler() {
167 Bridge bridge = getBridge();
168 if (bridge != null) {
169 BridgeHandler handler = bridge.getHandler();
170 if (handler instanceof FreeboxOsHandler) {
171 if (bridge.getStatus() == ThingStatus.ONLINE) {
172 updateStatus(ThingStatus.ONLINE);
173 return (FreeboxOsHandler) handler;
175 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
177 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_MISSING_ERROR);
180 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
186 public void dispose() {
188 ServiceRegistration<?> localReg = reg;
189 if (localReg != null) {
190 localReg.unregister();
195 private void startRefreshJob() {
196 removeJob("GlobalJob");
198 int refreshInterval = getConfigAs(ApiConsumerConfiguration.class).refreshInterval;
199 logger.debug("Scheduling state update every {} seconds for thing {}...", refreshInterval, getThing().getUID());
201 ThingStatusDetail detail = thing.getStatusInfo().getStatusDetail();
202 if (ThingStatusDetail.DUTY_CYCLE.equals(detail)) {
205 } catch (FreeboxException ignore) {
206 // An exception is normal if the box is rebooting then let's try again later...
207 addJob("Initialize", this::initialize, 10, TimeUnit.SECONDS);
212 addJob("GlobalJob", () -> {
215 } catch (FreeboxException e) {
216 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
218 }, 0, refreshInterval, TimeUnit.SECONDS);
221 private void removeJob(String name) {
222 ScheduledFuture<?> existing = jobs.get(name);
223 if (existing != null && !existing.isCancelled()) {
224 existing.cancel(true);
229 public void addJob(String name, Runnable command, long initialDelay, long delay, TimeUnit unit) {
231 jobs.put(name, scheduler.scheduleWithFixedDelay(command, initialDelay, delay, unit));
235 public void addJob(String name, Runnable command, long delay, TimeUnit unit) {
237 jobs.put(name, scheduler.schedule(command, delay, unit));
241 public void stopJobs() {
242 jobs.keySet().forEach(name -> removeJob(name));
246 protected boolean internalHandleCommand(String channelId, Command command) throws FreeboxException {
250 protected abstract void internalPoll() throws FreeboxException;
252 private void updateIfActive(String group, String channelId, State state) {
253 ChannelUID id = new ChannelUID(getThing().getUID(), group, channelId);
255 updateState(id, state);
259 protected void updateIfActive(String channelId, State state) {
260 ChannelUID id = new ChannelUID(getThing().getUID(), channelId);
262 updateState(id, state);
266 protected void updateChannelDateTimeState(String channelId, @Nullable ZonedDateTime timestamp) {
267 updateIfActive(channelId, timestamp == null ? UnDefType.NULL : new DateTimeType(timestamp));
270 protected void updateChannelDateTimeState(String group, String channelId, @Nullable ZonedDateTime timestamp) {
271 updateIfActive(group, channelId, timestamp == null ? UnDefType.NULL : new DateTimeType(timestamp));
274 protected void updateChannelOnOff(String group, String channelId, boolean value) {
275 updateIfActive(group, channelId, OnOffType.from(value));
278 protected void updateChannelOnOff(String channelId, boolean value) {
279 updateIfActive(channelId, OnOffType.from(value));
282 protected void updateChannelString(String group, String channelId, @Nullable String value) {
283 updateIfActive(group, channelId, value != null ? new StringType(value) : UnDefType.NULL);
286 protected void updateChannelString(String group, String channelId, @Nullable IPAddress value) {
287 updateIfActive(group, channelId, value != null ? new StringType(value.toCanonicalString()) : UnDefType.NULL);
290 protected void updateChannelString(String channelId, @Nullable String value) {
291 updateIfActive(channelId, value != null ? new StringType(value) : UnDefType.NULL);
294 protected void updateChannelString(String channelId, Enum<?> value) {
295 updateIfActive(channelId, new StringType(value.name()));
298 protected void updateChannelString(String group, String channelId, Enum<?> value) {
299 updateIfActive(group, channelId, new StringType(value.name()));
302 protected void updateChannelQuantity(String group, String channelId, double d, Unit<?> unit) {
303 updateChannelQuantity(group, channelId, new QuantityType<>(d, unit));
306 protected void updateChannelQuantity(String channelId, @Nullable QuantityType<?> quantity) {
307 updateIfActive(channelId, quantity != null ? quantity : UnDefType.NULL);
310 protected void updateChannelQuantity(String group, String channelId, @Nullable QuantityType<?> quantity) {
311 updateIfActive(group, channelId, quantity != null ? quantity : UnDefType.NULL);
314 protected void updateChannelDecimal(String group, String channelId, @Nullable Integer value) {
315 updateIfActive(group, channelId, value != null ? new DecimalType(value) : UnDefType.NULL);
318 protected void updateChannelQuantity(String group, String channelId, QuantityType<?> qtty, Unit<?> unit) {
319 updateChannelQuantity(group, channelId, qtty.toUnit(unit));
323 public void updateStatus(ThingStatus status, ThingStatusDetail statusDetail, @Nullable String description) {
324 super.updateStatus(status, statusDetail, description);
328 public Map<String, String> editProperties() {
329 return super.editProperties();
333 public void updateProperties(@Nullable Map<String, String> properties) {
334 super.updateProperties(properties);
338 public Configuration getConfig() {
339 return super.getConfig();
343 public int getClientId() {
344 return ((BigDecimal) getConfig().get(ClientConfiguration.ID)).intValue();
348 public MACAddress getMac() {
349 String mac = (String) getConfig().get(Thing.PROPERTY_MAC_ADDRESS);
350 return new MACAddressString(mac).getAddress();