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 static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
17 import java.util.Collection;
18 import java.util.List;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.freeboxos.internal.action.ActivePlayerActions;
23 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
24 import org.openhab.binding.freeboxos.internal.api.rest.PlayerManager;
25 import org.openhab.binding.freeboxos.internal.api.rest.PlayerManager.Configuration;
26 import org.openhab.binding.freeboxos.internal.api.rest.PlayerManager.ForegroundApp;
27 import org.openhab.binding.freeboxos.internal.api.rest.PlayerManager.Player;
28 import org.openhab.binding.freeboxos.internal.api.rest.PlayerManager.Status;
29 import org.openhab.core.library.unit.Units;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.binding.ThingHandlerService;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link ActivePlayerHandler} is responsible for handling everything associated to Freebox Player
39 * with api capabilities.
41 * @author Gaƫl L'hopital - Initial contribution
44 public class ActivePlayerHandler extends PlayerHandler implements FreeDeviceIntf {
45 private final Logger logger = LoggerFactory.getLogger(ActivePlayerHandler.class);
47 private final ChannelUID eventChannelUID;
49 private long uptime = -1;
51 public ActivePlayerHandler(Thing thing) {
53 eventChannelUID = new ChannelUID(getThing().getUID(), SYS_INFO, BOX_EVENT);
57 void initializeProperties(Map<String, String> properties) throws FreeboxException {
58 super.initializeProperties(properties);
59 Player player = getManager(PlayerManager.class).getDevice(getClientId());
60 if (player.reachable()) {
61 Configuration config = getManager(PlayerManager.class).getConfig(player.id());
63 properties.put(Thing.PROPERTY_SERIAL_NUMBER, config.serial());
64 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, config.firmwareVersion());
70 protected void internalPoll() throws FreeboxException {
72 if (thing.getStatus().equals(ThingStatus.ONLINE)) {
73 Player player = getManager(PlayerManager.class).getDevice(getClientId());
74 updateStatus(player.reachable() ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
75 if (player.reachable()) {
76 Status status = getManager(PlayerManager.class).getPlayerStatus(getClientId());
78 updateChannelString(PLAYER_STATUS, PLAYER_STATUS, status.powerState().name());
79 ForegroundApp foreground = status.foregroundApp();
80 if (foreground != null) {
81 updateChannelString(PLAYER_STATUS, PACKAGE, foreground._package());
84 Configuration config = getManager(PlayerManager.class).getConfig(getClientId());
86 uptime = checkUptimeAndFirmware(config.uptimeVal(), uptime, config.firmwareVersion());
91 updateChannelQuantity(SYS_INFO, UPTIME, uptime, Units.SECOND);
95 public void reboot() {
98 if (!getManager(PlayerManager.class).reboot(getClientId())) {
99 logger.warn("Unable to reboot the player - probably not reachable");
101 } catch (FreeboxException e) {
102 logger.warn("Error rebooting: {}", e.getMessage());
108 public Collection<Class<? extends ThingHandlerService>> getServices() {
109 return List.of(ActivePlayerActions.class);
113 public ChannelUID getEventChannelUID() {
114 return eventChannelUID;
118 public void triggerChannel(ChannelUID channelUID, String event) {
119 super.triggerChannel(channelUID, event);