Fix online/blocked channels. (#11451)

Fixes #7001

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
jlaur 2021-10-27 07:57:50 +02:00 committed by GitHub
parent 4d5fd84c49
commit 7ec833df18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 8 deletions

View File

@ -26,12 +26,14 @@ import org.openhab.binding.unifi.internal.api.model.UniFiClient;
public class UniFiClientCache extends UniFiCache<UniFiClient> {
public UniFiClientCache() {
super(PREFIX_MAC, PREFIX_IP, PREFIX_HOSTNAME, PREFIX_ALIAS);
super(PREFIX_ID, PREFIX_MAC, PREFIX_IP, PREFIX_HOSTNAME, PREFIX_ALIAS);
}
@Override
protected String getSuffix(UniFiClient client, String prefix) {
switch (prefix) {
case PREFIX_ID:
return client.getId();
case PREFIX_MAC:
return client.getMac();
case PREFIX_IP:

View File

@ -120,7 +120,7 @@ public abstract class UniFiClient {
@Override
public String toString() {
return String.format(
"UniFiClient{mac: '%s', ip: '%s', hostname: '%s', alias: '%s', wired: %b, blocked: %b, device: %s}",
mac, ip, hostname, alias, isWired(), blocked, getDevice());
"UniFiClient{id: '%s', mac: '%s', ip: '%s', hostname: '%s', alias: '%s', wired: %b, blocked: %b, device: %s}",
id, mac, ip, hostname, alias, isWired(), blocked, getDevice());
}
}

View File

@ -13,6 +13,8 @@
package org.openhab.binding.unifi.internal.api.model;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@ -40,12 +42,15 @@ import com.google.gson.GsonBuilder;
*
* @author Matthew Bowman - Initial contribution
* @author Patrik Wimnell - Blocking / Unblocking client support
* @author Jacob Laursen - Fix online/blocked channels (broken by UniFi Controller 5.12.35)
*/
@NonNullByDefault
public class UniFiController {
private final Logger logger = LoggerFactory.getLogger(UniFiController.class);
private Map<String, String> cidToIdCache = new ConcurrentHashMap<String, String>();
private UniFiSiteCache sitesCache = new UniFiSiteCache();
private UniFiDeviceCache devicesCache = new UniFiDeviceCache();
@ -172,18 +177,22 @@ public class UniFiController {
// Client API
public @Nullable UniFiClient getClient(@Nullable String id) {
public @Nullable UniFiClient getClient(@Nullable String cid) {
UniFiClient client = null;
if (id != null && !id.isBlank()) {
if (cid != null && !cid.isBlank()) {
// Prefer lookups through _id, until initialized use cid.
String id = cidToIdCache.get(cid);
synchronized (this) {
// mgb: first check active clients and fallback to insights if not found
client = clientsCache.get(id);
client = clientsCache.get(id != null ? id : cid);
if (client == null) {
client = insightsCache.get(id);
client = insightsCache.get(id != null ? id : cid);
}
}
if (client == null) {
logger.debug("Could not find a matching client for id = {}", id);
logger.debug("Could not find a matching client for cid = {}", cid);
} else {
cidToIdCache.put(cid, client.id);
}
}
return client;