-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomekit-device.js
More file actions
98 lines (94 loc) · 3.65 KB
/
homekit-device.js
File metadata and controls
98 lines (94 loc) · 3.65 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
module.exports = function (RED) {
const { HttpClient, IPDiscovery } = require('hap-controller');
const discovery = new IPDiscovery();
function DeviceNode(config) {
let selectionCriteria = JSON.parse(config.selectionCriteria);
RED.nodes.createNode(this, config);
var node = this;
this.PairingDataDB = RED.nodes.getNode(config.pairingData);
// this.status({ fill: "red", shape: "dot", text: "Not Found" });
SetDevStatus();
var seconds = 15, the_interval = seconds * 1000;
setInterval(async () => {
if (selectionCriteria.id) {
SetDevStatus(selectionCriteria);
}
}, the_interval);
discovery.on('serviceUp', async (service) => {
var devices = filterDevices(service);
if (devices[0] != null) {
let currentDevice = devices[0];
await SetDevStatus(currentDevice);
}
});
discovery.on('serviceDown', async (service) => {
var devices = filterDevices(service);
if (devices[0] != null) {
let currentDevice = devices[0];
await SetDevStatus(currentDevice);
}
});
discovery.on('serviceChanged', async (service) => {
var devices = filterDevices(service);
if (devices[0] != null) {
let currentDevice = devices[0];
await SetDevStatus(currentDevice);
}
});
async function SetDevStatus(currentDevice) {
let devList = discovery.list();
let item_exist = devList.some(item => item.id == selectionCriteria.id)
let item = devList.filter(item => item.id == selectionCriteria.id)[0]
if (item_exist) {
node.status({ fill: "orange", shape: "dot", text: "Found" });
if (item.availableToPair) {
node.status({ fill: "yellow", shape: "dot", text: "Ready to pair." });
}
let ppdata = await node.PairingDataDB.GetData(item.id);
if (!item.availableToPair && !(ppdata)) {
node.status({ fill: "blue", shape: "dot", text: "Not paired\nDevice reset needed." });
}
if (!item.availableToPair && (ppdata)) {
node.status({ fill: "green", shape: "dot", text: "paired" });
}
}
else {
node.status({ fill: "red", shape: "dot", text: "Not Found" });
}
}
discovery.start();
// Handle incoming messages with list of deivces in array.
node.on('input', async function (msg) {
if (msg.payload == null)
msg.payload = {};
let devList = discovery.list();
let item_exist = devList.some(item => item.id == selectionCriteria.id);
let item = devList.filter(item => item.id == selectionCriteria.id);
if (item_exist)
msg.payload.HomeKitAccessory = item[0];
msg.payload.pairingPin = config.pairingPin;
msg.payload.pairingData = await this.PairingDataDB.GetData(selectionCriteria.id);
node.send(msg);
});
node.on('close', function () {
// Perform cleanup tasks or additional closing logic here
// This function is executed when the node is removed from the flow
discovery.stop();
// Example: Clean up resources, close connections, etc.
this.log('Deivce node closing...');
// Perform any necessary cleanup tasks before the node is closed
});
function filterDevices(ldevices) {
if (!Array.isArray(ldevices)) {
ldevices = [ldevices];
}
return ldevices.filter(device => {
return (
(selectionCriteria.serial ? device.serial.toLowerCase() === selectionCriteria.serial.toLowerCase() : true) &&
(selectionCriteria.name ? device.name.toLowerCase() === selectionCriteria.name.toLowerCase() : false)
);
});
}
}
RED.nodes.registerType("homekit-device", DeviceNode);
}