]> git.basschouten.com Git - openhab-addons.git/blob
36530027713526fb1b4d6e8c12a1cbfc7641ab5a
[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.constants.AsuswrtBindingConstants.*;
16 import static org.openhab.binding.asuswrt.internal.constants.AsuswrtBindingSettings.*;
17 import static org.openhab.binding.asuswrt.internal.helpers.AsuswrtUtils.*;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import com.google.gson.JsonObject;
24
25 /**
26  * The {@link AsuswrtIpInfo} class stores IP data.
27  *
28  * @author Christian Wild - Initial contribution
29  */
30 @NonNullByDefault
31 public class AsuswrtIpInfo {
32     private final Logger logger = LoggerFactory.getLogger(AsuswrtIpInfo.class);
33     private AsuswrtTraffic traffic = new AsuswrtTraffic();
34     private String ifName = "";
35     private String hwAddress = "";
36     private String ipAddress = "";
37     private String ipProto = "";
38     private String subnet = "";
39     private String gateway = "";
40     private String dnsServer = "";
41     private Boolean connected = false;
42
43     public AsuswrtIpInfo() {
44     }
45
46     /**
47      * Constructor.
48      *
49      * @param ifName name of interface
50      * @param jsonObject with ipInfo
51      */
52     public AsuswrtIpInfo(String ifName, JsonObject jsonObject) {
53         this.ifName = ifName;
54         traffic = new AsuswrtTraffic(ifName);
55         setData(jsonObject);
56     }
57
58     /*
59      * Setters
60      */
61
62     public void setData(JsonObject jsonObject) {
63         if (ifName.startsWith(INTERFACE_LAN)) {
64             logger.trace("(AsuswrtIpInfo) setData for interface {}", INTERFACE_LAN);
65             hwAddress = jsonObjectToString(jsonObject, JSON_MEMBER_MAC, hwAddress);
66             ipAddress = jsonObjectToString(jsonObject, JSON_MEMBER_LAN_IP, ipAddress);
67             subnet = jsonObjectToString(jsonObject, JSON_MEMBER_LAN_NETMASK, subnet);
68             gateway = jsonObjectToString(jsonObject, JSON_MEMBER_LAN_GATEWAY, gateway);
69             ipProto = jsonObjectToString(jsonObject, JSON_MEMBER_LAN_PROTO, ipProto);
70         } else if (ifName.startsWith(INTERFACE_WAN)) {
71             logger.trace("(AsuswrtIpInfo) setData for interface {}", INTERFACE_WAN);
72             hwAddress = jsonObjectToString(jsonObject, JSON_MEMBER_MAC, hwAddress);
73             ipAddress = jsonObjectToString(jsonObject, JSON_MEMBER_WAN_IP, ipAddress);
74             subnet = jsonObjectToString(jsonObject, JSON_MEMBER_WAN_NETMASK, subnet);
75             gateway = jsonObjectToString(jsonObject, JSON_MEMBER_WAN_GATEWAY, gateway);
76             ipProto = jsonObjectToString(jsonObject, JSON_MEMBER_WAN_PROTO, ipProto);
77             dnsServer = jsonObjectToString(jsonObject, JSON_MEMBER_WAN_DNS_SERVER, dnsServer);
78             connected = (jsonObjectToInt(jsonObject, JSON_MEMBER_WAN_CONNECTED) == 1);
79         }
80         if (jsonObject.has(JSON_MEMBER_TRAFFIC)) {
81             traffic.setData(jsonObject.getAsJsonObject(JSON_MEMBER_TRAFFIC));
82         }
83     }
84
85     /*
86      * Getters
87      */
88
89     public AsuswrtTraffic getTraffic() {
90         return traffic;
91     }
92
93     public String getMAC() {
94         return hwAddress;
95     }
96
97     public String getIpAddress() {
98         return ipAddress;
99     }
100
101     public String getSubnet() {
102         return subnet;
103     }
104
105     public String getGateway() {
106         return gateway;
107     }
108
109     public String getIpProto() {
110         return ipProto;
111     }
112
113     public String getDNSNServer() {
114         return dnsServer;
115     }
116
117     public String getName() {
118         return ifName;
119     }
120
121     public Boolean isConnected() {
122         return connected;
123     }
124 }