From 53d7272e42138fe509eda5570563f5be5bf2c309 Mon Sep 17 00:00:00 2001 From: ebaauw Date: Sun, 19 Nov 2017 10:54:36 +0100 Subject: [PATCH] Handle unknown track metadata (#26) --- lib/ZPAccessory.js | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/ZPAccessory.js b/lib/ZPAccessory.js index 630be2c..b2de9a8 100644 --- a/lib/ZPAccessory.js +++ b/lib/ZPAccessory.js @@ -261,19 +261,20 @@ ZPAccessory.prototype.handleAVTransportEvent = function(data) { if (data) { this.parser.parseString(data, function(err, json) { if (!err && json['DIDL-Lite']) { - const type = json['DIDL-Lite'].item[0].res[0]._; + const item = json['DIDL-Lite'].item[0]; + const type = item.res[0]._; switch (type.split(':')[0]) { case 'x-rincon-stream': // Line in input. - track = json['DIDL-Lite'].item[0]['dc:title'][0]; // source + track = item['dc:title'][0]; // source break; case 'x-sonos-htastream': // SPDIF TV input. track = 'TV'; - const streamInfo = json['DIDL-Lite'].item[0]['r:streamInfo'][0]; + const streamInfo = item['r:streamInfo'][0]; // "0": no input; "2": stereo; "18": Dolby Digital 5.1; on = streamInfo !== "0"; break; case 'x-sonosapi-stream': // Radio stream. - track = json['DIDL-Lite'].item[0]['r:streamContent'][0]; // info + track = item['r:streamContent'][0]; // info if (track === '') { if (event['r:EnqueuedTransportURIMetaData']) { const data = event['r:EnqueuedTransportURIMetaData'][0].$.val; @@ -289,12 +290,18 @@ ZPAccessory.prototype.handleAVTransportEvent = function(data) { break; case 'x-file-cifs': // Library song. case 'x-sonos-spotify': // Spotify song. - /* falls through */ - default: - track = json['DIDL-Lite'].item[0]['dc:title'][0]; // song - // track = json['DIDL-Lite'].item[0]['dc:creator'][0]; // artist - // track = json['DIDL-Lite'].item[0]['upnp:album'][0]; // album - // track = json['DIDL-Lite'].item[0].res[0].$.duration; // duration + track = item['dc:title'][0]; // song + // track = item['dc:creator'][0]; // artist + // track = item['upnp:album'][0]; // album + // track = item.res[0].$.duration; // duration + break; + default: + if (item['dc:title']) { + track = item['dc:title'][0]; // song + } else { + this.log.warn('%s: unknown track metadata %j', this.name, item); + track = '(unknown)'; + } break; } }