#!/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 = 'eib'; # max 8 chars $class_id = 'eib'; # max 8 chars $separator = '-' x 80; $indent = ' ' x 2; #------------------------------------------------------------------------------- # global variables # my %configuration; $configuration{'writeCommand'} = '/usr/bin/knxtool groupswrite'; $configuration{'server'} = 'ip:localhost'; ################################################################################ # Input arguments # use Getopt::Std; my %opts; getopts('hvp:n:s:t:w:s:c:', \%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 url the server URL\n". "${indent}-c cmd the eibd groupswrite command\n". "\n". "Interfaces a Tapko EIB/KNX to RS232 bridge for EIB control.\n". "\n". "More information with: perldoc $0\n". "\n". "" ) if ($opts{h}); $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{'server'} = $opts{'s'} || $configuration{'server'}; $configuration{'writeCommand'} = $opts{'c'} || $configuration{'writeCommand'}; ################################################################################ # Internal functions # #------------------------------------------------------------------------------- # Translate an xPL command body to the corresponding eibd write command # sub build_eibd_command { my ($configuration_ref, %body) = @_; my $message_OK = 1; # group address my $group_address = $body{'group'}; if ($group_address eq '') { $message_OK = 0; } # data my $data = $body{'data'}; if ($data eq '') { $message_OK = 0; } $data =~ s/0x//g; # build command string my $command = "$$configuration_ref{'writeCommand'} $$configuration_ref{'server'}"; $command .= " $group_address $data > /dev/null"; if ($message_OK == 0) { $command = ''; } #print "message: $command\n"; return($command) } ################################################################################ # 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 the eibd daemon \"$configuration{'server'}\".\n"; print($indent . "class id: $class_id\n"); print($indent . "xPL client id: $xpl_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); if ( xpl_is_for_me($xpl_id, $target) ) { # process commands if ($schema eq "$class_id.basic") { if ($type eq 'xpl-cmnd') { my $command = $body{'command'}; if ($verbose > 0) { print("Received command from \"$source\"\n"); } my $message = build_eibd_command(\%configuration, %body); if ($message) { if ($verbose == 1) { print($indent . "sending \"$message\"\n"); } system("$message"); } } } } } } xpl_disconnect($xpl_socket, $xpl_id, $xpl_ip, $client_port); ################################################################################ # Documentation (access it with: perldoc ) # __END__ =head1 NAME xpl-eibdWrite.pl - Interfaces an EIB/KNX IP router for EIB control =head1 SYNOPSIS xpl-eibdWrite.pl [options] =head1 DESCRIPTION This xPL client translates xPL messages in order to send EIB telegrams via C. The C command and trigger messages contain following items: =over 8 =item B Telegram source address, only for trigger messages. =item B Group address. =item B The data (or value) associated to the group address. =back =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<-s url> Specify the C URL. =item B<-c cmd> Specify the executable C. =back =head1 TEST Make sure you have an C and C running on the machine. Start the the xPL EIB writer: C C<$SCRIPTS_BASE_DIR/xpl-eib-eibdWrite.pl -v -n home>. Turn light C<1/1/1> on: C C<$SCRIPTS_BASE_DIR/xpl-send.pl -v -t cmnd -d dspc-eib.home -c eib.basic group='1/1/1' data=0x01>. Turn light C<1/1/1> of: C<$SCRIPTS_BASE_DIR/xpl-send.pl -v -t cmnd -d dspc-eib.home -c eib.basic group='1/1/1' data=0x00>. =head1 AUTHOR Francois Corthay, DSPC =head1 VERSION 1.0, 2014 =cut