2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.freeboxos.internal.handler;
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.KEY_CODE;
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.List;
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;
37 import inet.ipaddr.IPAddress;
40 * The {@link PlayerHandler} is responsible for handling everything associated to any Freebox Player thing type.
42 * @author Gaƫl L'hopital - Initial contribution
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");
51 private final Logger logger = LoggerFactory.getLogger(PlayerHandler.class);
52 private @Nullable IPAddress ipAddress;
54 public PlayerHandler(Thing thing) {
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());
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);
72 return super.internalHandleCommand(channelId, command);
76 public void updateConnectivityChannels(LanHost host) {
77 super.updateConnectivityChannels(host);
78 ipAddress = host.getIpv4();
81 public void sendKey(String key, boolean longPress, int count) {
82 String aKey = key.toLowerCase();
83 IPAddress ip = ipAddress;
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) {
90 getManager(PlayerManager.class).sendKey(ip.toCanonicalString(), remoteCode, aKey, longPress, count);
91 } catch (FreeboxException e) {
92 logger.warn("Error sending key: {}", e.getMessage());
95 logger.warn("A remote code must be configured in the on the player thing.");
98 logger.warn("Key '{}' is not a valid key expression", key);
102 public void sendMultipleKeys(String keys) {
103 String[] keyChain = keys.split(",");
104 Arrays.stream(keyChain).forEach(key -> {
105 sendKey(key, false, 1);
110 public Collection<Class<? extends ThingHandlerService>> getServices() {
111 return List.of(PlayerActions.class);