Skip to content

Commit

Permalink
Refactor _bestof function for improved error handling and logging; en…
Browse files Browse the repository at this point in the history
…hance state management for Sonos playback
  • Loading branch information
htilly committed Dec 25, 2024
1 parent 8285780 commit 61456e9
Showing 1 changed file with 67 additions and 41 deletions.
108 changes: 67 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1080,12 +1080,15 @@ function _play(input, channel, userName, state) {
function _playInt(input, channel) {
sonos.selectQueue()
sonos.play().then(result => {
logger.info('playInt started playing' + result)
_status(channel, state)
logger.info('Started playing - ' + result)
}).catch(err => {
logger.error('Error occurred: ' + err)
logger.info('Error occurred: ' + err)
})
}



function _stop(input, channel, userName, state) {
_logUserAction(userName, 'stop');
if (channel !== global.adminChannel) {
Expand Down Expand Up @@ -1705,54 +1708,77 @@ async function _addplaylist(input, channel, userName) {
}


function _bestof(input, channel, userName) {
async function _bestof(input, channel, userName) {
_logUserAction(userName, 'bestof');
var [data, message] = spotify.searchSpotifyArtist(input, channel, userName, 1)
if (message) {
_slackMessage(message, channel)
}
if (!data) {
return
}
logger.debug('Result in _bestof: ' + JSON.stringify(data, null, 2))
var trackNames = []
for (var i = 1; i <= data.artists.items.length; i++) {
var spid = data.artists.items[0].id
var trackName = data.artists.items[i - 1].name
trackNames.push(trackName)
}
logger.info('_bestof spid:' + spid)
logger.info('_bestof trackName:' + trackName)

sonos.getCurrentState().then(state => {
logger.info('Got current state: ' + state)
try {
const [data, message] = await spotify.searchSpotifyArtist(input, channel, userName, 1);
if (message) {
_slackMessage(message, channel);
return;
}
if (!data) {
logger.warn('No data returned from Spotify search.');
return;
}

if (state === 'stopped') {
_flushInt(input, channel)
_addToSpotifyArtist(userName, trackName, spid, channel)
logger.info('Adding artist:' + trackName)
setTimeout(() => _playInt('play', channel), 1000)
} else if (state === 'playing') {
// Add the track to playlist...
_addToSpotifyArtist(userName, trackName, spid, channel)
} else if (state === 'paused') {
_addToSpotifyArtist(userName, trackName, spid, channel, function () {
logger.debug('Result in _bestof: ' + JSON.stringify(data, null, 2));

if (!data.artists || !data.artists.items || data.artists.items.length === 0) {
_slackMessage('No artists found matching the input.', channel);
return;
}

const spid = data.artists.items[0].id;
const trackName = data.artists.items[0].name;

logger.info('_bestof spid: ' + spid);
logger.info('_bestof trackName: ' + trackName);

const state = await sonos.getCurrentState();
logger.info('Got current Sonos state: ' + state);

switch (state) {
case 'stopped':
logger.info('Sonos is stopped. Flushing queue and adding new artist.');
await _flushInt(input, channel);
logger.info('Queue flushed successfully.');
await new Promise(resolve => setTimeout(resolve, 2000)); // Wait for 2 seconds
await _addToSpotifyArtist(userName, trackName, spid, channel);
logger.info('Artist added successfully. Starting playback...');
setTimeout(() => _playInt('play', channel), 2000); // Slight delay to ensure queue updates
break;
case 'playing':
logger.info('Sonos is playing. Adding artist to the current queue.');
await _addToSpotifyArtist(userName, trackName, spid, channel);
break;
case 'paused':
logger.info('Sonos is paused. Adding artist to queue.');
await _addToSpotifyArtist(userName, trackName, spid, channel);
if (channel === global.adminChannel) {
_slackMessage('Sonos is currently PAUSED. Type `resume` to start playing...', channel)
_slackMessage('Sonos is currently paused. Type `resume` to start playing.', channel);
}
})
} else if (state === 'transitioning') {
_slackMessage("Sonos says it is 'transitioning'. We've got no idea what that means either...", channel)
} else if (state === 'no_media') {
_slackMessage("Sonos reports 'no media'. Any idea what that means?", channel)
break;
default:
logger.warn(`Sonos state '${state}' is unknown or unsupported.`);
_slackMessage(`Sonos state '${state}' is not supported. Unable to process the request.`, channel);
break;
}
} catch (err) {
if (err.message.includes('upnp: statusCode 500')) {
logger.error('UPnP Error in _bestof function: ' + err.message);
_slackMessage('Failed to process the request due to a UPnP error. Please try again later.', channel);
} else {
_slackMessage("Sonos reports its state as '" + state + "'. Any idea what that means? I've got nothing.", channel)
logger.error('Error in _bestof function: ' + err.message);
_slackMessage('Failed to process the request. Please try again later.', channel);
}
}).catch(err => {
logger.error('Error occurred ' + err)
})
}
}





function _status(channel, state) {
sonos.getCurrentState().then(state => {
logger.info('Got current state: ' + state)
Expand Down

0 comments on commit 61456e9

Please sign in to comment.