forked from homebridge/HAP-NodeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccessory.js
76 lines (70 loc) · 2.48 KB
/
Accessory.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
var advertiser_fac = require("./Advertiser.js");
var server_fac = require("./Server.js");
var tcpServer_fac = require("./TCPServer.js");
var crypto = require("crypto");
var ed25519 = require("ed25519");
Accessory.prototype = {
publishAccessory: function publishAccessory() {
if (this._private.advertiser !== undefined) {
this._private.advertiser.startAdvertising();
}
if (this._private.tcpServer !== undefined) {
this._private.tcpServer.startServer();
}
if (this._private.hapServer !== undefined) {
this._private.hapServer.tcpServer = this._private.tcpServer;
this._private.hapServer.startHAPServer(this.targetPort + 1);
}
}
};
function Accessory(displayName, username, persistStore, targetPort, pincode, accessoryController) {
if (!(this instanceof Accessory)) {
return new Accessory(displayName, username, persistStore, targetPort, pincode, accessoryController);
}
this.displayName = displayName;
this.username = username;
this.targetPort = targetPort;
this.accessoryController = accessoryController;
this.accessoryInfo = new AccessoryInfo(this.displayName,this.username, pincode);
var savedKey = persistStore.getItem(username);
if (savedKey !== undefined) {
var keyPair = savedKey;
this.accessoryInfo.setKeyPair(keyPair);
} else {
console.log("Cannot find secret key, creating One...");
persistStore.setItem(username, this.accessoryInfo.keyPair);
persistStore.persistSync();
}
this._private = {
store: persistStore,
advertiser: new advertiser_fac.Advertiser(targetPort, this.displayName,this.username,"1","1"),
tcpServer: new tcpServer_fac.TCPServer(targetPort, targetPort+1, persistStore, this.accessoryInfo),
hapServer: new server_fac.HAPServer(this.accessoryInfo, function (connectPort, sharedSec){
this._private.tcpServer.enableEncryptionForSession(connectPort, sharedSec);
}.bind(this), persistStore, accessoryController)
}
this.accessoryController.tcpServer = this._private.tcpServer;
}
AccessoryInfo.prototype = {
setKeyPair: function setKeyPair(keys) {
this.keyPair = keys;
}
}
function AccessoryInfo(displayName,username, pincode) {
if (!(this instanceof AccessoryInfo)) {
return new AccessoryInfo(displayName,username);
}
this.displayName = displayName;
this.username = username;
this.pincode = pincode;
var seed = crypto.randomBytes(32);
var keyPair = ed25519.MakeKeypair(seed);
this.keyPair = {
signSk: keyPair.privateKey,
signPk: keyPair.publicKey
};
}
module.exports = {
Accessory: Accessory,
AccessoryInfo: AccessoryInfo
};