From 4d13c6fd7250ccd1c7bcd03411ab24dd11d0c1c4 Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Sun, 13 Oct 2024 11:23:40 +0200 Subject: [PATCH] [amazonechocontrol] Broken last voice command on Fire TV Cube (#592) * fixed: missing Content-Length header (BodyPublisher.ofByteArrays JDK 17 issue) Signed-off-by: Tom Blum trinitus01@googlemail.com * fixed: commited line endings Signed-off-by: Tom Blum trinitus01@googlemail.com * removed: logger Signed-off-by: Tom Blum trinitus01@googlemail.com * fixed: boken last voice command on Fire TV Cube Signed-off-by: Tom Blum * fixed: spotless aso Signed-off-by: Tom Blum * review Signed-off-by: Jan N. Klug --------- Signed-off-by: Tom Blum trinitus01@googlemail.com Signed-off-by: Tom Blum Signed-off-by: Jan N. Klug Co-authored-by: Jan N. Klug Signed-off-by: Jan N. Klug --- .../internal/handler/AccountHandler.java | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/bundles/org.smarthomej.binding.amazonechocontrol/src/main/java/org/smarthomej/binding/amazonechocontrol/internal/handler/AccountHandler.java b/bundles/org.smarthomej.binding.amazonechocontrol/src/main/java/org/smarthomej/binding/amazonechocontrol/internal/handler/AccountHandler.java index 9edde261ed..e438b7e4a4 100644 --- a/bundles/org.smarthomej.binding.amazonechocontrol/src/main/java/org/smarthomej/binding/amazonechocontrol/internal/handler/AccountHandler.java +++ b/bundles/org.smarthomej.binding.amazonechocontrol/src/main/java/org/smarthomej/binding/amazonechocontrol/internal/handler/AccountHandler.java @@ -29,6 +29,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; @@ -115,6 +116,7 @@ public class AccountHandler extends BaseBridgeHandler implements PushConnection. private final Map echoHandlers = new ConcurrentHashMap<>(); private final Set smartHomeDeviceHandlers = new CopyOnWriteArraySet<>(); private final Set flashBriefingProfileHandlers = new CopyOnWriteArraySet<>(); + private final Set deviceSerialNumbers = new CopyOnWriteArraySet<>(); private final Object synchronizeConnection = new Object(); private Map serialNumberDeviceMapping = new HashMap<>(); @@ -593,7 +595,8 @@ private EnabledFeedTO copyFeed(EnabledFeedTO feed) { public void onPushCommandReceived(PushCommandTO pushCommand) { logger.debug("Processing {}", pushCommand); String payload = pushCommand.payload; - switch (pushCommand.command) { + String command = pushCommand.command; + switch (command) { case "PUSH_ACTIVITY": // currently unused, seems to be removed, log a warning if it re-appears logger.warn("Activity detected: {}", pushCommand); @@ -624,15 +627,15 @@ public void onPushCommandReceived(PushCommandTO pushCommand) { if (echoHandler == null) { return; } - echoHandler.handlePushCommand(pushCommand.command, payload); - if ("PUSH_EQUALIZER_STATE_CHANGE".equals(pushCommand.command) - || "PUSH_VOLUME_CHANGE".equals(pushCommand.command)) { + echoHandler.handlePushCommand(command, payload); + if ("PUSH_EQUALIZER_STATE_CHANGE".equals(command) || "PUSH_VOLUME_CHANGE".equals(command)) { + deviceSerialNumbers.add(dopplerId.deviceSerialNumber); ScheduledFuture refreshActivityJob = this.refreshActivityJob; if (refreshActivityJob != null) { refreshActivityJob.cancel(false); } this.refreshActivityJob = scheduler.schedule( - () -> handlePushActivity(dopplerId.deviceSerialNumber, pushCommand.timeStamp), + () -> handlePushActivity(deviceSerialNumbers, pushCommand.timeStamp), handlerConfig.activityRequestDelay, TimeUnit.SECONDS); } } @@ -670,15 +673,24 @@ private List getCustomerActivity(@Nullable Long timesta return connection.getActivities(startTimestamp, endTimestamp); } - private void handlePushActivity(String deviceSerialNumber, @Nullable Long timestamp) { + private void handlePushActivity(Set deviceSerialNumbers, @Nullable Long timestamp) { List activityRecords = getCustomerActivity(timestamp); - EchoHandler echoHandler = echoHandlers.get(deviceSerialNumber); - if (echoHandler == null) { - logger.warn("Could not find thing handler for serialnumber {}", deviceSerialNumber); - return; + + Iterator iterator = deviceSerialNumbers.iterator(); + while (iterator.hasNext()) { + try { + String deviceSerialNumber = iterator.next(); + EchoHandler echoHandler = echoHandlers.get(deviceSerialNumber); + if (echoHandler == null) { + logger.warn("Could not find thing handler for serialnumber {}", deviceSerialNumber); + return; + } + activityRecords.stream().filter(r -> r.recordKey.endsWith(deviceSerialNumber)) + .forEach(echoHandler::handlePushActivity); + } finally { + iterator.remove(); + } } - activityRecords.stream().filter(r -> r.recordKey.endsWith(deviceSerialNumber)) - .forEach(echoHandler::handlePushActivity); } private @Nullable SmartHomeBaseDevice findSmartHomeDeviceJson(SmartHomeDeviceHandler handler) {