-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
80 lines (61 loc) · 2.15 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var Service;
var Characteristic;
var debug = require("debug")("FoscamSensorAccessory");
var crypto = require("crypto");
var MotionUpdater = require('./lib/logChecker').LogChecker;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-foscamsensor", "FoscamSensor", FoscamSensorAccessory);
}
function FoscamSensorAccessory(log, config) {
this.log = log;
// url info
this.name = config["name"];
this.ip = config["ip"];
this.interval = config["interval"] || 3000;
this.user = config["user"];
this.pwd = config["pwd"];
if(config["sn"]){
this.sn = config["sn"];
} else {
var shasum = crypto.createHash('sha1');
shasum.update(this.ip);
this.sn = shasum.digest('base64');
debug('Computed SN ' + this.sn);
}
}
FoscamSensorAccessory.prototype = {
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();
informationService
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.Manufacturer, "Homebridge")
.setCharacteristic(Characteristic.Model, "Foscam Sensor")
.setCharacteristic(Characteristic.SerialNumber, this.sn);
var service, changeAction;
service = new Service.MotionSensor();
changeAction = function(newState){
service.getCharacteristic(Characteristic.MotionDetected)
.setValue(newState);
};
var motionHandler = function(motion){
this.log("Motion Detected")
var newState = true;
changeAction(newState);
if(this.timer !== undefined) clearTimeout(this.timer);
this.timer = setTimeout(function(){changeAction(!newState);}, 15000);
}.bind(this);
var motionChecker = new MotionUpdater({
time: this.interval,
ip: this.ip,
user: this.user,
pwd: this.pwd
});
motionChecker.init();
motionChecker.on('motion',motionHandler);
return [informationService, service];
}
};