#!/usr/bin/perl use FindBin; # find the script's directory use lib $FindBin::Bin; # add that directory to the library path use xPL::common; ################################################################################ # constants # $vendor_id = 'dspc'; # from xplproject.org $device_id = 'hueClock'; # max 8 chars $class_id = 'lighting'; # max 8 chars $separator = '-' x 80; $indent = ' ' x 2; #------------------------------------------------------------------------------- # global variables # my %configuration; $configuration{'hoursBulbId'} = 1; $configuration{'minutesBulbId'} = 2; ################################################################################ # Input arguments # use Getopt::Std; my %opts; getopts('hvp:n:t:w:o:m:', \%opts); die("\n". "Usage: $0 [options]\n". "\n". "Options:\n". "${indent}-h display this help message\n". "${indent}-v verbose\n". "${indent}-n id the instance id (max. 12 chars)\n". "${indent}-t mins the heartbeat interval in minutes\n". "${indent}-w secs the startup sleep interval\n". "${indent}-o id the id of the hours bulb\n". "${indent}-m id the id of the minutes bulb\n". "\n". "Controls a Philips hue lighting system.\n". "\n". "More information with: perldoc $0\n". "\n". "" ) if ($opts{h}); my $verbose = $opts{v}; my $client_base_port = $opts{'p'} || 50000; my $instance_id = $opts{'n'} || xpl_build_automatic_instance_id; my $heartbeat_interval = $opts{'t'} || 5; my $startup_sleep_time = $opts{'w'} || 0; $configuration{'hoursBulbId'} = $opts{'o'} || $configuration{'hoursBulbId'}; $configuration{'minutesBulbId'} = $opts{'m'} || $configuration{'minutesBulbId'}; ################################################################################ # Internal functions # #------------------------------------------------------------------------------- # Transform hour and minute to hues # sub calc_hue_from_time { my ($hour, $minute) = @_; # hour hue my $hour_hue = int($hour * 2**16/24 + 0.5); # minute hue my $minute_hue = int($minute * 2**16/60 + 0.5); return($hour_hue, $minute_hue) } ################################################################################ # Catch control-C interrupt # $SIG{INT} = sub{ $xpl_end++ }; ################################################################################ # Main script # sleep($startup_sleep_time); # xPL parameters my $xpl_id = xpl_build_id($vendor_id, $device_id, $instance_id); my $xpl_ip = xpl_find_ip; # create xPL socket my ($client_port, $xpl_socket) = xpl_open_socket($xpl_port, $client_base_port); # display working parameters if ($verbose == 1) { system("clear"); print("$separator\n"); print("Controlling a Philips hue via bridge at \"$configuration{'bridge'}\".\n"); print($indent . "class id: $class_id\n"); print($indent . "instance id: $instance_id\n"); print("\n"); } #------------------------------------------------------------------------------- # Main loop # my $timeout = 1; my $last_heartbeat_time = 0; while ( (defined($xpl_socket)) && ($xpl_end == 0) ) { # check time and send heartbeat $last_heartbeat_time = xpl_send_heartbeat( $xpl_socket, $xpl_id, $xpl_ip, $client_port, $heartbeat_interval, $last_heartbeat_time ); # get xpl-UDP message with timeout my ($xpl_message) = xpl_get_message($xpl_socket, $timeout); # process XPL message if ($xpl_message) { my ($type, $source, $target, $schema, %body) = xpl_get_message_elements($xpl_message); # check for clock tick if ( ($type eq 'xpl-stat') and ($schema eq 'clock.tick') ) { # calculate hues my ($hour, $minute) = split(/h/, $body{'time'}); my ($hour_hue, $minute_hue) = calc_hue_from_time($hour, $minute); if ($verbose > 0) { print("\n"); print("It is now ${hour}h$minute\n"); print("${indent}Sending hues " . sprintf('%04X', $hour_hue) . "h, " . sprintf('%04X', $minute_hue) . "h\n" ); } xpl_send_message( $xpl_socket, $xpl_port, 'xpl-cmnd', $xpl_id, '*', 'lighting.basic', ( 'device' => $configuration{'hoursBulbId'}, 'color' => $hour_hue, 'saturation' => 100 ) ); xpl_send_message( $xpl_socket, $xpl_port, 'xpl-cmnd', $xpl_id, '*', 'lighting.basic', ( 'device' => $configuration{'minutesBulbId'}, 'color' => $minute_hue, 'saturation' => 100 ) ); } } } xpl_disconnect($xpl_socket, $xpl_id, $xpl_ip, $client_port); ################################################################################ # Documentation (access it with: perldoc ) # __END__ =head1 NAME xpl-hueClock.pl - Controls a Philips hue lighting system =head1 SYNOPSIS xpl-hueClock.pl [options] =head1 DESCRIPTION This xPL client listens to clock ticks and sends commands to 2 hue light bulbs. =head1 OPTIONS =over 8 =item B<-h> Display a help message. =item B<-v> Be verbose. =item B<-p port> Specify the base port from which the client searches for a free port. If not specified, the client will take a default value. =item B<-n id> Specify the instance id (name). The id is limited to 12 characters. If not specified, it is constructed from the host name and the serial port controller name. =item B<-t mins> Specify the number of minutes between two heartbeat messages. =item B<-w secs> Specify the number of seconds before sending the first heartbeat. This allows to start the client after the hub, thus eliminating an prospective startup delay of one heartbeat interval. =item B<-o id> Specify the hue hour light bulb's id. =item B<-m id> Specify the hue minute light bulb's id. =back =head1 TEST Make sure you have an C running on the machine. Start the hue controller: C. Start C in another terminal window. Launch the command C. Both bulb should turn red on and C display the control message. =head1 AUTHOR Francois Corthay, DSPC =head1 VERSION 1.0, 2014 =cut