[ 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => $method, 'content' => $body ] ]; $context = stream_context_create($options); return @json_decode(file_get_contents('http://'.self::$ip.'/api/'.$url, false, $context), true); } private static function get($api, $body = '') { return self::request('GET', $api, $body); } private static function put($api, $body = '') { return self::request('PUT', $api, $body); } private static function post($api, $body = '') { return self::request('POST', $api, $body); } private static function delete($api, $body = '') { return self::request('DELETE', $api, $body); } // public methods public static function getLightId($light) { if (isset($light) && !is_numeric($light)) { $lLight = strtolower($light); $lights = self::getLights(); foreach ($lights as $i => $l) { if (strtolower($l['name']) == $lLight) { return $i; } } } return $light; } public static function getGroupId($group) { if (isset($group) && !is_numeric($group)) { $lGroup = mb_strtolower($group); $groups = self::getGroups(); foreach ($groups as $i => $g) { if (mb_strtolower($g['name']) == $lGroup) { return $i; } } } return $group; } public static function getLights($light = null) { $light = self::getLightId($light); return self::get('lights'.($light ? "/$light" : '')); } public static function getGroups($group = null) { if (!is_numeric($group)) $group = self::$groups[$group]; return self::get('groups'.($group ? "/$group" : '')); } public static function setLight($light, $state) { $light = self::getLightId($light); $key = 'on'; if ($state == 'toggle') { $key = $state; $state = true; } if (strtolower($state) === 'off') $state = false; return self::put("lights/$light/state", json_encode([ $key => $state ? true : false ])); } public static function setGroup($group, $state) { $group = self::getGroupId($group); if ($state == 'toggle') { $groupData = self::get("groups/$group"); if ($groupData['state']['any_on']) $state = false; else $state = true; } if (strtolower($state) === 'off') $state = false; return self::put("groups/$group/action", json_encode([ 'on' => $state ? true : false ])); } }