This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
forked from omnibrain/node-red-contrib-find-my-iphone
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfind-my-iphone.js
72 lines (64 loc) · 1.89 KB
/
find-my-iphone.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
var iclouds = require('./icloud');
var iCloud = iclouds.iCloud;
module.exports = function (RED) {
function FindMyIphone(config) {
RED.nodes.createNode(this, config);
var node = this;
var device;
this.icloud = new iCloud(this.credentials.username, this.credentials.password);
this.devicename = config.devicename;
findMyPhone = this.icloud.findmyphone;
this.on('input', function (msg) {
node.status({fill: "yellow", shape: "ring", text: "getting device data..."});
node.icloud.getDevices(function (err, devices) {
var error = false;
// failed to get device information
if (err) {
node.error('Error getting devices', err);
error = true;
}
else if (!devices) {
node.error('No devices',err);
error = true;
}
else {
}
if (error) {
node.status({fill: "red", shape: "dot", text: "Connection failed"});
setTimeout(function () {
node.status({});
}, 10000)
return;
}
node.status({fill: "green", shape: "dot", text: "Connected"});
//node.info("the entered device name is " + node.devicename)
devices.forEach(function(d) {
//node.info("the received device name is " + d.name);
if (d.name == node.devicename) {
device=d;
return;
}
});
if (device) {
node.icloud.alertDevice(device.id, function(err) {
node.log("Device found: " + device.name);
});
}
// send all devices to the same output: [[msg, msg]]
node.send([devices.map(function (device) {
return {payload: device}
})]);
// clear the status
setTimeout(function () {
node.status({});
}, 2000)
});
});
}
RED.nodes.registerType("find-my-iphone", FindMyIphone, {
credentials: {
username: {type: "text"},
password: {type: "password"}
}
});
}