forked from vsubbotskyy/find-my-iphone-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-my-iphone.js
70 lines (62 loc) · 1.84 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
var icloud = require('./icloud');
module.exports = function (RED) {
function FindMyIphone(config) {
RED.nodes.createNode(this, config);
var node = this;
var device;
icloud.apple_id = this.credentials.username
icloud.password = this.credentials.password
this.devicename = config.devicename;
findMyPhone = icloud.findmyphone;
this.on('input', function (msg) {
node.status({fill: "yellow", shape: "ring", text: "getting device data..."});
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;
}
});
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"}
}
});
}