From de06a2b12de7b914ef3ac77540fec5113bd7a89b Mon Sep 17 00:00:00 2001 From: Pierre-Julien Cazaux Date: Wed, 21 Oct 2015 10:59:13 +0200 Subject: [PATCH] Semi working HTTP garage door implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, when use « Siri, open/close garage door » got the correct answer on logs but my iPhone reply something like « The garage door in now wrong » and the Insteon+ app icon is not updated. I quite beginner I would like a hint :) THX --- accessories/HttpGarageDoorOpener.js | 115 ++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 accessories/HttpGarageDoorOpener.js diff --git a/accessories/HttpGarageDoorOpener.js b/accessories/HttpGarageDoorOpener.js new file mode 100644 index 0000000..d6320a2 --- /dev/null +++ b/accessories/HttpGarageDoorOpener.js @@ -0,0 +1,115 @@ +/* +{ + "bridge": { + "name": "Homebridge", + "username": "CC:22:3D:E3:CE:30", + "port": 51826, + "pin": "031-45-154" + }, + + "description": "This is an example configuration file with all supported devices. You can use this as a template for creating your own configuration file containing devices you actually own.", + + "platforms": [], + "accessories": [ + { + "accessory": "HttpGarageDoorOpener", + "name": "Porte de Garage", + "description": "", + "open_url": "http://0.0.0.0:3000", + "http_method": "GET" + } + ] +} +*/ + +var Service = require("hap-nodejs").Service; +var Characteristic = require("hap-nodejs").Characteristic; + +var request = require("request"); + +module.exports = { + accessory: HttpGarageDoorOpener +} + +function HttpGarageDoorOpener(log, config) { + this.log = log; + this.open_url = config["open_url"]; + this.http_method = config["http_method"]; + this.garageDoorStatus = Characteristic.CurrentDoorState.CLOSED; +} + +HttpGarageDoorOpener.prototype = { + close: function (callback) { + this.garageDoorStatus = Characteristic.CurrentDoorState.CLOSED; + this.log("Door is", this.getCurrentDoorStateReadable()); + callback(); + }, + + open: function (callback) { + this.garageDoorStatus = Characteristic.CurrentDoorState.OPEN; + this.log("Door is", this.getCurrentDoorStateReadable()); + callback(); + }, + + identify: function() { + console.log("Identify the Door!"); + }, + + getServices: function () { + this.garageDoorOpenerService = new Service.GarageDoorOpener(); + + this.garageDoorOpenerService + .getCharacteristic(Characteristic.CurrentDoorState) + .on('get', this.getCurrentDoorState.bind(this)); + + this.garageDoorOpenerService + .getCharacteristic(Characteristic.TargetDoorState) + .on('set', this.setTargetDoorState.bind(this)); + + /* + garageDoorOpenerService + .getCharacteristic(Characteristic.ObstructionDetected) + .on('get', this.getObstructionDetected.bind(this)) + .on('set', this.setObstructionDetected.bind(this)); + */ + + var informationService = new Service.AccessoryInformation(); + informationService + .setCharacteristic(Characteristic.Manufacturer, "HTTP Manufacturer") + .setCharacteristic(Characteristic.Model, "HTTP Model") + .setCharacteristic(Characteristic.SerialNumber, "HTTP Serial Number"); + + return [informationService, this.garageDoorOpenerService]; + }, + + getCurrentDoorStateReadable: function () { + var textState = ""; + switch (this.garageDoorStatus) { + case 0: textState = "OPEN"; break; + case 1: textState = "CLOSED"; break; + case 2: textState = "OPENING"; break; + case 3: textState = "CLOSING"; break; + case 4: textState = "STOPPED"; break; + default: this.log("Unhandled CurrentDoorState"); + } + return textState; + }, + + getCurrentDoorState: function(callback) { + + this.log("The door is now", this.getCurrentDoorStateReadable() ,"("+ this.garageDoorStatus + ")"); + + var error = null; + var returnValue = this.state; + + callback(null, returnValue); + }, + + setTargetDoorState: function(value, callback) { + if(value === Characteristic.TargetDoorState.OPEN) { + this.open(callback); + } else { + this.close(callback); + }; + } +}; \ No newline at end of file