]> git.basschouten.com Git - openhab-addons.git/blob
e47f0d6d47e956734d4034b61ac2772d46521e68
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.ThingStatusDetail;
34 import org.openhab.core.thing.binding.ThingHandlerService;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * The {@link ActivePlayerHandler} is responsible for handling everything associated to Freebox Player
40  * with api capabilities.
41  *
42  * @author GaĆ«l L'hopital - Initial contribution
43  */
44 @NonNullByDefault
45 public class ActivePlayerHandler extends PlayerHandler implements FreeDeviceIntf {
46     private final Logger logger = LoggerFactory.getLogger(ActivePlayerHandler.class);
47
48     private final ChannelUID eventChannelUID;
49
50     private long uptime = -1;
51
52     public ActivePlayerHandler(Thing thing) {
53         super(thing);
54         statusDrivenByLanConnectivity = false;
55         eventChannelUID = new ChannelUID(getThing().getUID(), SYS_INFO, BOX_EVENT);
56     }
57
58     @Override
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());
64             if (config != null) {
65                 properties.put(Thing.PROPERTY_SERIAL_NUMBER, config.serial());
66                 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, config.firmwareVersion());
67             }
68         }
69     }
70
71     @Override
72     protected void internalPoll() throws FreeboxException {
73         super.internalPoll();
74         poll();
75     }
76
77     @Override
78     protected void internalForcePoll() throws FreeboxException {
79         super.internalForcePoll();
80         poll();
81     }
82
83     private void poll() throws FreeboxException {
84         if (reachable) {
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);
89             } else {
90                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/info-player-not-reachable");
91             }
92             if (player.reachable()) {
93                 Status status = getManager(PlayerManager.class).getPlayerStatus(getClientId());
94                 if (status != null) {
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());
99                     }
100                 }
101                 Configuration config = getManager(PlayerManager.class).getConfig(getClientId());
102                 if (config != null) {
103                     uptime = checkUptimeAndFirmware(config.uptimeVal(), uptime, config.firmwareVersion());
104                 } else {
105                     uptime = 0;
106                 }
107             }
108             updateChannelQuantity(SYS_INFO, UPTIME, uptime, Units.SECOND);
109         } else {
110             logger.debug("{}: poll with reachable={}", thing.getUID(), reachable);
111             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/info-player-not-reachable");
112         }
113     }
114
115     public void reboot() {
116         processReboot(() -> {
117             try {
118                 if (!getManager(PlayerManager.class).reboot(getClientId())) {
119                     logger.warn("Unable to reboot the player - probably not reachable");
120                 }
121             } catch (FreeboxException e) {
122                 logger.warn("Error rebooting: {}", e.getMessage());
123             }
124         });
125     }
126
127     @Override
128     public Collection<Class<? extends ThingHandlerService>> getServices() {
129         return List.of(ActivePlayerActions.class);
130     }
131
132     @Override
133     public ChannelUID getEventChannelUID() {
134         return eventChannelUID;
135     }
136
137     @Override
138     public void triggerChannel(ChannelUID channelUID, String event) {
139         super.triggerChannel(channelUID, event);
140     }
141 }