Skip to content

Commit

Permalink
Fix online/blocked channels. (openhab#11451)
Browse files Browse the repository at this point in the history
Fixes openhab#7001

Signed-off-by: Jacob Laursen <[email protected]>
Signed-off-by: Nick Waterton <[email protected]>
  • Loading branch information
jlaur authored and NickWaterton committed Dec 30, 2021
1 parent a18182e commit 28cfc3b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void reconnect() throws UniFiException {
@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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,12 +42,15 @@
*
* @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();
Expand Down Expand Up @@ -172,18 +177,22 @@ public void refresh() throws UniFiException {

// 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;
Expand Down

0 comments on commit 28cfc3b

Please sign in to comment.