306 lines
12 KiB
Matlab
Executable File
306 lines
12 KiB
Matlab
Executable File
function alert_Levels(rLL,rCrL,DatiLoad,DatiCrack,ARRAYdate_LL,ARRAYdate_CrL,...
|
|
ErrLoadLink,~,NodoLoadLink,NodoCrackLink,IDcentralina,DTcatena,...
|
|
sms,date,time,conn,FileName)
|
|
|
|
fileID = fopen(FileName,'a');
|
|
fmt = '%s \r';
|
|
text = 'alert_Levels function started';
|
|
fprintf(fileID,fmt,text);
|
|
fclose(fileID);
|
|
|
|
alert = 0;
|
|
alarm = 0;
|
|
if strcmp(IDcentralina,'ID0081') && strcmp(DTcatena,'DT0009') % Varazze - Casanova - Via Fossello
|
|
[numDate,~] = size(ARRAYdate_LL);
|
|
indexALA = zeros(1,2);
|
|
indexALE = zeros(1,2);
|
|
cI = 1;
|
|
cI2 = 1;
|
|
for s = 1:rLL
|
|
for n = 1:numDate
|
|
if DatiLoad(n,s) > 588.4 % kN
|
|
if ErrLoadLink(n,s) == 0 || ErrLoadLink(n,s) == 0.5 % Il nodo non è in errore
|
|
alarm = 1;
|
|
indexALA(cI,1) = n; % Data
|
|
indexALA(cI,2) = s; % sensore
|
|
cI = cI+1;
|
|
end
|
|
% elseif DatiLoad(n,s)-DatiLoad(n-1,s) > 0.3 % mm
|
|
% if ErrLoadLink(n,s) == 0 || ErrLoadLink(n,s) == 0.5 % Il nodo non è in errore
|
|
% alert = 1;
|
|
% indexALE(cI2,1) = n; % Data
|
|
% indexALE(cI2,2) = s; % fessurimetro
|
|
% cI2 = cI2+1;
|
|
% end
|
|
end
|
|
end
|
|
end
|
|
elseif strcmp(IDcentralina,'ID0082') && strcmp(DTcatena,'DT0010') % Riva di Tures
|
|
[numDate,~] = size(ARRAYdate_CrL);
|
|
indexALA = zeros(1,2);
|
|
indexALE = zeros(1,2);
|
|
cI = 1;
|
|
cI2 = 1;
|
|
fuoriSoglia1 = zeros(numDate,rCrL); % Sensore 1 > 50 mm
|
|
fuoriSoglia2 = zeros(numDate,rCrL); % Sensore 2 > 5 mm
|
|
fuoriSoglia3 = zeros(numDate,rCrL); % Sensore 1 > 20 mm per 10 letture
|
|
fuoriSoglia4 = zeros(numDate,rCrL); % Sensore 1 > 10 mm per 10 letture
|
|
RIF = zeros(1,rCrL);
|
|
for s = 1:rCrL
|
|
NodeNumCrL = num2str(cell2mat(NodoCrackLink(s,2))); % scarico i dati del primo nodo Crack Link
|
|
comando = ['select XShift from ElabDataView where EventDate = ''' ...
|
|
date ''' and EventTime >= ''' time ''' and UnitName = ''' IDcentralina ...
|
|
''' and ToolNameID = ''' DTcatena ''' and NodeNum = ' NodeNumCrL ' '];
|
|
curs = exec(conn,comando);
|
|
curs = fetch(curs);
|
|
Data_LastElab = curs.Data;
|
|
RIF(1,s) = cell2mat(Data_LastElab(1,1));
|
|
for n = 1:numDate
|
|
if DatiCrack(n,s) - RIF(1,s) > 50 % mm
|
|
fuoriSoglia1(n,s) = 1;
|
|
end
|
|
if DatiCrack(n,s) - RIF(1,s) > 5 % mm
|
|
fuoriSoglia2(n,s) = 1;
|
|
end
|
|
if n >= 10
|
|
cont = 0;
|
|
cont2 = 0;
|
|
for aa = n-9:n
|
|
if DatiCrack(aa,s) - RIF(1,s) > 20 % mm
|
|
cont = cont+1;
|
|
end
|
|
if DatiCrack(aa,s) - RIF(1,s) > 10 % mm
|
|
cont2 = cont2+1;
|
|
end
|
|
end
|
|
if cont >= 10
|
|
fuoriSoglia3(n,s) = 1;
|
|
end
|
|
if cont2 >= 10
|
|
fuoriSoglia4(n,s) = 1;
|
|
end
|
|
end
|
|
end
|
|
end
|
|
for s = 1:rCrL
|
|
for n = 10:numDate
|
|
if fuoriSoglia1(n,s) == 1
|
|
if fuoriSoglia3(n,s) == 1
|
|
if s == 1
|
|
if fuoriSoglia2(n,2) == 1 || fuoriSoglia2(n,3) == 1 || fuoriSoglia2(n,4) == 1
|
|
alarm = 1;
|
|
indexALA(cI,1) = n; % Data
|
|
indexALA(cI,2) = s; % sensore
|
|
cI = cI+1;
|
|
end
|
|
elseif s == 2
|
|
if fuoriSoglia2(n,1) == 1 || fuoriSoglia2(n,3) == 1 || fuoriSoglia2(n,4) == 1
|
|
alarm = 1;
|
|
indexALA(cI,1) = n; % Data
|
|
indexALA(cI,2) = s; % sensore
|
|
cI = cI+1;
|
|
end
|
|
elseif s == 3
|
|
if fuoriSoglia2(n,1) == 1 || fuoriSoglia2(n,2) == 1 || fuoriSoglia2(n,4) == 1
|
|
alarm = 1;
|
|
indexALA(cI,1) = n; % Data
|
|
indexALA(cI,2) = s; % sensore
|
|
cI = cI+1;
|
|
end
|
|
elseif s == 4
|
|
if fuoriSoglia2(n,1) == 1 || fuoriSoglia2(n,2) == 1 || fuoriSoglia2(n,3) == 1
|
|
alarm = 1;
|
|
indexALA(cI,1) = n; % Data
|
|
indexALA(cI,2) = s; % sensore
|
|
cI = cI+1;
|
|
end
|
|
end
|
|
end
|
|
end
|
|
if fuoriSoglia4(n,s) == 1
|
|
alert = 1;
|
|
indexALE(cI2,1) = n; % Data
|
|
indexALE(cI2,2) = s; % sensore
|
|
cI2 = cI2+1;
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
if alert == 1 || alarm == 1
|
|
|
|
%% Controllo la data
|
|
if alarm == 1
|
|
if strcmp(IDcentralina,'ID0081') && strcmp(DTcatena,'DT0009') % Varazze - Casanova - Via Fossello
|
|
dateALA = ARRAYdate_LL(indexALA(:,1));
|
|
nodeALA = NodoLoadLink(indexALA(:,2),2);
|
|
valALA = round(DatiLoad(indexALA(:,1),indexALA(:,2)),2);
|
|
desc = 'kN';
|
|
sms = 1;
|
|
elseif strcmp(IDcentralina,'ID0082') && strcmp(DTcatena,'DT0010') % Riva di Tures
|
|
dateALA = ARRAYdate_CrL(indexALA(:,1));
|
|
nodeALA = NodoCrackLink(indexALA(:,2),2);
|
|
valALA = round(DatiCrack(indexALA(:,1),indexALA(:,2)),2);
|
|
desc = 'mm';
|
|
sms = 1;
|
|
end
|
|
DataALA = dateALA(end);
|
|
end
|
|
if alert == 1
|
|
if strcmp(IDcentralina,'ID0081') && strcmp(DTcatena,'DT0009') % Varazze - Casanova - Via Fossello
|
|
dateALE = ARRAYdate_LL(indexALE(:,1));
|
|
nodeALE = NodoLoadLink(indexALE(:,2),2);
|
|
valALE = round(DatiLoad(indexALA(:,1),indexALA(:,2)),2);
|
|
desc = 'kN';
|
|
sms = 0;
|
|
elseif strcmp(IDcentralina,'ID0082') && strcmp(DTcatena,'DT0010') % Riva di Tures
|
|
dateALE = ARRAYdate_CrL(indexALE(:,1));
|
|
nodeALE = NodoCrackLink(indexALE(:,2),2);
|
|
valALE = round(DatiCrack(indexALA(:,1),indexALA(:,2)),2);
|
|
desc = 'mm';
|
|
sms = 0;
|
|
end
|
|
DataALE = dateALE(end);
|
|
end
|
|
|
|
FileNameALA = ['' IDcentralina '-' DTcatena '-Alarm.txt'];
|
|
if isfile(FileNameALA) == 1
|
|
A = importdata(FileNameALA);
|
|
[rA,~] = size(A);
|
|
if rA == 0
|
|
DataRifALE = 0;
|
|
DataRifALA = 0;
|
|
elseif rA == 1
|
|
DataRifALA = A(1,1);
|
|
DataRifALE = 0;
|
|
else
|
|
DataRifALA = A(1,1);
|
|
DataRifALE = A(2,1);
|
|
end
|
|
else
|
|
DataRifALE = 0;
|
|
DataRifALA = 0;
|
|
end
|
|
DATA_rif = max(DataRifALA,DataRifALE); % Data della mail
|
|
|
|
if alert == 1 || alarm == 1
|
|
if alert == 1 && alarm == 1
|
|
DATA = max(DataALA,DataALE);
|
|
DataWriteALA = DataALA;
|
|
DataWriteALE = DataALE;
|
|
elseif alert == 1
|
|
DATA = DataALE;
|
|
DataWriteALE = DataALE;
|
|
DataWriteALA = DataRifALA;
|
|
elseif alarm == 1
|
|
DATA = DataALA;
|
|
DataWriteALA = DataALA;
|
|
DataWriteALE = DataRifALE;
|
|
end
|
|
|
|
if DATA_rif >= DATA % Mail già inviata
|
|
Mail = 0;
|
|
else
|
|
Mail = 1;
|
|
end
|
|
else
|
|
DataWriteALA = DataRifALA;
|
|
DataWriteALE = DataRifALE;
|
|
end
|
|
|
|
if Mail == 1
|
|
allarme = 0;
|
|
DATAinsert = cell(1,9);
|
|
AL = 1;
|
|
for ii = 1:cI-1 % numero di allarmi
|
|
if DATA_rif < dateALA(ii) % Allarme non ancora uscito
|
|
Data = datestr(dateALA(ii),'yyyy-mm-dd HH:MM:SS');
|
|
Valore = valALA(ii);
|
|
Nodo = cell2mat(nodeALA(ii));
|
|
Livello = 2;
|
|
DATAinsert{AL,1} = 3; % Soglie tradizionali
|
|
DATAinsert{AL,2} = IDcentralina;
|
|
DATAinsert{AL,3} = DTcatena;
|
|
DATAinsert{AL,4} = Nodo;
|
|
DATAinsert{AL,5} = Data;
|
|
DATAinsert{AL,6} = Valore;
|
|
DATAinsert{AL,7} = Livello;
|
|
DATAinsert{AL,8} = sms;
|
|
DATAinsert{AL,9} = desc;
|
|
allarme = 1;
|
|
AL = AL+1;
|
|
end
|
|
end
|
|
for ii = 1:cI2-1 % numero di allerte
|
|
if DATA_rif < dateALE(ii) % Allerta non ancora uscita
|
|
Data = datestr(dateALE(ii),'yyyy-mm-dd HH:MM:SS');
|
|
Valore = valALE(ii);
|
|
Nodo = cell2mat(nodeALE(ii));
|
|
Livello = 1;
|
|
DATAinsert{AL,1} = 3; % Soglie tradizionali
|
|
DATAinsert{AL,2} = IDcentralina;
|
|
DATAinsert{AL,3} = DTcatena;
|
|
DATAinsert{AL,4} = Nodo;
|
|
DATAinsert{AL,5} = Data;
|
|
DATAinsert{AL,6} = Valore;
|
|
DATAinsert{AL,7} = Livello;
|
|
DATAinsert{AL,8} = sms;
|
|
DATAinsert{AL,9} = desc;
|
|
allarme = 1;
|
|
AL = AL+1;
|
|
end
|
|
end
|
|
|
|
if allarme == 1
|
|
AL = AL-1; % Numero totale di allarmi
|
|
for a = 1:AL
|
|
% Cerco se il dato è già presente
|
|
Data = DATAinsert{a,5};
|
|
nNodo = num2str(DATAinsert{a,4}); % Numero nodo
|
|
type = num2str(DATAinsert{a,1}); % tipologia
|
|
level = num2str(DATAinsert{a,7}); % livello allarme
|
|
value = num2str(DATAinsert{a,6}); % Valore
|
|
desc = DATAinsert{a,9}; % Descrizione
|
|
comando = ['select id, type_id, date_time from alarms where unit_name = ''' ...
|
|
IDcentralina ''' and tool_name = ''' DTcatena ''' and node_num = ''' ...
|
|
nNodo ''' and date_time = ''' Data ''' and type_id = ''' type ...
|
|
''' and alarm_level = ''' level ''' and registered_value = ''' value ...
|
|
''' and description like ''' desc ''' order by date_time'];
|
|
curs = exec(conn,comando);
|
|
curs = fetch(curs);
|
|
idDate = curs.Data;
|
|
if idDate == 0 || strcmp(idDate,'No Data') == 1
|
|
idElabData = 0; % 0 indica che il dato non è presente su DB
|
|
else
|
|
idElabData = cell2mat(idDate(:,1));
|
|
end
|
|
tablename = 'alarms';
|
|
colnames = {'type_id','unit_name','tool_name','node_num','date_time',...
|
|
'registered_value','alarm_level','send_sms','description'};
|
|
data = [DATAinsert(a,1),DATAinsert(a,2),DATAinsert(a,3),DATAinsert(a,4),...
|
|
DATAinsert(a,5),DATAinsert(a,6),DATAinsert(a,7),DATAinsert(a,8),DATAinsert(a,9)];
|
|
if idElabData == 0 % Scrivo
|
|
fastinsert(conn,tablename,colnames,data);
|
|
end
|
|
end
|
|
end
|
|
|
|
fileID = fopen(FileNameALA,'a');
|
|
outdat = fopen(FileNameALA,'wt+');
|
|
fmt = '%.10f \r';
|
|
fprintf(fileID,fmt,DataWriteALA);
|
|
fmt = '%.10f \r';
|
|
fprintf(fileID,fmt,DataWriteALE);
|
|
fclose(fileID);
|
|
|
|
end
|
|
end
|
|
|
|
text = ('alert_Levels function worked correctly');
|
|
fileID = fopen(FileName,'a');
|
|
fmt = '%s \r';
|
|
fprintf(fileID,fmt,text);
|
|
fclose(fileID);
|
|
|
|
end
|