]> git.basschouten.com Git - openhab-addons.git/blob
63a02eb4b64dc78410e35e1399629fa0aceeb24c
[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
39  * with api 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             if (config != null) {
63                 properties.put(Thing.PROPERTY_SERIAL_NUMBER, config.serial());
64                 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, config.firmwareVersion());
65             }
66         }
67     }
68
69     @Override
70     protected void internalPoll() throws FreeboxException {
71         super.internalPoll();
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());
77                 if (status != null) {
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());
82                     }
83                 }
84                 Configuration config = getManager(PlayerManager.class).getConfig(getClientId());
85                 if (config != null) {
86                     uptime = checkUptimeAndFirmware(config.uptimeVal(), uptime, config.firmwareVersion());
87                 } else {
88                     uptime = 0;
89                 }
90             }
91             updateChannelQuantity(SYS_INFO, UPTIME, uptime, Units.SECOND);
92         }
93     }
94
95     public void reboot() {
96         processReboot(() -> {
97             try {
98                 if (!getManager(PlayerManager.class).reboot(getClientId())) {
99                     logger.warn("Unable to reboot the player - probably not reachable");
100                 }
101             } catch (FreeboxException e) {
102                 logger.warn("Error rebooting: {}", e.getMessage());
103             }
104         });
105     }
106
107     @Override
108     public Collection<Class<? extends ThingHandlerService>> getServices() {
109         return List.of(ActivePlayerActions.class);
110     }
111
112     @Override
113     public ChannelUID getEventChannelUID() {
114         return eventChannelUID;
115     }
116
117     @Override
118     public void triggerChannel(ChannelUID channelUID, String event) {
119         super.triggerChannel(channelUID, event);
120     }
121 }