From 38f60ce3cc2eb725fb9accb2fa853704ff843abd Mon Sep 17 00:00:00 2001 From: battilo Date: Mon, 19 Nov 2018 23:17:25 +0100 Subject: [PATCH] din e ain --- .vstags | 8 +- LoadCSVData.pl | 196 +++++++++++++++++++++++++------------------------ 2 files changed, 103 insertions(+), 101 deletions(-) diff --git a/.vstags b/.vstags index 5d5e864..0d82eb2 100644 --- a/.vstags +++ b/.vstags @@ -7,11 +7,11 @@ checkCompanyDBGrant SetupNewComp.pl 124;" s checkCompanyDBSet SetupNewComp.pl 55;" s emailSend AlertNotReceived.pl 81;" s -emailSend LoadCSVData.pl 361;" s +emailSend LoadCSVData.pl 364;" s emailSend prova_mail.pl 76;" s generateRandomPassword SetupNewComp.pl 46;" s getLoggingTime AlertNotReceived.pl 21;" s -getMatlabCmd LoadCSVData.pl 290;" s +getMatlabCmd LoadCSVData.pl 292;" s getNodesType LoadCSVData.pl 233;" s getTimeStamp LoadCSVData.pl 49;" s getTimeStamp SetupNewComp.pl 26;" s @@ -19,11 +19,11 @@ getTimeStamp UnitCsvReceiverSmart.pl 14;" s getTimeStamp aruba/UnitCsvReceiverSmart.pl 15;" s getTimeStamp aruba/UnitCsvRetransmit.pl 14;" s getTimeStamp prova_mail.pl 17;" s -getUdbPwd LoadCSVData.pl 266;" s +getUdbPwd LoadCSVData.pl 268;" s getUnitName UnitCsvReceiverSmart.pl 28;" s getUnitName aruba/UnitCsvReceiverSmart.pl 29;" s makeEmailMsg AlertNotReceived.pl 28;" s -makeEmailMsg LoadCSVData.pl 319;" s +makeEmailMsg LoadCSVData.pl 322;" s makeEmailMsg prova_mail.pl 36;" s matlabCalc LoadCSVData.pl 91;" s setCompanyDBGrant SetupNewComp.pl 151;" s diff --git a/LoadCSVData.pl b/LoadCSVData.pl index 3f25319..d23bd01 100644 --- a/LoadCSVData.pl +++ b/LoadCSVData.pl @@ -91,7 +91,7 @@ sub writeOutSql { sub matlabCalc { getMatlabCmd(); $matlab_timestamp = getTimeStamp("db_ts"); - print getTimeStamp("log") . " - pid $$ >> $tool MatLab calc started...\n"; + print getTimeStamp("log") . " - pid $$ >> $unit - $tool MatLab calc started...\n"; if ($matlab_cmd =~ /_lnx$/) { $matlab_cmd = './run_' . $matlab_cmd . '.sh'; @@ -118,14 +118,14 @@ sub matlabCalc { $matlab_cmd = 'matlab_' . uc $matlab_cmd . '.cmd'; my @out_matlab = `ssh1 $matlab_ip 'cmd /c $matlab_cmd ' $tool`; if (index($out_matlab[-1], "Failure") != -1) { - print getTimeStamp("log"). " - pid $$ >> $tool MatLab calc failed.\n"; + print getTimeStamp("log"). " - pid $$ >> $unit - $tool MatLab calc failed.\n"; makeEmailMsg(); emailSend(); print getTimeStamp("log") . " - pid $$ >> Execution ended.\n"; exit(1); } } - print getTimeStamp("log"). " - pid $$ >> $tool MatLab calc executed.\n"; + print getTimeStamp("log"). " - pid $$ >> $unit - $tool MatLab calc executed.\n"; return; } @@ -160,7 +160,7 @@ sub trxelab { my $fileelab; if ( $ftp_filename eq "" ) { - $fileelab ='/var/lib/mysql-files/'. $tool . '_'. getTimeStamp(). '.csv'; #mettere quello che si prende da query + $fileelab ='/var/lib/mysql-files/'. $tool . '_'. getTimeStamp(). '.csv'; #mettere quello che si prende da query *** aggiungere $unit }else { $ftp_filename =~ s/(\$\w+)/$1/eeg; $ftp_filename =~ s/\s/_/g; @@ -178,7 +178,7 @@ sub trxelab { XShift, YShift, ZShift , X, Y, Z, HShift, HShiftDir, HShift_local, speed, speed_local, acceleration, acceleration_local, T_node, water_level, pressure, load_value, AlfaX, AlfaY, calcerr from ElabDataView - where ToolNameID = '$tool' and updated_at > '$matlab_timestamp' + where UnitName = '$unit', ToolNameID = '$tool' and updated_at > '$matlab_timestamp' order by ToolNameID DESC, EventDate, EventTime, convert(`NodeNum`, decimal) DESC ) resulting_set into outfile '$fileelab' @@ -198,7 +198,7 @@ sub trxelab { $ftp_parms{'Debug'} = 0; $ftp_parms{'Passive'} = 1; - print getTimeStamp("log") . " - pid $$ >> $tool ftp parm:\n" . Dumper(%ftp_parms); + print getTimeStamp("log") . " - pid $$ >> $unit - $tool ftp parm:\n" . Dumper(%ftp_parms); if ( !defined $duedate or($duedate eq '0000-00-00 00:00:00') @@ -222,9 +222,9 @@ sub trxelab { print getTimeStamp("log"). " - pid $$ >> ftp target lists ". scalar @{ $ftp->ls() }. " elements.\n"; $ftp->quit; - print getTimeStamp("log"). " - pid $$ >> $tool: $fileelab ftp put executed.\n"; + print getTimeStamp("log"). " - pid $$ >> $unit - $tool: $fileelab ftp put executed.\n"; } else { - print getTimeStamp("log"). " - pid $$ >> $tool: $fileelab ftp put didn't executed because due date reached.\n"; + print getTimeStamp("log"). " - pid $$ >> $unit - $tool: $fileelab ftp put didn't executed because due date reached.\n"; } return; } @@ -235,15 +235,16 @@ sub getNodesType { or die getTimeStamp("log"). " - pid $$ >> Could not connect to database: $DBI::errstr"; 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.ain as ain, n.din as din from nodes as n inner join tools as t on t.id = n.tool_id + inner join units as u on u.id = t.unit_id inner join nodetypes as y on n.nodetype_id = y.id - where t.name = "' . $tool . '" order by 3;' + where t.name = "' . $tool . '" and u.name = "' . $unit . '" order by 3;' ) or die getTimeStamp("log") . " - pid $$ >> $DBI::errstr"; $sth->execute(); - print getTimeStamp("log"). " - pid $$ >> $tool: " . $sth->rows . " rows selected to get node type/din/channels.\n"; + print getTimeStamp("log"). " - pid $$ >> $unit - $tool: " . $sth->rows . " rows selected to get node type/Ain/Din/channels.\n"; if ( $sth->rows < 0 ) { die getTimeStamp("log") . " - pid $$ >> No node channels selected.\n"; }else { @@ -251,6 +252,7 @@ sub getNodesType { while ( my $results = $sth->fetchrow_hashref ) { $channels[$row] = $results->{'channels'}; $nodetype[$row] = $results->{'type'}; + $ain[$row] = $results->{'ain'}; $din[ $row++ ] = $results->{'din'}; } } @@ -294,7 +296,8 @@ sub getMatlabCmd { my $sth = $dbh->prepare( 'select m.matcall, t.ftp_send , t.unit_id from matfuncs as m inner join tools as t on t.matfunc = m.id - where t.name = "' . $tool . '" ;' + inner join units as u on u.id = t.unit_id + where t.name = "' . $tool . '" and u.name = "' . $unit . '";' ) or die getTimeStamp("log") . " - pid $$ >> $DBI::errstr"; $sth->execute(); @@ -464,110 +467,109 @@ while ( my $line = ) { #print Dumper \@din; }elsif ( $tooltype eq "LOC" ) { - if ( !defined $din[0] or ($din[0] eq '0') ) { - print getTimeStamp("log"). " - pid $$ >> Din not defined.\n"; - print getTimeStamp("log"). " - pid $$ >> Execution ended.\n"; - exit; - } my ( $Vbat, $Tmod, $Ain1, $Ain2, $Din1, $Din2 ) = split( /;/, $NodeData ); + if ( scalar @din ) { + print getTimeStamp("log"). " - pid $$ >> Ain/Din not defined.\n"; + print getTimeStamp("log"). " - pid $$ >> Execution ended.\n"; + exit; + } + my $idx = 0; - my ($chDin1, $chDin2) = split("", $channels[0]); - $CSVData[$idx++] = $Vbat . ";" . $Tmod; - - if ($din[0] eq '1') { - if ( ($chDin1 eq '1') and ($chDin2 eq '9') ) { - $CSVData[$idx++] = $Din1; - }elsif ( ($chDin1 eq '9') and ($chDin2 eq '1') ){ - $CSVData[$idx++] = $Din2; - }elsif ( ($chDin1 eq '2') and ($chDin2 eq '9') ){ - $CSVData[$idx++] = $Din1 . ";" . $Ain1; - }elsif ( ($chDin1 eq '9') and ($chDin2 eq '2') ){ - $CSVData[$idx++] = $Din2 . ";" . $Ain2; - } - }elsif ($din[0] eq '2') { - if ( ($chDin1 eq '1') and ($chDin2 eq '1') ) { - $CSVData[$idx++] = $Din1; - $CSVData[$idx++] = $Din2; - }elsif ( ($chDin1 eq '2') and ($chDin2 eq '1') ){ - $CSVData[$idx++] = $Din1 . ";" . $Ain1; - $CSVData[$idx++] = $Din2; - }elsif ( ($chDin1 eq '1') and ($chDin2 eq '2') ){ - $CSVData[$idx++] = $Din1; - $CSVData[$idx++] = $Din1 . ";" . $Ain1; - }elsif ( ($chDin1 eq '2') and ($chDin2 eq '2') ){ - $CSVData[$idx++] = $Din1 . ";" . $Ain1; - $CSVData[$idx++] = $Din2 . ";" . $Ain2; - } - - } - - # $CSVData[1] = $Din1; - - - }elsif ( $tooltype eq "GD" ) { - my ( $div, $Vbat, $Tmod ) = - split( /;/, $NodeData ); - $CSVData[0] = $Vbat . ";" . $Tmod; - my ( $y, $m, $d, $t ) = split( /[\s\/]/, $first ); - my ( $h, $min, $sec ) = split( /:/, $t ); - $GDEventDate = $y . '-' . $m . '-' . $d; - $CSVData[1] = "DT". substr( $tool, 2, 4 ) . ';'. $h . ':'. ( $min - 1 ) . ';'. $h . ':'. ( $min + 1 ); - }else { - @CSVData = split( /\|/, $NodeData ); - } - - my $nodenum = 0; - foreach (@CSVData) { - $sql[ ( $outcount % $maxsqllines ) ] = ""; - my (@data) = grep { /\S/ } split( /\;/, $_ ); - if ( $nodenum eq 0 ) { - $toolbatt = $data[0]; - $tooltemp = $data[1]; - }else { - my ( $y, $m, $d, $t ) = split( /[\s\/]/, $first ); - if (length($y) != 4) { # G201 con la data invertita - my $w = $y; - $y = $d; - $m = sprintf('%02d',$m); - $d = sprintf('%02d',$w); - } - foreach (@data) { - if ( $sql[ ( $outcount % $maxsqllines ) ] eq "" ) { - $sql[ ( $outcount % $maxsqllines ) ] = "('$unit','$tool','$nodenum','$y-$m-$d','$t','$toolbatt','$tooltemp'"; + my $used_din = 0; + my $used_ain = 0; + for my $i (0 .. $#din){ + if ($din[i] eq 1) { + if ($used_din eq 0) { + $CSVData[$idx++] = $Din1; + $used_din = 1; + } + else { + $CSVData[$idx++] = $Din2; } - $sql[ ( $outcount % $maxsqllines ) ] .= ",'" . $_ . "'"; } - my $InsCompl = ",NULL" x ( 15 - $#data ); - $sql[ ( $outcount % $maxsqllines ) ] .= $InsCompl . ")"; - if ( ++$outcount % $maxsqllines eq 0 ) { - $sql[ ( $outcount % $maxsqllines ) - 1 ] .= ";\n"; - writeOutSql($tool); - }else { - $sql[ ( $outcount % $maxsqllines ) - 1 ] .= ",\n"; + elsif ($ain[i] eq 1) { + if ($used_ain eq 0) { + $CSVData[$idx++] = $Ain1; + $used_ain = 1; + } + else { + $CSVData[$idx++] = $Ain2; + } + } + elsif ($din[i] eq 2) { + $CSVData[$idx++] = $Din1 . ";" . $Din2; + } + elsif ($ain[i] eq 2) { + $CSVData[$idx++] = $Ain1 . ";" . $Ain2; } } - $nodenum++; } } + elsif ( $tooltype eq "GD" ) { + my ( $div, $Vbat, $Tmod ) = + split( /;/, $NodeData ); + $CSVData[0] = $Vbat . ";" . $Tmod; + my ( $y, $m, $d, $t ) = split( /[\s\/]/, $first ); + my ( $h, $min, $sec ) = split( /:/, $t ); + $GDEventDate = $y . '-' . $m . '-' . $d; + $CSVData[1] = "DT". substr( $tool, 2, 4 ) . ';'. $h . ':'. ( $min - 1 ) . ';'. $h . ':'. ( $min + 1 ); + }else { + @CSVData = split( /\|/, $NodeData ); + } + + my $nodenum = 0; + foreach (@CSVData) { + $sql[ ( $outcount % $maxsqllines ) ] = ""; + my (@data) = grep { /\S/ } split( /\;/, $_ ); + if ( $nodenum eq 0 ) { + $toolbatt = $data[0]; + $tooltemp = $data[1]; + }else { + my ( $y, $m, $d, $t ) = split( /[\s\/]/, $first ); + if (length($y) != 4) { # G201 con la data invertita + my $w = $y; + $y = $d; + $m = sprintf('%02d',$m); + $d = sprintf('%02d',$w); + } + foreach (@data) { + if ( $sql[ ( $outcount % $maxsqllines ) ] eq "" ) { + $sql[ ( $outcount % $maxsqllines ) ] = "('$unit','$tool','$nodenum','$y-$m-$d','$t','$toolbatt','$tooltemp'"; + } + $sql[ ( $outcount % $maxsqllines ) ] .= ",'" . $_ . "'"; + } + my $InsCompl = ",NULL" x ( 15 - $#data ); + $sql[ ( $outcount % $maxsqllines ) ] .= $InsCompl . ")"; + if ( ++$outcount % $maxsqllines eq 0 ) { + $sql[ ( $outcount % $maxsqllines ) - 1 ] .= ";\n"; + writeOutSql($tool); + }else { + $sql[ ( $outcount % $maxsqllines ) - 1 ] .= ",\n"; + } + } + $nodenum++; + } } } +} close FILE; $sql[ ( $outcount % $maxsqllines ) - 1 ] =~ s/,$/;/g; writeOutSql($tool); 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 { - matlabCalc(); - if ($ftp_send) { - if ( $tool eq 'DT0076' ) { - sleep(600); - } - trxelab(); - } + matlabCalc(); + + if ($ftp_send) { + if ( $tool eq 'DT0076' ) { + sleep(600); + } + trxelab(); + } } print getTimeStamp("log") . " - pid $$ >> Execution ended.\n";