Send command to server
public sendCommand ( Phue\Command\CommandInterface $command ) : mixed | ||
$command | Phue\Command\CommandInterface | Phue command |
return | mixed | Command result |
/** * Adjust individual light * * @param $id * @param $brightness * @return array */ public function adjustLight($id, $brightness) { try { $light = $this->client->sendCommand(new \Phue\Command\GetLightById($id)); $light->setOn(true); $command = new \Phue\Command\SetLightState($light); $command->brightness($brightness); $this->client->sendCommand($command); return array('command' => true); } catch (\Exception $e) { dd($e); return array('command' => $e->getMessage()); } }
/** * Set Color temperature * * @param int $value Color temperature value * * @return Light Self object */ public function setColorTemp($value) { $this->client->sendCommand((new SetLightState($this))->colorTemp((int) $value)); // Change both internal color temp and colormode state $this->attributes->state->ct = (int) $value; $this->attributes->state->colormode = 'ct'; return $this; }
/** * Delete user */ public function delete() { $this->client->sendCommand(new DeleteUser($this)); }
/** * Set link button state * * @param bool $state True for on, false for off * * @return self This object */ public function setLinkButtonOn($state = true) { $this->client->sendCommand(new SetBridgeConfig(['linkbutton' => (bool) $state])); $this->attributes->linkbutton = (bool) $state; return $this; }
/** * Delete schedule */ public function delete() { $this->client->sendCommand(new Command\DeleteSchedule($this)); }
/** * Disable the install notification * * @return self This object */ public function disableInstallNotification() { $this->client->sendCommand(new SetBridgeConfig(['swupdate' => ['notify' => false]])); $this->attributes->notify = false; return $this; }
/** * Set name of bridge * * @param string $name Name * * @return Bridge Self object */ public function setName($name) { $this->client->sendCommand(new SetBridgeConfig(['name' => (string) $name])); $this->attributes->name = (string) $name; return $this; }
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."); }