2 * Copyright (c) 2010-2024 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.ThingStatusDetail;
34 import org.openhab.core.thing.binding.ThingHandlerService;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The {@link ActivePlayerHandler} is responsible for handling everything associated to Freebox Player
40 * with api capabilities.
42 * @author Gaƫl L'hopital - Initial contribution
45 public class ActivePlayerHandler extends PlayerHandler implements FreeDeviceIntf {
46 private final Logger logger = LoggerFactory.getLogger(ActivePlayerHandler.class);
48 private final ChannelUID eventChannelUID;
50 private long uptime = -1;
52 public ActivePlayerHandler(Thing thing) {
54 statusDrivenByLanConnectivity = false;
55 eventChannelUID = new ChannelUID(getThing().getUID(), SYS_INFO, BOX_EVENT);
59 void initializeProperties(Map<String, String> properties) throws FreeboxException {
60 super.initializeProperties(properties);
61 Player player = getManager(PlayerManager.class).getDevice(getClientId());
62 if (player.reachable()) {
63 Configuration config = getManager(PlayerManager.class).getConfig(player.id());
65 properties.put(Thing.PROPERTY_SERIAL_NUMBER, config.serial());
66 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, config.firmwareVersion());
72 protected void internalPoll() throws FreeboxException {
78 protected void internalForcePoll() throws FreeboxException {
79 super.internalForcePoll();
83 private void poll() throws FreeboxException {
85 Player player = getManager(PlayerManager.class).getDevice(getClientId());
86 logger.debug("{}: poll with player.reachable() = {}", thing.getUID(), player.reachable());
87 if (player.reachable()) {
88 updateStatus(ThingStatus.ONLINE);
90 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/info-player-not-reachable");
92 if (player.reachable()) {
93 Status status = getManager(PlayerManager.class).getPlayerStatus(getClientId());
95 updateChannelString(PLAYER_STATUS, PLAYER_STATUS, status.powerState().name());
96 ForegroundApp foreground = status.foregroundApp();
97 if (foreground != null) {
98 updateChannelString(PLAYER_STATUS, PACKAGE, foreground._package());
101 Configuration config = getManager(PlayerManager.class).getConfig(getClientId());
102 if (config != null) {
103 uptime = checkUptimeAndFirmware(config.uptimeVal(), uptime, config.firmwareVersion());
108 updateChannelQuantity(SYS_INFO, UPTIME, uptime, Units.SECOND);
110 logger.debug("{}: poll with reachable={}", thing.getUID(), reachable);
111 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/info-player-not-reachable");
115 public void reboot() {
116 processReboot(() -> {
118 if (!getManager(PlayerManager.class).reboot(getClientId())) {
119 logger.warn("Unable to reboot the player - probably not reachable");
121 } catch (FreeboxException e) {
122 logger.warn("Error rebooting: {}", e.getMessage());
128 public Collection<Class<? extends ThingHandlerService>> getServices() {
129 return List.of(ActivePlayerActions.class);
133 public ChannelUID getEventChannelUID() {
134 return eventChannelUID;
138 public void triggerChannel(ChannelUID channelUID, String event) {
139 super.triggerChannel(channelUID, event);