Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const request = require(‘request’);
const url = require(‘url’);
function mySwitch(log, config) {
 this.log = log;
 this.getUrl = url.parse(config[‘getUrl’]);
 this.postUrl = url.parse(config[‘postUrl’]);
}
mySwitch.prototype = {
getSwitchOnCharacteristic: function (next) {
 const me = this;
 request({
 url: me.getUrl,
 method: ‘GET’,
 }, 
 function (error, response, body) {
 if (error) {
 me.log(‘STATUS: + response.statusCode);
 me.log(error.message);
 return next(error);
 }
 return next(null, body.currentState);
 });
 },
 
 setSwitchOnCharacteristic: function (on, next) {
 const me = this;
 request({
 url: me.postUrl,
 body: {‘targetState’: on},
 method: ‘POST’,
 headers: {‘Content-type’: ‘application/json’}
 },
 function (error, response) {
 if (error) {
 me.log(‘STATUS: + response.statusCode);
 me.log(error.message);
 return next(error);
 }
 return next();
 });
 }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment