2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.vdr.internal.svdrp;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
21 * The {@link SVDRPDiskStatus} contains SVDRP Response Data for DiskStatus queries
23 * @author Matthias Klocke - Initial contribution
26 public class SVDRPDiskStatus {
27 private static final Pattern PATTERN_DISK_STATUS = Pattern.compile("([\\d]*)MB ([\\d]*)MB ([\\d]*)%");
29 private long megaBytesFree = -1;
30 private long megaBytesTotal = -1;
31 private int percentUsed = -1;
33 private SVDRPDiskStatus() {
37 * parse object from SVDRP Client Response
39 * @param message SVDRP Client Response
40 * @return Disk Status Object
41 * @throws SVDRPParseResponseException thrown if response data is not parseable
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)));
55 * Get Megabytes Free on Disk
57 * @return megabytes free
59 public long getMegaBytesFree() {
64 * Set Megabytes Free on Disk
66 * @param megaBytesFree megabytes free
68 public void setMegaBytesFree(long megaBytesFree) {
69 this.megaBytesFree = megaBytesFree;
73 * Get Megabytes Total on Disk
75 * @return megabytes total
77 public long getMegaBytesTotal() {
78 return megaBytesTotal;
82 * Set Megabytes Total on Disk
84 * @param megaBytesTotal megabytes total
86 public void setMegaBytesTotal(long megaBytesTotal) {
87 this.megaBytesTotal = megaBytesTotal;
91 * Get Percentage Used on Disk
93 * @return percentage used
95 public int getPercentUsed() {
100 * Set Percentage Used on Disk
102 * @param percentUsed percentage used
104 public void setPercentUsed(int percentUsed) {
105 this.percentUsed = percentUsed;
109 * String Representation of SVDRPDiskStatus Object
112 public String toString() {
113 StringBuilder sb = new StringBuilder();
114 if (megaBytesTotal >= 0) {
115 sb.append("Total: " + megaBytesTotal + "MB" + System.lineSeparator());
117 if (megaBytesFree >= 0) {
118 sb.append("Free: " + megaBytesFree + "MB" + System.lineSeparator());
120 if (percentUsed >= 0) {
121 sb.append("Free: " + percentUsed + "%" + System.lineSeparator());
123 return sb.toString();