/
register.php
67 lines (39 loc) 路 1.33 KB
/
register.php
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
<?php
set_time_limit(0);
include "functions.php";
// Note: this is going to get confused if you have more than one bridge.
// Use the Hue "broker server discover process" to get the bridge details:
$bridges = json_decode(file_get_contents("http://www.meethue.com/api/nupnp"), true);
$bridge = ["ip"=>$bridges[0]["internalipaddress"], "id"=>$bridges[0]["id"]] ;
if (!$bridge["ip"]) {
echo "No bridge found.\n";
exit;
}
echo "Hue Bridge found! ".$bridge["id"]." on ".$bridge["ip"].".\n";
echo "Please press the big blue button.\n";
// Say hello to the bridge.
$username = false;
$tries = 0;
while ((!$username) && ($tries<10)) {
$response = post($bridge["ip"],["devicetype"=>"flue#bertie"]);
if ((isset($response[0]["error"]))&&($response[0]["error"]["description"]=="link button not pressed")) {
$tries++;
sleep(2);
} elseif ($response[0]["success"]["username"]) {
$username = $response[0]["success"]["username"];
} else {
$tries++;
sleep(2);
}
}
if ($username) {
echo "Thanks.\n";
echo "API username received! Hello ".$username.".\n";
// Save bridge details
file_put_contents("bridge.json", json_encode(["ip"=>$bridge["ip"], "id"=>$bridge["id"], "username"=>$username]));
echo "Details saved to bridge.json.\n";
echo "You're all set!\n";
} else {
echo "Gave up waiting :(\n";
}
?>