/** * Send request * * @param string $address API address * @param string $method Request method * @param \stdClass $body Post body * * @return string Request response * @throws Exception\ConnectionException * @throws \Exception */ public function sendRequest($address, $method = self::METHOD_GET, \stdClass $body = null) { // Build request url $url = "http://{$this->client->getHost()}/api/{$address}"; // Open connection $this->getAdapter()->open(); // Send and get response $results = $this->getAdapter()->send($url, $method, $body ? json_encode($body) : null); $status = $this->getAdapter()->getHttpStatusCode(); $contentType = $this->getAdapter()->getContentType(); // Close connection $this->getAdapter()->close(); // Throw connection exception if status code isn't 200 or wrong content type if ($status != 200 || $contentType != 'application/json') { throw new ConnectionException('Connection failure'); } // Parse json results $jsonResults = json_decode($results); // Get first element in array if it's an array response if (is_array($jsonResults)) { $jsonResults = $jsonResults[0]; } // Get error type if (isset($jsonResults->error)) { throw $this->getExceptionByType($jsonResults->error->type, $jsonResults->error->description); } // Get success object only if available if (isset($jsonResults->success)) { $jsonResults = $jsonResults->success; } return $jsonResults; }
/** * Send request * * @param string $address * API address * @param string $method * Request method * @param \stdClass $body * Post body * * @return \stdClass Json body */ protected function getJsonResponse($address, $method = self::METHOD_GET, \stdClass $body = null) { // Build request url $url = "http://{$this->client->getHost()}{$address}"; // Open connection $this->getAdapter()->open(); // Send and get response $results = $this->getAdapter()->send($url, $method, $body ? json_encode($body) : null); $status = $this->getAdapter()->getHttpStatusCode(); $contentType = $this->getAdapter()->getContentType(); // Throw connection exception if status code isn't 200 or wrong content type if ($status != 200 || $contentType != 'application/json') { throw new ConnectionException('Connection failure'); } // Parse json results $jsonResults = json_decode($results); return $jsonResults; }
protected function execute(InputInterface $input, OutputInterface $output) { $ip = $input->getArgument('ip'); $username = $input->getArgument('username'); if ($ip) { $ipAddress = $ip; } else { $output->writeln('Finding your Bridge...'); $bridges = json_decode(file_get_contents('https://www.meethue.com/api/nupnp')); /** * No bridges - stop. */ if (!$bridges) { $output->writeln('No bridges found - sorry!'); } /** * One bridge, grab IP. */ if (count($bridges) == 1) { $ipAddress = $bridges[0]->internalipaddress; } /** * Multiple bridges? Ask for IP. */ if (count($bridges) > 1) { $choices = []; foreach ($bridges as $bridge) { $choices[] = $bridge->internalipaddress; } $helper = $this->getHelper('question'); $question = new ChoiceQuestion('We found multiple bridges. Which one do you want to connect to?', $choices, 0); $question->setErrorMessage('Choice %s is invalid.'); $choice = $helper->ask($input, $output, $question); $ipAddress = $choice; $output->writeln('You have just selected: ' . $choice); } } if ($input->getOption('yell')) { // } /** * Username. */ $user = $username ? $username : get_current_user(); $user = $user . substr(md5(microtime()), rand(0, 26), 10); /** * Authenticate with Bridge. */ $client = new Client($ipAddress, $user); $output->writeln("Testing connection to bridge at {$client->getHost()}"); try { $client->sendCommand(new Ping()); } catch (ConnectionException $e) { $output->writeln("Issue connecting to bridge - " . $e->getMessage()); exit(1); } $output->writeln("Attempting to create user: "******"..."); $output->writeln("Press the Bridge's button! Waiting..."); $maxTries = 30; for ($i = 1; $i <= $maxTries; ++$i) { try { $response = $client->sendCommand(new CreateUser($user)); $output->write("\n"); $output->writeln("Successfully created new user: "******"."); } catch (Exception $e) { $output->writeln("Failure to create user. Please try again!"); $output->writeln("Reason: " . $e->getMessage()); break; } sleep(1); } /** * Save to memory. * @todo Would prefer a better way of doing this, if you can think of one. */ if ($ipAddress && $user) { file_put_contents("config.txt", json_encode(['ip' => $ipAddress, 'username' => $user])); } $output->writeln("Let there be light! Use `hue list` to see available commands."); }