Skip to content

Commit

Permalink
Merge pull request #816 from hplato/nanoleaf_aurora
Browse files Browse the repository at this point in the history
Nanoleaf aurora
  • Loading branch information
hplato committed Feb 2, 2021
2 parents 7e2113f + 041a8c1 commit 82f28f2
Showing 1 changed file with 89 additions and 32 deletions.
121 changes: 89 additions & 32 deletions lib/Nanoleaf_Aurora.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package Nanoleaf_Aurora;

# v2.0
# v3.0.2

#if any effect is changed, by definition the static child should be set to off.
#cmd data returns, need to check by command
Expand Down Expand Up @@ -106,6 +106,7 @@ $rest{brightness} = "state";
$rest{brightness2} = "state";
$rest{get_static} = "effects";
$rest{get_hsb} = "effects";
$rest{get_rhythm} = "effects";
$rest{identify} = "identify";

our %opts;
Expand All @@ -122,6 +123,7 @@ $opts{brightness} = "-response_code -json -put '{\"brightness\":{\"value\":";
$opts{brightness2} = "-response_code -json -put '{\"brightness\":{\"increment\":";
$opts{get_static} = "-response_code -json -put '{\"write\":{\"command\":\"request\",\"version\":\"1.0\",\"animName\":\"*Static*\"}}'";
$opts{get_hsb} = "-response_code -json -put '{\"write\":{\"command\":\"request\",\"version\":\"1.0\",\"animName\":\"*Solid*\"}}'";
$opts{get_rhythm} = "-response_code -json -put '{\"write\":{\"command\":\"requestAll\",\"version\":\"2.0\"}}'";
$opts{identify} = "-response_code -json -put '{}'";


Expand All @@ -145,7 +147,7 @@ sub new {
$self->{updating} = 0;
$self->{data}->{retry} = 0;
$self->{status} = "";
$self->{module_version} = "v1.2.01";
$self->{module_version} = "v3.0.2";
$self->{ssdp_timeout} = 4000;
$self->{last_static} = "";

Expand Down Expand Up @@ -184,6 +186,7 @@ sub new {
$self->{cmd_process} = new Process_Item;
$self->{cmd_process}->set_output( $self->{cmd_data_file} );
$self->{init} = 0;
$self->{init_rhythm} = 0;
$self->{init_data} = 0;
$self->{init_v_cmd} = 0;
&::MainLoop_post_add_hook( \&Nanoleaf_Aurora::process_check, 0, $self );
Expand Down Expand Up @@ -240,11 +243,16 @@ sub get_data {
}
elsif ( $self->{token} and $self->{url} ) {
$self->poll();

if ( ( defined $self->{data}->{panels} ) and ( $self->{init} == 0 ) ) {
main::print_log( "[Aurora:" . $self->{name} . "] " . $self->{module_version} . " Configuration Loaded" );
if (($self->{data}->{rhythm}) and ($self->{init_rhythm} == 0)) {
# #Find out which effects are Rhythm so do a requestAll on effects
$self->_push_JSON_data('get_rhythm');

} else {
main::print_log( "[Aurora:" . $self->{name} . "] " . $self->{module_version} . " Configuration Loaded" );
$self->print_info();
}
$active_auroras{ $self->{url} } = 1;
$self->print_info();
$self->{init} = 1;
}
}
Expand Down Expand Up @@ -424,7 +432,7 @@ sub process_check {

#Process any returned data from a command straing
if ( $self->{cmd_process_mode} eq "get_static" ) {
main::print_log( "[Aurora:" . $self->{name} . "] get_static returned" );
main::print_log( "[Aurora:" . $self->{name} . "] get_static returned" ) if ( $self->{debug} );
$self->{last_static} = $data->{animData} if ( defined $data->{animData} ); #just checked controller so update the static string
if ( ( defined $self->{static_check}->{string} ) and ( $self->{static_check}->{string} eq $data->{animData} ) ) {
$self->set_effect( $self->{static_check}->{effect} );
Expand All @@ -439,24 +447,43 @@ sub process_check {

}
if ( $self->{cmd_process_mode} eq "get_hsb" ) {
main::print_log( "[Aurora:" . $self->{name} . "] get_hsb returned" );
main::print_log( "[Aurora:" . $self->{name} . "] get_hsb returned" )if ( $self->{debug} );
#strangely, this query has proper hue/sat/brightness information and the general query doesn't
#TODO, figure out brightness when a solid
#print Dumper $data->{palette};
for ( my $i = 0; $i < @{$data->{palette}}; $i++ ) {
$self->{data}->{info}->{brightness}->{value2} .= ${$data->{palette}}[$i]->{brightness} . ",";
$self->{data}->{info}->{saturation}->{value2} .= ${$data->{palette}}[$i]->{saturation} . ",";
$self->{data}->{info}->{hue}->{value2} = ${$data->{palette}}[$i]->{hue} . ",";
}
chop($self->{data}->{info}->{brightness}->{value2});
chop($self->{data}->{info}->{saturation}->{value2});
chop($self->{data}->{info}->{hue}->{value2});
#print "B=".$self->{data}->{info}->{brightness}->{value2}." S=".$self->{data}->{info}->{saturation}->{value2}."H=".$self->{data}->{info}->{hue}->{value2} ."\n";
$self->{data}->{info}->{palette} = 1;


if (defined $data->{palette}) {
for ( my $i = 0; $i < @{$data->{palette}}; $i++ ) {
$self->{data}->{info}->{brightness}->{value2} .= ${$data->{palette}}[$i]->{brightness} . ",";
$self->{data}->{info}->{saturation}->{value2} .= ${$data->{palette}}[$i]->{saturation} . ",";
$self->{data}->{info}->{hue}->{value2} = ${$data->{palette}}[$i]->{hue} . ",";
}
chop($self->{data}->{info}->{brightness}->{value2});
chop($self->{data}->{info}->{saturation}->{value2});
chop($self->{data}->{info}->{hue}->{value2});
#print "B=".$self->{data}->{info}->{brightness}->{value2}." S=".$self->{data}->{info}->{saturation}->{value2}."H=".$self->{data}->{info}->{hue}->{value2} ."\n";
$self->{data}->{info}->{palette} = 1;
} else {
main::print_log( "[Aurora:" . $self->{name} . "] WARNING, no palette data returned from get_HSB call!" );
}
}

}

if ( $self->{cmd_process_mode} eq "get_rhythm" ) {
main::print_log( "[Aurora:" . $self->{name} . "] get_rhythm returned" ) if ( $self->{debug} );
if (defined $data->{animations}) {
for ( my $i = 0; $i < @{$data->{animations}}; $i++ ) {
$self->{data}->{rhythmdata}->{effects}->{${$data->{animations}}[$i]->{animName}} = 1 if (${$data->{animations}}[$i]->{pluginType} eq "rhythm");
}
} else {
main::print_log( "[Aurora:" . $self->{name} . "] No Rhythm effects found" );
}
#print Dumper $self->{data}->{info};
if ($self->{init_rhythm} == 0) {
main::print_log( "[Aurora:" . $self->{name} . "] " . $self->{module_version} . " Configuration Loaded" );
$self->print_info();
}
$self->{init_rhythm} = 1;
}
}

$self->poll;
}
Expand Down Expand Up @@ -508,6 +535,8 @@ sub _get_JSON_data {
my $token = "";
$token = "/" . $self->{token} if ( defined $self->{url} and lc $mode ne "auth" );
my $cmd = "get_url $params " . '"' . $self->{url} . $self->{api_path} . "$token/$rest{$mode}" . '"';
main::print_log( "[Aurora:" . $self->{name} . "] DEBUG Preparing command $cmd..." ) if ( $self->{debug} );;

if ( $self->{poll_process}->done() ) {
$self->{poll_process}->set($cmd);
$self->{poll_process}->start();
Expand Down Expand Up @@ -819,12 +848,16 @@ sub print_info {
if ( defined $self->{data}->{info}->{effects}->{list} ) {

foreach my $effect ( @{ $self->{data}->{info}->{effects}->{list} } ) {
main::print_log( "[Aurora:" . $self->{name} . "] - $effect" );
my $type = "[ ]";
$type = "[R]" if (exists $self->{data}->{rhythmdata}->{effects}->{$effect});
main::print_log( "[Aurora:" . $self->{name} . "] - $type $effect" );
}
}
else {
foreach my $effect ( @{ $self->{data}->{info}->{effects}->{effectsList} } ) {
main::print_log( "[Aurora:" . $self->{name} . "] - $effect" );
my $type = "[ ]";
$type = "[R]" if (exists $self->{data}->{rhythmdata}->{effects}->{$effect});
main::print_log( "[Aurora:" . $self->{name} . "] - $type $effect" );
}
}
main::print_log( "[Aurora:" . $self->{name} . "] -- Layout --" );
Expand Down Expand Up @@ -1025,14 +1058,29 @@ sub get_effect {
}

sub get_effects {
my ($self) = @_;
my ($self,$type) = @_;
my @effect_array = ();

if ( defined $self->{data}->{info}->{effects}->{list} ) { #beta structure
@effect_array = @{ $self->{data}->{info}->{effects}->{list} };
} else {
@effect_array = @{ $self->{data}->{info}->{effects}->{effectsList} };
#print Dumper $self->{data};
$type = "" unless ((lc $type eq "all") or (lc $type eq "rhythm") or (lc $type eq "standard"));
if ( defined $self->{data}->{info}->{effects}->{list} ) {

foreach my $effect ( @{ $self->{data}->{info}->{effects}->{list} } ) {
push(@effect_array, $effect) if (
(($self->{data}->{rhythmdata}->{effects}->{$effect}) and (lc $type eq "rhythm")) or
((lc $type eq "all") or ($type eq "")) or
((!$self->{data}->{rhythmdata}->{effects}->{$effect}) and (lc $type eq "standard")));
}
}
else {
foreach my $effect ( @{ $self->{data}->{info}->{effects}->{effectsList} } ) {
#print "**** $self->{data}->{rhythmdata}->{effects}->{$effect}, $effect, $type\n";
push(@effect_array, $effect) if (
(($self->{data}->{rhythmdata}->{effects}->{$effect}) and (lc $type eq "rhythm")) or
((lc $type eq "all") or ($type eq "")) or
((!$self->{data}->{rhythmdata}->{effects}->{$effect}) and (lc $type eq "standard")));
}
}

return @effect_array;
}

Expand Down Expand Up @@ -1123,14 +1171,22 @@ sub check_static {
return ('1');
}

sub is_rhythm_effect {
sub is_rhythm_active {
my ( $self) = @_;
my $return = 0;
$return = 1 if ($self->{data}->{info}->{rhythm}->{rhythmActive});

return $return;
}

sub is_rhythm_effect {
my ( $self,$effect) = @_;
my $return = 0;
$return = 1 if ($self->{data}->{info}->{rhythmdata}->{effects}->{$effect});

return $return;
}

sub print_static {
my ($self) = @_;

Expand Down Expand Up @@ -1301,8 +1357,8 @@ sub load_effects {
}

sub get_effects {
my ( $self ) = @_;
my @effects = $$self{master_object}->get_effects();
my ( $self,$type ) = @_;
my @effects = $$self{master_object}->get_effects("$type");

return @effects;

Expand Down Expand Up @@ -1431,3 +1487,4 @@ sub set {
# v1.1.01 - firmware v2.2.0 and rhythm module
# v1.1.03 - fixed a few typos
# v2.0.00 - added in hue/Saturation ability
# v3.0 - added rhythm attribute to get_effect.

0 comments on commit 82f28f2

Please sign in to comment.