Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
log.error('mqtt', err.toString())
})
mqttClient.on('offline', () => {
log.error('mqtt offline')
})
mqttClient.on('reconnect', () => {
log.info('mqtt reconnect')
})
// Start searching for devices
log.debug('Current config %o', config)
if (config.device) log.info('Start from device %s', config.device)
else log.info('Start searching for devices')
const sonosManager = new SonosManager()
// Pick the right initialization function.
const initialize = config.device ? sonosManager.InitializeFromDevice(config.device) : sonosManager.InitializeWithDiscovery(10)
initialize
.then(success => {
if (success) {
sonosManager.Devices.forEach(d => addDevice(d))
publishConnectionStatus()
} else {
log.info('No devices found')
}
})
.catch(err => {
log.error('Error in device discovery %o', err)
process.exit(300)
})
function addDevice (device) {
log.info('Add device %s %s', device.Name, device.host)
// Start listening for those events!
device.Events.on(SonosEvents.CurrentTrack, trackUri => {
publishTrackUri(device, trackUri)
})
device.Events.on(SonosEvents.CurrentTrackMetadata, metadata => {
publishCurrentTrack(device, metadata)
})
device.Events.on(SonosEvents.CurrentTransportStateSimple, state => {
publishState(device, state)
})
device.Events.on(SonosEvents.Mute, muted => {
publishMuted(device, muted)
})
device.Events.on(SonosEvents.Volume, volume => {
publishVolume(device, volume)
})
device.Events.on(SonosEvents.GroupName, groupName => {
function addDevice (device) {
log.info('Add device %s %s', device.Name, device.host)
// Start listening for those events!
device.Events.on(SonosEvents.CurrentTrack, trackUri => {
publishTrackUri(device, trackUri)
})
device.Events.on(SonosEvents.CurrentTrackMetadata, metadata => {
publishCurrentTrack(device, metadata)
})
device.Events.on(SonosEvents.CurrentTransportStateSimple, state => {
publishState(device, state)
})
device.Events.on(SonosEvents.Mute, muted => {
publishMuted(device, muted)
})
device.Events.on(SonosEvents.Volume, volume => {
publishVolume(device, volume)
})
device.Events.on(SonosEvents.GroupName, groupName => {
publishData(`${config.name}/status/${cleanName(device.Name)}/group`, groupName, cleanName(device.Name), true)
})
function addDevice (device) {
log.info('Add device %s %s', device.Name, device.host)
// Start listening for those events!
device.Events.on(SonosEvents.CurrentTrack, trackUri => {
publishTrackUri(device, trackUri)
})
device.Events.on(SonosEvents.CurrentTrackMetadata, metadata => {
publishCurrentTrack(device, metadata)
})
device.Events.on(SonosEvents.CurrentTransportStateSimple, state => {
publishState(device, state)
})
device.Events.on(SonosEvents.Mute, muted => {
publishMuted(device, muted)
})
device.Events.on(SonosEvents.Volume, volume => {
publishVolume(device, volume)
})
device.Events.on(SonosEvents.GroupName, groupName => {
publishData(`${config.name}/status/${cleanName(device.Name)}/group`, groupName, cleanName(device.Name), true)
})
device.Events.on(SonosEvents.Coordinator, coordinator => {
publishData(`${config.name}/status/${cleanName(device.Name)}/coordinator`, coordinator, cleanName(device.Name), true)
})
devices.push(device)
}
function addDevice (device) {
log.info('Add device %s %s', device.Name, device.host)
// Start listening for those events!
device.Events.on(SonosEvents.CurrentTrack, trackUri => {
publishTrackUri(device, trackUri)
})
device.Events.on(SonosEvents.CurrentTrackMetadata, metadata => {
publishCurrentTrack(device, metadata)
})
device.Events.on(SonosEvents.CurrentTransportStateSimple, state => {
publishState(device, state)
})
device.Events.on(SonosEvents.Mute, muted => {
publishMuted(device, muted)
})
device.Events.on(SonosEvents.Volume, volume => {
publishVolume(device, volume)
})
device.Events.on(SonosEvents.GroupName, groupName => {
publishData(`${config.name}/status/${cleanName(device.Name)}/group`, groupName, cleanName(device.Name), true)
})
device.Events.on(SonosEvents.Coordinator, coordinator => {
publishData(`${config.name}/status/${cleanName(device.Name)}/coordinator`, coordinator, cleanName(device.Name), true)
})
devices.push(device)
}
#!/usr/bin/env node
const pkg = require('../package.json')
const log = require('yalm')
const config = require('./config.js')
const mqtt = require('mqtt')
const SonosManager = require('@svrooij/sonos').SonosManager
const SonosEvents = require('@svrooij/sonos').SonosEvents
let mqttClient
const devices = []
function start () {
log.setLevel(config.verbosity)
log.info(pkg.name + ' ' + pkg.version + ' starting')
// MQTT Stuff
log.info('Parsing url %s', config.mqtt)
const url = new URL(config.mqtt)
// Define the will message (is send on disconnect).
const mqttOptions = {
will: {
topic: config.name + '/connected',
function addDevice (device) {
log.info('Add device %s %s', device.Name, device.host)
// Start listening for those events!
device.Events.on(SonosEvents.CurrentTrack, trackUri => {
publishTrackUri(device, trackUri)
})
device.Events.on(SonosEvents.CurrentTrackMetadata, metadata => {
publishCurrentTrack(device, metadata)
})
device.Events.on(SonosEvents.CurrentTransportStateSimple, state => {
publishState(device, state)
})
device.Events.on(SonosEvents.Mute, muted => {
publishMuted(device, muted)
})
device.Events.on(SonosEvents.Volume, volume => {
publishVolume(device, volume)
})
device.Events.on(SonosEvents.GroupName, groupName => {
publishData(`${config.name}/status/${cleanName(device.Name)}/group`, groupName, cleanName(device.Name), true)
})
device.Events.on(SonosEvents.Coordinator, coordinator => {
publishData(`${config.name}/status/${cleanName(device.Name)}/coordinator`, coordinator, cleanName(device.Name), true)
})
function cancelSubscriptions (device) {
device.Events.removeAllListeners(SonosEvents.CurrentTrack)
device.Events.removeAllListeners(SonosEvents.CurrentTrackMetadata)
device.Events.removeAllListeners(SonosEvents.CurrentTransportState)
device.Events.removeAllListeners(SonosEvents.Mute)
device.Events.removeAllListeners(SonosEvents.Volume)
}
function cancelSubscriptions (device) {
device.Events.removeAllListeners(SonosEvents.CurrentTrack)
device.Events.removeAllListeners(SonosEvents.CurrentTrackMetadata)
device.Events.removeAllListeners(SonosEvents.CurrentTransportState)
device.Events.removeAllListeners(SonosEvents.Mute)
device.Events.removeAllListeners(SonosEvents.Volume)
}
function cancelSubscriptions (device) {
device.Events.removeAllListeners(SonosEvents.CurrentTrack)
device.Events.removeAllListeners(SonosEvents.CurrentTrackMetadata)
device.Events.removeAllListeners(SonosEvents.CurrentTransportState)
device.Events.removeAllListeners(SonosEvents.Mute)
device.Events.removeAllListeners(SonosEvents.Volume)
}