]> git.basschouten.com Git - openhab-addons.git/blob
4680f176679d37e006bd64db881434a1fb79fbb9
[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.unifi.internal.api.dto;
14
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Objects;
20 import java.util.Set;
21 import java.util.function.Consumer;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27
28 import com.google.gson.JsonObject;
29
30 /**
31  * Data object to keep track of all port data, including all port_override data (both for ports and additional data) on
32  * a switch device.
33  *
34  * @author Hilbrand Bouwkamp - Initial contribution
35  */
36 @NonNullByDefault
37 public class UniFiSwitchPorts {
38
39     /**
40      * Port data grouped by port id.
41      */
42     private final Map<Integer, UniFiPortTuple> ports = new HashMap<>();
43     /**
44      * Additional none port specific override data. Keep track to send to device when updating override data.
45      */
46     private final Set<JsonObject> otherOverrides = new HashSet<>();
47
48     /**
49      * Return port data for the given port
50      *
51      * @param portIdx port to get the data for
52      * @return Return port data for the given port
53      */
54     public @Nullable UniFiPortTuple getPort(final int portIdx) {
55         return ports.get(portIdx);
56     }
57
58     /**
59      * Return port data for the given port or if none exists set a new data object and return it.
60      *
61      * @param portIdx port to get the data for
62      * @return Return port data for the given port or if none exists set a new data object and return it.
63      */
64     public UniFiPortTuple computeIfAbsent(final int portIdx) {
65         final UniFiPortTuple tuple = ports.computeIfAbsent(portIdx, t -> new UniFiPortTuple());
66         if (tuple == null) {
67             // This should never happen because ports can never contain a null value, and computeIfAbsent should never
68             // return null. However to satisfy the compiler a check for null was added.
69             throw new IllegalStateException("UniFiPortTuple is null for portIdx " + portIdx);
70         }
71         return tuple;
72     }
73
74     /**
75      * @return Returns the list of PoE Ports.
76      */
77     public List<UniFiPortTuple> getPoePorts() {
78         return ports.values().stream().filter(e -> e.getTable().isPortPoe()).collect(Collectors.toList());
79     }
80
81     /**
82      * Returns the override data as list with JSON objects after calling the updateMethod on the data for the given
83      * portIdx.
84      * The update method changes the data in the internal structure.
85      *
86      * @param portIdx port to call updateMethod for
87      * @param updateMethod method to call to update data for a specific port
88      * @return Returns a list of JSON objects of all override data
89      */
90     public List<JsonObject> updatedList(final int portIdx, final Consumer<UnfiPortOverrideJsonObject> updateMethod) {
91         @SuppressWarnings("null")
92         final List<UnfiPortOverrideJsonObject> updatedList = ports.entrySet().stream()
93                 .map(e -> e.getValue().getJsonElement()).filter(Objects::nonNull).collect(Collectors.toList());
94
95         updatedList.stream().filter(p -> p.getPortIdx() == portIdx).findAny().ifPresent(updateMethod::accept);
96
97         return Stream
98                 .concat(otherOverrides.stream(), updatedList.stream().map(UnfiPortOverrideJsonObject::getJsonObject))
99                 .collect(Collectors.toList());
100     }
101
102     /**
103      * Set the port override object. If it's for a specific port set bind it to the port data, otherwise store it as
104      * generic data.
105      *
106      * @param jsonObject JSON object to set
107      */
108     public void setOverride(final JsonObject jsonObject) {
109         if (UnfiPortOverrideJsonObject.hasPortIdx(jsonObject)) {
110             final UnfiPortOverrideJsonObject po = new UnfiPortOverrideJsonObject(jsonObject);
111             final UniFiPortTuple tuple = ports.get(po.getPortIdx());
112
113             if (tuple == null) {
114                 otherOverrides.add(jsonObject);
115             } else {
116                 tuple.setJsonElement(po);
117             }
118         } else {
119             otherOverrides.add(jsonObject);
120         }
121     }
122 }