]> git.basschouten.com Git - openhab-addons.git/blob
5a87f4664d505815f7d460aa4ef1f6f9ff4e1ede
[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.doorbird.internal.audio;
14
15 import java.io.IOException;
16 import java.util.Locale;
17 import java.util.Set;
18
19 import javax.sound.sampled.UnsupportedAudioFileException;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.doorbird.internal.handler.DoorbellHandler;
24 import org.openhab.core.audio.AudioFormat;
25 import org.openhab.core.audio.AudioSinkSync;
26 import org.openhab.core.audio.AudioStream;
27 import org.openhab.core.audio.UnsupportedAudioFormatException;
28 import org.openhab.core.audio.UnsupportedAudioStreamException;
29 import org.openhab.core.library.types.PercentType;
30
31 /**
32  * The audio sink for doorbird
33  *
34  * @author Gwendal Roulleau - Initial contribution
35  *
36  */
37 @NonNullByDefault
38 public class DoorbirdAudioSink extends AudioSinkSync {
39
40     private static final Set<AudioFormat> SUPPORTED_FORMATS = Set.of(AudioFormat.WAV);
41     private static final Set<Class<? extends AudioStream>> SUPPORTED_STREAMS = Set.of(AudioStream.class);
42
43     private DoorbellHandler doorbellHandler;
44
45     public DoorbirdAudioSink(DoorbellHandler doorbellHandler) {
46         this.doorbellHandler = doorbellHandler;
47     }
48
49     @Override
50     public String getId() {
51         return doorbellHandler.getThing().getUID().toString();
52     }
53
54     @Override
55     public @Nullable String getLabel(@Nullable Locale locale) {
56         return doorbellHandler.getThing().getLabel();
57     }
58
59     @Override
60     protected void processSynchronously(@Nullable AudioStream audioStream)
61             throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
62         if (audioStream == null) {
63             return;
64         }
65         try (ConvertedInputStream normalizedULAWStream = new ConvertedInputStream(audioStream)) {
66             doorbellHandler.sendAudio(normalizedULAWStream);
67         } catch (UnsupportedAudioFileException | IOException e) {
68             throw new UnsupportedAudioFormatException("Cannot send to the doorbird sink", audioStream.getFormat(), e);
69         }
70     }
71
72     @Override
73     public Set<AudioFormat> getSupportedFormats() {
74         return SUPPORTED_FORMATS;
75     }
76
77     @Override
78     public Set<Class<? extends AudioStream>> getSupportedStreams() {
79         return SUPPORTED_STREAMS;
80     }
81
82     @Override
83     public PercentType getVolume() {
84         return new PercentType(100);
85     }
86
87     @Override
88     public void setVolume(PercentType volume) {
89         throw new UnsupportedOperationException("Volume can not be set");
90     }
91 }