]> git.basschouten.com Git - openhab-addons.git/blob
07e5dfa8fddaff841087c14051c7ba60ceec8be1
[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.mpd.internal.protocol;
14
15 import java.util.Map;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Class for representing the status of a Music Player Daemon.
23  *
24  * @author Stefan Röllin - Initial contribution
25  */
26 @NonNullByDefault
27 public class MPDStatus {
28
29     public enum State {
30         PLAY,
31         PAUSE,
32         STOP
33     }
34
35     private final Logger logger = LoggerFactory.getLogger(MPDStatus.class);
36
37     private final State state;
38     private final int volume;
39
40     public MPDStatus(MPDResponse response) {
41         Map<String, String> values = MPDResponseParser.responseToMap(response);
42         state = parseState(values.getOrDefault("state", ""));
43         volume = parseVolume(values.getOrDefault("volume", "0"));
44     }
45
46     public State getState() {
47         return state;
48     }
49
50     public int getVolume() {
51         return volume;
52     }
53
54     private State parseState(String value) {
55         switch (value) {
56             case "play":
57                 return State.PLAY;
58             case "pause":
59                 return State.PAUSE;
60             case "stop":
61                 return State.STOP;
62         }
63
64         return State.STOP;
65     }
66
67     private int parseVolume(String value) {
68         try {
69             return Integer.parseInt(value);
70         } catch (NumberFormatException e) {
71             logger.debug("parseVolume of {} failed", value);
72         }
73         return 0;
74     }
75 }