forked from homebridge/HAP-NodeJS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BridgedCore.js
48 lines (40 loc) · 1.32 KB
/
BridgedCore.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
var fs = require('fs');
var path = require('path');
var storage = require('node-persist');
var uuid = require('./').uuid;
var Bridge = require('./').Bridge;
var Accessory = require('./').Accessory;
var accessoryLoader = require('./lib/AccessoryLoader');
console.log("HAP-NodeJS starting...");
// Initialize our storage system
storage.initSync();
// Start by creating our Bridge which will host all loaded Accessories
var bridge = new Bridge('Node Bridge', uuid.generate("Node Bridge"));
// Listen for bridge identification event
bridge.on('identify', function(paired, callback) {
console.log("Node Bridge identify");
callback(); // success
});
// Load up all accessories in the /accessories folder
var dir = path.join(__dirname, "accessories");
var accessories = accessoryLoader.loadDirectory(dir);
// Add them all to the bridge
accessories.forEach(function(accessory) {
bridge.addBridgedAccessory(accessory);
});
// Publish the Bridge on the local network.
bridge.publish({
username: "CC:22:3D:E3:CE:F6",
port: 51826,
pincode: "031-45-154",
category: Accessory.Categories.BRIDGE
});
var signals = { 'SIGINT': 2, 'SIGTERM': 15 };
Object.keys(signals).forEach(function (signal) {
process.on(signal, function () {
bridge.unpublish();
setTimeout(function (){
process.exit(128 + signals[signal]);
}, 1000)
});
});