Sync from remote server: 2025-10-12 18:56:41
This commit is contained in:
476
Tilt/alert_Levels.m
Executable file
476
Tilt/alert_Levels.m
Executable file
@@ -0,0 +1,476 @@
|
||||
function alert_Levels(ARRAYdateRL,IDcentralina,DTcatena,conn,FileName)
|
||||
|
||||
fileID = fopen(FileName,'a');
|
||||
fmt = '%s \r';
|
||||
text = 'alert_Levels function started';
|
||||
fprintf(fileID,fmt,text);
|
||||
fclose(fileID);
|
||||
|
||||
alarm1h = 0;
|
||||
alarm24h = 0;
|
||||
alert1h = 0;
|
||||
alert24h = 0;
|
||||
alarm = 0;
|
||||
alert = 0;
|
||||
if strcmp(IDcentralina,'ID0102') && strcmp(DTcatena,'LOC0102') == 1 || strcmp(IDcentralina,'ID0153') && strcmp(DTcatena,'DT0050') == 1 % Pluviometri A27 Cave Est e Fadalto
|
||||
DataScarico = ARRAYdateRL(1)-1.1;
|
||||
% Scarico di dati di pioggia
|
||||
NodeType = 'Rain Link';
|
||||
comando = ['select EventDate, EventTime, Z, calcerr from ElabDataView where EventDate = ''' ...
|
||||
datestr(DataScarico,'yyyy-mm-dd') ''' and EventTime >= ''' datestr(DataScarico,'HH:MM:SS') ''' and UnitName = ''' ...
|
||||
IDcentralina ''' and ToolNameID = ''' DTcatena ''' and NodeType = ''' NodeType ''' '];
|
||||
curs = exec(conn,comando);
|
||||
curs = fetch(curs);
|
||||
DATnodoRLd = curs.Data;
|
||||
% Scarico di dati di pioggia
|
||||
comando = ['select EventDate, EventTime, Z, calcerr from ElabDataView where EventDate > ''' ...
|
||||
datestr(DataScarico,'yyyy-mm-dd') ''' and UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
|
||||
DTcatena ''' and NodeType = ''' NodeType ''' '];
|
||||
curs = exec(conn,comando);
|
||||
curs = fetch(curs);
|
||||
DATnodoRL = curs.Data;
|
||||
[~,c1] = size(DATnodoRL);
|
||||
[~,c2] = size(DATnodoRLd);
|
||||
if c1 == c2
|
||||
DATnodoRL = [DATnodoRLd; DATnodoRL];
|
||||
elseif c1 < c2 % solo giorno di riferimento
|
||||
DATnodoRL = DATnodoRLd;
|
||||
end
|
||||
% Modifico il formato di data e ora in DATini.
|
||||
[rD,cD] = size(DATnodoRL);
|
||||
if rD ~=1 && cD ~=1
|
||||
T = [cell2mat(DATnodoRL(:,1)) repmat(' ', [rD,1]) cell2mat(DATnodoRL(:,2))];
|
||||
T = datenum(T);
|
||||
% Converto da Cell Array a Matrice.
|
||||
ARRAYdateRL = T;
|
||||
end
|
||||
RainCum = cell2mat(DATnodoRL(:,3));
|
||||
ErrRainLink = cell2mat(DATnodoRL(:,4));
|
||||
|
||||
[numDate,~] = size(ARRAYdateRL);
|
||||
% 24H
|
||||
d = 1;
|
||||
p = 1;
|
||||
diffDate = 0;
|
||||
n = 1;
|
||||
period = 1; % calcolo giornaliero
|
||||
ContSUP = [];
|
||||
Pioggia24h = zeros(numDate,1);
|
||||
for dd = 1:numDate
|
||||
while diffDate < period
|
||||
d = d+1;
|
||||
if d > numDate % Se d supera le date disponibili, esco dal ciclo while
|
||||
break
|
||||
end
|
||||
diffDate = ARRAYdateRL(d) - ARRAYdateRL(p);
|
||||
end
|
||||
if d >numDate
|
||||
break
|
||||
end
|
||||
ContSUP(n,1) = d; %#ok<*AGROW> % Creo matrice indici dell'estremo superiore della differenza
|
||||
ContINF(n,1) = p; % Creo matrice indici dell'estremo inferiore della differenza
|
||||
p = p+1; % passo alla data di partenza successiva
|
||||
d = p; % resetto il conto di d
|
||||
n = n+1;
|
||||
diffDate = 0;
|
||||
end
|
||||
check = isempty(ContSUP);
|
||||
if check == 0
|
||||
[nDate,~] = size(ContSUP);
|
||||
for dd = 1:nDate
|
||||
Pioggia24h(ContSUP(dd,1),1) = RainCum(ContSUP(dd,1))-RainCum(ContINF(dd,1));
|
||||
end
|
||||
end
|
||||
% 1H
|
||||
d = 1;
|
||||
p = 1;
|
||||
diffDate = 0;
|
||||
n = 1;
|
||||
period = 1/24; % calcolo orario
|
||||
ContSUP = [];
|
||||
Pioggia1h = zeros(numDate,1);
|
||||
for dd = 1:numDate
|
||||
while diffDate < period
|
||||
d = d+1;
|
||||
if d > numDate % Se d supera le date disponibili, esco dal ciclo while
|
||||
break
|
||||
end
|
||||
diffDate = ARRAYdateRL(d) - ARRAYdateRL(p);
|
||||
end
|
||||
if d >numDate
|
||||
break
|
||||
end
|
||||
ContSUP(n,1) = d; %#ok<*AGROW> % Creo matrice indici dell'estremo superiore della differenza
|
||||
ContINF(n,1) = p; % Creo matrice indici dell'estremo inferiore della differenza
|
||||
p = p+1; % passo alla data di partenza successiva
|
||||
d = p; % resetto il conto di d
|
||||
n = n+1;
|
||||
diffDate = 0;
|
||||
end
|
||||
check = isempty(ContSUP);
|
||||
if check == 0
|
||||
[nDate,~] = size(ContSUP);
|
||||
for dd = 1:nDate
|
||||
Pioggia1h(ContSUP(dd,1),1) = RainCum(ContSUP(dd,1))-RainCum(ContINF(dd,1));
|
||||
end
|
||||
end
|
||||
% Allerta
|
||||
indexALE1h = zeros(1,2);
|
||||
indexALE24h = zeros(1,2);
|
||||
indexALA1h = zeros(1,2);
|
||||
indexALA24h = zeros(1,2);
|
||||
c1h_ALE = 1;
|
||||
c24h_ALE = 1;
|
||||
c1h_ALA = 1;
|
||||
c24h_ALA = 1;
|
||||
|
||||
% ---SOGLIE---
|
||||
if strcmp(IDcentralina,'ID0102') && strcmp(DTcatena,'LOC0102') == 1
|
||||
soglia1h_ALE = 25; % mm
|
||||
soglia24h_ALE = 90; % mm
|
||||
soglia1h_ALA = 40; % mm
|
||||
soglia24h_ALA = 120; % mm
|
||||
Search_ALE = 1;
|
||||
Search_ALA = 1;
|
||||
elseif strcmp(IDcentralina,'ID0153') && strcmp(DTcatena,'DT0050')
|
||||
soglia1h_ALE = 20; % mm
|
||||
soglia24h_ALE = 80; % mm
|
||||
soglia1h_ALA = 30; % mm
|
||||
soglia24h_ALA = 110; % mm
|
||||
Search_ALE = 1;
|
||||
Search_ALA = 1;
|
||||
end
|
||||
|
||||
for n = 1:numDate
|
||||
%% ALLARMI
|
||||
if Search_ALA == 1
|
||||
if Pioggia1h(n,1) > soglia1h_ALA % mm
|
||||
if ErrRainLink(n,1) == 0 || ErrRainLink(n,1) == 0.5 % Il nodo non è in errore
|
||||
alarm1h = 1;
|
||||
alarm = 1;
|
||||
indexALA1h(c1h_ALA,1) = n; % Data
|
||||
indexALA1h(c1h_ALA,2) = Pioggia1h(n,1); % valore
|
||||
c1h_ALA = c1h_ALA+1;
|
||||
end
|
||||
end
|
||||
if Pioggia24h(n,1) > soglia24h_ALA % mm
|
||||
if ErrRainLink(n,1) == 0 || ErrRainLink(n,1) == 0.5 % Il nodo non è in errore
|
||||
alarm24h = 1;
|
||||
alarm = 1;
|
||||
indexALA24h(c24h_ALA,1) = n; % Data
|
||||
indexALA24h(c24h_ALA,2) = Pioggia24h(n,1); % valore
|
||||
c24h_ALA = c24h_ALA+1;
|
||||
end
|
||||
end
|
||||
end
|
||||
%% ALLERTE
|
||||
if Search_ALE == 1
|
||||
% Oraria
|
||||
if Search_ALA == 1
|
||||
if Pioggia1h(n,1) > soglia1h_ALE && Pioggia1h(n,1) <= soglia1h_ALA % mm
|
||||
continua = 1;
|
||||
else
|
||||
continua = 0;
|
||||
end
|
||||
else
|
||||
if Pioggia1h(n,1) > soglia1h_ALE % mm
|
||||
continua = 1;
|
||||
else
|
||||
continua = 0;
|
||||
end
|
||||
end
|
||||
if continua == 1
|
||||
if ErrRainLink(n,1) == 0 || ErrRainLink(n,1) == 0.5 % Il nodo non è in errore
|
||||
alert1h = 1;
|
||||
alert = 1;
|
||||
indexALE1h(c1h_ALE,1) = n; % Data
|
||||
indexALE1h(c1h_ALE,2) = Pioggia1h(n,1); % valore
|
||||
c1h_ALE = c1h_ALE+1;
|
||||
end
|
||||
end
|
||||
|
||||
% Giornaliera
|
||||
if Search_ALA == 1
|
||||
if Pioggia24h(n,1) > soglia24h_ALE && Pioggia24h(n,1) <= soglia24h_ALA % mm
|
||||
continua = 1;
|
||||
else
|
||||
continua = 0;
|
||||
end
|
||||
else
|
||||
if Pioggia24h(n,1) > soglia24h_ALE % mm
|
||||
continua = 1;
|
||||
else
|
||||
continua = 0;
|
||||
end
|
||||
end
|
||||
if continua == 1
|
||||
if ErrRainLink(n,1) == 0 || ErrRainLink(n,1) == 0.5 % Il nodo non è in errore
|
||||
alert24h = 1;
|
||||
alert = 1;
|
||||
indexALE24h(c24h_ALE,1) = n; % Data
|
||||
indexALE24h(c24h_ALE,2) = Pioggia24h(n,1); % valore
|
||||
c24h_ALE = c24h_ALE+1;
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if alarm == 1 || alert == 1
|
||||
|
||||
%% Controllo la data
|
||||
if strcmp(IDcentralina,'ID0102') == 1 && strcmp(DTcatena,'LOC0102') == 1 || ...
|
||||
strcmp(IDcentralina,'ID0153') == 1 && strcmp(DTcatena,'DT0050') == 1 % Pluviometri A27 Cave Est e Fadalto
|
||||
if alarm1h == 1 && alert1h == 1
|
||||
date1h_ALA = ARRAYdateRL(indexALA1h(:,1));
|
||||
date1h_ALE = ARRAYdateRL(indexALE1h(:,1));
|
||||
elseif alarm1h == 1
|
||||
date1h_ALE = 0;
|
||||
date1h_ALA = ARRAYdateRL(indexALA1h(:,1));
|
||||
elseif alert1h == 1
|
||||
date1h_ALE = ARRAYdateRL(indexALE1h(:,1));
|
||||
date1h_ALA = 0;
|
||||
else
|
||||
date1h_ALA = 0;
|
||||
date1h_ALE = 0;
|
||||
end
|
||||
if alarm24h == 1 && alert24h == 1
|
||||
date24h_ALA = ARRAYdateRL(indexALA24h(:,1));
|
||||
date24h_ALE = ARRAYdateRL(indexALE24h(:,1));
|
||||
elseif alarm24h == 1
|
||||
date24h_ALA = ARRAYdateRL(indexALA24h(:,1));
|
||||
date24h_ALE = 0;
|
||||
elseif alert24h == 1
|
||||
date24h_ALA = 0;
|
||||
date24h_ALE = ARRAYdateRL(indexALE24h(:,1));
|
||||
else
|
||||
date24h_ALA = 0;
|
||||
date24h_ALE = 0;
|
||||
end
|
||||
end
|
||||
DataALE = max(date1h_ALE(end),date24h_ALE(end));
|
||||
DataALA = max(date1h_ALA(end),date24h_ALA(end));
|
||||
|
||||
FileNameALA = ['' IDcentralina '-' DTcatena '-Alarm.txt'];
|
||||
if isfile(FileNameALA) == 1
|
||||
A = importdata(FileNameALA);
|
||||
[rA,~] = size(A);
|
||||
if rA == 0
|
||||
DataRif_ALE = 0;
|
||||
DataRif_ALA = 0;
|
||||
elseif rA == 1
|
||||
DataRif_ALE = A(1,1);
|
||||
DataRif_ALA = 0;
|
||||
else
|
||||
DataRif_ALE = A(1,1);
|
||||
DataRif_ALA = A(2,1);
|
||||
end
|
||||
else
|
||||
DataRif_ALE = 0;
|
||||
DataRif_ALA = 0;
|
||||
end
|
||||
|
||||
if alert == 1 || alarm == 1
|
||||
if alert == 1 && alarm == 1
|
||||
DATA = max(DataALA,DataALE);
|
||||
DataWrite_ALA = DataALA;
|
||||
DataWrite_ALE = DataALE;
|
||||
elseif alert == 1
|
||||
DATA = DataALE;
|
||||
DataWrite_ALE = DataALE;
|
||||
DataWrite_ALA = DataRif_ALA;
|
||||
elseif alarm == 1
|
||||
DATA = DataALA;
|
||||
DataWrite_ALE = DataRif_ALE;
|
||||
DataWrite_ALA = DataALA;
|
||||
end
|
||||
|
||||
% Allerta
|
||||
if alert == 1
|
||||
if date1h_ALE(end) > DataRif_ALE+1
|
||||
mail1h_ALE = 1;
|
||||
else
|
||||
mail1h_ALE = 0;
|
||||
end
|
||||
if date24h_ALE(end) > DataRif_ALE+1
|
||||
mail24h_ALE = 1;
|
||||
else
|
||||
mail24h_ALE = 0;
|
||||
end
|
||||
else
|
||||
mail1h_ALE = 0;
|
||||
mail24h_ALE = 0;
|
||||
end
|
||||
% Allarme
|
||||
if alarm == 1
|
||||
if date1h_ALA(end) > DataRif_ALA+1
|
||||
mail1h_ALA = 1;
|
||||
else
|
||||
mail1h_ALA = 0;
|
||||
end
|
||||
if date24h_ALA(end) > DataRif_ALA+1
|
||||
mail24h_ALA = 1;
|
||||
else
|
||||
mail24h_ALA = 0;
|
||||
end
|
||||
else
|
||||
mail1h_ALA = 0;
|
||||
mail24h_ALA = 0;
|
||||
end
|
||||
else
|
||||
DataWrite_ALE = DataRif_ALE;
|
||||
DataWrite_ALA = DataRif_ALA;
|
||||
fileID = fopen(FileNameALA,'a');
|
||||
outdat = fopen(FileNameALA,'wt+');
|
||||
fmt = '%.10f \r';
|
||||
fprintf(fileID,fmt,DataWrite_ALE);
|
||||
fmt = '%.10f \r';
|
||||
fprintf(fileID,fmt,DataWrite_ALA);
|
||||
fclose(fileID);
|
||||
end
|
||||
|
||||
if mail24h_ALA == 1 || mail24h_ALE == 1 || mail1h_ALA == 1 || mail1h_ALE == 1
|
||||
allarme = 0;
|
||||
allerta = 0;
|
||||
AL = 1;
|
||||
DATAinsert = cell(1,9);
|
||||
if strcmp(IDcentralina,'ID0102') && strcmp(DTcatena,'LOC0102') % Pluviometro A27
|
||||
sms = 1;
|
||||
NodeNum = 2;
|
||||
elseif strcmp(IDcentralina,'ID0153') && strcmp(DTcatena,'DT0050') == 1
|
||||
NodeNum = 1;
|
||||
sms = 1;
|
||||
end
|
||||
%% ALLARMI
|
||||
for ii = 1:c1h_ALA-1 % numero di allarmi orari
|
||||
if mail1h_ALA == 1 && ARRAYdateRL(indexALA1h(ii,1)) > DataRif_ALA+1
|
||||
Level = 3;
|
||||
sms = 1;
|
||||
desc = 'Soglia oraria';
|
||||
Data = datestr(ARRAYdateRL(indexALA1h(ii,1),1),'yyyy-mm-dd HH:MM:SS');
|
||||
Valore = Pioggia1h(indexALA1h(ii,1));
|
||||
DATAinsert{AL,1} = 3; % Allarme tipologia Soglie tradizionali
|
||||
DATAinsert{AL,2} = IDcentralina;
|
||||
DATAinsert{AL,3} = DTcatena;
|
||||
DATAinsert{AL,4} = NodeNum;
|
||||
DATAinsert{AL,5} = Data;
|
||||
DATAinsert{AL,6} = Valore;
|
||||
DATAinsert{AL,7} = Level;
|
||||
DATAinsert{AL,8} = sms;
|
||||
DATAinsert{AL,9} = desc;
|
||||
allarme = 1;
|
||||
AL = AL+1;
|
||||
end
|
||||
end
|
||||
for ii = 1:c24h_ALA-1 % numero di allarmi giornalieri
|
||||
if mail24h_ALA == 1 && ARRAYdateRL(indexALA24h(ii,1)) > DataRif_ALA+1
|
||||
Level = 4;
|
||||
sms = 1;
|
||||
desc = 'Soglia giornaliera';
|
||||
Data = datestr(ARRAYdateRL(indexALA24h(ii,1),1),'yyyy-mm-dd HH:MM:SS');
|
||||
Valore = Pioggia24h(indexALA24h(ii,1));
|
||||
DATAinsert{AL,1} = 3; % Allarme tipologia Soglie tradizionali
|
||||
DATAinsert{AL,2} = IDcentralina;
|
||||
DATAinsert{AL,3} = DTcatena;
|
||||
DATAinsert{AL,4} = NodeNum;
|
||||
DATAinsert{AL,5} = Data;
|
||||
DATAinsert{AL,6} = Valore;
|
||||
DATAinsert{AL,7} = Level;
|
||||
DATAinsert{AL,8} = sms;
|
||||
DATAinsert{AL,9} = desc;
|
||||
allarme = 1;
|
||||
AL = AL+1;
|
||||
end
|
||||
end
|
||||
%% Allerte
|
||||
for ii = 1:c1h_ALE-1 % numero di allerte orarie
|
||||
if mail1h_ALE == 1 && ARRAYdateRL(indexALE1h(ii,1)) > DataRif_ALE+1
|
||||
Level = 1;
|
||||
desc = 'Soglia oraria';
|
||||
Data = datestr(ARRAYdateRL(indexALE1h(ii,1),1),'yyyy-mm-dd HH:MM:SS');
|
||||
Valore = Pioggia1h(indexALE1h(ii,1));
|
||||
DATAinsert{AL,1} = 3; % Allarme tipologia Soglie tradizionali
|
||||
DATAinsert{AL,2} = IDcentralina;
|
||||
DATAinsert{AL,3} = DTcatena;
|
||||
DATAinsert{AL,4} = NodeNum;
|
||||
DATAinsert{AL,5} = Data;
|
||||
DATAinsert{AL,6} = Valore;
|
||||
DATAinsert{AL,7} = Level;
|
||||
DATAinsert{AL,8} = sms;
|
||||
DATAinsert{AL,9} = desc;
|
||||
allerta = 1;
|
||||
AL = AL+1;
|
||||
end
|
||||
end
|
||||
for ii = 1:c24h_ALE-1 % numero di alerte giornaliere
|
||||
if mail24h_ALE == 1 && ARRAYdateRL(indexALE24h(ii,1)) > DataRif_ALE+1
|
||||
Level = 2;
|
||||
desc = 'Soglia giornaliera';
|
||||
Data = datestr(ARRAYdateRL(indexALE24h(ii,1),1),'yyyy-mm-dd HH:MM:SS');
|
||||
Valore = Pioggia24h(indexALE24h(ii,1));
|
||||
DATAinsert{AL,1} = 3; % Allarme tipologia Soglie tradizionali
|
||||
DATAinsert{AL,2} = IDcentralina;
|
||||
DATAinsert{AL,3} = DTcatena;
|
||||
DATAinsert{AL,4} = NodeNum;
|
||||
DATAinsert{AL,5} = Data;
|
||||
DATAinsert{AL,6} = Valore;
|
||||
DATAinsert{AL,7} = Level;
|
||||
DATAinsert{AL,8} = sms;
|
||||
DATAinsert{AL,9} = desc;
|
||||
allerta = 1;
|
||||
AL = AL+1;
|
||||
end
|
||||
end
|
||||
|
||||
if allarme == 1 || allerta == 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;
|
||||
[~,cI] = size(idDate);
|
||||
if cI == 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,DataWrite_ALE);
|
||||
fmt = '%.10f \r';
|
||||
fprintf(fileID,fmt,DataWrite_ALA);
|
||||
fclose(fileID);
|
||||
end
|
||||
end
|
||||
|
||||
text = ('alert_Levels function worked correctly');
|
||||
fileID = fopen(FileName,'a');
|
||||
fmt = '%s \r';
|
||||
fprintf(fileID,fmt,text);
|
||||
fclose(fileID);
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user