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.Collections;
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 with api
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());
62 properties.put(Thing.PROPERTY_SERIAL_NUMBER, config.serial());
63 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, config.firmwareVersion());
68 protected void internalPoll() throws FreeboxException {
70 if (thing.getStatus().equals(ThingStatus.ONLINE)) {
71 Status status = getManager(PlayerManager.class).getPlayerStatus(getClientId());
72 updateChannelString(PLAYER_STATUS, PLAYER_STATUS, status.powerState().name());
73 ForegroundApp foreground = status.foregroundApp();
74 if (foreground != null) {
75 updateChannelString(PLAYER_STATUS, PACKAGE, foreground._package());
77 Configuration config = getManager(PlayerManager.class).getConfig(getClientId());
79 uptime = checkUptimeAndFirmware(config.uptimeVal(), uptime, config.firmwareVersion());
80 updateChannelQuantity(SYS_INFO, UPTIME, uptime, Units.SECOND);
84 public void reboot() {
87 getManager(PlayerManager.class).reboot(getClientId());
88 } catch (FreeboxException e) {
89 logger.warn("Error rebooting: {}", e.getMessage());
95 public Collection<Class<? extends ThingHandlerService>> getServices() {
96 return Collections.singletonList(ActivePlayerActions.class);
100 public ChannelUID getEventChannelUID() {
101 return eventChannelUID;
105 public void triggerChannel(ChannelUID channelUID, String event) {
106 super.triggerChannel(channelUID, event);