use device name instead of ID
This commit is contained in:
parent
527200abe7
commit
1c9706485c
@ -8,7 +8,7 @@ import (
|
||||
type args struct {
|
||||
SpotifyClientID string `arg:"--spotify-client-id,required,env:SPOTIFY_CLIENT_ID" placeholder:"SPOTIFY_CLIENT_ID"`
|
||||
SpotifyClientSecret string `arg:"--spotify-client-secret,required,env:SPOTIFY_CLIENT_SECRET" placeholder:"SPOTIFY_CLIENT_SECRET"`
|
||||
DeviceID string `arg:"--device-id,required,env:SPOTIFY_DEVICE_ID" placeholder:"SPOTIFY_DEVICE_ID" help:"Can be found later in the API"`
|
||||
DeviceName string `arg:"--device-name,required,env:SPOTIFY_DEVICE_NAME" placeholder:"SPOTIFY_DEVICE_NAME" help:"Can be found later in the API"`
|
||||
PlaylistID string `arg:"--playlist-id,required,env:SPOTIFY_PLAYLIST_ID" help:"Just the id" placeholder:"SPOTIFY_PLAYLIST_ID"`
|
||||
RedirectURL string `arg:"--redirect-url,required,env:SPOTIFY_REDIRECT_URL" help:"Must be the same as in the Spotify developer dashboard." placeholder:"SPOTIFY_REDIRECT_URL"`
|
||||
ListenAddr string `arg:"--listen-addr,env:LISTEN_ADDR" help:"Address to listen on" placeholder:"LISTEN_ADDR" default:":3000"`
|
||||
@ -23,7 +23,7 @@ func main() {
|
||||
morningalarm.New(morningalarm.MorningAlarmConfig{
|
||||
SpotifyClientID: args.SpotifyClientID,
|
||||
SpotifyClientSecret: args.SpotifyClientSecret,
|
||||
DeviceID: args.DeviceID,
|
||||
DeviceName: args.DeviceName,
|
||||
PlaylistID: args.PlaylistID,
|
||||
RedirectURL: args.RedirectURL,
|
||||
ListenAddr: args.ListenAddr,
|
||||
|
@ -15,7 +15,7 @@ type alarm struct {
|
||||
type MorningAlarmConfig struct {
|
||||
SpotifyClientID string `json:"spotifyClientId"`
|
||||
SpotifyClientSecret string `json:"spotifyClientSecret"`
|
||||
DeviceID string `json:"deviceId"`
|
||||
DeviceName string `json:"deviceName"`
|
||||
PlaylistID string `json:"wakeupContext"`
|
||||
RedirectURL string `json:"redirectUrl"`
|
||||
ListenAddr string `json:"listenAddr"`
|
||||
|
@ -114,7 +114,14 @@ func (ma *MorningAlarm) setupSpotify() error {
|
||||
// Can still return an error even if the playback was started successfully.
|
||||
func (ma *MorningAlarm) playWakeUpMusic() (bool, error) {
|
||||
playContextURI := spotify.URI("spotify:playlist:" + ma.config.PlaylistID)
|
||||
deviceID := spotify.ID(ma.config.DeviceID)
|
||||
|
||||
devID, err := ma.getDeviceIDFromName(ma.config.DeviceName)
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
deviceID := spotify.ID(devID)
|
||||
|
||||
playlist, err := ma.sp.GetPlaylistItems(context.Background(), spotify.ID(ma.config.PlaylistID))
|
||||
|
||||
@ -160,3 +167,19 @@ func (ma *MorningAlarm) getAvailableDevices() ([]Device, error) {
|
||||
|
||||
return availableDevices, nil
|
||||
}
|
||||
|
||||
func (ma *MorningAlarm) getDeviceIDFromName(name string) (string, error) {
|
||||
dev, err := ma.getAvailableDevices()
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for _, device := range dev {
|
||||
if device.Name == name {
|
||||
return device.ID, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("device with name %s not found", name)
|
||||
}
|
||||
|
@ -106,7 +106,7 @@ func (ma *MorningAlarm) setupWebserver() {
|
||||
"timezoneOffsetInH": offset / 60 / 60,
|
||||
"alarms": len(ma.cr.Entries()),
|
||||
"wakeupPlaylist": ma.config.PlaylistID,
|
||||
"deviceId": ma.config.DeviceID,
|
||||
"deviceName": ma.config.DeviceName,
|
||||
}
|
||||
|
||||
if nextIn != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user