]> git.basschouten.com Git - openhab-addons.git/blob
fc89e510a56532f9ebe623b784b13be50ca4a9b2
[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.KEY_CODE;
16
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.freeboxos.internal.action.PlayerActions;
25 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
26 import org.openhab.binding.freeboxos.internal.api.rest.LanBrowserManager.LanHost;
27 import org.openhab.binding.freeboxos.internal.api.rest.PlayerManager;
28 import org.openhab.binding.freeboxos.internal.api.rest.PlayerManager.Player;
29 import org.openhab.binding.freeboxos.internal.config.PlayerConfiguration;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.openhab.core.types.Command;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import inet.ipaddr.IPAddress;
38
39 /**
40  * The {@link PlayerHandler} is responsible for handling everything associated to any Freebox Player thing type.
41  *
42  * @author GaĆ«l L'hopital - Initial contribution
43  */
44 @NonNullByDefault
45 public class PlayerHandler extends HostHandler {
46     private static final List<String> VALID_REMOTE_KEYS = Arrays.asList("red", "green", "blue", "yellow", "power",
47             "list", "tv", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "vol_inc", "vol_dec", "mute", "prgm_inc",
48             "prgm_dec", "prev", "bwd", "play", "rec", "fwd", "next", "up", "right", "down", "left", "back", "swap",
49             "info", "epg", "mail", "media", "help", "options", "pip", "ok", "home");
50
51     private final Logger logger = LoggerFactory.getLogger(PlayerHandler.class);
52     private @Nullable IPAddress ipAddress;
53
54     public PlayerHandler(Thing thing) {
55         super(thing);
56     }
57
58     @Override
59     void initializeProperties(Map<String, String> properties) throws FreeboxException {
60         // We need to get and set the MAC address before calling super.initializeProperties
61         Player player = getManager(PlayerManager.class).getDevice(getClientId());
62         properties.put(Thing.PROPERTY_MAC_ADDRESS, player.mac().toColonDelimitedString());
63         properties.put(Thing.PROPERTY_MODEL_ID, player.deviceModel().name());
64         updateProperties(properties);
65         super.initializeProperties(properties);
66     }
67
68     @Override
69     protected boolean internalHandleCommand(String channelId, Command command) throws FreeboxException {
70         if (KEY_CODE.equals(channelId) && command instanceof StringType) {
71             sendKey(command.toString(), false, 1);
72             return true;
73         }
74
75         return super.internalHandleCommand(channelId, command);
76     }
77
78     @Override
79     public void updateConnectivityChannels(LanHost host) {
80         super.updateConnectivityChannels(host);
81         ipAddress = host.getIpv4();
82     }
83
84     public void sendKey(String key, boolean longPress, int count) {
85         String aKey = key.toLowerCase();
86         IPAddress ip = ipAddress;
87         if (ip == null) {
88             logger.warn("Player IP is unknown");
89         } else if (VALID_REMOTE_KEYS.contains(aKey)) {
90             String remoteCode = (String) getConfig().get(PlayerConfiguration.REMOTE_CODE);
91             if (remoteCode != null) {
92                 try {
93                     getManager(PlayerManager.class).sendKey(ip.toCanonicalString(), remoteCode, aKey, longPress, count);
94                 } catch (FreeboxException e) {
95                     logger.warn("Error sending key: {}", e.getMessage());
96                 }
97             } else {
98                 logger.warn("A remote code must be configured in the on the player thing.");
99             }
100         } else {
101             logger.warn("Key '{}' is not a valid key expression", key);
102         }
103     }
104
105     public void sendMultipleKeys(String keys) {
106         String[] keyChain = keys.split(",");
107         Arrays.stream(keyChain).forEach(key -> {
108             sendKey(key, false, 1);
109         });
110     }
111
112     @Override
113     public Collection<Class<? extends ThingHandlerService>> getServices() {
114         return List.of(PlayerActions.class);
115     }
116 }