forked from homebridge/HAP-NodeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Core.js
50 lines (40 loc) · 1.65 KB
/
Core.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
var path = require('path');
var storage = require('node-persist');
var uuid = require('./').uuid;
var Accessory = require('./').Accessory;
var accessoryLoader = require('./lib/AccessoryLoader');
console.log("HAP-NodeJS starting...");
// Initialize our storage system
storage.initSync();
// Our Accessories will each have their own HAP server; we will assign ports sequentially
var targetPort = 51826;
// Load up all accessories in the /accessories folder
var dir = path.join(__dirname, "accessories");
var accessories = accessoryLoader.loadDirectory(dir);
// Publish them all separately (as opposed to BridgedCore which publishes them behind a single Bridge accessory)
accessories.forEach(function(accessory) {
// To push Accessories separately, we'll need a few extra properties
if (!accessory.username)
throw new Error("Username not found on accessory '" + accessory.displayName +
"'. Core.js requires all accessories to define a unique 'username' property.");
if (!accessory.pincode)
throw new Error("Pincode not found on accessory '" + accessory.displayName +
"'. Core.js requires all accessories to define a 'pincode' property.");
// publish this Accessory on the local network
accessory.publish({
port: targetPort++,
username: accessory.username,
pincode: accessory.pincode
});
});
var signals = { 'SIGINT': 2, 'SIGTERM': 15 };
Object.keys(signals).forEach(function (signal) {
process.on(signal, function () {
for (var i = 0; i < accessories.length; i++) {
accessories[i].unpublish();
}
setTimeout(function (){
process.exit(128 + signals[signal]);
}, 1000)
});
});