Skip to content

Commit

Permalink
Next/Previous Track
Browse files Browse the repository at this point in the history
  • Loading branch information
ebaauw committed Jun 17, 2018
1 parent 1281ef7 commit 8eab182
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 56 deletions.
155 changes: 109 additions & 46 deletions lib/ZPAccessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ function ZPAccessory (platform, zp) {

this.infoService = new Service.AccessoryInformation()
this.infoService
.setCharacteristic(Characteristic.Manufacturer, 'homebridge-zp')
.setCharacteristic(Characteristic.Model, this.zp.model)
.setCharacteristic(Characteristic.SerialNumber, this.uuid_base)
.setCharacteristic(Characteristic.FirmwareRevision, this.zp.version)
.updateCharacteristic(Characteristic.Manufacturer, 'homebridge-zp')
.updateCharacteristic(Characteristic.Model, this.zp.model)
.updateCharacteristic(Characteristic.SerialNumber, this.uuid_base)
.updateCharacteristic(Characteristic.FirmwareRevision, this.zp.version)
this.services = [this.infoService]

this.groupService = new this.platform.SpeakerService(this.name, 'group')
Expand All @@ -64,9 +64,10 @@ function ZPAccessory (platform, zp) {
this.groupService.addOptionalCharacteristic(Characteristic.Mute)
this.groupService.getCharacteristic(Characteristic.Mute)
.on('set', this.setGroupMute.bind(this))
// this.groupService.addOptionalCharacteristic(Characteristic.ChangeTrack);
// this.groupService.getCharacteristic(Characteristic.ChangeTrack)
// .on('set', this.setGroupChangeTrack.bind(this));
this.groupService.addOptionalCharacteristic(Characteristic.ChangeTrack)
this.groupService.getCharacteristic(Characteristic.ChangeTrack)
.on('set', this.setGroupChangeTrack.bind(this))
.updateValue(0)
this.groupService.addOptionalCharacteristic(Characteristic.CurrentTrack)
this.groupService.addOptionalCharacteristic(Characteristic.SonosGroup)
this.services.push(this.groupService)
Expand Down Expand Up @@ -147,39 +148,53 @@ ZPAccessory.prototype.copyCoordinator = function () {
this.state.group.on, coordinator.state.group.on
)
this.state.group.on = coordinator.state.group.on
this.groupService.setCharacteristic(Characteristic.On, this.state.group.on)
this.groupService.updateCharacteristic(Characteristic.On, this.state.group.on)
}
if (this.state.group.volume !== coordinator.state.group.volume) {
this.log.debug(
'%s: set member group volume from %s to %s', this.name,
this.state.group.volume, coordinator.state.group.volume
)
this.state.group.volume = coordinator.state.group.volume
this.groupService.setCharacteristic(this.platform.VolumeCharacteristic, this.state.group.volume)
this.groupService.updateCharacteristic(this.platform.VolumeCharacteristic, this.state.group.volume)
}
if (this.state.group.mute !== coordinator.state.group.mute) {
this.log.debug(
'%s: set member group mute from %s to %s', this.name,
this.state.group.mute, coordinator.state.group.mute
)
this.state.group.mute = coordinator.state.group.mute
this.groupService.setCharacteristic(Characteristic.Mute, this.state.group.mute)
this.groupService.updateCharacteristic(Characteristic.Mute, this.state.group.mute)
}
if (this.state.group.track !== coordinator.state.group.track) {
this.log.debug(
'%s: set member track from %s to %s', this.name,
this.state.group.track, coordinator.state.group.track
)
this.state.group.track = coordinator.state.group.track
this.groupService.setCharacteristic(Characteristic.CurrentTrack, this.state.group.track)
this.groupService.updateCharacteristic(Characteristic.CurrentTrack, this.state.group.track)
}
if (this.state.group.name !== coordinator.state.group.name) {
this.log.debug(
'%s: set member sonos group from %s to %s', this.name,
this.state.group.name, coordinator.state.group.name
)
this.state.group.name = coordinator.state.group.name
this.groupService.setCharacteristic(Characteristic.SonosGroup, this.state.group.name)
this.groupService.updateCharacteristic(Characteristic.SonosGroup, this.state.group.name)
}
if (this.state.group.numberOfTracks !== coordinator.state.group.numberOfTracks) {
this.log.debug(
'%s: set member number of tracks from %s to %s', this.name,
this.state.group.numberOfTracks, coordinator.state.group.numberOfTracks
)
this.state.group.numberOfTracks = coordinator.state.group.numberOfTracks
}
if (this.state.group.currentTrack !== coordinator.state.group.currentTrack) {
this.log.debug(
'%s: set member current track number from %s to %s', this.name,
this.state.group.currentTrack, coordinator.state.group.currentTrack
)
this.state.group.currentTrack = coordinator.state.group.currentTrack
}
}
}
Expand All @@ -189,7 +204,7 @@ ZPAccessory.prototype.becomePlatformCoordinator = function () {
this.log('%s: platform coordinator', this.name)
this.platform.coordinator = this
this.state.zone.on = true
this.zoneService.setCharacteristic(Characteristic.On, this.state.zone.on)
this.zoneService.updateCharacteristic(Characteristic.On, this.state.zone.on)
}
}

Expand All @@ -198,7 +213,7 @@ ZPAccessory.prototype.quitPlatformCoordinator = function () {
this.platform.coordinator = null
}
this.state.zone.on = false
this.zoneService.setCharacteristic(Characteristic.On, this.state.zone.on)
this.zoneService.updateCharacteristic(Characteristic.On, this.state.zone.on)
}

// ===== SONOS EVENTS ==========================================================
Expand Down Expand Up @@ -258,14 +273,14 @@ ZPAccessory.prototype.handleGroupManagementEvent = function (data) {
this.coordinator = this
this.state.group.name = this.coordinator.zp.zone
this.log.info('%s: coordinator for group %s', this.name, this.state.group.name)
this.groupService.setCharacteristic(Characteristic.SonosGroup, this.state.group.name)
this.groupService.updateCharacteristic(Characteristic.SonosGroup, this.state.group.name)
for (const member of this.members()) {
member.coordinator = this
member.copyCoordinator()
}
if (this.platform.coordinator !== this) {
this.state.zone.on = false
this.zoneService.setCharacteristic(Characteristic.On, this.state.zone.on)
this.zoneService.updateCharacteristic(Characteristic.On, this.state.zone.on)
}
} else {
this.coordinator = this.platform.groupCoordinator(this.group)
Expand All @@ -274,7 +289,7 @@ ZPAccessory.prototype.handleGroupManagementEvent = function (data) {
this.copyCoordinator()
}
this.state.zone.on = true
this.zoneService.setCharacteristic(Characteristic.On, this.state.zone.on)
this.zoneService.updateCharacteristic(Characteristic.On, this.state.zone.on)
}
}

Expand All @@ -286,8 +301,10 @@ ZPAccessory.prototype.handleAVTransportEvent = function (data) {
}
let on = this.state.group.on
let track = this.state.group.track
let numberOfTracks = this.state.group.NumberOfTracks
let currentTrack = this.state.group.currentTrack
const event = json.Event.InstanceID[0]
// this.log.debug('%s: AVTransport event: %j', this.name, event);
// this.log.debug('%s: AVTransport event: %j', this.name, event)
if (event.TransportState) {
on = event.TransportState[0].$.val === 'PLAYING'
}
Expand Down Expand Up @@ -329,9 +346,9 @@ ZPAccessory.prototype.handleAVTransportEvent = function (data) {
case 'x-file-cifs': // Library song.
case 'x-sonos-spotify': // Spotify song.
track = item['dc:title'][0] // song
// track = item['dc:creator'][0]; // artist
// track = item['upnp:album'][0]; // album
// track = item.res[0].$.duration; // duration
// track = item['dc:creator'][0] // artist
// track = item['upnp:album'][0] // album
// track = item.res[0].$.duration // duration
break
default:
if (item['dc:title']) {
Expand All @@ -346,10 +363,16 @@ ZPAccessory.prototype.handleAVTransportEvent = function (data) {
})
}
}
if (event.NumberOfTracks) {
numberOfTracks = 1 * event.NumberOfTracks[0].$.val
}
if (event.CurrentTrack) {
currentTrack = 1 * event.CurrentTrack[0].$.val
}
if (on !== this.state.group.on) {
this.log.info('%s: power (play/pause) changed from %s to %s', this.name, this.state.group.on, on)
this.state.group.on = on
this.groupService.setCharacteristic(Characteristic.On, this.state.group.on)
this.groupService.updateCharacteristic(Characteristic.On, this.state.group.on)
for (const member of this.members()) {
member.copyCoordinator(this)
}
Expand All @@ -361,7 +384,27 @@ ZPAccessory.prototype.handleAVTransportEvent = function (data) {
this.state.group.track, track
)
this.state.group.track = track
this.groupService.setCharacteristic(Characteristic.CurrentTrack, this.state.group.track)
this.groupService.updateCharacteristic(Characteristic.CurrentTrack, this.state.group.track)
for (const member of this.members()) {
member.copyCoordinator()
}
}
if (numberOfTracks !== this.state.group.numberOfTracks) {
this.log.info(
'%s: number of tracks changed from %s to %s', this.name,
this.state.group.numberOfTracks, numberOfTracks
)
this.state.group.numberOfTracks = numberOfTracks
for (const member of this.members()) {
member.copyCoordinator()
}
}
if (currentTrack !== this.state.group.currentTrack) {
this.log.info(
'%s: current track number changed from %s to %s', this.name,
this.state.group.currentTrack, currentTrack
)
this.state.group.currentTrack = currentTrack
for (const member of this.members()) {
member.copyCoordinator()
}
Expand All @@ -376,7 +419,7 @@ ZPAccessory.prototype.handleGroupRenderingControlEvent = function (json) {
if (volume !== this.state.group.volume) {
this.log.info('%s: group volume changed from %s to %s', this.name, this.state.group.volume, volume)
this.state.group.volume = volume
this.groupService.setCharacteristic(this.platform.VolumeCharacteristic, this.state.group.volume)
this.groupService.updateCharacteristic(this.platform.VolumeCharacteristic, this.state.group.volume)
for (const member of this.members()) {
member.copyCoordinator(this)
}
Expand All @@ -387,7 +430,7 @@ ZPAccessory.prototype.handleGroupRenderingControlEvent = function (json) {
if (mute !== this.state.group.mute) {
this.log.info('%s: group mute changed from %s to %s', this.name, this.state.group.mute, mute)
this.state.group.mute = mute
this.groupService.setCharacteristic(Characteristic.Mute, this.state.group.mute)
this.groupService.updateCharacteristic(Characteristic.Mute, this.state.group.mute)
for (const member of this.members()) {
member.copyCoordinator(this)
}
Expand All @@ -407,39 +450,39 @@ ZPAccessory.prototype.handleRenderingControlEvent = function (data) {
if (volume !== this.state.zone.volume) {
this.log.info('%s: volume changed from %s to %s', this.name, this.state.zone.volume, volume)
this.state.zone.volume = volume
this.zoneService.setCharacteristic(this.platform.VolumeCharacteristic, this.state.zone.volume)
this.zoneService.updateCharacteristic(this.platform.VolumeCharacteristic, this.state.zone.volume)
}
}
if (event.Mute) {
const mute = event.Mute[0].$.val === '1'
if (mute !== this.state.zone.mute) {
this.log.info('%s: mute changed from %s to %s', this.name, this.state.zone.mute, mute)
this.state.zone.mute = mute
this.zoneService.setCharacteristic(Characteristic.Mute, this.state.zone.mute)
this.zoneService.updateCharacteristic(Characteristic.Mute, this.state.zone.mute)
}
}
if (event.Bass) {
const bass = Number(event.Bass[0].$.val)
if (bass !== this.state.zone.bass) {
this.log.info('%s: bass changed from %s to %s', this.name, this.state.zone.bass, bass)
this.state.zone.bass = bass
this.zoneService.setCharacteristic(Characteristic.Bass, this.state.zone.bass)
this.zoneService.updateCharacteristic(Characteristic.Bass, this.state.zone.bass)
}
}
if (event.Treble) {
const treble = Number(event.Treble[0].$.val)
if (treble !== this.state.zone.treble) {
this.log.info('%s: treble changed from %s to %s', this.name, this.state.zone.treble, treble)
this.state.zone.treble = treble
this.zoneService.setCharacteristic(Characteristic.Treble, this.state.zone.treble)
this.zoneService.updateCharacteristic(Characteristic.Treble, this.state.zone.treble)
}
}
if (event.Loudness) {
const loudness = event.Loudness[0].$.val === '1'
if (loudness !== this.state.zone.loudness) {
this.log.info('%s: loudness changed from %s to %s', this.name, this.state.zone.loudness, loudness)
this.state.zone.loudness = loudness
this.zoneService.setCharacteristic(Characteristic.Loudness, this.state.zone.loudness)
this.zoneService.updateCharacteristic(Characteristic.Loudness, this.state.zone.loudness)
}
}
})
Expand Down Expand Up @@ -648,7 +691,7 @@ ZPAccessory.prototype.setGroupVolume = function (volume, callback) {
this.log.error('%s: set group volume: %s', this.name, err)
return callback(err)
}
// this.state.group.volume = volume;
// this.state.group.volume = volume
return callback()
})
}
Expand All @@ -672,25 +715,45 @@ ZPAccessory.prototype.setGroupMute = function (mute, callback) {
this.log.error('%s: set group mute: %s', this.name, err)
return callback(err)
}
// this.state.group.mute = mute;
// this.state.group.mute = mute
return callback()
})
}

// ZPAccessory.prototype.setGroupTrack = function(track, callback) {
// if (track === 0) {
// return callback();
// }
// if (track > 0) {
// this.log.info('%s: next track', this.name);
// } else {
// this.log.info('%s: previous track', this.name);
// }
// callback();
// setTimeout(() => {
// this.groupService.setCharacteristic(Characteristic.Track, 0);
// }, 100);
// };
ZPAccessory.prototype.setGroupChangeTrack = function (track, callback) {
if (track === 0) {
return callback()
}
setTimeout(() => {
this.groupService.updateCharacteristic(Characteristic.ChangeTrack, 0)
}, 500)
if (!this.isCoordinator) {
return this.coordinator.setGroupChangeTrack(track, callback)
}
const newTrack = this.state.group.currentTrack + track
if (track > 0 && newTrack <= this.state.group.numberOfTracks) {
this.log.debug('%s: next track (%s/%s)', this.name, newTrack, this.state.group.numberOfTracks)
this.zp.next((err, success) => {
if (err) {
this.log.error('%s: next track: %s', this.name, err)
return callback(err)
}
return callback()
})
} else if (track < 0 && newTrack >= 1) {
this.log.debug('%s: previous track (%s/%s)', this.name, newTrack, this.state.group.numberOfTracks)
this.zp.previous((err, success) => {
if (err) {
this.log.error('%s: previous track: %s', this.name, err)
return callback(err)
}
return callback()
})
} else {
this.log.debug('%s: track (%s/%s)', this.name, this.state.group.currentTrack, this.state.group.numberOfTracks)
return callback()
}
}

// ===== SONOS INTERACTION =====================================================

Expand Down
Loading

0 comments on commit 8eab182

Please sign in to comment.