Files
matlab-python/Tilt/AlertReport.m

429 lines
17 KiB
Matlab
Executable File

function [checkALRPT,nomeALRPT_PDF] = AlertReport(IDcentralina,DTcatena,Sito,...
NodeNum,Depth,data,DataNum,LVL,TipoDisp,conn,FileName)
% Funzione per la creazione di report automatici di allertamento
% *** INPUT ***
%%% data -> data corrispondente all'OOA dell'evento
%%% DataNum -> numero di valori nel dataset dell'evento
%%% LVL -> livello dell'evento
%%% NodeNum, Depth -> numero e profondità del nodo
%%% TipoDisp -> spostamenti locali o cumulati (LOCALI DA IMPLEMENTARE)
fileID = fopen(FileName,'a');
fmt = '%s \r';
text = 'AlertReport function started';
fprintf(fileID,fmt,text);
fclose(fileID);
try
%% Importo librerie Matlab Report Generator e abilito compilazione DOM
import mlreportgen.dom.*
import mlreportgen.report.*
makeDOMCompilable()
%% Creo report e info
tempoALRPT = datestr(now,'dd-mm-yyyy HH:MM:SS');
tempoinvio = datestr(datenum(tempoALRPT),'ddmmyy_HHMMSS');
nomeALRPT = ['AlertReport-' IDcentralina '-' DTcatena '-' NodeNum '-' tempoinvio];
docALRPT = Report(nomeALRPT,'pdf');
nomeALRPT_PDF = [nomeALRPT '.pdf'];
open(docALRPT)
PG = Chapter;
Font_text = '12pt';
Font_text2 = '14pt';
Font_report = 'Times';
wm = Watermark('sfondoALRPT.png');
docALRPT.Layout.Watermark = wm;
titolo = Paragraph('Report automatico di allertamento');
titolo.Style = {HAlign('center'),FontSize('28pt'),Bold(1),...
OuterMargin('0in','0in','0.2in','0in'),FontFamily(Font_report)};
sottotitolo = Paragraph(tempoALRPT);
sottotitolo.Style = {HAlign('center'),FontSize('12pt'),Bold(1),FontFamily(Font_report)};
InfoNodo = [NodeNum ' (' Depth ' m)'];
dd = data(1:2);
mm = data(4:5);
yyyy = data(7:10);
dataOOA =([yyyy '-' mm '-' dd]);
oraOOA = datestr(data,'HH:MM:SS');
dataMESE = datestr(datenum(dataOOA)-30, 'yyyy-mm-dd');
%% costruisco il dataset mensile degli spostamenti
% predendo come riferimento data e ora di OOA, scarico tutti i dati
% disponibili a partire dal giorno (e ora) localizzato 30gg prima di
% OOA per il calcolo della soglia di spostamento equivalente
comando = ['select EventDate, EventTime, HShift, EventTimestamp from ElabDataView where EventDate = ''' ...
dataMESE ''' and EventTime >= ''' oraOOA ''' and ToolNameID = ''' DTcatena ...
''' and UnitName = ''' IDcentralina ...
''' and NodeNum = ''' NodeNum ''' order by EventTimestamp'];
Dati1 = fetch(conn,comando); % dati del singolo giorno 30gg prima di OOA
comando = ['select EventDate, EventTime, HShift, EventTimestamp from ElabDataView where EventDate > ''' ...
dataMESE ''' and EventDate < ''' dataOOA ''' and ToolNameID = ''' DTcatena ...
''' and UnitName = ''' IDcentralina ...
''' and NodeNum = ''' NodeNum ''' order by EventTimestamp'];
Dati2 = fetch(conn,comando); % dati da 30gg prima di OOA fino a OOA
comando = ['select EventDate, EventTime, HShift, EventTimestamp from ElabDataView where EventDate = ''' ...
dataOOA ''' and EventTime < ''' oraOOA ''' and ToolNameID = ''' DTcatena ...
''' and UnitName = ''' IDcentralina ...
''' and NodeNum = ''' NodeNum ''' order by EventTimestamp'];
Dati3 = fetch(conn,comando); % dati da OOA in avanti
% il dataset di spostamento inizia dal dato subito PRECEDENTE all'OOA -->
% lo inserisco all'inizio e lo tolgo dal dataset mensile
dataEV(1,1) = Dati3(end,1);
oraEV(1,1) = Dati3(end,2);
datasetEV(1,1) = Dati3(end,3);
Dati3(end,:) = [];
% riorganizzo i dati
datiMESE = [Dati1; Dati2; Dati3];
[rDM,~] = size(datiMESE);
for m = 1:rDM
tempoMESE{m,1} = strjoin(datiMESE(m,1:2));
datasetMESE(m,1) = datiMESE(m,3);
end
%% costruisco il dataset dell'evento critico
% A partire da data OOA, scarico tutti i dati disponibili e sfrutto DataNum
% per prendere il numero giusto di valori (relativi al dataset
% dell'evento)
comando = ['select EventDate, EventTime, EventTimestamp from ElabDataView where EventDate = ''' ...
dataOOA ''' and EventTime >= ''' oraOOA ''' and ToolNameID = ''' DTcatena ...
''' and UnitName = ''' IDcentralina ...
''' and NodeNum = ''' NodeNum ''' order by EventTimestamp'];
DateEV1 = fetch(conn,comando);
comando = ['select EventDate, EventTime, EventTimestamp from ElabDataView where EventDate > ''' ...
dataOOA ''' and ToolNameID = ''' DTcatena ''' and UnitName = ''' IDcentralina ...
''' and NodeNum = ''' NodeNum ''' order by EventTimestamp'];
DateEV2 = fetch(conn,comando);
DateEV_temp = [DateEV1; DateEV2];
DateEV = DateEV_temp(1:DataNum,:);
[rT,~] = size(DateEV);
for tmp = 1:rT
conta = tmp+1; % aggiungo dopo il primo dato precedente all'OOA
dataEV{conta,1} = datestr(DateEV(tmp,1),'yyyy-mm-dd');
oraEV{conta,1} = datestr(DateEV(tmp,2),'HH:MM:SS');
comando = ['select HShift from ElabDataView where EventDate = ''' ...
dataEV{conta,1} ''' and EventTime = ''' oraEV{conta,1} ''' and ToolNameID = ''' DTcatena ...
''' and UnitName = ''' IDcentralina ...
''' and NodeNum = ''' NodeNum ''' '];
datasetEV(conta,1) = fetch(conn,comando);
end
tempmat = [dataEV oraEV];
[rDO,~] = size(tempmat);
for d = 1:rDO
tempoEV{d,1} = strjoin(tempmat(d,:));
end
durataevento = datenum(tempoEV(end))-datenum(tempoEV(1));
% array per grafico -> punto OOA
timerefEV = [tempoEV{1,:};tempoEV{1,:}];
datarefEV = [0; datasetEV(1,1)];
%% ricavo soglia di spostamento equivalente
% soglia basata sugli spostamenti equivalenti calcolati su un periodo
% pari a quello in cui si è verificato l'evento critico
[rDT,~] = size(datasetMESE);
[rDS,~] = size(datasetEV);
ind = 1;
for c = rDS:rDT
if ind == 1 % primo dato
eqdispMESE(ind,1) = abs(cell2mat(datasetMESE(c,1)) - cell2mat(datasetMESE(ind,1)));
tempoeqdispMESE(ind,1) = tempoMESE(c,:);
ind = ind +1;
else % dati seguenti
% controllo che l'intervallo scelto corrisponda alla durata
% dell'evento critico
dT1 = abs((datenum(tempoMESE(c,1))-datenum(tempoMESE(ind,1)))-durataevento);
dT2 = abs((datenum(tempoMESE(c,1))-datenum(tempoMESE(ind-1,1)))-durataevento);
dT3 = abs((datenum(tempoMESE(c,1))-datenum(tempoMESE(ind+1,1)))-durataevento);
if abs(dT1)<abs(dT2) && abs(dT1)<abs(dT3)
% se la durata dell'intervallo scelto è la più
% vicina alla durata effettiva dell'evento, gli spostamenti
% sono correttamente riferiti a un intervallo di pari durata
eqdispMESE(ind,1) = abs(cell2mat(datasetMESE(c,1)) - cell2mat(datasetMESE(ind,1)));
tempoeqdispMESE(ind,1) = tempoMESE(c,:);
ind = ind + 1;
else
% se i controlli non sono verificati è possibile che sia
% presente un salto temporale nei dati -> non ha senso
% calcolare gli spostamenti equivalenti perchè l'intervallo
% temporale è differente, i valori vengono quindi segnati e
% successivamente rimossi dal calcolo della soglia
eqdispMESE(ind,1) = -99;
tempoeqdispMESE(ind,1) = tempoMESE(c,:);
ind = ind + 1;
end
end
end
% calcolo soglia senza valori anomali
eqdispMESEth = eqdispMESE(eqdispMESE ~= -99);
mediaMESE = mean(eqdispMESEth);
varMESE = std(eqdispMESEth);
sogliaMESE = (mediaMESE + 3*varMESE)*1000; % soglia spostamento equivalente (mm)
%%% dati vari per grafici e tabelle
% valori anomali pari a 0 nei grafici
[red,~] = size(eqdispMESE);
for s = 1:red
if eqdispMESE(s,1) == -99
eqdispMESE(s,1) = 0;
end
end
datasetTOT = [datasetMESE; datasetEV];
tempoTOT = [tempoMESE; tempoEV];
[rTOT,~] = size(datasetTOT);
ind = rTOT-rDS*2+1;
p = 1;
for t = rTOT-rDS+1:rTOT % dataset per grafico spostamenti equivalenti
eqdispEV(p,1) = abs(cell2mat(datasetTOT(t,1)) - cell2mat(datasetTOT(ind,1)));
tempoeqdispEV(p,1) = tempoTOT(t,:);
ind = ind+1;
p = p+1;
end
dispEV = eqdispEV(end,1)*1000; %spostamento dell'evento critico
tempodispEV = tempoeqdispEV(end,:);
eqdispTOT = [eqdispMESE*1000; eqdispEV*1000];
tempoeqdispTOT = [tempoeqdispMESE; tempoeqdispEV];
sogliaPLOT = [sogliaMESE; sogliaMESE];
temposogliaPLOT = [datenum(tempoTOT(1,:))-20; datenum(tempoTOT(end,:))+20];
%immagine del livello di attività del sito
if LVL == 5 && dispEV < sogliaMESE
imglevel = Image('activity1.jpg');
elseif LVL == 4 && incrDIPS >= sogliaMESE
imglevel = Image('activity2.jpg');
elseif LVL == 5 && dispEV >= sogliaMESE
imglevel = Image('activity3.jpg');
elseif LVL == 1.5 % falso allarme
imglevel = Image('activity4.jpg');
else
imglevel = Image('activity1.jpg');
end
imglevel.Style = {Height('2.4cm')};
%% grafici spostamenti e spostamenti equivalenti
% SPOSTAMENTI
figure(1)
subplot(2,1,1); % (righe colonne posizione)
pbaspect([3 1 1]);
disp = plot(datenum(tempoEV),cell2mat(datasetEV),'Color','b','LineWidth',1);
hold on
month = plot(datenum(tempoMESE),cell2mat(datasetMESE),'Color','b','LineWidth',1);
ref = plot(datenum(timerefEV),cell2mat(datarefEV),':','Color','k','LineWidth',0.75);
OOA = plot(datenum(timerefEV(2,:)),cell2mat(datarefEV(2,:)),'.','Color','r','MarkerSize',12);
dLIM(1,1) = min(cell2mat(datasetMESE));
dLIM(2,1) = min(cell2mat(datasetEV));
dLIM(1,2) = max(cell2mat(datasetMESE));
dLIM(2,2) = max(cell2mat(datasetEV));
MIN = min(dLIM(:,1));
MAX = max(dLIM(:,2));
ylim([MIN-0.01*MIN MAX+0.01*MAX]);
xlim([datenum(tempoTOT(1,:))-5; datenum(tempoTOT(end,:))+5]);
if TipoDisp == 1
sp = 'locali';
else
sp = 'cumulati';
end
title(['Spostamenti ' sp ' - nodo ' NodeNum]);
xlabel('Data [gg/mm/aaaa]');
ylabel('Spostamento [m]');
h = gca;
h.XAxis.MinorTick = 'on';
datetick('x','dd/mm/yy','keepticks','keeplimits');
ax = gca;
ax.FontSize = 7;
grid on
grid minor
% SPOSTAMENTI EQUIVALENTI
hold off
subplot(2,1,2); % (righe colonne posizione)
pbaspect([3 1 1]);
eqplot = plot(datenum(tempoeqdispTOT),eqdispTOT);
hold on
crit = plot(datenum(tempodispEV),dispEV,'.','Color','k','MarkerSize',15);
thr = plot(temposogliaPLOT, sogliaPLOT,'--','Color','r','Linewidth',1);
xlim([datenum(tempoeqdispTOT(1,:))-5; datenum(tempoeqdispTOT(end,:))+5]);
maxincr = max(eqdispTOT);
if maxincr >= sogliaMESE
ylim([0 maxincr+0.2*maxincr]);
else
ylim([0 sogliaMESE+0.2*sogliaMESE]);
end
title(['Spostamenti equivalenti - nodo ' NodeNum]);
xlabel('Data [gg/mm/aaaa]');
ylabel('Spostamento [mm]');
h = gca;
h.XAxis.MinorTick = 'on';
ax = gca;
ax.FontSize = 7;
datetick('x','dd/mm/yy','keepticks','keeplimits');
grid on
grid minor
TempName = ['AlertReport-' IDcentralina '-' DTcatena '-' tempoinvio '-EqDisp.png'];
saveas(eqplot,TempName);
ChartDispEq = Image(TempName);
ChartDispEq.Style = {ScaleToFit};
close(figure(1))
% immagine legenda per grafici
legIMG = Image('legendaSpEq.png');
legIMG.Style = {ScaleToFit,OuterMargin('0in','0in','-0.2in','0in')};
%% Creo tabelle e campi corrispondenti
% tabella info sito
tabsito(1,1) = Paragraph();
tabsito(1,1).Style = {FontSize(Font_text),FontFamily(Font_report)};
t1a = Text('Sito: ');
t1a.Bold = 1;
t1b = Text(Sito);
append(tabsito(1,1),t1a);
append(tabsito(1,1),t1b);
tabsito(2,1) = Paragraph();
tabsito(2,1).Style = {FontSize(Font_text),FontFamily(Font_report)};
t2a = Text('Centralina: ');
t2a.Bold = 1;
t2b = Text(IDcentralina);
append(tabsito(2,1),t2a);
append(tabsito(2,1),t2b);
tabsito(3,1) = Paragraph();
tabsito(3,1).Style = {FontSize(Font_text),FontFamily(Font_report)};
t3a = Text('Array: ');
t3a.Bold = 1;
t3b = Text(DTcatena);
append(tabsito(3,1),t3a);
append(tabsito(3,1),t3b);
tabsito(4,1) = Paragraph();
tabsito(4,1).Style = {FontSize(Font_text),FontFamily(Font_report)};
t4a = Text(['Nodo (profondit' char(224) '): ']);
t4a.Bold = 1;
t4b = Text(InfoNodo);
append(tabsito(4,1),t4a);
append(tabsito(4,1),t4b);
tabsito(5,1) = Paragraph();
tabsito(5,1).Style = {FontSize(Font_text),FontFamily(Font_report)};
t5a = Text('Inizio evento: ');
t5a.Bold = 1;
t5b = Text(data);
append(tabsito(5,1),t5a);
append(tabsito(5,1),t5b);
lot1 = Table({tabsito, imglevel});
lot1.TableEntriesStyle = {VAlign('middle')};
lot1.Style = {ResizeToFitContents(false),Width("100%"),...
OuterMargin('0in','0in','0.3in','0in')};
grps(1) = TableColSpecGroup;
grps(1).Span = 2;
specs(1) = TableColSpec;
specs(1).Span = 1;
specs(1).Style = {Width("80%")};
specs(2) = TableColSpec;
specs(2).Span = 1;
specs(2).Style = {Width("20%")};
grps(1).ColSpecs = specs;
lot1.ColSpecGroups = grps;
% tabella info spostamenti evento
tabE1(1,1) = Paragraph('Spostamento generato dall''evento [mm]');
tabE1(2,1) = Paragraph(num2str((dispEV),'%0.2f'));
tabE2(1,1) = Paragraph('Spostamento totale mensile [mm]');
tabE2(2,1) = Paragraph(num2str(((cell2mat(datasetEV(end))-cell2mat(datasetMESE(1)))*1000),'%0.2f'));
tabE3(1,1) = Paragraph('Soglia di spostamento [mm]');
tabE3(2,1) = Paragraph(num2str((sogliaMESE),'%0.2f'));
tabE1(1,1).Style ={FontSize(Font_text),Bold(1),HAlign('center'),...
Hyphenation(false),FontFamily(Font_report)};
tabE1(2,1).Style ={FontSize(Font_text2),HAlign('center'),...
InnerMargin('0in','0in','0.2in','0in'),FontFamily(Font_report)};
tabE2(1,1).Style ={FontSize(Font_text),Bold(1),HAlign('center'),...
Hyphenation(false),FontFamily(Font_report)};
tabE2(2,1).Style ={FontSize(Font_text2),HAlign('center'),...
InnerMargin('0in','0in','0.2in','0in'),FontFamily(Font_report)};
tabE3(1,1).Style ={FontSize(Font_text),Bold(1),HAlign('center'),...
Hyphenation(false),FontFamily(Font_report)};
tabE3(2,1).Style ={FontSize(Font_text2),HAlign('center'),...
InnerMargin('0in','0in','0.2in','0in'),FontFamily(Font_report)};
lot2 = Table({tabE1, tabE2, tabE3});
lot2.TableEntriesStyle = {VAlign('middle')};
lot2.Style = {ResizeToFitContents(false),Width("100%"),...
OuterMargin('0in','0in','0.3in','0in')};
%% Inserisco nel report e chiudo
add(PG,titolo);
add(PG,sottotitolo);
add(PG,lot1);
add(PG,ChartDispEq);
add(PG,legIMG);
add(PG,lot2);
add(docALRPT,PG);
close(docALRPT);
checkALRPT = 1;
%% Scrivo nel log
fileID = fopen(FileName,'a');
text = 'AlertReport function worked correctly - Document created!';
fmt = '%s \r';
fprintf(fileID,fmt,text);
fclose(fileID);
catch err
FileErrALRPT = ['ErrorFile-AlertReport-' IDcentralina '-' DTcatena '-' datestr(today) '-' datestr(now,'hhMMss') '.txt'];
fid = fopen(FileErrALRPT,'a+');
fprintf(fid, '%s', err.getReport('extended','hyperlinks','off'));
fclose(fid);
checkALRPT = 0; % non allego il report nella mail (documento non generato)
% Email
setpref('Internet','E_mail','alert@aseltd.eu');
setpref('Internet','SMTP_Server','smtps.aruba.it');
setpref('Internet','SMTP_Username','alert@aseltd.eu');
setpref('Internet','SMTP_Password','Ase#2013!20@bat');
props=java.lang.System.getProperties;
pp=props.setProperty('mail.smtp.auth','true'); %#ok
pp=props.setProperty('mail.smtp.socketFactory.class','javax.net.ssl.SSLSocketFactory'); %#ok
pp=props.setProperty('mail.smtp.socketFactory.port','465'); %#ok
subject_ITA_ALRPT = ('CRASH Alert Report');
message_ITA_ALRPT = cellstr(['La funzione Alert Report ha al proprio interno qualche minchiata '...
'per cui crasha nel creare il report di allertamento di ' Sito...
', centralin ' IDcentralina ' catena ' DTcatena '. Fate qualcosa vi prego!']);
messagealrpt = cellstr('Tanti Blip e Blop a voi, ');
message2alrpt = cellstr('Tilt - Alert Report');
message_ITA_ALRPT = [message_ITA_ALRPT; messagealrpt; message2alrpt];
recipients_ASEALRPT{1,1} = 'alessandro.valletta@aseltd.eu';
recipients_ASEALRPT{2,1} = 'andrea.carri@aseltd.eu';
allegatoERR_ALR = FileErrALRPT;
sendmail(recipients_ASEALRPT, subject_ITA_ALRPT, message_ITA_ALRPT, allegatoERR_ALR)
end
end