]> git.basschouten.com Git - openhab-addons.git/blob
5141e46743387b6040407bb870588c3e43c8a44c
[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.api.rest;
14
15 import java.util.Map;
16
17 import javax.ws.rs.core.UriBuilder;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
22 import org.openhab.binding.freeboxos.internal.api.Response;
23 import org.openhab.binding.freeboxos.internal.api.rest.MediaReceiverManager.Receiver;
24
25 /**
26  * The {@link MediaReceiverManager} is the Java class used to handle api requests related to air media receivers
27  *
28  * @author GaĆ«l L'hopital - Initial contribution
29  */
30 @NonNullByDefault
31 public class MediaReceiverManager extends ListableRest<Receiver, MediaReceiverManager.ReceiverResponse> {
32     private static final String SUB_PATH = "receivers";
33
34     public static record Receiver(boolean passwordProtected, //
35             Map<MediaType, Boolean> capabilities, //
36             String name // This name is the UPnP name of the host
37     ) {
38     }
39
40     protected static class ReceiverResponse extends Response<Receiver> {
41     }
42
43     public enum Action {
44         START,
45         STOP,
46         UNKNOWN
47     }
48
49     public enum MediaType {
50         VIDEO,
51         PHOTO,
52         AUDIO,
53         SCREEN,
54         UNKNOWN
55     }
56
57     private static record Request(String password, String action, String mediaType, @Nullable String media,
58             int position) {
59         Request(String password, Action action, MediaType mediaType, @Nullable String media, int position) {
60             this(password, action.name().toLowerCase(), mediaType.name().toLowerCase(), media, position);
61         }
62     }
63
64     public MediaReceiverManager(FreeboxOsSession session, UriBuilder uriBuilder) throws FreeboxException {
65         super(session, LoginManager.Permission.NONE, ReceiverResponse.class, uriBuilder.path(SUB_PATH));
66     }
67
68     public @Nullable Receiver getReceiver(String receiverName) throws FreeboxException {
69         return getDevices().stream().filter(rcv -> receiverName.equals(rcv.name())).findFirst().orElse(null);
70     }
71
72     public void sendToReceiver(String receiver, String password, Action action, MediaType type)
73             throws FreeboxException {
74         sendToReceiver(receiver, new Request(password, action, type, null, 0));
75     }
76
77     public void sendToReceiver(String receiver, String password, Action action, MediaType type, String url)
78             throws FreeboxException {
79         sendToReceiver(receiver, new Request(password, action, type, url, 0));
80     }
81
82     private void sendToReceiver(String receiver, Request payload) throws FreeboxException {
83         post(payload, receiver);
84     }
85 }