diff --git a/README.md b/README.md index 9c1eaba..2e0b193 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,18 @@ # WiFi-Radio -[![dependencies Status](https://david-dm.org/ent8r/wifiradio/status.svg)](https://david-dm.org/ent8r/wifiradio) -[![Travis](https://travis-ci.org/ENT8R/wifiradio.svg?branch=master)](https://travis-ci.org/ENT8R/wifiradio) -[![NPM Version](http://img.shields.io/npm/v/wifiradio.svg)](https://www.npmjs.org/package/wifiradio) -[![NPM Downloads](https://img.shields.io/npm/dm/wifiradio.svg)](https://www.npmjs.org/package/wifiradio) + +[![dependencies Status](https://david-dm.org/ent8r/wifiradio/status.svg)](https://david-dm.org/ent8r/wifiradio) [![Travis](https://travis-ci.org/ENT8R/wifiradio.svg?branch=master)](https://travis-ci.org/ENT8R/wifiradio) [![NPM Version](http://img.shields.io/npm/v/wifiradio.svg)](https://www.npmjs.org/package/wifiradio) [![NPM Downloads](https://img.shields.io/npm/dm/wifiradio.svg)](https://www.npmjs.org/package/wifiradio) NodeJS module for controlling WiFi-radios ## Installation - npm install wifiradio --save + +``` +npm install wifiradio --save +``` ## Usage -```js + +```javascript const wifiradio = require('wifiradio'); const ip = '192.168.178.27'; //Change this to the ip adress of your radio @@ -18,15 +20,27 @@ const pin = '1234'; //This is the default PIN for the radio. (Works in most case const radio = new wifiradio(ip, pin); -radio.getPower() - .then(console.log); +radio.setPower(1).then(function() { + radio.getPower().then(function(res) { + console.log('Power: ' + res); + radio.getMute().then(function(res) { + console.log('Mute: ' + res); + radio.getText().then(function(res) { + console.log('Text: ' + res); + radio.getMode().then(function(res) { + console.log('Mode: ' + res); + }); + }); + }); + }); +}); ``` ## Features ### Power -```js +```javascript //Turn on radio.setPower(1); @@ -40,7 +54,7 @@ radio.getPower() ### Mute -```js +```javascript //Mute on radio.setMute(1); @@ -54,7 +68,7 @@ radio.getMute() ### Volume -```js +```javascript //Set volume (value from 1-20) radio.setVolume(10); @@ -65,7 +79,7 @@ radio.getVolume() ### Modes -```js +```javascript //Set a mode radio.setMode(2); @@ -76,7 +90,7 @@ radio.getMode() ### Display -```js +```javascript //Get the first line of the display radio.getName() .then(console.log); @@ -91,4 +105,5 @@ radio.getText() There are many more requests that could be done by this module. If you think that something is missing just open an issue for that or make a pull request. If you need some help, you can have a look [here](https://github.com/flammy/fsapi/blob/master/FSAPI.md) for some further requests. ## License + [GPL-3.0](https://github.com/ENT8R/wifiradio/blob/master/LICENSE) diff --git a/index.js b/index.js index becb039..c5d898e 100644 --- a/index.js +++ b/index.js @@ -6,140 +6,140 @@ let pin; //GET a session ID function getSessionID() { - return new Promise((resolve, reject) => { - request({ - url: `http://${ip}/fsapi/CREATE_SESSION`, - qs: { - pin, - }, - }, (error, response, body) => { - if (error || response.statusCode !== 200) { - return reject(error); - } - parseXML(body, (err, parseResult) => { - if (err) { - return reject(err); - } - resolve(parseResult.fsapiResponse.sessionId[0]); - }); - }); + return new Promise((resolve, reject) => { + request({ + url: `http://${ip}/fsapi/CREATE_SESSION`, + qs: { + pin, + }, + }, (error, response, body) => { + if (error || response.statusCode !== 200) { + return reject(error); + } + parseXML(body, (err, parseResult) => { + if (err) { + return reject(err); + } + resolve(parseResult.fsapiResponse.sessionId[0]); + }); }); + }); }; //Make a request with a given operation and a possible value function makeRequest(operation, value) { - return new Promise((resolve, reject) => { - getSessionID().then(sid => { - request({ - url: `http://${ip}/fsapi/${operation}`, - qs: { - pin, - sid, - value, - }, - }, (error, response, body) => { - if (error || response.statusCode !== 200) { - return reject(error); - } - parseXML(body, (err, result) => { - if (err) { - return reject(err); - } - resolve(result); - }); - }); + return new Promise((resolve, reject) => { + getSessionID().then(sid => { + request({ + url: `http://${ip}/fsapi/${operation}`, + qs: { + pin, + sid, + value, + }, + }, (error, response, body) => { + if (error || response.statusCode !== 200) { + return reject(error); + } + parseXML(body, (err, result) => { + if (err) { + return reject(err); + } + resolve(result); }); + }); }); + }); }; //SET Power function setPower(state) { - return makeRequest('SET/netRemote.sys.power', state.toString()); + return makeRequest('SET/netRemote.sys.power', state.toString()); }; //GET Power function getPower() { - return makeRequest('GET/netRemote.sys.power') - .then(result => parseInt(result.fsapiResponse.value[0].u8[0])); + return makeRequest('GET/netRemote.sys.power') + .then(result => parseInt(result.fsapiResponse.value[0].u8[0])); }; //GET the first line of the display function getName() { - return makeRequest('GET/netRemote.play.info.name') - .then(result => result.fsapiResponse.value[0].c8_array[0]); + return makeRequest('GET/netRemote.play.info.name') + .then(result => result.fsapiResponse.value[0].c8_array[0]); }; //GET the second line of the display function getText() { - return makeRequest('GET/netRemote.play.info.text') - .then(result => result.fsapiResponse.value[0].c8_array[0]); + return makeRequest('GET/netRemote.play.info.text') + .then(result => result.fsapiResponse.value[0].c8_array[0]); }; //SET mute state function setMute(state) { - return makeRequest('SET/netRemote.sys.audio.mute', state.toString()); + return makeRequest('SET/netRemote.sys.audio.mute', state.toString()); }; //GET mute state function getMute() { - return makeRequest('GET/netRemote.sys.audio.mute') - .then(result => parseInt(result.fsapiResponse.value[0].u8[0])); + return makeRequest('GET/netRemote.sys.audio.mute') + .then(result => parseInt(result.fsapiResponse.value[0].u8[0])); }; //SET current mode function setMode(mode) { - return makeRequest('SET/netRemote.sys.mode', mode.toString()); + return makeRequest('SET/netRemote.sys.mode', mode.toString()); }; //GET current mode function getMode() { - return makeRequest('GET/netRemote.sys.mode') - .then(result => parseInt(result.fsapiResponse.value[0].u32[0])); + return makeRequest('GET/netRemote.sys.mode') + .then(result => parseInt(result.fsapiResponse.value[0].u32[0])); }; //LIST available modes; returns a JSON array of all available modes //TODO: this method does not work at the moment. Needs some research function listModes() { - return makeRequest('LIST_GET_NEXT/netRemote.sys.caps.validModes/-1') - .then(result => JSON.stringify(result.fsapiResponse.item[0].field)); + return makeRequest('LIST_GET_NEXT/netRemote.sys.caps.validModes/-1') + .then(result => JSON.stringify(result.fsapiResponse.item[0].field)); } //SET the volume; accepts all values from 1-20 function setVolume(value) { - return makeRequest('SET/netRemote.sys.audio.volume', value.toString()); + return makeRequest('SET/netRemote.sys.audio.volume', value.toString()); }; //GET the volume function getVolume() { - return makeRequest('GET/netRemote.sys.audio.volume') - .then(result => parseInt(result.fsapiResponse.value[0].u8[0])); + return makeRequest('GET/netRemote.sys.audio.volume') + .then(result => parseInt(result.fsapiResponse.value[0].u8[0])); }; //And finally export the module -module.exports = function (ipAdress, radioPin) { - if (!ipAdress) { - throw new Error('No IP adress specified'); - } +module.exports = function(ipAdress, radioPin) { + if (!ipAdress) { + throw new Error('No IP adress specified'); + } - if (!radioPin) { - throw new Error('No PIN specified'); - } + if (!radioPin) { + throw new Error('No PIN specified'); + } - ip = ipAdress; - pin = radioPin; + ip = ipAdress; + pin = radioPin; - this.getPower = getPower; - this.setPower = setPower; + this.getPower = getPower; + this.setPower = setPower; - this.getName = getName; - this.getText = getText; + this.getName = getName; + this.getText = getText; - this.setMute = setMute; - this.getMute = getMute; + this.setMute = setMute; + this.getMute = getMute; - this.setMode = setMode; - this.getMode = getMode; + this.setMode = setMode; + this.getMode = getMode; - this.setVolume = setVolume; - this.getVolume = getVolume; + this.setVolume = setVolume; + this.getVolume = getVolume; };