]> git.basschouten.com Git - openhab-addons.git/blob
7d4823ebffff5d0946b837c39f1ba45dfda67047
[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.pulseaudio.internal.items;
14
15 import java.util.List;
16 import java.util.Map;
17 import java.util.regex.Pattern;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.pulseaudio.internal.handler.DeviceIdentifier;
22
23 /**
24  * GenericAudioItems are any kind of items that deal with audio data and can be
25  * muted or their volume can be changed.
26  *
27  * @author Tobias Bräutigam - Initial contribution
28  */
29 @NonNullByDefault
30 public abstract class AbstractAudioDeviceConfig extends AbstractDeviceConfig {
31
32     public enum State {
33         SUSPENDED,
34         IDLE,
35         RUNNING,
36         CORKED,
37         DRAINED
38     }
39
40     protected @Nullable State state;
41     protected boolean muted;
42     protected int volume;
43     protected @Nullable Module module;
44     protected String secondaryIdentifier;
45     protected Map<String, String> properties;
46
47     public AbstractAudioDeviceConfig(int id, String name, @Nullable String secondaryIdentifier,
48             Map<String, String> properties, @Nullable Module module) {
49         super(id, name);
50         this.module = module;
51         this.secondaryIdentifier = secondaryIdentifier == null ? "" : secondaryIdentifier;
52         this.properties = properties;
53     }
54
55     /**
56      *
57      * @param deviceIdentifier The device identifier to check against
58      * @return true if this device match the requested identifier, false otherwise
59      */
60     public boolean matches(DeviceIdentifier deviceIdentifier) {
61         boolean matches = getPaName().equalsIgnoreCase(deviceIdentifier.getNameOrDescription())
62                 || secondaryIdentifier.equalsIgnoreCase(deviceIdentifier.getNameOrDescription());
63         if (!matches) {
64             return false; // stop analysis right here, no need to parse properties
65         } else {
66             List<Pattern> additionalFilters = deviceIdentifier.getAdditionalFilters();
67             if (additionalFilters.isEmpty()) { // the additionalFilter property is not defined, don't check against
68                 return true;
69             } else {
70                 for (Pattern patternToMatch : additionalFilters) {
71                     if (!properties.values().stream().anyMatch(value -> patternToMatch.matcher(value).find())) {
72                         return false;
73                     }
74                 }
75                 return true;
76             }
77         }
78     }
79
80     public @Nullable Module getModule() {
81         return module;
82     }
83
84     public @Nullable State getState() {
85         return state;
86     }
87
88     public void setState(State state) {
89         this.state = state;
90     }
91
92     public boolean isMuted() {
93         return muted;
94     }
95
96     public void setMuted(boolean muted) {
97         this.muted = muted;
98     }
99
100     public int getVolume() {
101         return volume;
102     }
103
104     public void setVolume(int volume) {
105         this.volume = volume;
106     }
107
108     @Override
109     public String toString() {
110         return this.getClass().getSimpleName() + " #" + id + " (Module: " + module + ") " + name + ", muted: " + muted
111                 + ", state: " + state + ", volume: " + volume;
112     }
113 }