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