bencevans / node-sonos

🔈 Sonos Media Player Interface/Client
https://www.npmjs.com/package/sonos
MIT License
700 stars 146 forks source link

Question: "ReferenceError: Sonos is not defined" in your sample #494

Closed AlexMaghen closed 3 years ago

AlexMaghen commented 3 years ago

Question

Thanks so much fir this wonderful thing. I successfully installed using "npm -g install sonos" but then when I ran my very first test (from your sample), I got a "Sonos is not defined" error. Here's the full text of the .js file I'm trying to run (below). Sure I'm doing something stupid. Not sure what! :)...

//======================================== const { DeviceDiscovery } = require('sonos')

// event on all found... DeviceDiscovery((device) => { console.log('found device at ' + device.host)

// mute every device... device.setMuted(true) .then(d => console.log(${d.host} now muted)) })

// find one device DeviceDiscovery().once('DeviceAvailable', (device) => { console.log('found device at ' + device.host)

// get all groups sonos = new Sonos(device.host) sonos.getAllGroups().then(groups => { groups.forEach(group => { console.log(group.Name); }) }) })

hklages commented 3 years ago

Hi. Try the other example -thats easy. Does that work?

Give your SONOS player a static IP address (hostname) and use this code with modified ip address

const { Sonos } = require('sonos')

const device = new Sonos('192.168.1.56');

device.play()
  .then(() => console.log('now playing'))

device.getVolume()
  .then((volume) => console.log(`current volume = ${volume}`))
hklages commented 3 years ago

The 2nd example:

const { Sonos } = require('sonos')

// enter your hostname (static) 
const sonosPlayer = new Sonos('192.168.178.37')

doCommands(sonosPlayer)
    .then( (success) => {
       console.log(`message >>${JSON.stringify(success)}`)
    })
    .catch((error) => {
        console.log(`error >>${JSON.stringify(error, Object.getOwnPropertyNames(error))}`)
    })

 async function doCommands (sonosPlayer) {
     // get data such as name
     const zoneAttributes = await sonosPlayer.getZoneAttrs()
     console.log(`zone info >>${JSON.stringify(zoneAttributes)}`)

     // get all groups
     const groups = await sonosPlayer.getAllGroups()
     console.log(`all groups >>${JSON.stringify(groups)}`)

    //For leaving use await sonosPlayer.leaveGroup() 

     await sonosPlayer.setVolume(20)

    // ... 

     return 'finished DoCommands'
 }
AlexMaghen commented 3 years ago

Thank you so much for this. I am sorry to be a pest but let me tack on two questions.

  1. Identifying a specific sonosPlayer to act on by its "Name" without having ip addresses: in jishi/node-sonos-http-api (which I do not want to use), one can just identify a player by its name. Not sure what it does underneath, but I assume it scans the LAN for that device? I would love to be able to do that here so that a "known" IP address is not needed. Can that be done here?

  2. Play in "item" in Sonos Favorites by name on a given sonosPlayer: The advantage of this is that items in Sonos Favorites provide a level of abstraction in that they can be a file, a stream, a Spotify item, whatever, and the code doesn't need to know.

One last thing: I am doing this for a friend. I am not experienced in the bizarrely kind and helpful ways of the opensource world(!). At the risk of being rude, I am happy to pay for help (are the opensource gods going to smite me?! :). In any case, thanks again!

hklages commented 3 years ago

Dont worry - thats not rude. Payment means: some kind of contract, that means terms & conditions, tax and perhaps issues with an employer. So donations are more common in open source. You can spend a beer, coffee or ... link to donation - cappuccino. But all is free and there is no commitment.

What is your final system: a single web page with some buttons and these buttons trigger the programs or to integrate it into a bigger system? Home-automation? System of webpages?

AlexMaghen commented 3 years ago

@hklages - Thanks so much for the feedback (of all kinds! - and contribution will certainly be on its way). I have a fun and crazy home-automation thing going on and all of the parts are working beautifully except this last part with Sonos. Ultimately, I end up needing two executable files (.js) on my Raspberry Pi which, when run do the following: PLAYIT.js:

STOPIT.js

My Raspberry Pi is already working properly interacting with node-sonos, but I just cannot get my head around these complexities. ALSO, there are only 5 Zone Players in my system but there's a reason why I cannot rely on known IPs.

I can do far better than Cappuccino money were you (or someone) to do this for me as I am suffering trying to make it happen! :)

In any case, thanks so much for your help and STAY SAFE!

mmathys commented 3 years ago

I opened a PR for this: #497

github-actions[bot] commented 3 years ago

:tada: This issue has been resolved in version 1.14.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] commented 3 years ago

:tada: This issue has been resolved in version 1.15.0-alpha.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket: