Permalink
Browse files

a bunch of stuff, shit, im tired

  • Loading branch information...
peledies committed Dec 7, 2017
1 parent d189749 commit e2d7a88a8357a74c668645bbab3bbfcfe4182247
Showing with 288 additions and 17 deletions.
  1. +1 −0 bin/console
  2. +1 −1 src/Effect/GroupPop.php
  3. +122 −0 src/Effect/GroupWave.php
  4. +1 −1 src/Effect/LightPop.php
  5. +72 −9 src/Effect/Wave.php
  6. +1 −2 src/Helpers/Bridge.php
  7. +10 −4 src/Light/Update.php
  8. +80 −0 src/Traits/Validate.php
View
@@ -26,6 +26,7 @@ $console->addCommands(array(
new Hue\Effect\Loop(),
new Hue\Effect\LightPop(),
new Hue\Effect\GroupPop(),
new Hue\Effect\GroupWave(),
));
// End of relevant addition
View
@@ -20,7 +20,7 @@ protected function configure()
->addOption('red','r',InputOption::VALUE_REQUIRED,'Red value (0 - 255)')
->addOption('green','g',InputOption::VALUE_REQUIRED,'Green value (0 - 255)')
->addOption('blue','b',InputOption::VALUE_REQUIRED,'Blue value (0 - 255)')
->setName('effect:grouppop')
->setName('effect:group-pop')
->setDescription('flash a color then return')
;
}
View
@@ -0,0 +1,122 @@
<?php
namespace Hue\Effect;
require_once 'vendor/autoload.php';
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
class GroupWave extends Command
{
use \Hue\Traits\Sort;
use \Hue\Traits\Validate;
use \Hue\Traits\Attributes;
// starting value for red
protected $red = 0;
// starting value for green
protected $green = 125;
// starting value for blue
protected $blue = 125;
protected function configure()
{
$this
->setName('effect:group-wave')
->addOption('id','i',InputOption::VALUE_REQUIRED,'Group ID')
->addOption('name','m',InputOption::VALUE_REQUIRED,'Group Name')
->addOption('transition','t',InputOption::VALUE_REQUIRED,'Transition Speed (seconds)')
->setDescription('front to back wave for a group')
;
}
/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$client = new \Hue\Helpers\Client();
$this->validate($input, ['i','m']);
$this->nameToIdOrId($client);
$transition = (is_null($input->getOption('transition')))? 3 : (int) $input->getOption('transition');
$light_ids = array_shift(array_map(function($group){
return $this->getAttributes($group)->lights;
}, $client->getGroups()));
$light_obs = array_filter($client->getLights(), function($light) use ($light_ids){
return (in_array($light->getId(), $light_ids));
});
$lights = array_map(function($light){
return [
'id' => $light->getId()
, 'name' => $light->getName()
];
}, $light_obs);
$groups = [];
// group into row arrays
for ($i = 'A'; $i !== 'Z'; $i++){
$groups[] = array_filter($lights, function($light) use ($i){
return (preg_match("/$i/",$light['name']));
});
}
// order the rows by matrix order
foreach ($groups as $key => $group) {
$groups[$key] = $this->sortByKey($group, 'name');
if(empty($groups[$key])){
unset($groups[$key]);
}
}
$wave = 0;
while(true){
$xy = \Phue\Helper\ColorConversion::convertRGBToXY(
$this->red
, $this->green
, $this->blue
);
foreach ($groups as $key => $group) {
foreach ($group as $bulb) {
$light = new \Phue\Command\SetLightState($bulb['id']);
$command = $light
->hue(0)
->saturation(255)
->colorTemp(153)
->xy($xy['x'], $xy['y'])
->transitionTime($transition);
$client->sendCommand($command);
}
sleep($transition);
}
$wave++;
$this->red = rand(0, 255);
$this->green = rand(0, 255);
$this->blue = rand(0, 255);
dump("Wave - $wave");
dump("R: $this->red, G: $this->green B: $this->blue");
}
}
}
View
@@ -20,7 +20,7 @@ protected function configure()
->addOption('red','r',InputOption::VALUE_REQUIRED,'Red value (0 - 255)')
->addOption('green','g',InputOption::VALUE_REQUIRED,'Green value (0 - 255)')
->addOption('blue','b',InputOption::VALUE_REQUIRED,'Blue value (0 - 255)')
->setName('effect:lightpop')
->setName('effect:light-pop')
->setDescription('flash a color then return')
;
}
View
@@ -7,15 +7,26 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
class Wave extends Command
{
use \Hue\Traits\Sort;
// starting value for red
protected $red = 0;
// starting value for green
protected $green = 125;
// starting value for blue
protected $blue = 125;
protected function configure()
{
$this
->setName('effect:wave')
->addOption('transition','t',InputOption::VALUE_REQUIRED,'Transition Speed (seconds)')
->setDescription('front to back wave')
;
}
@@ -26,17 +37,69 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$client = new \Hue\Helpers\Client();
$light = $client->sendCommand(
new \Phue\Command\GetLightById($id)
);
$transition = (is_null($input->getOption('transition')))? 3 : (int) $input->getOption('transition');
$lights = array_map(function($light){
return [
'id' => $light->getId()
, 'name' => $light->getName()
];
}, $client->getLights());
dump($light);
$light->delete();
$groups = [];
// group into row arrays
for ($i = 'A'; $i !== 'Z'; $i++){
$groups[] = array_filter($lights, function($light) use ($i){
return (preg_match("/$i/",$light['name']));
});
}
// order the rows by matrix order
foreach ($groups as $key => $group) {
$groups[$key] = $this->sortByKey($group, 'name');
if(empty($groups[$key])){
unset($groups[$key]);
}
}
$wave = 0;
while(true){
$xy = \Phue\Helper\ColorConversion::convertRGBToXY(
$this->red
, $this->green
, $this->blue
);
foreach ($groups as $key => $group) {
foreach ($group as $bulb) {
$light = new \Phue\Command\SetLightState($bulb['id']);
$command = $light
->hue(0)
->saturation(255)
->colorTemp(153)
->xy($xy['x'], $xy['y'])
->transitionTime($transition);
$client->sendCommand($command);
}
sleep($transition);
}
$wave++;
$this->red = rand(0, 255);
$this->green = rand(0, 255);
$this->blue = rand(0, 255);
dump("Wave - $wave");
dump("R: $this->red, G: $this->green B: $this->blue");
}
}
View
@@ -14,8 +14,7 @@ public function __construct(){
// Don't continue if bad response
if ($response === false) {
echo "Request failed. Ensure that you have internet connection.";
exit(1);
throw new \Exception("Request failed. Ensure that you have internet connection, and that the bridge is properly connected", 1);
}
// Parse the JSON response
View
@@ -12,11 +12,13 @@
class Update extends Command
{
use \Hue\Traits\Attributes;
use \Hue\Traits\Validate;
protected function configure()
{
$this
->addOption('id','i',InputOption::VALUE_REQUIRED,'Bulb ID')
->addOption('name','m',InputOption::VALUE_REQUIRED,'Bulb Name')
->addOption('red','r',InputOption::VALUE_REQUIRED,'Red value (0 - 255)')
->addOption('green','g',InputOption::VALUE_REQUIRED,'Green value (0 - 255)')
->addOption('blue','b',InputOption::VALUE_REQUIRED,'Blue value (0 - 255)')
@@ -32,13 +34,17 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$client = new \Hue\Helpers\Client();
$this->validate($input, ['i','m','r','g','b']);
$light = new \Phue\Command\SetLightState($input->getOption('id'));
$this->nameToIdOrId($client);
$light = new \Phue\Command\SetLightState($this->id);
$xy = \Phue\Helper\ColorConversion::convertRGBToXY(
$input->getOption('red')
, $input->getOption('green')
, $input->getOption('blue')
$this->red
, $this->green
, $this->blue
);
$command = $light
View
@@ -0,0 +1,80 @@
<?php
namespace Hue\Traits;
use Symfony\Component\Console\Input\InputInterface;
trait Validate {
public function validate(InputInterface $input, Array $validate = []){
$throw = false;
$msg = [];
$lookup = [
'i' => 'id'
, 'm' => 'name'
, 'r' => 'red'
, 'g' => 'green'
, 'b' => 'blue'
];
$cast = [
'i' => 'int'
, 'm' => 'string'
, 'r' => 'int'
, 'g' => 'int'
, 'b' => 'int'
];
foreach ($validate as $option) {
if(array_key_exists($option, $lookup)){
$this->{$lookup[$option]} = ($cast[$option] == 'int')
? (int) $input->getOption($lookup[$option])
: $input->getOption($lookup[$option]);
}
}
if(in_array('r', $validate) && is_null($this->red)){
$throw = true;
$msg[] = "\n\nYou must specify -r [red] (0 - 255)";
}
if(in_array('g', $validate) && is_null($this->green)){
$throw = true;
$msg[] = "\n\nYou must specify -g [green] (0 - 255)";
}
if(in_array('b', $validate) && is_null($this->blue)){
$throw = true;
$msg[] = "\n\nYou must specify -b [blue] (0 - 255)";
}
if(
( in_array('i', $validate) && !isset($this->id) )
&&
( in_array('m', $validate) && !isset($this->name) )
){
$throw = true;
$msg[] = "\n\nYou must specify either -m [name] or -i [id] of a bulb";
}
if($throw){
throw new \Exception(implode('', $msg), 1);
}
}
public function nameToIdOrId(\Hue\Helpers\Client $client){
if(!is_null($this->name)){
$light = array_filter($client->getLights(), function($light) {
return (strtolower($light->getName()) == strtolower($this->name));
});
if($light){
$this->id = (int) array_shift($light)->getId();
}else{
throw new \Exception("Bulb with name [ {$this->name} ] could not be found");
}
}
return (int) $this->id;
}
}

0 comments on commit e2d7a88

Please sign in to comment.