]> git.basschouten.com Git - openhab-addons.git/blob
174062cca1fda44fdd5383dab9c666a890fade58
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.cache;
14
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Objects;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.function.Function;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.unifi.internal.api.dto.UniFiClient;
27 import org.openhab.binding.unifi.internal.api.dto.UniFiDevice;
28 import org.openhab.binding.unifi.internal.api.dto.UniFiPortTable;
29 import org.openhab.binding.unifi.internal.api.dto.UniFiSite;
30 import org.openhab.binding.unifi.internal.api.dto.UniFiWlan;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Class to manager cache for the controller keeping track of all specific cache objects.
36  *
37  * @author Matthew Bowman - Initial contribution
38  * @author Hilbrand Bouwkamp - Moved cache to this dedicated class.
39  */
40 @NonNullByDefault
41 public class UniFiControllerCache {
42
43     private final Logger logger = LoggerFactory.getLogger(UniFiControllerCache.class);
44
45     private final UniFiSiteCache sitesCache = new UniFiSiteCache();
46     private final UniFiWlanCache wlansCache = new UniFiWlanCache();
47     private final UniFiDeviceCache devicesCache = new UniFiDeviceCache();
48     private final UniFiClientCache clientsCache = new UniFiClientCache();
49     private final UniFiClientCache insightsCache = new UniFiClientCache();
50     private final Map<String, Map<Integer, UniFiPortTable>> devicesToPortTables = new ConcurrentHashMap<>();
51
52     public void clear() {
53         sitesCache.clear();
54         wlansCache.clear();
55         devicesCache.clear();
56         clientsCache.clear();
57         insightsCache.clear();
58     }
59
60     // Sites Cache
61
62     public List<UniFiSite> setSites(final UniFiSite @Nullable [] sites) {
63         sitesCache.putAll(sites);
64         return List.of(sites);
65     }
66
67     public @Nullable UniFiSite getSite(final @Nullable String id) {
68         return sitesCache.get(id);
69     }
70
71     public Collection<UniFiSite> getSites() {
72         return sitesCache.values();
73     }
74
75     // Wlans Cache
76
77     public void putWlans(final UniFiWlan @Nullable [] wlans) {
78         wlansCache.putAll(wlans);
79     }
80
81     public @Nullable UniFiWlan getWlan(@Nullable final String id) {
82         return wlansCache.get(id);
83     }
84
85     public Collection<UniFiWlan> getWlans() {
86         return wlansCache.values();
87     }
88
89     // Devices Cache
90
91     public void putDevices(final UniFiDevice @Nullable [] devices) {
92         devicesCache.putAll(devices);
93         if (devices != null) {
94             Stream.of(devices).filter(Objects::nonNull).forEach(d -> {
95                 Stream.ofNullable(d.getPortTable()).filter(ptl -> ptl.length > 0 && ptl[0].isPortPoe()).forEach(pt -> {
96                     Stream.of(pt).forEach(p -> p.setDevice(d));
97                     devicesToPortTables.put(d.getMac(),
98                             Stream.of(pt).collect(Collectors.toMap(UniFiPortTable::getPortIdx, Function.identity())));
99                 });
100             });
101         }
102     }
103
104     public @Nullable UniFiDevice getDevice(@Nullable final String id) {
105         return devicesCache.get(id);
106     }
107
108     public Map<Integer, UniFiPortTable> getSwitchPorts(@Nullable final String deviceId) {
109         return deviceId == null ? Map.of() : devicesToPortTables.getOrDefault(deviceId, Map.of());
110     }
111
112     public Collection<Map<Integer, UniFiPortTable>> getSwitchPorts() {
113         return devicesToPortTables.values();
114     }
115
116     // Clients Cache
117
118     public void putClients(final UniFiClient @Nullable [] clients) {
119         clientsCache.putAll(clients);
120     }
121
122     public Collection<UniFiClient> getClients() {
123         return clientsCache.values();
124     }
125
126     public long countClients(final UniFiSite site, final Function<UniFiClient, Boolean> filter) {
127         return getClients().stream().filter(c -> site.isSite(c.getSite())).filter(filter::apply).count();
128     }
129
130     public @Nullable UniFiClient getClient(@Nullable final String cid) {
131         UniFiClient client = null;
132         if (cid != null && !cid.isBlank()) {
133             synchronized (this) {
134                 // mgb: first check active clients and fallback to insights if not found
135                 client = clientsCache.get(cid);
136                 if (client == null) {
137                     final String id = clientsCache.getId(cid);
138
139                     client = insightsCache.get(id == null ? cid : id);
140                 }
141             }
142             if (client == null) {
143                 logger.debug("Could not find a matching client for cid = {}", cid);
144             }
145         }
146         return client;
147     }
148
149     public synchronized Stream<UniFiClient> getClientStreamForSite(final UniFiSite site) {
150         return clientsCache.values().stream().filter(client -> client.getSite().equals(site));
151     }
152
153     // Insights Cache
154
155     public void putInsights(final UniFiClient @Nullable [] insights) {
156         insightsCache.putAll(insights);
157     }
158 }