]> git.basschouten.com Git - openhab-addons.git/blob
52c8063664735ef0101898b3af15b2766343000e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.freeboxos.internal.handler;
14
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
16
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Map;
20
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;
36
37 /**
38  * The {@link ActivePlayerHandler} is responsible for handling everything associated to Freebox Player with api
39  * capabilities.
40  *
41  * @author GaĆ«l L'hopital - Initial contribution
42  */
43 @NonNullByDefault
44 public class ActivePlayerHandler extends PlayerHandler implements FreeDeviceIntf {
45     private final Logger logger = LoggerFactory.getLogger(ActivePlayerHandler.class);
46
47     private final ChannelUID eventChannelUID;
48
49     private long uptime = -1;
50
51     public ActivePlayerHandler(Thing thing) {
52         super(thing);
53         eventChannelUID = new ChannelUID(getThing().getUID(), SYS_INFO, BOX_EVENT);
54     }
55
56     @Override
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());
64         }
65     }
66
67     @Override
68     protected void internalPoll() throws FreeboxException {
69         super.internalPoll();
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());
76             }
77             Configuration config = getManager(PlayerManager.class).getConfig(getClientId());
78
79             uptime = checkUptimeAndFirmware(config.uptimeVal(), uptime, config.firmwareVersion());
80             updateChannelQuantity(SYS_INFO, UPTIME, uptime, Units.SECOND);
81         }
82     }
83
84     public void reboot() {
85         processReboot(() -> {
86             try {
87                 getManager(PlayerManager.class).reboot(getClientId());
88             } catch (FreeboxException e) {
89                 logger.warn("Error rebooting: {}", e.getMessage());
90             }
91         });
92     }
93
94     @Override
95     public Collection<Class<? extends ThingHandlerService>> getServices() {
96         return List.of(ActivePlayerActions.class);
97     }
98
99     @Override
100     public ChannelUID getEventChannelUID() {
101         return eventChannelUID;
102     }
103
104     @Override
105     public void triggerChannel(ChannelUID channelUID, String event) {
106         super.triggerChannel(channelUID, event);
107     }
108 }