]> git.basschouten.com Git - openhab-addons.git/blob
be0a10f85e3c0c6d4e39b8f44722d57991f92895
[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.AsuswrtErrorConstants.*;
17 import static org.openhab.binding.asuswrt.internal.helpers.AsuswrtUtils.isValidMacAddress;
18
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.stream.Collectors;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.google.gson.JsonObject;
29
30 /**
31  * The {@link AsuswrtClientList} class stores a list of {@link AsuswrtClientInfo}.
32  *
33  * @author Christian Wild - Initial contribution
34  */
35 @NonNullByDefault
36 public class AsuswrtClientList implements Iterable<AsuswrtClientInfo> {
37     private final Logger logger = LoggerFactory.getLogger(AsuswrtClientList.class);
38     private List<AsuswrtClientInfo> clientList = new ArrayList<>();
39
40     public AsuswrtClientList() {
41     }
42
43     public AsuswrtClientList(JsonObject jsonObject) {
44         setData(jsonObject);
45     }
46
47     @Override
48     public Iterator<AsuswrtClientInfo> iterator() {
49         return clientList.iterator();
50     }
51
52     /**
53      * Sets the {@link AsuswrtClientList} using a {@link JsonObject}.
54      */
55     public void setData(JsonObject jsonObject) {
56         clientList.clear();
57         try {
58             JsonObject jsonList = jsonObject.getAsJsonObject(JSON_MEMBER_CLIENTS);
59             // Remove the member MAC list, it contains only online clients
60             jsonList.remove(JSON_MEMBER_MACLIST);
61             jsonList.remove(JSON_MEMBER_API_LEVEL);
62             // Iterate over the MAC addresses
63             jsonList.keySet().forEach(macAddress -> {
64                 if (isValidMacAddress(macAddress)) {
65                     AsuswrtClientInfo clientInfo = new AsuswrtClientInfo(jsonList.getAsJsonObject(macAddress));
66                     addClient(clientInfo);
67                 } else {
68                     logger.trace("getClientlist: {} '{}'", ERR_INVALID_MAC_ADDRESS, macAddress);
69                 }
70             });
71         } catch (Exception e) {
72             logger.debug("getClientlist: {} - {}'", ERR_JSON_FORMAT, e.getMessage());
73         }
74     }
75
76     /**
77      * Adds {@link AsuswrtClientInfo} to the list.
78      */
79     private void addClient(AsuswrtClientInfo clientInfo) {
80         clientList.add(clientInfo);
81     }
82
83     /*
84      * Getters
85      */
86
87     /**
88      * Gets {@link AsuswrtClientInfo} from the list for a client based on its name.
89      *
90      * @param clientName the name of the client for which the info is returned
91      */
92     public AsuswrtClientInfo getClientByName(String clientName) {
93         for (AsuswrtClientInfo client : this.clientList) {
94             if (client.getName().equals(clientName)) {
95                 return client;
96             }
97         }
98         return new AsuswrtClientInfo();
99     }
100
101     /**
102      * Gets {@link AsuswrtClientInfo} from the list for a client based on its MAC address.
103      *
104      * @param clientMAC the MAC address of the client for which the info is returned
105      */
106     public AsuswrtClientInfo getClientByMAC(String clientMAC) {
107         for (AsuswrtClientInfo client : this.clientList) {
108             if (client.getMac().equals(clientMAC)) {
109                 return client;
110             }
111         }
112         return new AsuswrtClientInfo();
113     }
114
115     /**
116      * Gets {@link AsuswrtClientInfo} from the list for a client based on its IP address.
117      *
118      * @param clientIP the IP address of the client for which the info is returned
119      */
120     public AsuswrtClientInfo getClientByIP(String clientIP) {
121         for (AsuswrtClientInfo client : this.clientList) {
122             if (client.getIP().equals(clientIP)) {
123                 return client;
124             }
125         }
126         return new AsuswrtClientInfo();
127     }
128
129     /*
130      * Returns a <code>;</code> separated list with client names and MAC addresses.
131      */
132     public String getClientList() {
133         StringBuilder clients = new StringBuilder();
134         for (AsuswrtClientInfo client : this.clientList) {
135             clients.append(client.getName() + " [" + client.getMac() + "]; ");
136         }
137         return clients.toString();
138     }
139
140     /*
141      * Returns a <code>;</code> separated list with client names.
142      */
143     public String getClientNames() {
144         return clientList.stream().map(AsuswrtClientInfo::getName).collect(Collectors.joining("; "));
145     }
146
147     /**
148      * Returns the number of clients in the list.
149      */
150     public Integer getCount() {
151         return clientList.size();
152     }
153
154     /*
155      * Returns a <code>;</code> separated list with MAC addresses.
156      */
157     public String getMacAddresses() {
158         StringBuilder clients = new StringBuilder();
159         for (AsuswrtClientInfo client : this.clientList) {
160             clients.append(client.getMac() + "; ");
161         }
162         return clients.toString();
163     }
164
165     /**
166      * Returns a {@link AsuswrtClientList} of online clients.
167      */
168     public AsuswrtClientList getOnlineClients() {
169         AsuswrtClientList clients = new AsuswrtClientList();
170         for (AsuswrtClientInfo client : this.clientList) {
171             if (client.isOnline()) {
172                 clients.addClient(client);
173             }
174         }
175         return clients;
176     }
177 }