#!/usr/bin/perl use Time::HiRes qw( usleep ); use DBI; open (CONFIG, "connect("DBI:mysql:$conf{DatabaseName}:$conf{DatabaseServer}",$conf{DatabaseUser},$conf{DatabaseSecret}); $X = 0; while( $X != 1 ) { $Query_lookque = "SELECT COUNT(*) FROM Input_Que"; my $lookque = $db_homenet->prepare( $Query_lookque ); $lookque->execute(); my ( $rows ); $lookque->bind_columns( undef, \$rows ); while( $lookque->fetch() ){} if ( $rows >= 1 ) { $data = ""; $Query_check_inputque = "SELECT * FROM `Input_Que` WHERE 1 LIMIT 1"; my $check_inputque = $db_homenet->prepare( $Query_check_inputque ); $check_inputque->execute(); my( $nr, $timestamp, $busname, $data ); $check_inputque->bind_columns( undef, \$nr, \$timestamp, \$busname, \$data ); while( $check_inputque->fetch() ){ } print "$nr - $timestamp - $busname - $data"; if ( $data =~ /.*(\w{2})r(\w{2})=(\w{2})/ ) # memory read data { $data_type = "alive_message"; $dev_id = $1; $memory_location = $2; $data = $3; print "MEMORY-DATA-READ van $dev_id op bus $busname: Mem_loc = $memory_location, data = $data\n"; } elsif ( $data =~ /.*(\w{2})M(\w{2})=(\w{2})/ ) # Meter reading { $data_type = "meter_reading"; $dev_id = $1; $dev_line = $2; $data = $3; print "METER-READING van $dev_id op bus $busname: Device_line = $dev_line, data = $data\n"; } elsif ( $data =~ /.*(\w{2})(\w{1})(\w{2})=(\w{2})/ ) # alive message { $data_type = "alive_message"; $dev_id = $1; $short_type = $2; $version_high = $3; $version_low = $4; print "ALIVE-MESSAGE van $dev_id op bus $busname: Type = $short_type\n"; $query_sendcommand = "UPDATE `state-table` SET `lastchange` = '$timestamp' WHERE `device` =('$dev_id') && `busname` =('$busname') LIMIT 1 ;"; $db_homenet->do( $query_sendcommand ); } elsif ( $data =~ /.*(\w{2})(\w{2})>(\w+)>/ ) # hard command { $data_type = "hard_command"; $sender_id = $1; $receiver_id = $2; $command = $3; print "HARD-COMMAND van $sender_id naar $receiver_id op bus $busname: Command = $command\n"; received_command($sender_id, $busname, $receiver_id, $busname, $command); } elsif ( $data =~ /.*(\w+)>(\w+)>(.+)>(\w+)>/ ) # soft command { $data_type = "soft_command"; $receiver_id = $3; $command = $4; $sender_id = "S3"; print "SOFT-COMMAND naar $receiver_id op bus $busname: Command = $command\n"; received_command($sender_id, $busname, $receiver_id, $busname, $command); } elsif ( $data =~ /.*(\w+)\?(.+)\?/ ) # memory read request { $data_type = "memory_read_request"; $receiver_id = $1; $memory_location = $2; print "MEMORY-READ-REQUEST naar $receiver_id op bus $busname: Memory Location = $memory_location\n"; } elsif ( $data =~ /.*(\w+)=(.{2})(.{2})=/ ) # memory write request { $data_type = "memory_write_request"; $receiver_id = $1; $memory_location = $2; $write_data = $3; print "MEMORY-WRITE naar $receiver_id op bus $busname: Memory Location = $memory_location, data = $write_data \n"; } else { print "data kan niet worden ontcijfert! \n"; } $Query_delete_from_que = "DELETE FROM `Input_Que` WHERE `nr` = $nr LIMIT 1"; $db_homenet->do( $Query_delete_from_que ); print " ******************************************************************************* \n"; } usleep 100000 } sub received_command { print "(1)\n"; ($command_source, $sbus, $command_destination, $dbus, $data) = @_; print "@_\n"; # First: We get the device type and software version! print "(2)\n"; $cod = "$command_destination"; # $Query_gettype = "SELECT `type` , `softv_high` , `softv_low` FROM `master_devices` WHERE naam = ( '$command_destination' ) && bus = ( '$dbus' ) LIMIT 1"; $Query_gettype = "SELECT `type` , `softv_high` , `softv_low` FROM `master_devices` WHERE naam = ( '$cod' ) LIMIT 1"; print "$Query_gettype\n"; my $gettype = $db_homenet->prepare( $Query_gettype ); print "(3)\n"; $gettype->execute(); print "(4)\n"; my( $type, $swh, $swl ); print "(5)\n"; $gettype->bind_columns( undef, \$type, \$swh, \$swl ); print "(6)\n"; while( $gettype->fetch() ){} print "$type\n"; print "$swh\n"; print "$swl\n"; print "(7)\n"; $devtype = "$type.$swh.$swl"; print "$type\n"; print "$swh\n"; print "$swl\n"; print "Device Type = $devtype\n"; # Then we look up the command in the command_matrix! $Query_getline = "SELECT `line`, `activate`, `deactivate`, `switch`, `timerswitch`, `value` FROM `command_matrix` WHERE device_type = ('$devtype') && ( activate = ('$data') || deactivate = ('$data') || switch = ('$data') || timerswitch = ('$data') || value = ('$data') ) LIMIT 1"; my $getline = $db_homenet->prepare( $Query_getline ); # print "$Query_getline\n"; $getline->execute(); my( $line, $activate, $deactivate, $switch, $timerswitch, $value ); $getline->bind_columns( undef, \$line, \$activate, \$deactivate, \$switch, \$timerswitch, \$value ); while( $getline->fetch() ){} # print "$line, $on, $off, $switch, $timerswitch, $value\n" ; # print "$command_destination\n"; $sdev = "$command_destination.$line"; print "slave-device = $sdev\n"; # Next we see what command it is! # print "-$data-\n"; # print "-$activate-\n"; # print "-$deactivate-\n"; if ( $activate =~ $data ) { print "activate\n"; } elsif ( $deactivate =~ $data ) { print "deactivate\n"; } elsif ( $switch =~ $data ) { print "switch\n"; } elsif ( $timerswitch =~ $data ) { print "timerswitch\n"; } elsif ( $value =~ $data ) { print "value\n"; } }