[homeconnect] Retrieve list of programs when an appliance becomes available (#10773)

Cache the list of programs for an appliance

Fix #10771

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2021-05-30 09:37:10 +02:00 committed by GitHub
parent 4ad6eda961
commit f99f4b6361
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -80,6 +80,7 @@ public class HomeConnectApiClient {
private final HttpClient client;
private final String apiUrl;
private final Map<String, List<AvailableProgramOption>> availableProgramOptionsCache;
private final Map<String, List<AvailableProgram>> programsCache;
private final OAuthClientService oAuthClientService;
private final CircularQueue<ApiRequest> communicationQueue;
private final ApiBridgeConfiguration apiBridgeConfiguration;
@ -91,6 +92,7 @@ public class HomeConnectApiClient {
this.apiBridgeConfiguration = apiBridgeConfiguration;
availableProgramOptionsCache = new ConcurrentHashMap<>();
programsCache = new ConcurrentHashMap<>();
apiUrl = simulated ? API_SIMULATOR_BASE_URL : API_BASE_URL;
communicationQueue = new CircularQueue<>(COMMUNICATION_QUEUE_SIZE);
if (apiRequestHistory != null) {
@ -610,7 +612,16 @@ public class HomeConnectApiClient {
public List<AvailableProgram> getPrograms(String haId)
throws CommunicationException, AuthorizationException, ApplianceOfflineException {
return getAvailablePrograms(haId, BASE_PATH + haId + "/programs");
List<AvailableProgram> programs;
if (programsCache.containsKey(haId)) {
logger.debug("Returning cached programs for '{}'.", haId);
programs = programsCache.get(haId);
programs = programs != null ? programs : Collections.emptyList();
} else {
programs = getAvailablePrograms(haId, BASE_PATH + haId + "/programs");
programsCache.put(haId, programs);
}
return programs;
}
public List<AvailableProgram> getAvailablePrograms(String haId)

View File

@ -248,6 +248,7 @@ public abstract class AbstractHomeConnectThingHandler extends BaseThingHandler i
} else if (isThingOffline() && !KEEP_ALIVE.equals(event.getType())) {
updateStatus(ONLINE);
logger.debug("Set {} to ONLINE and update channels. haId={}", getThing().getLabel(), getThingHaId());
updateSelectedProgramStateDescription();
updateChannels();
}