]> git.basschouten.com Git - openhab-addons.git/blob
930afce285ef4a8a69de31ff901a00af85bb45bd
[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.asuswrt.internal.structures;
14
15 import static org.openhab.binding.asuswrt.internal.helpers.AsuswrtUtils.jsonObjectToInt;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18
19 import com.google.gson.JsonObject;
20
21 /**
22  * The {@link AsuswrtUsage} class handles usage statistics
23  *
24  * @author Christian Wild - Initial contribution
25  */
26 @NonNullByDefault
27 public class AsuswrtUsage {
28     private Integer free = 0;
29     private Integer used = 0;
30     private Integer total = 0;
31
32     public AsuswrtUsage() {
33     }
34
35     /**
36      * Constructor.
37      *
38      * @param jsonObject jsonObject data is stored
39      * @param totalKey name of key total available is stored
40      * @param usedKey name of key used is stored
41      */
42     public AsuswrtUsage(JsonObject jsonObject, String totalKey, String usedKey) {
43         setData(jsonObject, totalKey, usedKey);
44     }
45
46     /**
47      * Constructor.
48      *
49      * @param totalUsage the total usage
50      * @param used the usage
51      */
52     public AsuswrtUsage(Integer totalUsage, Integer used) {
53         setData(totalUsage, used);
54     }
55
56     /*
57      * Setters
58      */
59
60     /**
61      * Sets the usage data from a JSON object.
62      *
63      * @param jsonObject the JSON object containing the data
64      * @param totalKey the key name with the 'total available' value
65      * @param usedKey the key name with the 'used' value
66      */
67     public void setData(JsonObject jsonObject, String totalKey, String usedKey) {
68         total = jsonObjectToInt(jsonObject, totalKey, 0);
69         used = jsonObjectToInt(jsonObject, usedKey, 0);
70         free = total - used;
71     }
72
73     /**
74      * Sets usage data from integer values.
75      *
76      * @param totalUsage the total available value
77      * @param used the used value
78      */
79     public void setData(Integer totalUsage, Integer used) {
80         total = totalUsage;
81         this.used = used;
82         free = total - used;
83     }
84
85     /*
86      * Getters
87      */
88
89     public Integer getTotal() {
90         return total;
91     }
92
93     public Integer getUsed() {
94         return used;
95     }
96
97     public Integer getFree() {
98         return free;
99     }
100
101     public Integer getUsedPercent() {
102         if (total > 0) {
103             return ((used * 100) / total);
104         }
105         return 0;
106     }
107
108     public Integer getFreePercent() {
109         if (total > 0) {
110             return ((free * 100) / total);
111         }
112         return 0;
113     }
114 }