jeroen_h has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I've written this little script that's behaving strangely (to me, but I'm not an expierenced programmer ;-) )
In a sub routine the script gets data from a (MySQL) database. When I call the schript from one place everything works fine and when I call it from another place (in exactly the same way) there are no results comming back from the database.
When I do a print of the database Query and past that into phpmyadmin for testing, everything works fine, also with the query that does not return the data...
Can anyone point me at what I'm doing wrong here?
I don't know where the problem comes from so I'll post the whole script I have so far... (Sorry for the large part of code!)
The script works for the Hard Command but not for the Soft Command. Thanks a lot for any help you can give me on this!
#!/usr/bin/perl use Time::HiRes qw( usleep ); use DBI; open (CONFIG, "<homenet.actiontrigger.conf"); while (readline CONFIG) { if ( $_ !~ /^\s*#\s*/ ) { if ($_ =~ /\S+/ ) { if ( $_ =~ /^\s*(\S+)\s*=\s*(\S+)\s*#*(.*)$/ ) {}; $conf_key = $1; $conf_value = $2; $conf{$conf_key} = $conf_value; } } } close (CONFIG); $script_name = "action trigger"; $db_homenet = DBI->connect("DBI:mysql:$conf{DatabaseName}:$conf{Databa +seServer}",$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_inp +utque ); $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: D +evice_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: T +ype = $short_type\n"; $query_sendcommand = "UPDATE `state-table` SET `la +stchange` = '$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_i +d, $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 $busn +ame: Command = $command\n"; received_command($sender_id, $busname, $receiver_i +d, $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 bu +s $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 $busn +ame: 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') || sw +itch = ('$data') || timerswitch = ('$data') || value = ('$data') ) LI +MIT 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"; } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Database problem
by dws (Chancellor) on Aug 06, 2005 at 15:51 UTC | |
by jeroen_h (Initiate) on Aug 06, 2005 at 18:57 UTC | |
Re: Database problem
by trammell (Priest) on Aug 06, 2005 at 15:30 UTC | |
by jeroen_h (Initiate) on Aug 06, 2005 at 15:57 UTC | |
Re: Database problem
by rnahi (Curate) on Aug 06, 2005 at 16:09 UTC | |
by jeroen_h (Initiate) on Aug 06, 2005 at 19:11 UTC | |
Re: Database problem
by graff (Chancellor) on Aug 06, 2005 at 16:10 UTC | |
Re: Database problem
by CountZero (Bishop) on Aug 06, 2005 at 16:47 UTC | |
by jeroen_h (Initiate) on Aug 06, 2005 at 17:47 UTC | |
Re: Database problem
by jeroen_h (Initiate) on Aug 07, 2005 at 11:08 UTC |
Back to
Seekers of Perl Wisdom