#!/usr/bin/perl use Data::Dumper; use HTTP::Tiny; 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 = 'switch'; # max 8 chars $class_id = 'control'; # max 8 chars $separator = '-' x 80; $indent = ' ' x 2; #------------------------------------------------------------------------------- # global variables # my %configuration; $configuration{'switch'} = 'thanatos.cofnet'; $configuration{'user'} = 'control'; ################################################################################ # Input arguments # use Getopt::Std; my %opts; getopts('hvp:n:t:w:s:u:', \%opts); die("\n". "Usage: $0 [options]\n". "\n". "Options:\n". "${indent}-h display this help message\n". "${indent}-v verbose\n". "${indent}-p port the base UDP port\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}-s switch the IP address of the MSN switch\n". "${indent}-u name a username in the hue switch whitelist\n". "\n". "Controls a MSNswitch.\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{'switch'} = $opts{'b'} || $configuration{'switch'}; $configuration{'user'} = $opts{'u'} || $configuration{'user'}; ################################################################################ # Internal functions # #------------------------------------------------------------------------------- # Send command to CGI # sub cgi_send { my ($URL, $method, $parameters) = @_; # send message if ($parameters) { # $URL = "$URL?$parameters"; $URL = "$URL -d \"$parameters\""; } #print "$URL\n"; # my $response = HTTP::Tiny->new->request($method, "$URL"); my $response = `curl $URL 2>/dev/null`; #print Dumper($response); # get answer # $response = $response->{'content'}; return($response) } #------------------------------------------------------------------------------- # Translate an xPL message to the corresponding MSNswitch CGI arguments # sub build_msnSwitch_request_arguments { my (%body) = @_; my $message = ''; # interpret arguments my $outlet_id = lc($body{outlet}); my $value = lc($body{command}); if ( ($outlet_id ne '') and ($value ne '') ) { $value =~ s/off/0/; $value =~ s/on/1/; $message .= "outlet=$outlet_id"; $message .= "&command=$value"; } return($message) } #------------------------------------------------------------------------------- # Translate a MSNswitch XML message to the corresponding status # sub build_status { my ($xml) = @_; my %status; # get outlets status my $outlets_status = $xml; #print "$outlets_status\n"; $outlets_status =~ s/.*//; $outlets_status =~ s/<\/outlet_status.*>//; $outlets_status =~ s/0/off/g; $outlets_status =~ s/1/on/g; my @outlets_status = split(/,/, $outlets_status); # write to hash $status{'outlet1'} = $outlets_status[0]; $status{'outlet2'} = $outlets_status[1]; return(%status) } ################################################################################ # 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 MSNswitch at \"$configuration{'switch'}\".\n"); print($indent . "class id : $class_id\n"); print($indent . "instance id: $instance_id\n"); print($indent . "user : $configuration{'user'}\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); if ( xpl_is_for_me($xpl_id, $target) ) { # process commands if ($schema eq "$class_id.basic") { if ($type eq 'xpl-cmnd') { if ($verbose > 0) { print("\n"); print("Received command from \"$source\"\n"); } my $command = build_msnSwitch_request_arguments(%body); #print "-> $command\n"; if ($command ne '') { my $URL = "http://$configuration{'user'}\@$configuration{'switch'}"; $URL .= '/control.cgi'; if ($verbose == 1) { print $indent, "sending: \"$command\" to \"$URL\"\n"; } my $response = cgi_send($URL, 'GET', $command); #print "-> $response\n"; } } } # process replies if ($schema eq "$class_id.request") { if ($type eq 'xpl-cmnd') { if ($verbose == 1) { print "\n"; print("Received request from \"$source\"\n"); } my $URL = "http://$configuration{'user'}\@$configuration{'switch'}"; $URL .= "/outlet_status.xml"; my $status = cgi_send($URL, 'GET'); #print "-> $status\n"; my %status = build_status($status); if ($verbose == 1) { print $indent, "outlet1: $status{'outlet1'}, outlet2: $status{'outlet2'}\n"; } xpl_send_message( $xpl_socket, $xpl_port, 'xpl-stat', $xpl_id, '*', "$class_id.status", %status ); } } } } } xpl_disconnect($xpl_socket, $xpl_id, $xpl_ip, $client_port); ################################################################################ # Documentation (access it with: perldoc ) # __END__ =head1 NAME xpl-msnSwitch.pl - Controls a MSNswitch =head1 SYNOPSIS xpl-msnSwitch.pl [options] =head1 DESCRIPTION This xPL client sends commands to a MSNswitch in order to power on or off each of its two outlets. =head1 OPTIONS =over 8 =item B<-h> Display a help message. =item B<-v> Be verbose. "${indent}-s switch the IP address of the MSN switch\n". "${indent}-u name a username in the hue switch whitelist\n". =item B<-s switch> Specify the switch's IP address. =item B<-u name> Provide one of the usernames. =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<-b str> Specify the hue bridge's URL. =item B<-u str> Specify a passwordless username of the switch's list. =back =head1 TEST Make sure you have an C running on the machine. Start the switch controller: C. Start C in another terminal window. Turn a switch on: C and C<$SCRIPTS_BASE_DIR/xpl-send.pl -v -d dspc-switch.loungeAmps -c control.basic outlet=1 command=on>. Ask for a switch's staus: C<$SCRIPTS_BASE_DIR/xpl-send.pl -v -d dspc-switch.loungeAmps -c control.request command=request>. =head1 AUTHOR Francois Corthay, DSPC =head1 VERSION 1.1, 2015 =cut