Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[unifi] Fix online/blocked channels after client is disconnected #11451

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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