forked from lucavb/homebridge-hc-sr501
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (40 loc) · 1.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var Service;
var Characteristic;
var HomebridgeAPI;
var Gpio = require('onoff').Gpio;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
HomebridgeAPI = homebridge;
homebridge.registerAccessory("homebridge-hc-sr501", "HC-SR501", SR501);
};
function SR501(log, config) {
this.log = log;
this.name = config.name;
this.pinId = config.pinId;
this.motionSensor = new Gpio(this.pinId, 'in', 'both');
// info service
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, "PIR Manufacturer")
.setCharacteristic(Characteristic.Model, config.model || "HC-SR501")
.setCharacteristic(Characteristic.SerialNumber, config.serial || "4BD53931-D4A9-4850-8E7D-8A51A942FA29");
this.service = new Service.MotionSensor(this.name);
this.service.getCharacteristic(Characteristic.MotionDetected)
.on('get', this.getState.bind(this));
var that = this;
this.motionSensor.watch(function(err, value) {
that.service.getCharacteristic(Characteristic.MotionDetected)
.updateValue(value, null, "motionsensor_handler");
});
process.on('SIGINT', function () {
that.motionSensor.unexport();
});
}
SR501.prototype.getState = function(callback) {
var val = this.motionSensor.readSync();
callback(null, val);
};
SR501.prototype.getServices = function() {
return [this.informationService, this.service];
};