/
phue-create-user
executable file
·60 lines (47 loc) · 1.38 KB
/
phue-create-user
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
#!/usr/bin/env php
<?php
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
} else {
require_once __DIR__ . '/../../../autoload.php';
}
// Show usage if host and username not passed
if (!isset($argv[1], $argv[2])) {
echo "Philips Hue User Creator", "\n\n",
"Usage:", "\n",
" <host> <username>", "\n\n";
exit(1);
}
// Initialize client
$client = new \Phue\Client($argv[1], $argv[2]);
echo "Testing connection to bridge at {$client->getHost()}", "\n";
try {
$client->sendCommand(
new \Phue\Command\Ping
);
} catch (\Phue\Transport\Exception\ConnectionException $e) {
echo "Issue connecting to bridge", "\n";
exit(1);
}
echo "Attempting to create user:", "\n",
"Press the Bridge's button!", "\n",
"Waiting.";
// Try X times to create user
$maxTries = 30;
for ($i = 1; $i <= $maxTries; ++$i) {
try {
$response = $client->sendCommand(
new \Phue\Command\CreateUser($argv[2])
);
echo "\n", "Successfully created new user: {$response->username}", "\n\n";
break;
} catch (\Phue\Transport\Exception\LinkButtonException $e) {
echo ".";
} catch (Exception $e) {
echo "\n", "Failure to create user. Please try again!",
"\n", "Reason: {$e->getMessage()}", "\n\n";
break;
}
sleep(1);
}
exit(1);