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

[amazonechocontrol] Broken last voice command on Fire TV Cube #592

Merged
merged 7 commits into from
Oct 13, 2024
Merged
Changes from 6 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 @@ -27,8 +27,11 @@
import java.time.chrono.ChronoZonedDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -141,6 +144,8 @@ public class AccountHandler extends BaseBridgeHandler implements PushConnection.
private boolean disposing = false;
private @Nullable AccountTO accountInformation;

Set<String> deviceSerialNumbers = Collections.synchronizedSet(new LinkedHashSet<>());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you use ConcurrentHashMap.newKeySet()? That's what we usually do if we want to create a new thread-safe Set.


public AccountHandler(Bridge bridge, Storage<String> stateStorage, Gson gson, HttpClient httpClient,
HTTP2Client http2Client, AmazonEchoControlCommandDescriptionProvider commandDescriptionProvider) {
super(bridge);
Expand Down Expand Up @@ -593,7 +598,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);
Expand Down Expand Up @@ -624,15 +630,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);
}
}
Expand Down Expand Up @@ -670,15 +676,24 @@ private List<CustomerHistoryRecordTO> getCustomerActivity(@Nullable Long timesta
return connection.getActivities(startTimestamp, endTimestamp);
}

private void handlePushActivity(String deviceSerialNumber, @Nullable Long timestamp) {
private void handlePushActivity(Set<String> deviceSerialNumbers, @Nullable Long timestamp) {
List<CustomerHistoryRecordTO> activityRecords = getCustomerActivity(timestamp);
EchoHandler echoHandler = echoHandlers.get(deviceSerialNumber);
if (echoHandler == null) {
logger.warn("Could not find thing handler for serialnumber {}", deviceSerialNumber);
return;

Iterator<String> 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) {
Expand Down
Loading