This repository has been archived by the owner on Dec 25, 2018. It is now read-only.
forked from danray0424/phpHue
/
HueBridge.php
127 lines (107 loc) · 3.77 KB
/
HueBridge.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
require( __DIR__ . '/pest-master/PestJSON.php' );
require(__DIR__ . '/HueError.php');
require(__DIR__ . '/HueLight.php');
require(__DIR__ . '/HueScene.php');
class HueBridge
{
private $bridgeAddress;
private $authKey;
/**
* Construct a new HueBridge connection
*
* @param $bridgeAddress string The IP or FQDN to a hue bridge
* @param $authKey string The 32 characters long auth key
* @throws InvalidArgumentException If one of the parameters is invalid
**/
public function __construct($bridgeAddress, $authKey)
{
if ( strlen($bridgeAddress) == 0 )
throw new InvalidArgumentException(
'Parameter $bridgeAddress is empty');
if ( strlen($authKey) != 32 )
throw new InvalidArgumentException(
'Parameter $authKey is invalid. Length has to be 32');
$this->bridgeAddress = $bridgeAddress;
$this->authKey = $authKey;
}
public function makePest()
{
return new Pest( "http://" .$this->bridgeAddress. "/api/" .$this->authKey. "/" );
}
/**
* Gets a new API key from the bridge
* Hint: You have to press the button on the bridge first and then
* call this function
*
* @param $bridgeAddress string The IP or FQDN to the hue bridge
* @return string with the API key
* @throws HueError If no connection to the bridge could be established
* or the bridge refused to give one
* @throws InvalidArgumentException If the bridge address is not valid
**/
public static function fetchAPIAccessKey($bridgeAddress)
{
if ( strlen($bridgeAddress) == 0 )
throw new InvalidArgumentException(
'Parameter $bridgeAddress is empty');
$pest = new Pest('http://' . $bridgeAddress . '/api');
$data = json_encode(array(
'devicetype' => 'phpHue'
));;
$result = json_decode($pest->post('', $data), true);
if ( is_null($result) ) {
throw new HueError('No connection to the Hue bridge');
}
else if ( array_key_exists('error', $result[0]) ) {
throw new HueError($result[0]['error']['description']);
}
else if ( array_key_exists('success', $result[0]) ) {
return $result[0]['success']['username'];
}
else {
throw new HueError('Something went terribly bad. Should not happen');
}
}
/**
* Get all known lights
*
* @return array Containing HueLight objects
* @throws HueError If no communication with the API was possible
**/
public function getLights() {
$lights = array();
$pest = $this->makePest();
$result = json_decode($pest->get('lights'), true);
if ( is_null($result) ) {
throw new HueError('Was not able to retrieve lights');
}
foreach ( array_keys($result) as $currentLight ) {
$lights[] = new HueLight($this,
(int) $currentLight,
$result[$currentLight]);
}
return $lights;
}
public function getScenes() {
$scenes = array();
$pest = $this->makePest();
$result = json_decode($pest->get('scenes'), true);
if ( is_null($result) )
throw new HueErro('Was not able to retrieve scenes');
foreach ( array_keys($result) as $currentScene ) {
$scenes[] = new HueScene($this,
$currentScene,
$result[$currentScene]);
}
return $scenes;
}
// Gets an array of currently configured schedules
public function schedules()
{
$pest = $this->makePest();
$result = json_decode( $pest->get( "schedules" ), true );
return $result;
}
}
?>