]> git.basschouten.com Git - openhab-addons.git/blob
7b671baaf7ac780277e5755bdd27cc81583b2832
[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, Action action, MediaType mediaType, @Nullable String media,
58             int position) {
59     }
60
61     public MediaReceiverManager(FreeboxOsSession session, UriBuilder uriBuilder) throws FreeboxException {
62         super(session, LoginManager.Permission.NONE, ReceiverResponse.class, uriBuilder.path(SUB_PATH));
63     }
64
65     public @Nullable Receiver getReceiver(String receiverName) throws FreeboxException {
66         return getDevices().stream().filter(rcv -> receiverName.equals(rcv.name())).findFirst().orElse(null);
67     }
68
69     public void sendToReceiver(String receiver, String password, Action action, MediaType type)
70             throws FreeboxException {
71         sendToReceiver(receiver, new Request(password, action, type, null, 0));
72     }
73
74     public void sendToReceiver(String receiver, String password, Action action, MediaType type, String url)
75             throws FreeboxException {
76         sendToReceiver(receiver, new Request(password, action, type, url, 0));
77     }
78
79     private void sendToReceiver(String receiver, Request payload) throws FreeboxException {
80         post(payload, GenericResponse.class, receiver);
81     }
82 }