Skip to content

Commit

Permalink
v2.0 - hue/sat mode
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Jul 16, 2020
1 parent 7322870 commit 68a1e12
Showing 1 changed file with 109 additions and 9 deletions.
118 changes: 109 additions & 9 deletions lib/Nanoleaf_Aurora.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package Nanoleaf_Aurora;

# v1.1.03
# v2.0

#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 @@ -43,6 +43,17 @@ use IO::Socket::INET;
# $aurora_static1 = new Nanoleaf_Aurora_Static($aurora, "effect string");
# $aurora_comm = new Nanoleaf_Aurora_Comm($aurora);

#Set static string, ie "3 82 1 255 0 255 0 20 60 1 0 255 255 0 20 118 1 0 0 0 0 20",

#<nPanels> = 3
#<numFrames> = 1
#Panel 1:
#PanelId: 82, R:255, G:0, B:255, W:0, TransitionTime:20*0.1s
#Panel 2:
#PanelId: 60, R:0, G:255, B:255, W:0, TransitionTime:20*0.1s
#Panel 3:
#PanelId:118, R:0, G:0, B:0, W:0, TransitionTime:20*0.1s

# MH.INI settings
# If the token is auto generated, it will be written to the mh.ini. MH.INI settings can be used
# instead of object definitions
Expand Down Expand Up @@ -88,27 +99,32 @@ $rest{off} = "state";
#$rest{set_effect} = "effects/select";
$rest{set_effect} = "effects";
$rest{set_static} = "effects";
$rest{set_hsb} = "effects";
#$rest{brightness} = "state/brightness";
#$rest{brightness2} = "state/brightness";
$rest{brightness} = "state";
$rest{brightness2} = "state";
$rest{get_static} = "effects";
$rest{get_hsb} = "effects";
$rest{identify} = "identify";

our %opts;
$opts{info} = "-ua";
$opts{auth} = "-json -post '{}'";
$opts{on} = "-response_code -json -put '{\"on\":true}'";
$opts{off} = "-response_code -json -put '{\"on\":false}'";
$opts{on} = "-response_code -json -put '{\"on\":{\"value\":true}}'";
$opts{off} = "-response_code -json -put '{\"on\":{\"value\":false}}'";
$opts{set_effect} = "-response_code -json -put '{\"select\":";
$opts{set_static} = "-response_code -json -put '{\"write\":{\"command\":\"display\",\"version\":\"1.0\",\"animType\":\"static\",\"animData\":";
$opts{set_hsb} = "-response_code -json -put '{\"write\":{\"command\":\"display\",\"version\":\"1.0\",\"animType\":\"solid\",\"palette\":";
#$opts{brightness} = "-response_code -json -put '{\"value\":";
#$opts{brightness2} = "-response_code -json -put '{\"increment\":";
$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{identify} = "-response_code -json -put '{}'";


my $api_path = "/api/v1";
our %active_auroras = ();

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

Expand Down Expand Up @@ -369,7 +385,7 @@ sub process_check {
main::print_log( "[Aurora:" . $self->{name} . "] Background Command " . $self->{cmd_process_mode} . " process completed" )
if ( $self->{debug} );
$self->get_data(); #poll since the command is done to get a new state

$self->{data}->{info}->{palette} = 0; #assume this is not a solid effect change.
my $file_data = &main::file_read( $self->{cmd_data_file} );

my ($responsecode) = $file_data =~ /^RESPONSECODE:(\d+)\n/;
Expand All @@ -379,7 +395,7 @@ sub process_check {
#print "success\n\n" if (($responsecode == 204) or ($responsecode == 200));
my $com_status = "offline";

if ( ( $responsecode == 204 ) or ( $responsecode == 200 ) ) {
if ( ( $responsecode == 204 ) or ( $responsecode == 200 ) or ( $responsecode == 404) ) { #added 404 since a few calls can generate 404 if the data isn't present (like querying static without a static scene active)
$com_status = "online";

# Successful commands should be [200 OK, 204 No Content]
Expand Down Expand Up @@ -422,6 +438,24 @@ sub process_check {
}

}
if ( $self->{cmd_process_mode} eq "get_hsb" ) {
main::print_log( "[Aurora:" . $self->{name} . "] get_hsb returned" );
#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;

}

}

$self->poll;
Expand Down Expand Up @@ -736,8 +770,22 @@ sub print_info {
else {
main::print_log( "[Aurora:" . $self->{name} . "] State:\t OFF" );
}
main::print_log(
"[Aurora:" . $self->{name} . "] Mode:\t " . $self->{data}->{info}->{state}->{colorMode} . " " . $self->{data}->{info}->{effects}->{select} );
main::print_log("[Aurora:" . $self->{name} . "] Mode:\t " . $self->{data}->{info}->{state}->{colorMode} . " " . $self->{data}->{info}->{effects}->{select} );

if ($self->{data}->{info}->{palette} == 1) {
main::print_log( "[Aurora:"
. $self->{name}
. "] Brightness:\t "
. $self->{data}->{info}->{state}->{brightness}->{value2});
main::print_log( "[Aurora:"
. $self->{name}
. "] Hue:\t\t "
. $self->{data}->{info}->{state}->{hue}->{value2});
main::print_log( "[Aurora:"
. $self->{name}
. "] Saturation:\t "
. $self->{data}->{info}->{state}->{sat}->{value2});
} else {
main::print_log( "[Aurora:"
. $self->{name}
. "] Brightness:\t "
Expand All @@ -759,6 +807,7 @@ sub print_info {
. $self->{data}->{info}->{state}->{sat}->{min} . "-"
. $self->{data}->{info}->{state}->{sat}->{max}
. "]" );
}
main::print_log( "[Aurora:"
. $self->{name}
. "] Color Temp:\t "
Expand Down Expand Up @@ -837,7 +886,11 @@ sub process_data {
}
$self->{init_data} = 1;
}

if (( lc $self->{data}->{info}->{effects}->{select} eq "*dynamic*" ) or ( lc $self->{data}->{info}->{effects}->{select} eq "*solid*" )) {
$self->{data}->{info}->{palette} = 1;
} else {
$self->{data}->{info}->{palette} = 0;
}
if ( $self->{previous}->{info}->{firmwareVersion} ne $self->{data}->{info}->{firmwareVersion} ) {
main::print_log(
"[Aurora:" . $self->{name} . "] Firmware changed from $self->{previous}->{info}->{firmwareVersion} to $self->{data}->{info}->{firmwareVersion}" );
Expand Down Expand Up @@ -1050,6 +1103,17 @@ sub set_static {

}

#TODO set hue and Saturation directly
sub set_hsb {
my ( $self, $hue, $saturation, $brightness ) = @_;

my $params = $opts{set_hsb} . '[{ "hue":' . $hue . ',"saturation":' . $saturation . ',"brightness":' . $brightness . "}],";
$params .= '"colorType": "HSB"}}' . "'";

$self->_push_JSON_data( 'set_hsb', $params );
return ('1');
}

sub check_static {
my ( $self, $string, $prev_effect ) = @_;

Expand Down Expand Up @@ -1082,6 +1146,39 @@ sub get_static {
return ('1');
}

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

$self->_push_JSON_data('get_hsb');
return ('1');
}

sub hue {
my ( $self) = @_;
my $return = $self->{data}->{info}->{state}->{hue}->{value};
$return = $self->{data}->{info}->{state}->{hue}->{value2} if ($self->{data}->{info}->{palette} == 1);
return $return;
}

sub saturation {
my ( $self) = @_;
my $return = $self->{data}->{info}->{state}->{sat}->{value};
$return = $self->{data}->{info}->{state}->{sat}->{value2} if ($self->{data}->{info}->{palette} == 1);
return $return;
}

sub brightness {
my ( $self) = @_;
my $return = $self->{data}->{info}->{state}->{brightness}->{value};
$return = $self->{data}->{info}->{state}->{brightness}->{value2} if ($self->{data}->{info}->{palette} == 1);
return $return;
}

sub ct {
my ( $self) = @_;
return $self->{data}->{info}->{state}->{brightness}->{value}
}

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

Expand Down Expand Up @@ -1226,6 +1323,7 @@ sub new {
$$self{loop} = 0;
$$self{string} = $static_string if ( defined $static_string );
$object->register( $self, 'static' );
$self->SUPER::set('off'); #turn off at initialization and then set when data comes in.
return $self;

}
Expand Down Expand Up @@ -1330,4 +1428,6 @@ sub set {
# v1.0.13 - ability to print and purge the command queue in case a network error prevents clearing, empty poll queue if max reached
# v1.0.14 - commands now queue properly
# v1.0.15 - fixed polling
# 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

0 comments on commit 68a1e12

Please sign in to comment.