]> git.basschouten.com Git - openhab-addons.git/blob
714952984146899bd9d10e626a9de0dea1ed30ff
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.vdr.internal.svdrp;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16
17 /**
18  * The {@link SVDRPVolume} contains SVDRP Response Data for Volume Object
19  *
20  * @author Matthias Klocke - Initial contribution
21  */
22 @NonNullByDefault
23 public class SVDRPVolume {
24
25     private int volume = -1;
26
27     private SVDRPVolume() {
28     }
29
30     /**
31      * parse object from SVDRP Client Response
32      *
33      * @param message SVDRP Client Response
34      * @return Volume Object
35      * @throws SVDRPParseResponseException thrown if response data is not parseable
36      */
37     public static SVDRPVolume parse(String message) throws SVDRPParseResponseException {
38         SVDRPVolume volume = new SVDRPVolume();
39         try {
40             String vol = message.substring(message.lastIndexOf(" ") + 1, message.length());
41             if ("mute".equals(vol)) {
42                 volume.setVolume(0);
43             } else {
44                 int val = Integer.parseInt(vol);
45                 val = val * 100 / 255;
46                 volume.setVolume(val);
47             }
48         } catch (NumberFormatException nex) {
49             throw new SVDRPParseResponseException(nex.getMessage(), nex);
50         } catch (IndexOutOfBoundsException ie) {
51             throw new SVDRPParseResponseException(ie.getMessage(), ie);
52         }
53         return volume;
54     }
55
56     /**
57      * Get Volume in Percent
58      *
59      * @param volume Volume in Percent
60      */
61     private void setVolume(int volume) {
62         this.volume = volume;
63     }
64
65     /**
66      * Set Volume in Percent
67      *
68      * @return Volume in Percent
69      */
70     public int getVolume() {
71         return volume;
72     }
73
74     /**
75      * String Representation of SVDRPDiskStatus Object
76      */
77     @Override
78     public String toString() {
79         StringBuilder sb = new StringBuilder();
80         if (volume > -1) {
81             sb.append("Volume: ");
82             sb.append(String.valueOf(volume));
83         }
84         return sb.toString();
85     }
86 }