From 8a68b30baf391cc269fcdb0bcef5f579cd57fba7 Mon Sep 17 00:00:00 2001 From: Maps Date: Fri, 24 Jul 2015 14:11:25 +0200 Subject: [PATCH] automatic report generation and sending via email, still have to put more information in the report body --- user_interface/coral_scanner.js | 1 + user_interface/coral_scanner.pm | 53 +++++++++- user_interface/csv2xls.sh | 5 + user_interface/report.pl | 25 +++++ user_interface/report.pm | 166 ++++++++++++++++++++++++++++++++ 5 files changed, 249 insertions(+), 1 deletion(-) create mode 100755 user_interface/csv2xls.sh create mode 100755 user_interface/report.pl create mode 100644 user_interface/report.pm diff --git a/user_interface/coral_scanner.js b/user_interface/coral_scanner.js index 31ec044..2fda2f4 100644 --- a/user_interface/coral_scanner.js +++ b/user_interface/coral_scanner.js @@ -53,6 +53,7 @@ $(document).ready(function(){ unfolds($("#show_pmt_spectrum"),$("#pmt_spectrum_container")); unfolds($("#show_coral_scanner_settings"),$("#coral_scanner_settings_container")); unfolds($("#show_scan_pattern"),$("#scan_pattern_container")); + unfolds($("#show_report_settings"),$("#report_settings_container")); diff --git a/user_interface/coral_scanner.pm b/user_interface/coral_scanner.pm index 30d4b68..9596425 100644 --- a/user_interface/coral_scanner.pm +++ b/user_interface/coral_scanner.pm @@ -21,6 +21,7 @@ our @ISA = qw/has_settings/; # assimilate the methods of the has_settings class use pmt_ro; use table_control; +use report; use misc_subs; @@ -75,6 +76,7 @@ sub new { $self->{pmt_ro} = pmt_ro->new(); $self->{table_control} = table_control->new(); + $self->{report} = report->new(); $self->load_settings(); @@ -224,6 +226,11 @@ sub main_html { $self->{table_control}->settings_form(); print ""; + print "

report settings

"; + print "
"; + $self->{report}->settings_form(); + print "
"; + print ""; print end_html(); @@ -358,6 +365,9 @@ sub scan_sample { }); print ">>> scan completed!\n\n"; + print ">>> sending report ...\n\n"; + $self->compile_report(); + print ">>> done!\n\n"; return ""; @@ -503,6 +513,8 @@ sub scan_to_ascii { my $scan = $self->{scan_shm}->readShm(); my $tc = $self->{table_control}; + my $tofile = $options{tofile}; + my $sample_rect_x1 = $tc->{settings}->{sample_rect_x1}; my $sample_rect_x2 = $tc->{settings}->{sample_rect_x2}; my $sample_rect_y1 = $tc->{settings}->{sample_rect_y1}; @@ -526,7 +538,13 @@ sub scan_to_ascii { } push(@rows,join("\t",@cols)); } - print join("\n",@rows); + if(defined($tofile)){ + open(FILE,"> $tofile") or die "cannot open output file $tofile\n"; + print FILE join("\n",@rows); + close(FILE); + } else { + print join("\n",@rows); + } return " "; } @@ -618,6 +636,39 @@ sub scan_to_svg { } +sub compile_report { + + my $self = shift; + my %options = @_; + + my $timestamp = strftime("%Y-%m-%d_%H:%M:%S", localtime()); + + + my $storable = "./report/".$timestamp."_scan.storable"; + system("cp /dev/shm/".$self->{scan_shm}->{shmName}." $storable"); + my $dump = "./report/".$timestamp."_scan.dump"; + + my $csv = "./report/".$timestamp."_scan.csv"; + my $svg = "./report/".$timestamp."_scan.svg"; + my $xls = $csv.".xls"; + + open(DUMP,"> $dump") or die "cannot open $dump for writing \n"; + print DUMP Dumper($self->last_scan()); + close(DUMP); + + $self->scan_to_ascii(tofile => $csv); + $self->scan_to_svg(svg_file => $svg); + system("./csv2xls.sh $csv"); + + $self->{report}->email( + text => "Scan has finished at $timestamp, all recorded data is attached", + attachments => join(",",($csv,$svg,$xls,$storable,$dump)) + ); + + return " "; + +} + diff --git a/user_interface/csv2xls.sh b/user_interface/csv2xls.sh new file mode 100755 index 0000000..a8fc467 --- /dev/null +++ b/user_interface/csv2xls.sh @@ -0,0 +1,5 @@ +#!/bin/bash +a=$(mktemp) +cat $1 | perl -pi -e 's/[\t ]/,/g;' > $a +ssconvert $a $1.xls +rm $a diff --git a/user_interface/report.pl b/user_interface/report.pl new file mode 100755 index 0000000..71c66db --- /dev/null +++ b/user_interface/report.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl + +#################################################################################### +## This is a simple script to dispatch a perl module's subs from a CGI request ## +#################################################################################### + +use strict; +use warnings; +use CGI_dispatch; + +use report; +my $self = report->new(); + +# go only to methods that are in the following dispatch table: +# if associated value is one, sub can be called via CGI +my $dispatch_table = { + help => 1, + test => 1, + load_settings => 1, + save_settings => 1, + reset_settings => 1, + settings_form => 1 +}; + +CGI_dispatch::dispatch_sub(package => $self);#, dispatch_table => $dispatch_table); diff --git a/user_interface/report.pm b/user_interface/report.pm new file mode 100644 index 0000000..736068d --- /dev/null +++ b/user_interface/report.pm @@ -0,0 +1,166 @@ +package report; + + +use strict; +use warnings; +use POSIX qw/strftime/; +use POSIX; + + +use Net::SMTP::SSL; +use MIME::Base64 qw( encode_base64 ); + +use File::Basename; + +use misc_subs; +use has_settings; +our @ISA = qw/has_settings/; # assimilate the methods of the has_settings class + +## methods + +sub new { + my $class = shift; + my %options = @_; + + my $self = {}; # put tons of default values here (if you wish); + + $self->{constants} = { + }; + + $self->{settings_file} = "./".__PACKAGE__.".settings"; + + $self->{default_settings} = { # hard default settings + recipients => "", + from => "", + sender_address => "", + sender_password => "", + subject => "Coral Scan finished", + smtp_server => "smtp.sth.sth", + smtp_port => "465", + }; + + $self->{settings_desc} = { + recipients => "A comma separated list of email addresses of people who are to receive a confirmation + and the data when a scan has finished.", + from => "The displayed address of the sender", + sender_address => "Email address of account which is used to send the confirmation email.", + sender_password => "Password of the account which is used to send the confirmation email.", + subject => "Confirmation email subject", + smtp_server => "Outgoing mail server (SMTP)", + smtp_port => "The port of the outgoing SMTP connection", + }; + + $self->{has_run} = {}; # remember which subs already have run + + $self->{settings} = {%{$self->{default_settings}}}; + + $self = { + %$self, + %options + }; + bless($self, $class); + $self->load_settings(); + + return $self; +} + + +sub email { + + my $self = shift; + my %options = @_; + + my $to = $self->{settings}->{recipients}; + $to =~ s/\s//g; + my @recipients = split(',',$to); + + my $attch_list = $options{attachments} || ""; + $attch_list =~ s/\s//g; + my @attachments = split(',',$attch_list); + + my $text = $options{text} || ""; + + my $account = $self->{settings}->{sender_address}; + my $password = $self->{settings}->{sender_password}; + + my $boundary = 'frontier'; + + for my $recipient (@recipients) { + + my $smtp = Net::SMTP::SSL->new( + Host => $self->{settings}->{smtp_server}, + Port => $self->{settings}->{smtp_port}, + Timeout => 120 + ); # connect to an SMTP server + die "Couldn't open connection: $!" if (!defined $smtp ); + + + $smtp->auth($account,$password); + $smtp->mail( $account ); # use the sender's address here + $smtp->to( $recipient ); # recipient's address + $smtp->data(); # Start the mail + # Send the header. + $smtp->datasend("To: $recipient\n"); + $smtp->datasend("From: ".$self->{settings}->{from}."\n"); + $smtp->datasend("Subject: ".$self->{settings}->{subject}."\n"); + + $smtp->datasend("MIME-Version: 1.0\n"); + $smtp->datasend("Content-type: multipart/mixed;\n\tboundary=\"$boundary\"\n"); + $smtp->datasend("\n"); + $smtp->datasend("--$boundary\n"); + $smtp->datasend("Content-type: text/plain\n"); + $smtp->datasend("Content-Disposition: quoted-printable\n"); +# $smtp->datasend("\nToday\'s files are attached:\n"); +# $smtp->datasend("\nHave a nice day! :)\n"); + $smtp->datasend("\n".$text."\n"); + $smtp->datasend("--$boundary\n"); + + for my $attachBinaryFile (@attachments) { + my $attachBinaryFileName = (fileparse($attachBinaryFile))[0] ; + $smtp->datasend("Content-Type: binary; name=\"$attachBinaryFileName\"\n"); + $smtp->datasend("Content-Transfer-Encoding: base64\n"); + $smtp->datasend("Content-Disposition: attachment; filename=\"$attachBinaryFileName\"\n"); + $smtp->datasend("\n"); + my $buf; + open(DAT, "$attachBinaryFile") || die("Could not open binary file!"); + binmode(DAT); + # local $/=undef; + while (read(DAT, my $picture, 4096)) { + $buf = &encode_base64( $picture ); + $smtp->datasend($buf); + } + close(DAT); + $smtp->datasend("\n"); + $smtp->datasend("--$boundary\n"); + } + + $smtp->dataend(); # Finish sending the mail + $smtp->quit; # Close the SMTP connection + } + + + +} + + + + + + + + + + + +sub help { + my $self = shift; + my $verbose = shift; + print "This is the help message!\n"; +# pod2usage(verbose => $verbose); + exit; + +} + + + +1; -- 2.43.0