This commit is contained in:
2017-11-01 01:54:19 +01:00
parent 01310ad23d
commit d551688285

View File

@@ -1,481 +1,481 @@
#!/usr/bin/perl #!/usr/bin/perl
use warnings; use warnings;
use strict; use strict;
use DBI; use DBI;
use Getopt::Long; use Getopt::Long;
use Net::FTP; use Net::FTP;
use List::Util qw( max ); use List::Util qw( max );
use Data::Dumper qw(Dumper); use Data::Dumper qw(Dumper);
$|++; # Autoflush $|++; # Autoflush
my ( $filename, $sqlout ); my ( $filename, $sqlout );
my $username = 'ase_lar'; my $username = 'ase_lar';
my $password = 'laravel'; my $password = 'laravel';
my $db_lar = 'ase_lar'; my $db_lar = 'ase_lar';
my $db_sp = 'ase_lar'; my $db_sp = 'ase_lar';
my $server = 'localhost'; my $server = 'localhost';
my $db_name = "ase_mums"; my $db_name = "ase_mums";
my $table = "RAWDATA"; my $table = "RAWDATA";
my ( @CSVData, @sql ); my ( @CSVData, @sql );
my $outcount = 0; my $outcount = 0;
my $outfilenum = 0; my $outfilenum = 0;
my $maxsqllines = 10000; my $maxsqllines = 10000;
my ( my (
$tool, $toolbatt, $tooltemp, $tooltype, @channels, $tool, $toolbatt, $tooltemp, $tooltype, @channels,
@NewData, @din, @nodetype, $GDEventDate @NewData, @din, @nodetype, $GDEventDate
); );
my $matlab_ip = "160.78.21.71"; my $matlab_ip = "160.78.21.71";
my $matlab_cmd = "matlab.cmd"; my $matlab_cmd = "matlab.cmd";
my $matlab_timestamp = ""; my $matlab_timestamp = "";
my $ftp_send = 1; my $ftp_send = 1;
my $ftp_addrs; my $ftp_addrs;
my $ftp_user; my $ftp_user;
my $ftp_passwd; my $ftp_passwd;
my $ftp_parm; my $ftp_parm;
my $ftp_filename; my $ftp_filename;
my $ftp_target; my $ftp_target;
my $unit_id; my $unit_id;
sub getTimeStamp sub getTimeStamp
{ # parm [ts] => timestamp for filename; log => timestamp for log { # parm [ts] => timestamp for filename; log => timestamp for log
my $format = "%04d%02d%02d%02d%02d%02d"; my $format = "%04d%02d%02d%02d%02d%02d";
my ($p1) = @_; my ($p1) = @_;
if ( defined $p1 and $p1 eq "log" ) { if ( defined $p1 and $p1 eq "log" ) {
$format = "%04d%02d%02d %02d:%02d:%02d"; $format = "%04d%02d%02d %02d:%02d:%02d";
} }
if ( defined $p1 and $p1 eq "db_ts" ) { if ( defined $p1 and $p1 eq "db_ts" ) {
$format = "%04d-%02d-%02d %02d:%02d:%02d"; $format = "%04d-%02d-%02d %02d:%02d:%02d";
} }
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
localtime(time); localtime(time);
my $timestamp = my $timestamp =
sprintf( $format, $year + 1900, $mon + 1, $mday, $hour, $min, $sec ); sprintf( $format, $year + 1900, $mon + 1, $mday, $hour, $min, $sec );
return $timestamp; return $timestamp;
} }
sub writeOutSql { sub writeOutSql {
my ($tn) = @_; my ($tn) = @_;
my $outfile = $sqlout . "_" . $outfilenum++; my $outfile = $sqlout . "_" . $outfilenum++;
open SQLOUT, ">", $outfile open SQLOUT, ">", $outfile
or die( getTimeStamp("log") or die( getTimeStamp("log")
. " - pid $$ >> Error: opening output file " . " - pid $$ >> Error: opening output file "
. $outfile . $outfile
. ":$!\n" ); . ":$!\n" );
print getTimeStamp("log") . " - pid $$ >> file $outfile created\n"; print getTimeStamp("log") . " - pid $$ >> file $outfile created\n";
print SQLOUT "/* lock " print SQLOUT "/* lock "
. $db_name . $db_name
. ".$table table */\nLOCK TABLES " . ".$table table */\nLOCK TABLES "
. $db_name . $db_name
. ".$table WRITE;\n"; . ".$table WRITE;\n";
print SQLOUT "INSERT IGNORE INTO " . $db_name . ".$table\n"; print SQLOUT "INSERT IGNORE INTO " . $db_name . ".$table\n";
print SQLOUT print SQLOUT
"(`UnitName`,`ToolNameID`,`NodeNum`,`EventDate`,`EventTime`,`BatLevel`,`Temperature`,\n"; "(`UnitName`,`ToolNameID`,`NodeNum`,`EventDate`,`EventTime`,`BatLevel`,`Temperature`,\n";
print SQLOUT print SQLOUT
"`Val0`,`Val1`,`Val2`,`Val3`,`Val4`,`Val5`,`Val6`,`Val7`,`Val8`,`Val9`,`ValA`,`ValB`,`ValC`,`ValD`,`ValE`,`ValF`)\n"; "`Val0`,`Val1`,`Val2`,`Val3`,`Val4`,`Val5`,`Val6`,`Val7`,`Val8`,`Val9`,`ValA`,`ValB`,`ValC`,`ValD`,`ValE`,`ValF`)\n";
print SQLOUT "VALUES\n"; print SQLOUT "VALUES\n";
print SQLOUT @sql; print SQLOUT @sql;
@sql = (); @sql = ();
print SQLOUT "/* unlock table */\nUNLOCK TABLES;\n"; print SQLOUT "/* unlock table */\nUNLOCK TABLES;\n";
if ( $table eq 'RAWDATA' ) { if ( $table eq 'RAWDATA' ) {
print SQLOUT "/* call stored procedure to copy in " print SQLOUT "/* call stored procedure to copy in "
. $db_sp . $db_sp
. "*/\ncall " . "*/\ncall "
. $db_sp . $db_sp
. ".CopyAndFill('$tn');\n"; . ".CopyAndFill('$tn');\n";
} }
close SQLOUT close SQLOUT
or die( getTimeStamp("log") or die( getTimeStamp("log")
. " - pid $$ >> Error in closing file " . " - pid $$ >> Error in closing file "
. $outfile . $outfile
. "\n" ); . "\n" );
my @args = ( "mysql", "--login-path=asepath", "-e source " . $outfile ); my @args = ( "mysql", "--login-path=asepath", "-e source " . $outfile );
system(@args) == 0 system(@args) == 0
or die( getTimeStamp("log") . " - pid $$ >> system @args failed: $?\n" ); or die( getTimeStamp("log") . " - pid $$ >> system @args failed: $?\n" );
print getTimeStamp("log") . " - pid $$ >> file $outfile loaded into DB\n"; print getTimeStamp("log") . " - pid $$ >> file $outfile loaded into DB\n";
unlink $outfile; unlink $outfile;
return; return;
} }
sub matlabCalc { sub matlabCalc {
getMatlabCmd(); getMatlabCmd();
$matlab_timestamp = getTimeStamp("db_ts"); $matlab_timestamp = getTimeStamp("db_ts");
print getTimeStamp("log") . " - pid $$ >> $tool MatLab calc started...\n"; print getTimeStamp("log") . " - pid $$ >> $tool MatLab calc started...\n";
my @out_matlab = `ssh1 $matlab_ip 'cmd /c $matlab_cmd ' $tool`; my @out_matlab = `ssh1 $matlab_ip 'cmd /c $matlab_cmd ' $tool`;
print getTimeStamp("log") print getTimeStamp("log")
. " - pid $$ >> $tool MatLab calc executed: $out_matlab[-1]"; . " - pid $$ >> $tool MatLab calc executed: $out_matlab[-1]";
#print getTimeStamp("log") . " - @out_matlab"; #print getTimeStamp("log") . " - @out_matlab";
#print getTimeStamp("log") . " - pid $$ >> $out_matlab[-1]"; #print getTimeStamp("log") . " - pid $$ >> $out_matlab[-1]";
return; return;
} }
sub trxelab { sub trxelab {
my $dbh = my $dbh =
DBI->connect( "DBI:mysql:$db_lar;host=$server", $username, $password ) DBI->connect( "DBI:mysql:$db_lar;host=$server", $username, $password )
or die getTimeStamp("log") or die getTimeStamp("log")
. " - pid $$ >> Could not connect to database: $DBI::errstr"; . " - pid $$ >> Could not connect to database: $DBI::errstr";
my $sth = $dbh->prepare( my $sth = $dbh->prepare(
'select ftp_addrs, ftp_user, ftp_passwd, ftp_parm, ftp_filename, ftp_target from units 'select ftp_addrs, ftp_user, ftp_passwd, ftp_parm, ftp_filename, ftp_target from units
where id = ' . $unit_id . ';' where id = ' . $unit_id . ';'
) or die getTimeStamp("log") . " - pid $$ >> $DBI::errstr"; ) or die getTimeStamp("log") . " - pid $$ >> $DBI::errstr";
$sth->execute(); $sth->execute();
if ( $sth->rows < 0 ) { if ( $sth->rows < 0 ) {
die getTimeStamp("log") . " - pid $$ >> No unit ftp params selected.\n"; die getTimeStamp("log") . " - pid $$ >> No unit ftp params selected.\n";
} }
else { else {
while ( my $results = $sth->fetchrow_hashref ) { while ( my $results = $sth->fetchrow_hashref ) {
$ftp_addrs = $results->{'ftp_addrs'}; $ftp_addrs = $results->{'ftp_addrs'};
$ftp_user = $results->{'ftp_user'}; $ftp_user = $results->{'ftp_user'};
$ftp_passwd = $results->{'ftp_passwd'}; $ftp_passwd = $results->{'ftp_passwd'};
$ftp_parm = $results->{'ftp_parm'}; $ftp_parm = $results->{'ftp_parm'};
$ftp_filename = $results->{'ftp_filename'}; $ftp_filename = $results->{'ftp_filename'};
$ftp_target = $results->{'ftp_target'}; $ftp_target = $results->{'ftp_target'};
} }
} }
$sth->finish; $sth->finish;
# Disconnect # Disconnect
my $fileelab; my $fileelab;
if ( $ftp_filename eq "" ) { if ( $ftp_filename eq "" ) {
$fileelab = $fileelab =
'/var/lib/mysql-files/' '/var/lib/mysql-files/'
. $tool . '_' . $tool . '_'
. getTimeStamp() . getTimeStamp()
. '.csv'; #mettere quello che si prende da query . '.csv'; #mettere quello che si prende da query
} }
else { else {
$ftp_filename =~ s/(\$\w+)/$1/eeg; $ftp_filename =~ s/(\$\w+)/$1/eeg;
$ftp_filename =~ s/\s/_/g; $ftp_filename =~ s/\s/_/g;
$fileelab = $fileelab =
'/var/lib/mysql-files/' '/var/lib/mysql-files/'
. $ftp_filename . '_' . $ftp_filename . '_'
. getTimeStamp() . '.csv'; . getTimeStamp() . '.csv';
} }
my $sthdo = $dbh->do( my $sthdo = $dbh->do(
qq{ qq{
select * from ( select * from (
select 'ToolNameID', 'EventDate', 'EventTime', 'NodeNum', 'NodeType', 'NodeDepth', select 'ToolNameID', 'EventDate', 'EventTime', 'NodeNum', 'NodeType', 'NodeDepth',
'XShift', 'YShift', 'ZShift' , 'X', 'Y', 'Z', 'HShift', 'HShiftDir', 'HShift_local', 'XShift', 'YShift', 'ZShift' , 'X', 'Y', 'Z', 'HShift', 'HShiftDir', 'HShift_local',
'week_shift', 'week_shift_x', 'week_shift_y', 'week_shift_z', 'T_node', 'water_level', 'pressure', 'load_value' 'week_shift', 'week_shift_x', 'week_shift_y', 'week_shift_z', 'T_node', 'water_level', 'pressure', 'load_value'
union all union all
select ToolNameID, EventDate, EventTime, NodeNum, NodeType, NodeDepth, select ToolNameID, EventDate, EventTime, NodeNum, NodeType, NodeDepth,
XShift, YShift, ZShift , X, Y, Z, HShift, HShiftDir, HShift_local, XShift, YShift, ZShift , X, Y, Z, HShift, HShiftDir, HShift_local,
week_shift, week_shift_x, week_shift_y, week_shift_z, T_node, water_level, pressure, load_value week_shift, week_shift_x, week_shift_y, week_shift_z, T_node, water_level, pressure, load_value
from ElabDataView from ElabDataView
where ToolNameID = '$tool' and created_at > '$matlab_timestamp' where ToolNameID = '$tool' and created_at > '$matlab_timestamp'
order by ToolNameID DESC, EventDate, EventTime, convert(`NodeNum`, decimal) DESC order by ToolNameID DESC, EventDate, EventTime, convert(`NodeNum`, decimal) DESC
) resulting_set ) resulting_set
into outfile '$fileelab' into outfile '$fileelab'
fields terminated by ',' optionally enclosed by '"' lines terminated by '\n'} fields terminated by ',' optionally enclosed by '"' lines terminated by '\n'}
) or die getTimeStamp("log") . " - pid $$ >> $DBI::errstr"; ) or die getTimeStamp("log") . " - pid $$ >> $DBI::errstr";
$dbh->disconnect; $dbh->disconnect;
$ftp_parm //= ""; $ftp_parm //= "";
$ftp_parm =~ s/\s//g; $ftp_parm =~ s/\s//g;
my %ftp_parms = ( my %ftp_parms = (
'Host' => $ftp_addrs, 'Host' => $ftp_addrs,
); );
foreach my $parm (split(/,/, $ftp_parm)) foreach my $parm (split(/,/, $ftp_parm))
{ {
my ($key, $val) = split(/=>/, $parm); my ($key, $val) = split(/=>/, $parm);
$ftp_parms{$key} = $val; $ftp_parms{$key} = $val;
} }
print getTimeStamp("log") . " - pid $$ >> $tool ftp parm:\n" . Dumper(%ftp_parms); print getTimeStamp("log") . " - pid $$ >> $tool ftp parm:\n" . Dumper(%ftp_parms);
my $ftp = Net::FTP->new( %ftp_parms ) my $ftp = Net::FTP->new( %ftp_parms )
or die getTimeStamp("log") or die getTimeStamp("log")
. " - pid $$ >> Cannot connect to $ftp_addrs: $@ \n"; . " - pid $$ >> Cannot connect to $ftp_addrs: $@ \n";
$ftp->login( $ftp_user, $ftp_passwd ) $ftp->login( $ftp_user, $ftp_passwd )
or die getTimeStamp("log") or die getTimeStamp("log")
. " - pid $$ >> Cannot login " . " - pid $$ >> Cannot login "
. $ftp->message . "\n"; . $ftp->message . "\n";
$ftp_target //= ""; $ftp_target //= "";
if ( $ftp_target ne "" ) { if ( $ftp_target ne "" ) {
$ftp->cwd($ftp_target) $ftp->cwd($ftp_target)
or die getTimeStamp("log") or die getTimeStamp("log")
. " - pid $$ >> Change remote dir failed " . " - pid $$ >> Change remote dir failed "
. $ftp->message . "\n"; . $ftp->message . "\n";
} }
print getTimeStamp("log") print getTimeStamp("log")
. " - pid $$ >> ftp target: " . " - pid $$ >> ftp target: "
. $ftp->pwd() . ".\n"; . $ftp->pwd() . ".\n";
$ftp->put($fileelab) $ftp->put($fileelab)
or die getTimeStamp("log") or die getTimeStamp("log")
. " - pid $$ >> Put failed " . " - pid $$ >> Put failed "
. $ftp->message . "\n"; . $ftp->message . "\n";
print getTimeStamp("log") print getTimeStamp("log")
. " - pid $$ >> ftp target files list:\n" . " - pid $$ >> ftp target files list:\n"
. Dumper($ftp->ls()); . Dumper($ftp->ls());
$ftp->quit; $ftp->quit;
print getTimeStamp("log") print getTimeStamp("log")
. " - pid $$ >> $tool: $fileelab ftp put executed.\n"; . " - pid $$ >> $tool: $fileelab ftp put executed.\n";
return; return;
} }
sub getNodesType { sub getNodesType {
my $dbh = my $dbh =
DBI->connect( "DBI:mysql:$db_lar;host=$server", $username, $password ) DBI->connect( "DBI:mysql:$db_lar;host=$server", $username, $password )
or die getTimeStamp("log") or die getTimeStamp("log")
. " - pid $$ >> Could not connect to database: $DBI::errstr"; . " - pid $$ >> Could not connect to database: $DBI::errstr";
my $sth = $dbh->prepare( my $sth = $dbh->prepare(
'select t.name as name, n.seq as seq, n.num as num, n.channels as channels, y.type as type, n.din as din 'select t.name as name, n.seq as seq, n.num as num, n.channels as channels, y.type as type, n.din as din
from nodes as n from nodes as n
inner join tools as t on t.id = n.tool_id inner join tools as t on t.id = n.tool_id
inner join nodetypes as y on n.nodetype_id = y.id inner join nodetypes as y on n.nodetype_id = y.id
where t.name = "' . $tool . '" order by 3;' where t.name = "' . $tool . '" order by 3;'
) or die getTimeStamp("log") . " - pid $$ >> $DBI::errstr"; ) or die getTimeStamp("log") . " - pid $$ >> $DBI::errstr";
$sth->execute(); $sth->execute();
if ( $sth->rows < 0 ) { if ( $sth->rows < 0 ) {
die getTimeStamp("log") . " - pid $$ >> No node channels selected.\n"; die getTimeStamp("log") . " - pid $$ >> No node channels selected.\n";
} }
else { else {
my $row = 0; my $row = 0;
while ( my $results = $sth->fetchrow_hashref ) { while ( my $results = $sth->fetchrow_hashref ) {
$channels[$row] = $results->{'channels'}; $channels[$row] = $results->{'channels'};
$nodetype[$row] = $results->{'type'}; $nodetype[$row] = $results->{'type'};
$din[ $row++ ] = $results->{'din'}; $din[ $row++ ] = $results->{'din'};
} }
} }
$sth->finish; $sth->finish;
# Disconnect # Disconnect
$dbh->disconnect; $dbh->disconnect;
return; return;
} }
sub getUdbPwd { sub getUdbPwd {
my $dbh = my $dbh =
DBI->connect( "DBI:mysql:$db_lar;host=$server", $username, $password ) DBI->connect( "DBI:mysql:$db_lar;host=$server", $username, $password )
or die getTimeStamp("log") or die getTimeStamp("log")
. " - pid $$ >> Could not connect to database: $DBI::errstr"; . " - pid $$ >> Could not connect to database: $DBI::errstr";
my $sth = $dbh->prepare( my $sth = $dbh->prepare(
"SELECT db_user, db_pwd FROM ase_lar.companies where db_name = '" "SELECT db_user, db_pwd FROM ase_lar.companies where db_name = '"
. $db_name . $db_name
. "';" ) . "';" )
or die getTimeStamp("log") . " - pid $$ >> $DBI::errstr"; or die getTimeStamp("log") . " - pid $$ >> $DBI::errstr";
$sth->execute(); $sth->execute();
if ( $sth->rows < 0 ) { if ( $sth->rows < 0 ) {
die getTimeStamp("log") . " - pid $$ >> No password selected.\n"; die getTimeStamp("log") . " - pid $$ >> No password selected.\n";
} }
else { else {
my $results = $sth->fetchrow_hashref; my $results = $sth->fetchrow_hashref;
$password = $results->{'db_pwd'}; $password = $results->{'db_pwd'};
$username = $results->{'db_user'}; $username = $results->{'db_user'};
} }
$sth->finish; $sth->finish;
# Disconnect # Disconnect
$dbh->disconnect; $dbh->disconnect;
return; return;
} }
sub getMatlabCmd { sub getMatlabCmd {
my $dbh = my $dbh =
DBI->connect( "DBI:mysql:$db_lar;host=$server", $username, $password ) DBI->connect( "DBI:mysql:$db_lar;host=$server", $username, $password )
or die getTimeStamp("log") or die getTimeStamp("log")
. " - pid $$ >> Could not connect to database: $DBI::errstr"; . " - pid $$ >> Could not connect to database: $DBI::errstr";
my $sth = $dbh->prepare( my $sth = $dbh->prepare(
'select m.matcall, t.ftp_send , t.unit_id from matfuncs as m 'select m.matcall, t.ftp_send , t.unit_id from matfuncs as m
inner join tools as t on t.matfunc = m.id inner join tools as t on t.matfunc = m.id
where t.name = "' . $tool . '" ;' where t.name = "' . $tool . '" ;'
) or die getTimeStamp("log") . " - pid $$ >> $DBI::errstr"; ) or die getTimeStamp("log") . " - pid $$ >> $DBI::errstr";
$sth->execute(); $sth->execute();
if ( $sth->rows < 0 ) { if ( $sth->rows < 0 ) {
die getTimeStamp("log") die getTimeStamp("log")
. " - pid $$ >> No tool's matlab function selected.\n"; . " - pid $$ >> No tool's matlab function selected.\n";
} }
else { else {
my $mfunc; my $mfunc;
while ( my $results = $sth->fetchrow_hashref ) { while ( my $results = $sth->fetchrow_hashref ) {
$mfunc = uc $results->{'matcall'}; $mfunc = uc $results->{'matcall'};
$ftp_send = $results->{'ftp_send'}; $ftp_send = $results->{'ftp_send'};
$unit_id = $results->{'unit_id'}; $unit_id = $results->{'unit_id'};
} }
$matlab_cmd = 'matlab_' . $mfunc . '.cmd'; $matlab_cmd = 'matlab_' . $mfunc . '.cmd';
} }
$sth->finish; $sth->finish;
# Disconnect # Disconnect
$dbh->disconnect; $dbh->disconnect;
return; return;
} }
my $starttime = getTimeStamp("log"); my $starttime = getTimeStamp("log");
print "$starttime - pid $$ >> Start execution.\n"; print "$starttime - pid $$ >> Start execution.\n";
GetOptions( GetOptions(
"filename=s" => \$filename, "filename=s" => \$filename,
"sqlout=s" => \$sqlout, "sqlout=s" => \$sqlout,
"dbname=s" => \$db_name "dbname=s" => \$db_name
) or die("Error in command line arguments\n"); ) or die("Error in command line arguments\n");
open FILE, $filename open FILE, $filename
or die( "Error: opening input file " . $filename . "\n" ); or die( "Error: opening input file " . $filename . "\n" );
if ( $db_name ne 'asega' ) { if ( $db_name ne 'asega' ) {
$db_name =~ s/aseu/dbn/; $db_name =~ s/aseu/dbn/;
$db_sp = $db_name; $db_sp = $db_name;
getUdbPwd(); getUdbPwd();
$db_lar = $db_name; $db_lar = $db_name;
$table = "RAWDATACOR"; $table = "RAWDATACOR";
} }
else { else {
$db_name = 'ase_mums'; $db_name = 'ase_mums';
} }
my ( $fileDate, $fileTime ) = split( /\s/, <FILE> ); my ( $fileDate, $fileTime ) = split( /\s/, <FILE> );
my ( $unittype, $unit ) = split( /\s/, uc <FILE> ); my ( $unittype, $unit ) = split( /\s/, uc <FILE> );
$unit =~ s/;+$//; $unit =~ s/;+$//;
while ( my $line = <FILE> ) { while ( my $line = <FILE> ) {
my ( $first, $NodeData ) = split( /;/, $line, 2 ); my ( $first, $NodeData ) = split( /;/, $line, 2 );
if ( defined $first and ( $first ne '' ) ) { if ( defined $first and ( $first ne '' ) ) {
$first =~ s/;+$//; $first =~ s/;+$//;
if ( !defined $NodeData or ( $NodeData =~ m/^;+/ ) ) { if ( !defined $NodeData or ( $NodeData =~ m/^;+/ ) ) {
my @info = ( split( /[\/,\.]/, $first ) ); my @info = ( split( /[\/,\.]/, $first ) );
if ( defined $info[3] && $info[3] =~ m/^DT\d\d\d\d$/i ) { if ( defined $info[3] && $info[3] =~ m/^DT\d\d\d\d$/i ) {
$tool = uc $info[3]; $tool = uc $info[3];
$tooltype = uc $info[2]; $tooltype = uc $info[2];
print getTimeStamp("log") print getTimeStamp("log")
. " - pid $$ >> Found $tooltype tool name: $tool\n"; . " - pid $$ >> Found $tooltype tool name: $tool\n";
getNodesType(); getNodesType();
} }
elsif ( defined $info[2] && $info[2] =~ m/^LOC\d*$/i ) { elsif ( defined $info[2] && $info[2] =~ m/^LOC\d*$/i ) {
$tool = uc $info[2]; $tool = uc $info[2];
$tooltype = 'LOC'; $tooltype = 'LOC';
print getTimeStamp("log") print getTimeStamp("log")
. " - pid $$ >> Found $tooltype tool name: $tool\n"; . " - pid $$ >> Found $tooltype tool name: $tool\n";
getNodesType(); getNodesType();
} }
elsif ( defined $info[3] && $info[3] =~ m/^GD\d*$/i ) { elsif ( defined $info[3] && $info[3] =~ m/^GD\d*$/i ) {
$tool = uc $info[3]; $tool = uc $info[3];
$tooltype = 'GD'; $tooltype = 'GD';
print getTimeStamp("log") print getTimeStamp("log")
. " - pid $$ >> Found $tooltype tool name: $tool\n"; . " - pid $$ >> Found $tooltype tool name: $tool\n";
} }
} }
else { else {
$NodeData =~ s/\x0d{0,1}\x0a\Z//s; $NodeData =~ s/\x0d{0,1}\x0a\Z//s;
$NodeData =~ s/;+$//; $NodeData =~ s/;+$//;
#$NodeData =~ s/\r\n\z//; #$NodeData =~ s/\r\n\z//;
if ( $tooltype eq "MUX" ) { if ( $tooltype eq "MUX" ) {
#$NodeData =~ s/Dis\./N\/A/g; #$NodeData =~ s/Dis\./N\/A/g;
@NewData = grep /\S/, split( /\||;/, $NodeData ); @NewData = grep /\S/, split( /\||;/, $NodeData );
my $idx = 0; my $idx = 0;
$CSVData[ $idx++ ] = join( ';', splice( @NewData, 0, 2 ) ); $CSVData[ $idx++ ] = join( ';', splice( @NewData, 0, 2 ) );
foreach my $ch (@channels) { foreach my $ch (@channels) {
$CSVData[ $idx++ ] = $CSVData[ $idx++ ] =
join( ';', splice( @NewData, 0, $ch ) ); join( ';', splice( @NewData, 0, $ch ) );
} }
#print Dumper \@NewData; #print Dumper \@NewData;
#print Dumper \@din; #print Dumper \@din;
} }
elsif ( $tooltype eq "LOC" ) { elsif ( $tooltype eq "LOC" ) {
if ( !defined $din[0] ) { if ( !defined $din[0] ) {
print getTimeStamp("log") print getTimeStamp("log")
. " - pid $$ >> Din not defined.\n"; . " - pid $$ >> Din not defined.\n";
print getTimeStamp("log") print getTimeStamp("log")
. " - pid $$ >> Execution ended.\n"; . " - pid $$ >> Execution ended.\n";
exit; exit;
} }
my ( $Vbat, $Tmod, $Ain1, $Ain2, $Din1, $Din2 ) = my ( $Vbat, $Tmod, $Ain1, $Ain2, $Din1, $Din2 ) =
split( /;/, $NodeData ); split( /;/, $NodeData );
$CSVData[0] = $Vbat . ";" . $Tmod; $CSVData[0] = $Vbat . ";" . $Tmod;
$CSVData[1] = $Din1; $CSVData[1] = $Din1;
} }
elsif ( $tooltype eq "GD" ) { elsif ( $tooltype eq "GD" ) {
my ( $div, $Vbat, $Tmod ) = my ( $div, $Vbat, $Tmod ) =
split( /;/, $NodeData ); split( /;/, $NodeData );
$CSVData[0] = $Vbat . ";" . $Tmod; $CSVData[0] = $Vbat . ";" . $Tmod;
my ( $y, $m, $d, $t ) = split( /[\s\/]/, $first ); my ( $y, $m, $d, $t ) = split( /[\s\/]/, $first );
my ( $h, $min, $sec ) = split( /:/, $t ); my ( $h, $min, $sec ) = split( /:/, $t );
$GDEventDate = $y . '-' . $m . '-' . $d; $GDEventDate = $y . '-' . $m . '-' . $d;
$CSVData[1] = "DT" $CSVData[1] = "DT"
. substr( $tool, 2, 4 ) . ';' . substr( $tool, 2, 4 ) . ';'
. $h . ':' . $h . ':'
. ( $min - 1 ) . ';' . ( $min - 1 ) . ';'
. $h . ':' . $h . ':'
. ( $min + 1 ); . ( $min + 1 );
} }
else { else {
@CSVData = split( /\|/, $NodeData ); @CSVData = split( /\|/, $NodeData );
} }
my $nodenum = 0; my $nodenum = 0;
foreach (@CSVData) { foreach (@CSVData) {
$sql[ ( $outcount % $maxsqllines ) ] = ""; $sql[ ( $outcount % $maxsqllines ) ] = "";
my (@data) = grep { /\S/ } split( /\;/, $_ ); my (@data) = grep { /\S/ } split( /\;/, $_ );
if ( $nodenum eq 0 ) { if ( $nodenum eq 0 ) {
$toolbatt = $data[0]; $toolbatt = $data[0];
$tooltemp = $data[1]; $tooltemp = $data[1];
} }
else { else {
my ( $y, $m, $d, $t ) = split( /[\s\/]/, $first ); my ( $y, $m, $d, $t ) = split( /[\s\/]/, $first );
foreach (@data) { foreach (@data) {
if ( $sql[ ( $outcount % $maxsqllines ) ] eq "" ) { if ( $sql[ ( $outcount % $maxsqllines ) ] eq "" ) {
$sql[ ( $outcount % $maxsqllines ) ] = $sql[ ( $outcount % $maxsqllines ) ] =
"('$unit','$tool','$nodenum','$y-$m-$d','$t','$toolbatt','$tooltemp'"; "('$unit','$tool','$nodenum','$y-$m-$d','$t','$toolbatt','$tooltemp'";
} }
$sql[ ( $outcount % $maxsqllines ) ] .= ",'" . $_ . "'"; $sql[ ( $outcount % $maxsqllines ) ] .= ",'" . $_ . "'";
} }
my $InsCompl = ",NULL" x ( 15 - $#data ); my $InsCompl = ",NULL" x ( 15 - $#data );
$sql[ ( $outcount % $maxsqllines ) ] .= $InsCompl . ")"; $sql[ ( $outcount % $maxsqllines ) ] .= $InsCompl . ")";
if ( ++$outcount % $maxsqllines eq 0 ) { if ( ++$outcount % $maxsqllines eq 0 ) {
$sql[ ( $outcount % $maxsqllines ) - 1 ] .= ";\n"; $sql[ ( $outcount % $maxsqllines ) - 1 ] .= ";\n";
writeOutSql($tool); writeOutSql($tool);
} }
else { else {
$sql[ ( $outcount % $maxsqllines ) - 1 ] .= ",\n"; $sql[ ( $outcount % $maxsqllines ) - 1 ] .= ",\n";
} }
} }
$nodenum++; $nodenum++;
} }
} }
} }
} }
close FILE; close FILE;
$sql[ ( $outcount % $maxsqllines ) - 1 ] =~ s/,$/;/g; $sql[ ( $outcount % $maxsqllines ) - 1 ] =~ s/,$/;/g;
writeOutSql($tool); writeOutSql($tool);
if ( $tooltype eq "GD" ) { if ( $tooltype eq "GD" ) {
print getTimeStamp("log") . " - pid $$ >> tool GD: nothing to do.\n"; print getTimeStamp("log") . " - pid $$ >> tool GD: nothing to do.\n";
} }
else { else {
matlabCalc(); matlabCalc();
if ($ftp_send) { if ($ftp_send) {
if ( $tool eq 'DT0076' ) { if ( $tool eq 'DT0076' ) {
sleep(600); sleep(600);
} }
trxelab(); trxelab();
} }
} }
print getTimeStamp("log") . " - pid $$ >> Execution ended.\n"; print getTimeStamp("log") . " - pid $$ >> Execution ended.\n";
exit; exit;