Created
December 24, 2018 08:10
-
-
Save plu/6275a79d41acb2c4ea3509cb1dec9daa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
var Service, Characteristic; | |
module.exports = function(homebridge) { | |
Service = homebridge.hap.Service; | |
Characteristic = homebridge.hap.Characteristic; | |
homebridge.registerAccessory('homebridge-httpdoor', 'Httpdoor', DoorAccessory); | |
} | |
function DoorAccessory(log, config) { | |
this.log = log; | |
this.name = config['name']; | |
this.url = config['url']; | |
this.doorService = new Service.Door(this.name); | |
this.doorService | |
.getCharacteristic(Characteristic.CurrentPosition) | |
.on('get', this.getPosition.bind(this)); | |
this.doorService | |
.getCharacteristic(Characteristic.TargetPosition) | |
.on('get', this.getPosition.bind(this)) | |
.on('set', this.setPosition.bind(this)); | |
} | |
DoorAccessory.prototype.getPosition = function(callback) { | |
callback(null, 0); | |
} | |
DoorAccessory.prototype.setPosition = function(state, callback) { | |
request.get({ | |
url: this.url | |
}, function(err, response, body) { | |
if (!err && response.statusCode == 200) { | |
var self = this; | |
this.log('Opening door'); | |
setTimeout(function() { | |
self.doorService.setCharacteristic(Characteristic.CurrentPosition, 100); | |
setTimeout(function() { | |
callback(null, state); | |
//self.doorService.setCharacteristic(Characteristic.CurrentPosition, 0); | |
}, 1000); | |
}, 1000); | |
} else { | |
this.log("Error '%s' setting door state. Response: %s", err, body); | |
callback(err || new Error("Error setting door state.")); | |
} | |
}.bind(this)); | |
}, | |
DoorAccessory.prototype.getServices = function() { | |
return [this.doorService]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment