]> git.basschouten.com Git - openhab-addons.git/blob
34c8ae7935eeb06a9de280f480ea90a35c3eb582
[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.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         super.initializeProperties(properties);
61         Player player = getManager(PlayerManager.class).getDevice(getClientId());
62         properties.put(Thing.PROPERTY_MODEL_ID, player.deviceModel().name());
63     }
64
65     @Override
66     protected boolean internalHandleCommand(String channelId, Command command) throws FreeboxException {
67         if (KEY_CODE.equals(channelId) && command instanceof StringType) {
68             sendKey(command.toString(), false, 1);
69             return true;
70         }
71
72         return super.internalHandleCommand(channelId, command);
73     }
74
75     @Override
76     public void updateConnectivityChannels(LanHost host) {
77         super.updateConnectivityChannels(host);
78         ipAddress = host.getIpv4();
79     }
80
81     public void sendKey(String key, boolean longPress, int count) {
82         String aKey = key.toLowerCase();
83         IPAddress ip = ipAddress;
84         if (ip == null) {
85             logger.warn("Player IP is unknown");
86         } else if (VALID_REMOTE_KEYS.contains(aKey)) {
87             String remoteCode = (String) getConfig().get(PlayerConfiguration.REMOTE_CODE);
88             if (remoteCode != null) {
89                 try {
90                     getManager(PlayerManager.class).sendKey(ip.toCanonicalString(), remoteCode, aKey, longPress, count);
91                 } catch (FreeboxException e) {
92                     logger.warn("Error sending key: {}", e.getMessage());
93                 }
94             } else {
95                 logger.warn("A remote code must be configured in the on the player thing.");
96             }
97         } else {
98             logger.warn("Key '{}' is not a valid key expression", key);
99         }
100     }
101
102     public void sendMultipleKeys(String keys) {
103         String[] keyChain = keys.split(",");
104         Arrays.stream(keyChain).forEach(key -> {
105             sendKey(key, false, 1);
106         });
107     }
108
109     @Override
110     public Collection<Class<? extends ThingHandlerService>> getServices() {
111         return List.of(PlayerActions.class);
112     }
113 }