Skip to content

Commit

Permalink
try spotify fix
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <>
  • Loading branch information
Jan N. Klug committed Nov 3, 2023
1 parent 3816ab9 commit f4f6683
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.smarthomej.binding.amazonechocontrol.internal.dto.request;
package org.smarthomej.binding.amazonechocontrol.internal.dto;

import org.eclipse.jdt.annotation.NonNull;

/**
* The {@link AnnouncementTargetDeviceTO} encapsulates a target device for an announcement target
* The {@link DeviceIdTO} encapsulates a target device for an announcement target
*
* @author Jan N. Klug - Initial contribution
*/
public class AnnouncementTargetDeviceTO {
public class DeviceIdTO {
public String deviceSerialNumber;
public String deviceTypeId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.smarthomej.binding.amazonechocontrol.internal.dto.request.AnnouncementTargetDeviceTO;
import org.smarthomej.binding.amazonechocontrol.internal.types.Notification;

import com.google.gson.Gson;
Expand All @@ -51,8 +50,8 @@ public static Map<String, Object> mapToMap(Gson gson, Object o) {
return Objects.requireNonNullElse(gson.fromJson(json, MAP_TYPE_TOKEN), Map.of());
}

public static AnnouncementTargetDeviceTO mapAnnouncementTargetDevice(DeviceTO device) {
AnnouncementTargetDeviceTO targetDevice = new AnnouncementTargetDeviceTO();
public static DeviceIdTO mapAnnouncementTargetDevice(DeviceTO device) {
DeviceIdTO targetDevice = new DeviceIdTO();
targetDevice.deviceTypeId = device.deviceType;
targetDevice.deviceSerialNumber = device.serialNumber;
return targetDevice;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.List;

import org.eclipse.jdt.annotation.NonNull;
import org.smarthomej.binding.amazonechocontrol.internal.dto.DeviceIdTO;

/**
* The {@link AnnouncementTargetTO} encapsulate the target section of an announcement
Expand All @@ -23,7 +24,7 @@
*/
public class AnnouncementTargetTO {
public String customerId;
public List<AnnouncementTargetDeviceTO> devices = List.of();
public List<DeviceIdTO> devices = List.of();

@Override
public @NonNull String toString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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 EligibilityTO} encapsulates the eligibility section of a media session
*
* @author Jan N. Klug - Initial contribution
*/
public class EligibilityTO {
public boolean isEligible;
public String reasonCode;

@Override
public @NonNull String toString() {
return "EligibilityTO{isEligible=" + isEligible + ", reasonCode='" + reasonCode + "'}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* 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 ListMediaSessionTO} encapsulates the response to /api/np/list-media.sessions
*
* @author Jan N. Klug - Initial contribution
*/
public class ListMediaSessionTO {
public List<MediaSessionTO> mediaSessionList = List.of();

@Override
public @NonNull String toString() {
return "ListMediaSessionTO{mediaSessionList=" + mediaSessionList + "}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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;
import org.smarthomej.binding.amazonechocontrol.internal.dto.DeviceIdTO;

import com.google.gson.annotations.SerializedName;

/**
* The {@link MediaSessionEndpointTO} encapsulates a single endpoint information for a media session
*
* @author Jan N. Klug - Initial contribution
*/
public class MediaSessionEndpointTO {
@SerializedName("__type")
public String type;
public String encryptedFriendlyName;
public DeviceIdTO id;

@Override
public @NonNull String toString() {
return "MediaSessionEndpointTO{type='" + type + "', encryptedFriendlyName='" + encryptedFriendlyName + "', id="
+ id + "}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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;
import org.smarthomej.binding.amazonechocontrol.internal.dto.PlayerStateInfoTO;

/**
* The {@link MediaSessionTO} encapsulates a single media session
*
* @author Jan N. Klug - Initial contribution
*/
public class MediaSessionTO {
public EligibilityTO castEligibility;
public List<MediaSessionEndpointTO> endpointList = List.of();
public PlayerStateInfoTO nowPlayingData;

@Override
public @NonNull String toString() {
return "MediaSessionTO{castEligibility=" + castEligibility + ", endpointList=" + endpointList
+ ", nowPlayingData=" + nowPlayingData + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,8 @@ public void onPushCommandReceived(PushCommandTO pushCommand) {
break;
case "NotifyMediaSessionsUpdated":
// we can't determine which session was updated, but it only makes sense for currently playing devices
echoHandlers.forEach(e -> e.refreshAudioPlayerState(true));
// echoHandlers.forEach(e -> e.refreshAudioPlayerState(true));
echoHandlers.forEach(EchoHandler::updateMediaSessions);
break;
default:
logger.warn("Detected unknown command from activity stream: {}", pushCommand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
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.CustomerHistoryRecordVoiceTO;
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.PlayerStateTO;
import org.smarthomej.binding.amazonechocontrol.internal.types.Notification;
Expand Down Expand Up @@ -953,6 +955,30 @@ public void handleNowPlayingUpdated(PlayerStateInfoTO playerState) {
}
}

public void updateMediaSessions() {
findConnection().ifPresent(connection -> {
try {
DeviceTO device = this.device;
if (device == null || !isPlaying) {
return;
}
String url = connection.getAlexaServer() + "/api/np/list-media-sessions?deviceSerialNumber="
+ device.serialNumber + "&deviceType=" + device.deviceType;
List<MediaSessionTO> mediaSessions = connection.getRequestBuilder().get(url)
.syncSend(ListMediaSessionTO.class).mediaSessionList;
for (MediaSessionTO mediaSession : mediaSessions) {
if (findIn(mediaSession.endpointList, e -> e.id.deviceSerialNumber, device.serialNumber)
.isPresent()) {
updateMediaPlayerState(mediaSession.nowPlayingData, connection.isSequenceNodeQueueRunning(),
1000);
}
}
} catch (ConnectionException e) {
logger.warn("Failed to update media sessions for {}: {}", thing.getUID(), e.getMessage());
}
});
}

/**
* Refresh the audio player state
*
Expand Down

0 comments on commit f4f6683

Please sign in to comment.