]> git.basschouten.com Git - openhab-addons.git/blob
0145ef735da0160a6ff0c1b8ffb1d724de93a673
[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.vdr.internal.svdrp;
14
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19
20 /**
21  * The {@link SVDRPDiskStatus} contains SVDRP Response Data for DiskStatus queries
22  *
23  * @author Matthias Klocke - Initial contribution
24  */
25 @NonNullByDefault
26 public class SVDRPDiskStatus {
27     private static final Pattern PATTERN_DISK_STATUS = Pattern.compile("([\\d]*)MB ([\\d]*)MB ([\\d]*)%");
28
29     private long megaBytesFree = -1;
30     private long megaBytesTotal = -1;
31     private int percentUsed = -1;
32
33     private SVDRPDiskStatus() {
34     }
35
36     /**
37      * parse object from SVDRP Client Response
38      *
39      * @param message SVDRP Client Response
40      * @return Disk Status Object
41      * @throws SVDRPParseResponseException thrown if response data is not parseable
42      */
43     public static SVDRPDiskStatus parse(String message) throws SVDRPParseResponseException {
44         SVDRPDiskStatus status = new SVDRPDiskStatus();
45         Matcher matcher = PATTERN_DISK_STATUS.matcher(message);
46         if (matcher.find() && matcher.groupCount() == 3) {
47             status.setMegaBytesTotal(Long.parseLong(matcher.group(1)));
48             status.setMegaBytesFree(Long.parseLong(matcher.group(2)));
49             status.setPercentUsed(Integer.parseInt(matcher.group(3)));
50         }
51         return status;
52     }
53
54     /**
55      * Get Megabytes Free on Disk
56      *
57      * @return megabytes free
58      */
59     public long getMegaBytesFree() {
60         return megaBytesFree;
61     }
62
63     /**
64      * Set Megabytes Free on Disk
65      *
66      * @param megaBytesFree megabytes free
67      */
68     public void setMegaBytesFree(long megaBytesFree) {
69         this.megaBytesFree = megaBytesFree;
70     }
71
72     /**
73      * Get Megabytes Total on Disk
74      *
75      * @return megabytes total
76      */
77     public long getMegaBytesTotal() {
78         return megaBytesTotal;
79     }
80
81     /**
82      * Set Megabytes Total on Disk
83      *
84      * @param megaBytesTotal megabytes total
85      */
86     public void setMegaBytesTotal(long megaBytesTotal) {
87         this.megaBytesTotal = megaBytesTotal;
88     }
89
90     /**
91      * Get Percentage Used on Disk
92      *
93      * @return percentage used
94      */
95     public int getPercentUsed() {
96         return percentUsed;
97     }
98
99     /**
100      * Set Percentage Used on Disk
101      *
102      * @param percentUsed percentage used
103      */
104     public void setPercentUsed(int percentUsed) {
105         this.percentUsed = percentUsed;
106     }
107
108     /**
109      * String Representation of SVDRPDiskStatus Object
110      */
111     @Override
112     public String toString() {
113         StringBuilder sb = new StringBuilder();
114         if (megaBytesTotal >= 0) {
115             sb.append("Total: " + megaBytesTotal + "MB" + System.lineSeparator());
116         }
117         if (megaBytesFree >= 0) {
118             sb.append("Free: " + megaBytesFree + "MB" + System.lineSeparator());
119         }
120         if (percentUsed >= 0) {
121             sb.append("Free: " + percentUsed + "%" + System.lineSeparator());
122         }
123         return sb.toString();
124     }
125 }