Kedves Lista!
Becopyzom levélbe a scriptemet. A központ típusához lehet, hogy át kell
írni. Perl-ben van írva, kell hozzá DBI MySQL csomag, és serialport kezelo
(libdevice-serialport-perl). Daemon módban fut, rendesen belinkeltem a
/etc/init-d-be, és az rcX.d-be symlinkeltem szabvány módon, így induláskor
automatikusan indul.
Sarge és Etch alatt teszteltem, x86 és PReP (IBM RS-6000 43P) architektúra
alatt is tökéletesen muködik.
#!/usr/bin/perl
# serial port logger program
#
use POSIX qw(setsid);
use Device::SerialPort 0.12;
use DBI;
my $dsn = 'DBI:mysql:telefon';
my $db_user_name = 'mysql_user';
my $db_password = 'mysql_pass';
chdir "/";
umask 0;
open STDIN, '/dev/null' or die "/dev/null hiba";
open STDOUT, '>/var/log/telkozpont_out.log' or die "out log hiba";
open STDERR, '>/var/log/telkozpont_err.log' or die "error log hiba";
defined(my $pid = fork) or die "Fork hiba";
exit if $pid;
setsid or die "Nem lehet elindítani a programot!";
$LOGDIR = "/var/log"; # path to data file
$LOGFILE = "telkozpont.log"; # file name to output to
$PORT = "/dev/ttyS1"; # port to watch
#
#
# Serial Settings
#
#
$ob = Device::SerialPort->new ($PORT) || die "Can't Open $PORT: $!";
$ob->baudrate(9600) || die "failed setting baudrate";
#$ob->parity("mark") || die "failed setting parity";
$ob->databits(8) || die "failed setting databits";
$ob->handshake("none") || die "failed setting handshake";
$ob->write_settings || die "no settings";
#
# Send a string to the port
#
#
# $pass=$ob->write("AT");
sleep 1;
#
# open the logfile, and Port
#
open(LOG,">>${LOGDIR}/${LOGFILE}")
||die "can't open smdr file $LOGDIR/$LOGFILE for append: $SUB $!\n";
open(DEV, "<$PORT")
|| die "Cannot open $PORT: $_";
select(LOG), $| = 1; # set nonbufferd mode
open(LOG2,">>/var/log/tel_mysql.log") || die "log2";
select(LOG2),$| = 1;
#
# Loop forver, logging data to the log file
#
my $mainstring;
my $day;
my $time;
my $ext;
my $co;
my $dnum;
my $duration;
my $duration1;
my $duration2;
my $acc;
my $hour;
my $minute;
my $year;
my $month;
my $ampm;
while($_ = <DEV>){ # print input device to file
print LOG $_;
$mainstring .= $_;
if ($_ =~ /\n/)
{
$day = substr $mainstring,3,2;
$month = substr $mainstring,0,2;
$year = substr $mainstring,6,2;
$hour = substr $mainstring,9,2;
$minute = substr $mainstring,12,2;
$duration1 = substr $mainstring,57,5;
$duration2 = substr $mainstring,63,2;
$duration = $duration1 . ":" . $duration2;
$dnum = substr $mainstring,26,20;
$co = substr $mainstring,23,2;
$ext = substr $mainstring,19,3;
$ampm = substr $mainstring,14,2;
$acc = substr $mainstring,66,4;
if ($ampm eq "PM")
{
if ($hour eq "00") {$hour = "12";}
if ($hour == "01") {$hour = "13";}
if ($hour == "02") {$hour = "14";}
if ($hour == "03") {$hour = "15";}
if ($hour == "04") {$hour = "16";}
if ($hour == "05") {$hour = "17";}
if ($hour == "06") {$hour = "18";}
if ($hour == "07") {$hour = "19";}
if ($hour == "08") {$hour = "20";}
if ($hour == "09") {$hour = "21";}
if ($hour == "10") {$hour = "22";}
if ($hour == "11") {$hour = "23";}
if ($hour == "12") {$hour = "12";}
}
my $hiv_timestamp = "20" . $year . "-" . $month . "-" . $day . " " .
$hour . ":" . $minute . ":00";
my $mysql_string = "INSERT INTO hivasok (hiv_id, hiv_timestamp,
hiv_ext, hiv_co, hiv_num, hiv_duration, hiv_acc) values
('','$hiv_timestamp','$ext','$co','$dnum','$duration','$acc');\n";
if (($ext eq 'Ext') || ($ext eq '---') || ($ext eq ''))
{
print LOG2 "---INIT MARK---\n";
}
else
{
my $dbh = DBI->connect($dsn, $db_user_name, $db_password) or die "MySQL
hiba: " . DBI->errstr;
print LOG2 $mysql_string;
my $sth = $dbh->prepare($mysql_string) or die "MySQL hiba: " .
$dbh->errstr;
$sth->execute();
$dbh->disconnect();
}
$mainstring = '';
}
$mainstring = '';
}
undef $ob;
_________________________________________________
linux lista - [email protected]
http://mlf2.linux.rulez.org/mailman/listinfo/linux