From 255a064c081bd796cd913e8acee4b46c1acdb5ec Mon Sep 17 00:00:00 2001 From: Kevin Mathy Date: Fri, 30 Oct 2015 22:55:53 +0100 Subject: [PATCH] Http Accessory improvements : accessory type (switch or light), and ligtht brightness handling toggle --- accessories/Http.js | 180 ++++++++++++++++++++++++-------------------- 1 file changed, 97 insertions(+), 83 deletions(-) diff --git a/accessories/Http.js b/accessories/Http.js index d64560f..a5475a7 100644 --- a/accessories/Http.js +++ b/accessories/Http.js @@ -3,109 +3,123 @@ var Characteristic = require("hap-nodejs").Characteristic; var request = require("request"); module.exports = { - accessory: HttpAccessory + accessory: HttpAccessory } function HttpAccessory(log, config) { - this.log = log; + this.log = log; - // url info - this.on_url = config["on_url"]; - this.off_url = config["off_url"]; - this.brightness_url = config["brightness_url"]; - this.http_method = config["http_method"]; - this.username = config["username"]; - this.password = config["password"]; + // url info + this.on_url = config["on_url"]; + this.off_url = config["off_url"]; + this.brightness_url = config["brightness_url"]; + this.http_method = config["http_method"]; + this.username = config["username"]; + this.password = config["password"]; + this.service = config["service"] || "Switch"; + this.name = config["name"]; + this.brightnessHandling = config["brightnessHandling"] || "no"; } HttpAccessory.prototype = { - httpRequest: function(url, method, username, password, callback) { - request({ - url: url, - method: method, - auth: { - user: username, - pass: password, - sendImmediately: false - } - }, - function (error, response, body) { - callback(error, response, body) - }) - }, + httpRequest: function(url, method, username, password, callback) { + request({ + url: url, + method: method, + auth: { + user: username, + pass: password, + sendImmediately: false + } + }, + function(error, response, body) { + callback(error, response, body) + }) + }, - setPowerState: function(powerOn, callback) { - var url; + setPowerState: function(powerOn, callback) { + var url; - if (powerOn) { - url = this.on_url; - this.log("Setting power state to on"); - } - else { - url = this.off_url; - this.log("Setting power state to off"); - } + if (powerOn) { + url = this.on_url; + this.log("Setting power state to on"); + } else { + url = this.off_url; + this.log("Setting power state to off"); + } - this.httpRequest(url, this.http_method, this.username, this.password, function(error, response, body) { - if (error) { - this.log('HTTP power function failed: %s', error.message); - callback(error); - } - else { - this.log('HTTP power function succeeded!'); - this.log(response); - this.log(body); - this.log(this.username); - this.log(this.password); - callback(); - } - }.bind(this)); - }, + this.httpRequest(url, this.http_method, this.username, this.password, function(error, response, body) { + if (error) { + this.log('HTTP power function failed: %s', error.message); + callback(error); + } else { + this.log('HTTP power function succeeded!'); + this.log(response); + this.log(body); + this.log(this.username); + this.log(this.password); + callback(); + } + }.bind(this)); + }, - setBrightness: function(level, callback) { - var url = this.brightness_url.replace("%b", level) + setBrightness: function(level, callback) { + var url = this.brightness_url.replace("%b", level) - this.log("Setting brightness to %s", level); + this.log("Setting brightness to %s", level); - this.httpRequest(url, this.http_method, this.username, this.password, function(error, response, body) { - if (error) { - this.log('HTTP brightness function failed: %s', error); - callback(error); - } - else { - this.log('HTTP brightness function succeeded!'); - callback(); - } - }.bind(this)); - }, + this.httpRequest(url, this.http_method, this.username, this.password, function(error, response, body) { + if (error) { + this.log('HTTP brightness function failed: %s', error); + callback(error); + } else { + this.log('HTTP brightness function succeeded!'); + callback(); + } + }.bind(this)); + }, - identify: function(callback) { - this.log("Identify requested!"); - callback(); // success - }, + identify: function(callback) { + this.log("Identify requested!"); + callback(); // success + }, - getServices: function() { + getServices: function() { - // you can OPTIONALLY create an information service if you wish to override - // the default values for things like serial number, model, etc. - var informationService = new Service.AccessoryInformation(); + // you can OPTIONALLY create an information service if you wish to override + // the default values for things like serial number, model, etc. + var informationService = new Service.AccessoryInformation(); - informationService - .setCharacteristic(Characteristic.Manufacturer, "HTTP Manufacturer") - .setCharacteristic(Characteristic.Model, "HTTP Model") - .setCharacteristic(Characteristic.SerialNumber, "HTTP Serial Number"); + informationService + .setCharacteristic(Characteristic.Manufacturer, "HTTP Manufacturer") + .setCharacteristic(Characteristic.Model, "HTTP Model") + .setCharacteristic(Characteristic.SerialNumber, "HTTP Serial Number"); - var lightbulbService = new Service.Lightbulb(); + if (this.service == "Switch") { + var switchService = new Service.Switch(this.name); - lightbulbService - .getCharacteristic(Characteristic.On) - .on('set', this.setPowerState.bind(this)); + switchService + .getCharacteristic(Characteristic.On) + .on('get', this.getPowerOn.bind(this)) + .on('set', this.setPowerOn.bind(this)); - lightbulbService - .addCharacteristic(new Characteristic.Brightness()) - .on('set', this.setBrightness.bind(this)); + return [switchService]; + } else if (this.service == "Light") { + var lightbulbService = new Service.Lightbulb(this.name); - return [informationService, lightbulbService]; - } + lightbulbService + .getCharacteristic(Characteristic.On) + .on('set', this.setPowerState.bind(this)); + + if (this.brightnessHandling == "yes") { + + lightbulbService + .addCharacteristic(new Characteristic.Brightness()) + .on('set', this.setBrightness.bind(this)); + } + + return [informationService, lightbulbService]; + } + } };