2 * Copyright (c) 2010-2023 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.asuswrt.internal.structures;
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;
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.stream.Collectors;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.gson.JsonObject;
31 * The {@link AsuswrtClientList} class stores a list of {@link AsuswrtClientInfo}.
33 * @author Christian Wild - Initial contribution
36 public class AsuswrtClientList implements Iterable<AsuswrtClientInfo> {
37 private final Logger logger = LoggerFactory.getLogger(AsuswrtClientList.class);
38 private List<AsuswrtClientInfo> clientList = new ArrayList<>();
40 public AsuswrtClientList() {
43 public AsuswrtClientList(JsonObject jsonObject) {
48 public Iterator<AsuswrtClientInfo> iterator() {
49 return clientList.iterator();
53 * Sets the {@link AsuswrtClientList} using a {@link JsonObject}.
55 public void setData(JsonObject jsonObject) {
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);
68 logger.trace("getClientlist: {} '{}'", ERR_INVALID_MAC_ADDRESS, macAddress);
71 } catch (Exception e) {
72 logger.debug("getClientlist: {} - {}'", ERR_JSON_FORMAT, e.getMessage());
77 * Adds {@link AsuswrtClientInfo} to the list.
79 private void addClient(AsuswrtClientInfo clientInfo) {
80 clientList.add(clientInfo);
88 * Gets {@link AsuswrtClientInfo} from the list for a client based on its name.
90 * @param clientName the name of the client for which the info is returned
92 public AsuswrtClientInfo getClientByName(String clientName) {
93 for (AsuswrtClientInfo client : this.clientList) {
94 if (client.getName().equals(clientName)) {
98 return new AsuswrtClientInfo();
102 * Gets {@link AsuswrtClientInfo} from the list for a client based on its MAC address.
104 * @param clientMAC the MAC address of the client for which the info is returned
106 public AsuswrtClientInfo getClientByMAC(String clientMAC) {
107 for (AsuswrtClientInfo client : this.clientList) {
108 if (client.getMac().equals(clientMAC)) {
112 return new AsuswrtClientInfo();
116 * Gets {@link AsuswrtClientInfo} from the list for a client based on its IP address.
118 * @param clientIP the IP address of the client for which the info is returned
120 public AsuswrtClientInfo getClientByIP(String clientIP) {
121 for (AsuswrtClientInfo client : this.clientList) {
122 if (client.getIP().equals(clientIP)) {
126 return new AsuswrtClientInfo();
130 * Returns a <code>;</code> separated list with client names and MAC addresses.
132 public String getClientList() {
133 StringBuilder clients = new StringBuilder();
134 for (AsuswrtClientInfo client : this.clientList) {
135 clients.append(client.getName() + " [" + client.getMac() + "]; ");
137 return clients.toString();
141 * Returns a <code>;</code> separated list with client names.
143 public String getClientNames() {
144 return clientList.stream().map(AsuswrtClientInfo::getName).collect(Collectors.joining("; "));
148 * Returns the number of clients in the list.
150 public Integer getCount() {
151 return clientList.size();
155 * Returns a <code>;</code> separated list with MAC addresses.
157 public String getMacAddresses() {
158 StringBuilder clients = new StringBuilder();
159 for (AsuswrtClientInfo client : this.clientList) {
160 clients.append(client.getMac() + "; ");
162 return clients.toString();
166 * Returns a {@link AsuswrtClientList} of online clients.
168 public AsuswrtClientList getOnlineClients() {
169 AsuswrtClientList clients = new AsuswrtClientList();
170 for (AsuswrtClientInfo client : this.clientList) {
171 if (client.isOnline()) {
172 clients.addClient(client);