Skip to content

Commit

Permalink
Fix customer history retrieval and internal list item support
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <[email protected]>
(cherry picked from commit f2f6c36)
Signed-off-by: Jan N. Klug <>
  • Loading branch information
Jan N. Klug committed Dec 5, 2023
1 parent aad3064 commit 69cd45d
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,12 @@
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.DeviceNotificationStatesTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.DoNotDisturbDeviceStatusesTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.EndpointTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.ListItemTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.ListMediaSessionTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.MediaSessionTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.MusicProviderTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.NamedListsInfoTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.NamedListsItemsTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.NotificationListResponseTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.NotificationSoundResponseTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.PlayerStateTO;
Expand Down Expand Up @@ -592,14 +595,35 @@ public List<CustomerHistoryRecordTO> getActivities(long startTime, long endTime)
+ "&endTime=" + endTime + "&maxRecordSize=1";
CustomerHistoryRecordsTO customerHistoryRecords = requestBuilder.get(url)
.syncSend(CustomerHistoryRecordsTO.class);
return customerHistoryRecords.customerHistoryRecords.stream().sorted(Comparator.comparing(r -> r.timestamp))
.toList();
return customerHistoryRecords.customerHistoryRecords.stream()
.filter(r -> !"DEVICE_ARBITRATION".equals(r.utteranceType))
.sorted(Comparator.comparing(r -> r.timestamp)).toList();
} catch (ConnectionException e) {
logger.info("getting activities failed", e);
}
return List.of();
}

public @Nullable NamedListsInfoTO getNamedListInfo(String listId) {
try {
String url = getAlexaServer() + "/api/namedLists/" + listId + "?_=" + System.currentTimeMillis();
return requestBuilder.get(url).syncSend(NamedListsInfoTO.class);
} catch (ConnectionException e) {
logger.info("getting information for list {} failed", listId, e);
}
return null;
}

public List<ListItemTO> getNamedListItems(String listId) {
try {
String url = getAlexaServer() + "/api/namedLists/" + listId + "/items?_=" + System.currentTimeMillis();
return requestBuilder.get(url).syncSend(NamedListsItemsTO.class).list;
} catch (ConnectionException e) {
logger.info("getting items from list '{}' failed", listId, e);
}
return List.of();
}

public List<BluetoothStateTO> getBluetoothConnectionStates() {
try {
String url = getAlexaServer() + "/api/bluetooth?cached=true";
Expand Down Expand Up @@ -841,15 +865,16 @@ private void sendTextToSpeech() {
Iterator<TextWrapper> iterator = textToSpeeches.values().iterator();
while (iterator.hasNext()) {
TextWrapper textToSpeech = iterator.next();
logger.trace("Executing textToSpeech {}", textToSpeech);
try {
List<DeviceTO> devices = textToSpeech.getDevices();
if (!devices.isEmpty()) {
executeSequenceCommandWithVolume(devices, "Alexa.Speak",
Map.of("textToSpeak", textToSpeech.getText()), textToSpeech.getTtsVolumes(),
textToSpeech.getStandardVolumes());
}
} catch (Exception e) {
logger.warn("send textToSpeech fails with unexpected error", e);
} catch (RuntimeException e) {
logger.warn("Send textToSpeech failed with unexpected error", e);
}
iterator.remove();
}
Expand Down Expand Up @@ -885,24 +910,25 @@ public void textCommand(DeviceTO device, String text, @Nullable Integer ttsVolum
}
}

private synchronized void sendTextCommand() {
// we lock new TTS until we have dispatched everything
private void sendTextCommand() {
// we lock new textCommands until we have dispatched everything
Lock lock = Objects.requireNonNull(locks.computeIfAbsent(TimerType.TEXT_COMMAND, k -> new ReentrantLock()));
lock.lock();

try {
Iterator<TextWrapper> iterator = textCommands.values().iterator();
while (iterator.hasNext()) {
TextWrapper textCommand = iterator.next();
logger.trace("Executing textCommand {}", textCommand);
try {
List<DeviceTO> devices = textCommand.getDevices();
if (!devices.isEmpty()) {
executeSequenceCommandWithVolume(devices, "Alexa.TextCommand",
Map.of("text", textCommand.getText()), textCommand.getTtsVolumes(),
textCommand.getStandardVolumes());
}
} catch (Exception e) {
logger.warn("send textCommand fails with unexpected error", e);
} catch (RuntimeException e) {
logger.warn("Sending textCommand failed with unexpected error", e);
}
iterator.remove();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@ public List<DeviceTO> getDevices() {
public String getText() {
return text;
}

@Override
public String toString() {
return "TextWrapper{" + "devices=" + devices + ", text='" + text + "'" + ", ttsVolumes=" + ttsVolumes
+ ", standardVolumes=" + standardVolumes + "}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2021-2023 Contributors to the SmartHome/J project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.smarthomej.binding.amazonechocontrol.internal.dto.push;

/**
* The {@link PushListItemChangeTO} encapsulates a PUSH_LIST_ITEM_CHANGE message
*
* @author Jan N. Klug - Initial contribution
*/
public class PushListItemChangeTO {
public String listId;
public String listItemId;
public int version;
public String eventName;
public String destinationUserId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) 2021-2023 Contributors to the SmartHome/J project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.smarthomej.binding.amazonechocontrol.internal.dto.response;

import org.eclipse.jdt.annotation.NonNull;

/**
* The {@link ListItemTO} encapsulates a single list item
*
* @author Jan N. Klug - Initial contribution
*/
public class ListItemTO {
public String listId;
public boolean shoppingListItem;
public String customerId;
public long createdDateTime;
public boolean completed;
public String id;
public String value;
public int version;
public long updatedDateTime;

@Override
public @NonNull String toString() {
return "ListItemTO{listId='" + listId + "', shoppingListItem=" + shoppingListItem + ", customerId='"
+ customerId + "', createdDateTime=" + createdDateTime + ", completed=" + completed + ", id='" + id
+ "', value='" + value + "', version=" + version + ", updatedDateTime=" + updatedDateTime + "}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (c) 2021-2023 Contributors to the SmartHome/J project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.smarthomej.binding.amazonechocontrol.internal.dto.response;

import java.util.List;

import org.eclipse.jdt.annotation.NonNull;

/**
* The {@link NamedListsInfoTO} encapsulates the response of /api/namedLists/listId
*
* @author Jan N. Klug - Initial contribution
*/
public class NamedListsInfoTO {
public List<String> listIds;
public long updatedDate;
public String type;
public int version;
public boolean defaultList;
public boolean archived;
public String itemId;
public long createdDate;
public Object listReorderVersion;
public Object originalAudioId;
public String customerId;
public String name;
public Object nbestItems;

@Override
public @NonNull String toString() {
return "NamedListsInfoTO{listIds=" + listIds + ", updatedDate=" + updatedDate + ", type='" + type
+ "', version=" + version + ", defaultList=" + defaultList + ", archived=" + archived + ", itemId='"
+ itemId + "', createdDate=" + createdDate + ", listReorderVersion=" + listReorderVersion
+ ", originalAudioId=" + originalAudioId + ", customerId='" + customerId + "', name=" + name
+ ", nbestItems=" + nbestItems + "}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2021-2023 Contributors to the SmartHome/J project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.smarthomej.binding.amazonechocontrol.internal.dto.response;

import java.util.List;

import org.eclipse.jdt.annotation.NonNull;

/**
* The {@link NamedListsItemsTO} encapsulate the response of /api/namedLists/listId/items
*
* @author Jan N. Klug - Initial contribution
*/
public class NamedListsItemsTO {
public Object listReorderVersion;
public List<ListItemTO> list;

@Override
public @NonNull String toString() {
return "NamedListsItemsTO{listReorderVersion=" + listReorderVersion + ", list=" + list + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@
import org.smarthomej.binding.amazonechocontrol.internal.dto.push.PushCommandTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.push.PushDeviceTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.push.PushDopplerIdTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.push.PushListItemChangeTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.request.SendConversationDTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.AccountTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.BluetoothStateTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.CustomerHistoryRecordTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.ListItemTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.MusicProviderTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.response.WakeWordTO;
import org.smarthomej.binding.amazonechocontrol.internal.dto.smarthome.JsonSmartHomeDevice;
Expand Down Expand Up @@ -646,6 +648,12 @@ public void onPushCommandReceived(PushCommandTO pushCommand) {
// echoHandlers.forEach(e -> e.refreshAudioPlayerState(true));
echoHandlers.values().forEach(EchoHandler::updateMediaSessions);
break;
case "PUSH_LIST_ITEM_CHANGE":
PushListItemChangeTO itemChange = Objects
.requireNonNull(gson.fromJson(payload, PushListItemChangeTO.class));
List<ListItemTO> lists = connection.getNamedListItems(itemChange.listId);
// TODO: create channels
break;
default:
logger.warn("Detected unknown command from activity stream: {}", pushCommand);
}
Expand Down

0 comments on commit 69cd45d

Please sign in to comment.