]> git.basschouten.com Git - openhab-addons.git/blob
08e266168b552b79bbea6b7ac90401d3dd8f95ef
[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.onkyo.internal.handler;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.openhab.binding.onkyo.internal.OnkyoBindingConstants;
19 import org.openhab.core.io.transport.upnp.UpnpIOParticipant;
20 import org.openhab.core.io.transport.upnp.UpnpIOService;
21 import org.openhab.core.library.types.StringType;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.binding.BaseThingHandler;
24 import org.openhab.core.types.Command;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * The {@link OnkyoUpnpHandler} is a base class for ThingHandlers for devices which support UPnP playback.
30  *
31  * @author Paul Frank - Initial contribution
32  * @author Laurent Garnier - Separated into OnkyoUpnpHandler and OnkyoAudioSink
33  */
34 public abstract class OnkyoUpnpHandler extends BaseThingHandler implements UpnpIOParticipant {
35
36     private final Logger logger = LoggerFactory.getLogger(OnkyoUpnpHandler.class);
37
38     private UpnpIOService service;
39
40     public OnkyoUpnpHandler(Thing thing, UpnpIOService upnpIOService) {
41         super(thing);
42         this.service = upnpIOService;
43     }
44
45     protected void handlePlayUri(Command command) {
46         if (command instanceof StringType) {
47             try {
48                 playMedia(command.toString());
49
50             } catch (IllegalStateException e) {
51                 logger.warn("Cannot play URI ({})", e.getMessage());
52             }
53         }
54     }
55
56     public void playMedia(String url) {
57         stop();
58         removeAllTracksFromQueue();
59
60         if (!url.startsWith("x-") && (!url.startsWith("http"))) {
61             url = "x-file-cifs:" + url;
62         }
63
64         setCurrentURI(url, "");
65
66         play();
67     }
68
69     public void stop() {
70         Map<String, String> inputs = new HashMap<>();
71         inputs.put("InstanceID", "0");
72
73         Map<String, String> result = service.invokeAction(this, "AVTransport", "Stop", inputs);
74
75         for (String variable : result.keySet()) {
76             this.onValueReceived(variable, result.get(variable), "AVTransport");
77         }
78     }
79
80     private void play() {
81         Map<String, String> inputs = new HashMap<>();
82         inputs.put("InstanceID", "0");
83         inputs.put("Speed", "1");
84         Map<String, String> result = service.invokeAction(this, "AVTransport", "Play", inputs);
85
86         for (String variable : result.keySet()) {
87             this.onValueReceived(variable, result.get(variable), "AVTransport");
88         }
89     }
90
91     private void removeAllTracksFromQueue() {
92         Map<String, String> inputs = new HashMap<>();
93         inputs.put("InstanceID", "0");
94
95         Map<String, String> result = service.invokeAction(this, "AVTransport", "RemoveAllTracksFromQueue", inputs);
96
97         for (String variable : result.keySet()) {
98             this.onValueReceived(variable, result.get(variable), "AVTransport");
99         }
100     }
101
102     private void setCurrentURI(String uri, String uriMetaData) {
103         if (uri != null && uriMetaData != null) {
104             Map<String, String> inputs = new HashMap<>();
105
106             try {
107                 inputs.put("InstanceID", "0");
108                 inputs.put("CurrentURI", uri);
109                 inputs.put("CurrentURIMetaData", uriMetaData);
110             } catch (NumberFormatException ex) {
111                 logger.error("Action Invalid Value Format Exception {}", ex.getMessage());
112             }
113
114             Map<String, String> result = service.invokeAction(this, "AVTransport", "SetAVTransportURI", inputs);
115
116             for (String variable : result.keySet()) {
117                 this.onValueReceived(variable, result.get(variable), "AVTransport");
118             }
119         }
120     }
121
122     @Override
123     public String getUDN() {
124         return (String) this.getConfig().get(OnkyoBindingConstants.UDN_PARAMETER);
125     }
126
127     @Override
128     public void onValueReceived(String variable, String value, String service) {
129         logger.debug("received variable {} with value {} from service {}", variable, value, service);
130     }
131
132     @Override
133     public void onServiceSubscribed(String service, boolean succeeded) {
134     }
135
136     @Override
137     public void onStatusChanged(boolean status) {
138     }
139 }