예제 #1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /**
      * Get the chosen light ID.
      */
     $lightID = $input->getArgument('id');
     /**
      * Create client.
      */
     $client = new Client(Hue::getConfig()->ip, Hue::getConfig()->username);
     /**
      * Check that the chosen light ID exists and is reachable.
      */
     if (!isset($client->getLights()[$lightID]) || !$client->getLights()[$lightID]->isReachable()) {
         $output->writeln('<error>Light doesn\'t exist or is not reachable!</error>');
         $output->writeln('Use `hue lights` to see available lights.');
         exit(1);
     }
     $light = $client->getLights()[$lightID];
     /**
      * Turn all light on or off.
      */
     $on = $input->getArgument('on');
     if ($on) {
         $to = true;
         if ($on == 'off') {
             $to = false;
         }
         /**
          * If set to on, turn on.
          */
         $light->setOn($to);
         /**
          * If set to off, turn off & stop here.
          */
         if (!$to) {
             return true;
         }
     }
     /**
      * Get the color argument.
      */
     $color = $input->getArgument('color');
     /**
      * Handle if only 'on' param set (intended to be color).
      */
     if ($on && $on !== 'on' && $on !== 'off' && !$color) {
         $color = $on;
     }
     /**
      * Possible moods.
      */
     $possible_moods = Colors::colors();
     /**
      * If have mood, set it.
      */
     if ($color) {
         /**
          * If mood matches possible ones, go ahead.
          */
         if (isset($possible_moods[$color])) {
             $color = $possible_moods[$color];
             /**
              * Set the light to be that color.
              */
             $light->setXY($color[0], $color[1])->setBrightness(100);
         } else {
             $output->writeln('<question>That doesn\'t look to be a supported color! Try one of these:</question>');
             foreach ($possible_moods as $possible_mood => $mood_colors) {
                 $output->writeln('<comment>' . ucfirst($possible_mood) . '</comment>');
             }
         }
     }
     /**
             if ($color) {
                 /**
     * Calculate X & Y points.
     */
     /*
         $xy = Converter::hexToXY($color);
     
         $red = [0.675, 0.322];
         $green = [0.409, 0.518];
         $blue = [0.167, 0.04];
     
         $area = 1/2*(-$green[1]*$blue[0] + $red[1]*(-$green[0] + $blue[0]) + $red[0]*($green[1] - $blue[1]) + $green[0]*$blue[1]);
         $alpha = 1/(2*$area)*($red[1]*$blue[0] - $red[0]*$blue[1] + ($blue[1] - $red[1])*$xy[0] + ($red[0] - $blue[0])*$xy[1]);
         $beta = 1/(2*$area)*($red[0]*$green[1] - $red[1]*$green[0] + ($red[1] - $green[1])*$xy[0] + ($green[0] - $red[0])*$xy[1]);
         $gamma = 1 - $alpha - $beta;
         $altX = $alpha*$red[0] + $beta*$green[0] + $gamma*$blue[0];
         $altY = $alpha*$red[1] + $beta*$green[1] + $gamma*$blue[1];
     
         if ($alpha > 1 && $beta > 1 && $gamma > 1) {
             $useXY = $xy;
         } else {
             $useXY = [$altX, $altY];
         }
     
         $client->sendCommand(
             (new SetLightState($light))
                 ->xy($useXY[0], $useXY[1])
         );
     }
     */
     /**
      * If effect option on, set the effect.
      */
     $effect = $input->getOption('effect') ? $input->getOption('effect') : 'none';
     if ($light->isOn() && $light->isReachable()) {
         $light->setEffect($effect);
     }
     /**
      * If brightness option on, set it.
      */
     if ($input->getOption('brightness') && $light->isOn() && $light->isReachable()) {
         $light->setBrightness($input->getOption('brightness'));
     }
     /**
      * If rename option set, rename.
      */
     if ($input->getOption('rename')) {
         $light->setName($input->getOption('rename'));
     }
 }
예제 #2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /**
      * Create client.
      */
     $client = new Client(Hue::getConfig()->ip, Hue::getConfig()->username);
     /**
      * Turn all lights on or off.
      */
     $on = $input->getArgument('on');
     if ($on) {
         $to = true;
         if ($on == 'off') {
             $to = false;
         }
         /**
          * If set to on, turn on.
          */
         foreach ($client->getLights() as $lightId => $light) {
             $light->isReachable() ? $light->setOn($to) : '';
         }
         /**
          * If set to off, turn off & stop here.
          */
         if (!$to) {
             return true;
         }
     }
     $color = $input->getArgument('color');
     /**
      * Handle if only 'on' param set (intended to be color).
      */
     if ($on && $on !== 'on' && $on !== 'off' && !$color) {
         $color = $on;
     }
     /**
      * Possible moods.
      */
     $possible_moods = Colors::colors();
     /**
      * If have mood, set it.
      */
     if ($color) {
         /**
          * If mood matches possible ones, go ahead.
          */
         if (isset($possible_moods[$color])) {
             $color = $possible_moods[$color];
             /**
              * Set each light to be that color.
              */
             foreach ($client->getLights() as $lightId => $light) {
                 if ($light->isReachable()) {
                     $light->setXY($color[0], $color[1])->setBrightness(100);
                 }
             }
         } else {
             $output->writeln('<question>That doesn\'t look to be a supported color! Try one of these:</question>');
             foreach ($possible_moods as $possible_mood => $mood_colors) {
                 $output->writeln('<comment>' . ucfirst($possible_mood) . '</comment>');
             }
         }
     }
     /**
      * No arguments added, just list available lights.
      */
     if (!$on && !$color) {
         if ($client->getLights()) {
             foreach ($client->getLights() as $lightId => $light) {
                 $rows[] = [$light->getId(), $light->getName(), $light->getType(), $light->isOn() ? '✔' : '✘', $light->isReachable() ? '✔' : '✘'];
             }
             $table = new Table($output);
             $table->setHeaders(array('ID', 'Name', 'Type', 'On', 'Reachable'))->setRows($rows);
             $table->render();
         }
     }
     /**
      * If effect option on, set the effect for all lights.
      */
     $effect = $input->getOption('effect') ? $input->getOption('effect') : 'none';
     foreach ($client->getLights() as $lightId => $light) {
         if ($light->isOn() && $light->isReachable()) {
             $light->setEffect($effect);
         }
     }
     /**
      * If brightness option on, set it.
      */
     if ($input->getOption('brightness')) {
         foreach ($client->getLights() as $lightId => $light) {
             if ($light->isOn() && $light->isReachable()) {
                 $light->setBrightness($input->getOption('brightness'));
             }
         }
     }
 }