function SirenON = alert_Levels(IDcentralina,DTcatena,ARRAYdateTrL,ARRAYdateSS,...
    ARRAYdateRSN,datainiRSN,ARRAYdateRSNHR,ARRAYdateLL,ARRAYdateGF,ARRAYdateGS,SEL,MEL,...
    val_TrL,val_SS,LoadDef,DatiGFLink,DatiGSLink,rTrL,rRSN,rRSNHR,rLL,yesTrL,yesSS,...
    yesRSN,yesRSNHR,yesLL,yesGF,yesGS,NomeSito,date,conn,FileName)

fileID = fopen(FileName,'a');
fmt = '%s \r';
text = 'alert_Levels function started';
fprintf(fileID,fmt,text);
fclose(fileID);

% Scarico ID centralina
comando = ['select id from units where name = ''' IDcentralina ''' ']; 
curs = exec(conn,comando);
curs = fetch(curs);
unitID = num2str(cell2mat(curs.Data));

SirenON = 0;

% Creazione file di testo con data di riferimento per l'attivazione
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
        DataRifALE = A(1,1);
        DataRifALA = 0;
    else
        DataRifALE = A(1,1);
        DataRifALA = A(2,1);
    end
else
    DataRifALE = 0;
    DataRifALA = 0;
end
DATA_rif = max(DataRifALA,DataRifALE); % Data della mail

fileID = fopen(FileName,'a');
fmt = '%s \r';

%% Allertamento
% --- Trigger ---
Tr = 0;
[rT,cT] = size(val_TrL);
if yesTrL == 1
    nTr = 0;
    numTr1 = 1;
    numTr2 = 1;
    for t = 1:rT
        if val_TrL(t,1) == 1
            dataTrig(numTr1,1) = cellstr(datestr(ARRAYdateTrL(t),'yyyy/mm/dd HH:MM:SS'));
            if strcmp(DTcatena,'LOC0060') == 1 % Vobarno
                Tr = 1; % Vobarno
                nTr = 1; % Trigger 1
                text = 'Trigger detected at Vobarno';
                fprintf(fileID,fmt,text);
                fclose(fileID);
                % Rielaboro i Moduli RSN di riferimento di default
                % --- Modulo 1 ---
                DTmodulo1 = 'DT0001';
                rilancio = ['/usr/local/matlab_func/run_RSN_lnx.sh /usr/local/MATLAB/MATLAB_Runtime/v93 '...
                    IDcentralina ' '  DTmodulo1 ''];
                status = system(rilancio);
                fileID = fopen(FileName,'a');
                fmt = '%s \r';
                text = 'DT0001 BPM module re-elaborated correctly';
                fprintf(fileID,fmt,text);
                fprintf(fileID,fmt,status);
                fclose(fileID);
                % --- Modulo 2 ---
                DTmodulo2 = 'DT0002';
                rilancio = ['/usr/local/matlab_func/run_RSN_lnx.sh /usr/local/MATLAB/MATLAB_Runtime/v93 '...
                    IDcentralina ' '  DTmodulo2 ''];
                status = system(rilancio);
                fileID = fopen(FileName,'a');
                fmt = '%s \r';
                text = 'DT0002 BPM module re-elaborated correctly';
                fprintf(fileID,fmt,text);
                fprintf(fileID,fmt,status);
                fclose(fileID);
                % --- Modulo 3 ---
                DTmodulo3 = 'DT0003';
                rilancio = ['/usr/local/matlab_func/run_RSN_lnx.sh /usr/local/MATLAB/MATLAB_Runtime/v93 '...
                    IDcentralina ' '  DTmodulo3 ''];
                status = system(rilancio);
                fileID = fopen(FileName,'a');
                fmt = '%s \r';
                text = 'DT0003 BPM module re-elaborated correctly';
                fprintf(fileID,fmt,text);
                fprintf(fileID,fmt,status);
            elseif strcmp(DTcatena,'LOC0102') == 1 || strcmp(DTcatena,'LOC0103') == 1
                Tr = 2; % A27 - Trigger
                text = 'Trigger detected';
            elseif strcmp(IDcentralina,'ID0153') == 1
                Tr = 3; % A27 - Fadalto
                text = 'Trigger Nesa detected';
            end
            numTr1 = numTr1+1;
        end
        if cT > 1 && val_TrL(t,2) == 1
            dataTrig(numTr2,1) = cellstr(datestr(ARRAYdateTrL(t),'yyyy/mm/dd HH:MM:SS'));
            if nTr == 1
                nTr = 3; % Trigger 1 e 2 contemporaneamente
            else
                nTr = 2; % Trigger 2
            end
            if strcmp(DTcatena,'LOC0060') == 1 % Vobarno
                text = 'Trigger detected at Vobarno';
                fprintf(fileID,fmt,text);
                fclose(fileID);
                % Rielaboro i Moduli RSN di riferimento di default
                % --- Modulo 1 ---
                DTmodulo4 = 'DT0004';
                rilancio = ['/usr/local/matlab_func/run_RSN_lnx.sh /usr/local/MATLAB/MATLAB_Runtime/v93 '...
                    IDcentralina ' '  DTmodulo4 ''];
                status = system(rilancio);
                fileID = fopen(FileName,'a');
                fmt = '%s \r';
                text = 'DT0004 BPM module re-elaborated correctly';
                fprintf(fileID,fmt,text);
                fprintf(fileID,fmt,status);
                fclose(fileID);
                % --- Modulo 2 ---
                DTmodulo5 = 'DT0005';
                rilancio = ['/usr/local/matlab_func/run_RSN_lnx.sh /usr/local/MATLAB/MATLAB_Runtime/v93 '...
                    IDcentralina ' '  DTmodulo5 ''];
                status = system(rilancio);
                fileID = fopen(FileName,'a');
                fmt = '%s \r';
                text = 'DT0005 BPM module re-elaborated correctly';
                fprintf(fileID,fmt,text);
                fprintf(fileID,fmt,status);
            end
        end
    end
    if Tr ~= 0
        fprintf(fileID,fmt,text);
    end
end
% --- Shock Sensor ---
SS = 0; % Non è scattato nessuno Shock Sensor
if yesSS == 1
    DataSS = [];
    [rS,~] = size(val_SS);
    ss = 1;
    for s = 1:rS
        if val_SS(s,1) == 1
            SS = 1;
            DataSS(ss,1) = ARRAYdateSS(s,1);
            ss = ss+1;
        end
    end
end
% --- RSN Link ---
if yesRSN == 1
    if Tr == 1
        DataScarico = datestr(ARRAYdateTrL(end)-30,'yyyy/mm/dd'); % scarico a partire da un mese prima
    elseif SS == 1
        DataScarico = datestr(DataSS(end,1)-30,'yyyy/mm/dd'); % scarico a partire da un mese prima
    else
        DataScarico = datestr(ARRAYdateRSN(end)-30,'yyyy/mm/dd');
    end
    NodeType = 'RSN Link';
    if strcmp(IDcentralina,'ID0060') == 1 % Vobarno
        if nTr == 1 || nTr == 3
            % --- DT0001 ---
            DTcatena1 = 'DT0001';
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate >= ''' ...
                DataScarico ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena1 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSN = curs.Data;
            % Modifico il formato di data e ora in DATini.
            [rD,cD] = size(DATnodoRSN);
            if rD ~=1 && cD ~=1
                T = [cell2mat(DATnodoRSN(:,1)) repmat(' ', [rD,1]) cell2mat(DATnodoRSN(:,2))];
                T = datenum(T);
                % Converto da Cell Array a Matrice.
                DataRSN_Rif1 = T;
            end
            AlfaX_Rif1 = cell2mat(DATnodoRSN(:,3));
            ErrRSNLink_Rif1 = cell2mat(DATnodoRSN(:,4));
            % Primo valore elaborato
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate = ''' ...
                date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena1 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSN_0 = curs.Data;
            [rD,cD] = size(DATnodoRSN_0);
            if rD == 1 && cD == 1
                comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate > ''' ...
                    date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena1 ...
                    ''' and NodeType = ''' NodeType ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoRSN_0 = curs.Data;
                AlfaX_1 = cell2mat(DATnodoRSN_0(1,3));
            else
                AlfaX_1 = cell2mat(DATnodoRSN_0(1,3));
            end
            
            % --- DT0002 ---
            DTcatena2 = 'DT0002';
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate >= ''' ...
                DataScarico ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena2 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSN = curs.Data;
            % Modifico il formato di data e ora in DATini.
            [rD,cD] = size(DATnodoRSN);
            if rD ~=1 && cD ~=1
                T = [cell2mat(DATnodoRSN(:,1)) repmat(' ', [rD,1]) cell2mat(DATnodoRSN(:,2))];
                T = datenum(T);
                % Converto da Cell Array a Matrice.
                DataRSN_Rif2 = T;
            end
            AlfaX_Rif2 = cell2mat(DATnodoRSN(:,3));
            ErrRSNLink_Rif2 = cell2mat(DATnodoRSN(:,4));
            % Primo valore elaborato
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate = ''' ...
                date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena2 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSN_0 = curs.Data;
            [rD,cD] = size(DATnodoRSN_0);
            if rD == 1 && cD == 1
                comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate > ''' ...
                    date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena2 ...
                    ''' and NodeType = ''' NodeType ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoRSN_0 = curs.Data;
                AlfaX_2 = cell2mat(DATnodoRSN_0(1,3));
            else
                AlfaX_2 = cell2mat(DATnodoRSN_0(1,3));
            end
            
            % --- DT0003 ---
            DTcatena3 = 'DT0003';
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate >= ''' ...
                DataScarico ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena3 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSN = curs.Data;
            % Modifico il formato di data e ora in DATini.
            [rD,cD] = size(DATnodoRSN);
            if rD ~=1 && cD ~=1
                T = [cell2mat(DATnodoRSN(:,1)) repmat(' ', [rD,1]) cell2mat(DATnodoRSN(:,2))];
                T = datenum(T);
                % Converto da Cell Array a Matrice.
                DataRSN_Rif3 = T;
            end
            AlfaX_Rif3 = cell2mat(DATnodoRSN(:,3));
            ErrRSNLink_Rif3 = cell2mat(DATnodoRSN(:,4));
            % Primo valore elaborato
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate = ''' ...
                date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena3 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSN_0 = curs.Data;
            [rD,cD] = size(DATnodoRSN_0);
            if rD == 1 && cD == 1
                comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate > ''' ...
                    date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena3 ...
                    ''' and NodeType = ''' NodeType ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoRSN_0 = curs.Data;
                AlfaX_3 = cell2mat(DATnodoRSN_0(1,3));
            else
                AlfaX_3 = cell2mat(DATnodoRSN_0(1,3));
            end
        end
        
        if nTr == 2 || nTr == 3
            % --- DT0004 ---
            DTcatena4 = 'DT0004';
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate >= ''' ...
                DataScarico ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena4 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSN = curs.Data;
            % Modifico il formato di data e ora in DATini.
            [rD,cD] = size(DATnodoRSN);
            if rD ~=1 && cD ~=1
                T = [cell2mat(DATnodoRSN(:,1)) repmat(' ', [rD,1]) cell2mat(DATnodoRSN(:,2))];
                T = datenum(T);
                % Converto da Cell Array a Matrice.
                DataRSN_Rif4 = T;
            end
            AlfaX_Rif4 = cell2mat(DATnodoRSN(:,3));
            ErrRSNLink_Rif4 = cell2mat(DATnodoRSN(:,4));
            % Primo valore elaborato
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate = ''' ...
                date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena4 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSN_0 = curs.Data;
            [rD,cD] = size(DATnodoRSN_0);
            if rD == 1 && cD == 1
                comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate > ''' ...
                    date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena4 ...
                    ''' and NodeType = ''' NodeType ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoRSN_0 = curs.Data;
                AlfaX_4 = cell2mat(DATnodoRSN_0(1,3));
            else
                AlfaX_4 = cell2mat(DATnodoRSN_0(1,3));
            end
            
            % --- DT0005 ---
            DTcatena5 = 'DT0005';
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate >= ''' ...
                DataScarico ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena5 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSN = curs.Data;
            % Modifico il formato di data e ora in DATini.
            [rD,cD] = size(DATnodoRSN);
            if rD ~=1 && cD ~=1
                T = [cell2mat(DATnodoRSN(:,1)) repmat(' ', [rD,1]) cell2mat(DATnodoRSN(:,2))];
                T = datenum(T);
                % Converto da Cell Array a Matrice.
                DataRSN_Rif5 = T;
            end
            AlfaX_Rif5 = cell2mat(DATnodoRSN(:,3));
            ErrRSNLink_Rif5 = cell2mat(DATnodoRSN(:,4));
            % Primo valore elaborato
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate = ''' ...
                date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena5 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSN_0 = curs.Data;
            [rD,cD] = size(DATnodoRSN_0);
            if rD == 1 && cD == 1
                comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate > ''' ...
                    date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena5 ...
                    ''' and NodeType = ''' NodeType ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoRSN_0 = curs.Data;
                AlfaX_5 = cell2mat(DATnodoRSN_0(1,3));
            else
                AlfaX_5 = cell2mat(DATnodoRSN_0(1,3));
            end
        end
        text = 'Last month data of RSN Link sensors recovered for Vobarno site';
        fprintf(fileID,fmt,text);
    else
        comando = ['select EventDate, EventTime, AlfaX, AlfaY, calcerr from ElabDataView where EventDate >= ''' ...
            DataScarico ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
            ''' and NodeType = ''' NodeType ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATnodoRSN = curs.Data;
        % Modifico il formato di data e ora in DATini.
        [rD,cD] = size(DATnodoRSN);
        if rD ~=1 && cD ~=1
            T = [cell2mat(DATnodoRSN(:,1)) repmat(' ', [rD,1]) cell2mat(DATnodoRSN(:,2))];
            T = datenum(T);
            % Converto da Cell Array a Matrice.
            DataRSN_Rif = T;
        end
        AlfaX_Rif = cell2mat(DATnodoRSN(:,3));
        AlfaY_Rif = cell2mat(DATnodoRSN(:,4));
        ErrRSNLink_Rif = cell2mat(DATnodoRSN(:,5));
        % Primo valore elaborato
        comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate = ''' ...
            date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
            ''' and NodeType = ''' NodeType ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        DATnodoRSN_0 = curs.Data;
        [rD,cD] = size(DATnodoRSN_0);
        if rD == 1 && cD == 1
            comando = ['select EventDate, EventTime, AlfaX calcerr from ElabDataView where EventDate > ''' ...
                date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSN_0 = curs.Data;
            AlfaX_0 = cell2mat(DATnodoRSN_0(1,3));
        else
            AlfaX_0 = cell2mat(DATnodoRSN_0(1,3));
        end
        text = 'Last month data of RSN Link sensors recovered';
        fprintf(fileID,fmt,text);
    end
end

% --- RSN Link HR ---
if Tr == 1
    DataScarico = datestr(ARRAYdateTrL(end)-30,'yyyy/mm/dd'); % scarico a partire da un mese prima
    NodeType = 'RSN Link HR';
    if strcmp(IDcentralina,'ID0060') == 1 % Vobarno
        if nTr == 1 || nTr == 3
            % --- DT0001 ---
            DTcatena1 = 'DT0001';
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate >= ''' ...
                DataScarico ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena1 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSNHR = curs.Data;
            % Modifico il formato di data e ora in DATini.
            [rD,cD] = size(DATnodoRSNHR);
            if rD ~=1 && cD ~=1
                T = [cell2mat(DATnodoRSNHR(:,1)) repmat(' ', [rD,1]) cell2mat(DATnodoRSNHR(:,2))];
                T = datenum(T);
                % Converto da Cell Array a Matrice.
                DataRSNHR_Rif1 = T;
            end
            AlfaX_HR_Rif1 = cell2mat(DATnodoRSNHR(:,3));
            ErrRSNLink_HR_Rif1 = cell2mat(DATnodoRSNHR(:,4));
            % Primo valore elaborato
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate = ''' ...
                date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena1 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSNHR_0 = curs.Data;
            [rD,cD] = size(DATnodoRSNHR_0);
            if rD == 1 && cD == 1
                comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate > ''' ...
                    date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena1 ...
                    ''' and NodeType = ''' NodeType ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoRSNHR_0 = curs.Data;
                AlfaX_HR_1 = cell2mat(DATnodoRSNHR_0(1,3));
            else
                AlfaX_HR_1 = cell2mat(DATnodoRSNHR_0(1,3));
            end

            % --- DT0002 ---
            DTcatena2 = 'DT0002';
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate >= ''' ...
                DataScarico ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena2 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSNHR = curs.Data;
            % Modifico il formato di data e ora in DATini.
            [rD,cD] = size(DATnodoRSNHR);
            if rD ~=1 && cD ~=1
                T = [cell2mat(DATnodoRSNHR(:,1)) repmat(' ', [rD,1]) cell2mat(DATnodoRSNHR(:,2))];
                T = datenum(T);
                % Converto da Cell Array a Matrice.
                DataRSNHR_Rif2 = T;
            end
            AlfaX_HR_Rif2 = cell2mat(DATnodoRSNHR(:,3));
            ErrRSNLink_HR_Rif2 = cell2mat(DATnodoRSNHR(:,4));
            % Primo valore elaborato
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate = ''' ...
                date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena2 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSNHR_0 = curs.Data;
            [rD,cD] = size(DATnodoRSNHR_0);
            if rD == 1 && cD == 1
                comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate > ''' ...
                    date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena2 ...
                    ''' and NodeType = ''' NodeType ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoRSNHR_0 = curs.Data;
                AlfaX_HR_2 = cell2mat(DATnodoRSNHR_0(1,3));
            else
                AlfaX_HR_2 = cell2mat(DATnodoRSNHR_0(1,3));
            end

            % --- DT0003 ---
            DTcatena3 = 'DT0003';
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate >= ''' ...
                DataScarico ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena3 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSNHR = curs.Data;
            % Modifico il formato di data e ora in DATini.
            [rD,cD] = size(DATnodoRSNHR);
            if rD ~=1 && cD ~=1
                T = [cell2mat(DATnodoRSNHR(:,1)) repmat(' ', [rD,1]) cell2mat(DATnodoRSNHR(:,2))];
                T = datenum(T);
                % Converto da Cell Array a Matrice.
                DataRSNHR_Rif3 = T;
            end
            AlfaX_HR_Rif3 = cell2mat(DATnodoRSNHR(:,3));
            ErrRSNLink_HR_Rif3 = cell2mat(DATnodoRSNHR(:,4));
            % Primo valore elaborato
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate = ''' ...
                date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena3 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSNHR_0 = curs.Data;
            [rD,cD] = size(DATnodoRSNHR_0);
            if rD == 1 && cD == 1
                comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate > ''' ...
                    date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena3 ...
                    ''' and NodeType = ''' NodeType ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoRSNHR_0 = curs.Data;
                AlfaX_HR_3 = cell2mat(DATnodoRSNHR_0(1,3));
            else
                AlfaX_HR_3 = cell2mat(DATnodoRSNHR_0(1,3));
            end
        end

        if nTr == 2 || nTr == 3
            % --- DT0004 ---
            DTcatena4 = 'DT0004';
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate >= ''' ...
                DataScarico ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena4 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSNHR = curs.Data;
            % Modifico il formato di data e ora in DATini.
            [rD,cD] = size(DATnodoRSNHR);
            if rD ~=1 && cD ~=1
                T = [cell2mat(DATnodoRSNHR(:,1)) repmat(' ', [rD,1]) cell2mat(DATnodoRSNHR(:,2))];
                T = datenum(T);
                % Converto da Cell Array a Matrice.
                DataRSNHR_Rif4 = T;
            end
            AlfaX_HR_Rif4 = cell2mat(DATnodoRSNHR(:,3));
            ErrRSNLink_HR_Rif4 = cell2mat(DATnodoRSNHR(:,4));
            % Primo valore elaborato
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate = ''' ...
                date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena4 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSNHR_0 = curs.Data;
            [rD,cD] = size(DATnodoRSNHR_0);
            if rD == 1 && cD == 1
                comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate > ''' ...
                    date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena4 ...
                    ''' and NodeType = ''' NodeType ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoRSNHR_0 = curs.Data;
                AlfaX_HR_4 = cell2mat(DATnodoRSNHR_0(1,3));
            else
                AlfaX_HR_4 = cell2mat(DATnodoRSNHR_0(1,3));
            end

            % --- DT0005 ---
            DTcatena5 = 'DT0005';
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate >= ''' ...
                DataScarico ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena5 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSNHR = curs.Data;
            % Modifico il formato di data e ora in DATini.
            [rD,cD] = size(DATnodoRSNHR);
            if rD ~=1 && cD ~=1
                T = [cell2mat(DATnodoRSNHR(:,1)) repmat(' ', [rD,1]) cell2mat(DATnodoRSNHR(:,2))];
                T = datenum(T);
                % Converto da Cell Array a Matrice.
                DataRSNHR_Rif5 = T;
            end
            AlfaX_HR_Rif5 = cell2mat(DATnodoRSNHR(:,3));
            ErrRSNLink_HR_Rif5 = cell2mat(DATnodoRSNHR(:,4));
            % Primo valore elaborato
            comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate = ''' ...
                date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena5 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoRSNHR_0 = curs.Data;
            [rD,cD] = size(DATnodoRSNHR_0);
            if rD == 1 && cD == 1
                comando = ['select EventDate, EventTime, AlfaX, calcerr from ElabDataView where EventDate > ''' ...
                    date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena5 ...
                    ''' and NodeType = ''' NodeType ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoRSNHR_0 = curs.Data;
                AlfaX_HR_5 = cell2mat(DATnodoRSNHR_0(1,3));
            else
                AlfaX_HR_5 = cell2mat(DATnodoRSNHR_0(1,3));
            end
        end
        text = 'Last month data of RSN HR Link sensors recovered for Vobarno site';
        fprintf(fileID,fmt,text);
    end
end

% --- Load Link ---
if Tr == 1
    DataScarico = datestr(ARRAYdateTrL(end)-30,'yyyy/mm/dd'); % scarico a partire da un mese prima
    NodeType = 'Load Link';
    if strcmp(IDcentralina,'ID0060') == 1 % Vobarno
        if nTr == 1 || nTr == 3
            % --- DT0001 ---
            DTcatena1 = 'DT0001';
            comando = ['select EventDate, EventTime, load_value, calcerr from ElabDataView where EventDate >= ''' ...
                DataScarico ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena1 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoLL = curs.Data;
            % Modifico il formato di data e ora in DATini.
            [rD,cD] = size(DATnodoLL);
            if rD ~=1 && cD ~=1
                T = [cell2mat(DATnodoLL(:,1)) repmat(' ', [rD,1]) cell2mat(DATnodoLL(:,2))];
                T = datenum(T);
                % Converto da Cell Array a Matrice.
                DataLL_Rif1 = T;
            end
            Load_Rif1 = cell2mat(DATnodoLL(:,3));
            ErrLoadLink_Rif1 = cell2mat(DATnodoLL(:,4));
            % Primo valore elaborato
            comando = ['select EventDate, EventTime, load_value, calcerr from ElabDataView where EventDate = ''' ...
                date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena1 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoLL_0 = curs.Data;
            [rD,cD] = size(DATnodoLL_0);
            if rD == 1 && cD == 1
                comando = ['select EventDate, EventTime, load_value, calcerr from ElabDataView where EventDate > ''' ...
                    date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena1 ...
                    ''' and NodeType = ''' NodeType ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoLL_0 = curs.Data;
                Load_1 = cell2mat(DATnodoLL_0(1,3));
            else
                Load_1 = cell2mat(DATnodoLL_0(1,3));
            end

            % --- DT0002 ---
            DTcatena2 = 'DT0002';
            comando = ['select EventDate, EventTime, load_value, calcerr from ElabDataView where EventDate >= ''' ...
                DataScarico ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena2 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoLL = curs.Data;
            % Modifico il formato di data e ora in DATini.
            [rD,cD] = size(DATnodoLL);
            if rD ~=1 && cD ~=1
                T = [cell2mat(DATnodoLL(:,1)) repmat(' ', [rD,1]) cell2mat(DATnodoLL(:,2))];
                T = datenum(T);
                % Converto da Cell Array a Matrice.
                DataLL_Rif2 = T;
            end
            Load_Rif2 = cell2mat(DATnodoLL(:,3));
            ErrLoadLink_Rif2 = cell2mat(DATnodoLL(:,4));
            % Primo valore elaborato
            comando = ['select EventDate, EventTime, load_value, calcerr from ElabDataView where EventDate = ''' ...
                date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena2 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoLL_0 = curs.Data;
            [rD,cD] = size(DATnodoLL_0);
            if rD == 1 && cD == 1
                comando = ['select EventDate, EventTime, load_value, calcerr from ElabDataView where EventDate > ''' ...
                    date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena2 ...
                    ''' and NodeType = ''' NodeType ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoLL_0 = curs.Data;
                Load_2 = cell2mat(DATnodoLL_0(1,3));
            else
                Load_2 = cell2mat(DATnodoLL_0(1,3));
            end

            % --- DT0003 ---
            DTcatena3 = 'DT0003';
            comando = ['select EventDate, EventTime, load_value, calcerr from ElabDataView where EventDate >= ''' ...
                DataScarico ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena3 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoLL = curs.Data;
            % Modifico il formato di data e ora in DATini.
            [rD,cD] = size(DATnodoLL);
            if rD ~=1 && cD ~=1
                T = [cell2mat(DATnodoLL(:,1)) repmat(' ', [rD,1]) cell2mat(DATnodoLL(:,2))];
                T = datenum(T);
                % Converto da Cell Array a Matrice.
                DataLL_Rif3 = T;
            end
            Load_Rif3 = cell2mat(DATnodoLL(:,3));
            ErrLoadLink_Rif3 = cell2mat(DATnodoLL(:,4));
            % Primo valore elaborato
            comando = ['select EventDate, EventTime, load_value, calcerr from ElabDataView where EventDate = ''' ...
                date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena3 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoLL_0 = curs.Data;
            [rD,cD] = size(DATnodoLL_0);
            if rD == 1 && cD == 1
                comando = ['select EventDate, EventTime, load_value, calcerr from ElabDataView where EventDate > ''' ...
                    date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena3 ...
                    ''' and NodeType = ''' NodeType ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoLL_0 = curs.Data;
                Load_3 = cell2mat(DATnodoLL_0(1,3));
            else
                Load_3 = cell2mat(DATnodoLL_0(1,3));
            end
        end

        if nTr == 2 || nTr == 3
            % --- DT0004 ---
            DTcatena4 = 'DT0004';
            comando = ['select EventDate, EventTime, load_value, calcerr from ElabDataView where EventDate >= ''' ...
                DataScarico ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena4 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoLL = curs.Data;
            % Modifico il formato di data e ora in DATini.
            [rD,cD] = size(DATnodoLL);
            if rD ~=1 && cD ~=1
                T = [cell2mat(DATnodoLL(:,1)) repmat(' ', [rD,1]) cell2mat(DATnodoLL(:,2))];
                T = datenum(T);
                % Converto da Cell Array a Matrice.
                DataLL_Rif4 = T;
            end
            Load_Rif4 = cell2mat(DATnodoLL(:,3));
            ErrLoadLink_Rif4 = cell2mat(DATnodoLL(:,4));
            % Primo valore elaborato
            comando = ['select EventDate, EventTime, load_value, calcerr from ElabDataView where EventDate = ''' ...
                date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena4 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoLL_0 = curs.Data;
            [rD,cD] = size(DATnodoLL_0);
            if rD == 1 && cD == 1
                comando = ['select EventDate, EventTime, load_value, calcerr from ElabDataView where EventDate > ''' ...
                    date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena4 ...
                    ''' and NodeType = ''' NodeType ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoLL_0 = curs.Data;
                Load_4 = cell2mat(DATnodoLL_0(1,3));
            else
                Load_4 = cell2mat(DATnodoLL_0(1,3));
            end

            % --- DT0005 ---
            DTcatena5 = 'DT0005';
            comando = ['select EventDate, EventTime, load_value, calcerr from ElabDataView where EventDate >= ''' ...
                DataScarico ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena5 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoLL = curs.Data;
            % Modifico il formato di data e ora in DATini.
            [rD,cD] = size(DATnodoLL);
            if rD ~=1 && cD ~=1
                T = [cell2mat(DATnodoLL(:,1)) repmat(' ', [rD,1]) cell2mat(DATnodoLL(:,2))];
                T = datenum(T);
                % Converto da Cell Array a Matrice.
                DataLL_Rif5 = T;
            end
            Load_Rif5 = cell2mat(DATnodoLL(:,3));
            ErrLoadLink_Rif5 = cell2mat(DATnodoLL(:,4));
            % Primo valore elaborato
            comando = ['select EventDate, EventTime, load_value, calcerr from ElabDataView where EventDate = ''' ...
                date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena5 ...
                ''' and NodeType = ''' NodeType ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            DATnodoLL_0 = curs.Data;
            [rD,cD] = size(DATnodoLL_0);
            if rD == 1 && cD == 1
                comando = ['select EventDate, EventTime, load_value, calcerr from ElabDataView where EventDate > ''' ...
                    date ''' and  UnitName = ''' IDcentralina ''' and ToolNameID = ''' DTcatena5 ...
                    ''' and NodeType = ''' NodeType ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                DATnodoLL_0 = curs.Data;
                Load_5 = cell2mat(DATnodoLL_0(1,3));
            else
                Load_5 = cell2mat(DATnodoLL_0(1,3));
            end
        end
        text = 'Last month data of Load Link sensors recovered for Vobarno site';
        fprintf(fileID,fmt,text);
    end
end

%% ---SOGLIE---
% Allerta
alarm = 0;
alert = 0;
AL = 1;
% --- Trigger ---
if yesTrL == 1
    if Tr == 1 % Vobarno
        if ARRAYdateTrL(end) > DATA_rif
            DataRifALE = datestr(ARRAYdateTrL(end),'yyyy/mm/dd HH:MM:SS'); % data attivazione allerta
            % Codice
            if nTr == 1 || nTr == 3
                text = 'Inizio confronto soglie relative per il sito di Vobarno';
                fprintf(fileID,fmt,text);
                % --- CALCOLO SOGLIE RELATIVE ---
                % --- Modulo RSN 1 ---
                % MEMS
                indexRSN_1 = find(DataRSN_Rif1(:,1)<=datenum(dataTrig)); % Dati precedenti al trigger
                TrigRSN_1 = AlfaX_Rif1(indexRSN_1(end),1)-AlfaX_Rif1(indexRSN_1(end)-1,1);
                TrendRSN_1 = AlfaX_Rif1(indexRSN_1(1:end-1),1);
                if abs(TrigRSN_1) > 3*std(abs(diff(TrendRSN_1)))
                    RSN_1 = 1;
                elseif ErrRSNLink_Rif1(indexRSN_1(end),1) == 1 && ErrRSNLink_Rif1(indexRSN_1(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                    RSN_1 = 0.5;
                else
                    RSN_1 = 0;
                end
                % Ampolla
                indexRSNHR_1 = find(DataRSNHR_Rif1(:,1)<=datenum(dataTrig)); % Dati precedenti al trigger
                TrigRSNHR_1 = AlfaX_HR_Rif1(indexRSNHR_1(end),1)-AlfaX_HR_Rif1(indexRSNHR_1(end)-1,1);
                TrendRSNHR_1 = AlfaX_HR_Rif1(indexRSNHR_1(1:end-1),1);
                if abs(TrigRSNHR_1) > 3*std(abs(diff(TrendRSNHR_1)))
                    RSNHR_1 = 1;
                elseif ErrRSNLink_HR_Rif1(indexRSNHR_1(end),1) == 1 && ErrRSNLink_HR_Rif1(indexRSNHR_1(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                    RSNHR_1 = 0.5;
                else
                    RSNHR_1 = 0;
                end
                % Cella di carico
                indexLL_1 = find(DataLL_Rif1(:,1)<=datenum(dataTrig)); % Dati precedenti al trigger
                TrigLL_1 = Load_Rif1(indexLL_1(end),1)-Load_Rif1(indexLL_1(end)-1,1);
                TrendLL_1 = Load_Rif1(indexLL_1(1:end-1),1);
                if abs(TrigLL_1) > 3*std(abs(diff(TrendLL_1)))
                    LL_1 = 1;
                elseif ErrLoadLink_Rif1(indexLL_1(end),1) == 1 && ErrLoadLink_Rif1(indexLL_1(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                    LL_1 = 0.5;
                else
                    LL_1 = 0;
                end
                Somma_1 = RSN_1+RSNHR_1+LL_1;
                if Somma_1 >= 2
                    Level_1 = 2;
                else
                    Level_1 = 1;
                end
                
                % --- Modulo RSN 2 ---
                % MEMS
                indexRSN_2 = find(DataRSN_Rif2(:,1)<=datenum(dataTrig)); % Dati precedenti al trigger
                TrigRSN_2 = AlfaX_Rif2(indexRSN_2(end),1)-AlfaX_Rif2(indexRSN_2(end)-1,1);
                TrendRSN_2 = AlfaX_Rif2(indexRSN_2(1:end-1),1);
                if abs(TrigRSN_2) > 3*std(abs(diff(TrendRSN_2)))
                    RSN_2 = 1;
                elseif ErrRSNLink_Rif2(indexRSN_2(end),1) == 1 && ErrRSNLink_Rif2(indexRSN_2(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                    RSN_2 = 0.5;
                else
                    RSN_2 = 0;
                end
                % Ampolla
                indexRSNHR_2 = find(DataRSNHR_Rif2(:,1)<=datenum(dataTrig)); % Dati precedenti al trigger
                TrigRSNHR_2 = AlfaX_HR_Rif2(indexRSNHR_2(end),1)-AlfaX_HR_Rif2(indexRSNHR_2(end)-1,1);
                TrendRSNHR_2 = AlfaX_HR_Rif2(indexRSNHR_2(1:end-1),1);
                if abs(TrigRSNHR_2) > 3*std(abs(diff(TrendRSNHR_2)))
                    RSNHR_2 = 1;
                elseif ErrRSNLink_HR_Rif2(indexRSNHR_2(end),1) == 1 && ErrRSNLink_HR_Rif2(indexRSNHR_2(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                    RSNHR_2 = 0.5;
                else
                    RSNHR_2 = 0;
                end
                % Cella di carico
                indexLL_2 = find(DataLL_Rif2(:,1)<=datenum(dataTrig)); % Dati precedenti al trigger
                TrigLL_2 = Load_Rif2(indexLL_2(end),1)-Load_Rif2(indexLL_2(end)-1,1);
                TrendLL_2 = Load_Rif2(indexLL_2(1:end-1),1);
                if abs(TrigLL_2) > 3*std(abs(diff(TrendLL_2)))
                    LL_2 = 1;
                elseif ErrLoadLink_Rif2(indexLL_2(end),1) == 1 && ErrLoadLink_Rif2(indexLL_2(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                    LL_2 = 0.5;
                else
                    LL_2 = 0;
                end
                Somma_2 = RSN_2+RSNHR_2+LL_2;
                if Somma_2 >= 2
                    Level_2 = 2;
                else
                    Level_2 = 1;
                end
                
                % --- Modulo RSN 3 ---
                % MEMS
                indexRSN_3 = find(DataRSN_Rif3(:,1)<=datenum(dataTrig)); % Dati precedenti al trigger
                TrigRSN_3 = AlfaX_Rif3(indexRSN_3(end),1)-AlfaX_Rif3(indexRSN_3(end)-1,1);
                TrendRSN_3 = AlfaX_Rif3(indexRSN_3(1:end-1),1);
                if abs(TrigRSN_3) > 3*std(abs(diff(TrendRSN_3)))
                    RSN_3 = 1;
                elseif ErrRSNLink_Rif3(indexRSN_3(end),1) == 1 && ErrRSNLink_Rif3(indexRSN_3(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                    RSN_3 = 0.5;
                else
                    RSN_3 = 0;
                end
                % Ampolla
                indexRSNHR_3 = find(DataRSNHR_Rif3(:,1)<=datenum(dataTrig)); % Dati precedenti al trigger
                TrigRSNHR_3 = AlfaX_HR_Rif3(indexRSNHR_3(end),1)-AlfaX_HR_Rif3(indexRSNHR_3(end)-1,1);
                TrendRSNHR_3 = AlfaX_HR_Rif3(indexRSNHR_3(1:end-1),1);
                if abs(TrigRSNHR_3) > 3*std(abs(diff(TrendRSNHR_3)))
                    RSNHR_3 = 1;
                elseif ErrRSNLink_HR_Rif3(indexRSNHR_3(end),1) == 1 && ErrRSNLink_HR_Rif3(indexRSNHR_3(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                    RSNHR_3 = 0.5;
                else
                    RSNHR_3 = 0;
                end
                % Cella di carico
                indexLL_3 = find(DataLL_Rif3(:,1)<=datenum(dataTrig)); % Dati precedenti al trigger
                TrigLL_3 = Load_Rif3(indexLL_3(end),1)-Load_Rif3(indexLL_3(end)-1,1);
                TrendLL_3 = Load_Rif3(indexLL_3(1:end-1),1);
                if abs(TrigLL_3) > 3*std(abs(diff(TrendLL_3)))
                    LL_3 = 1;
                elseif ErrLoadLink_Rif3(indexLL_3(end),1) == 1 && ErrLoadLink_Rif3(indexLL_3(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                    LL_3 = 0.5;
                else
                    LL_3 = 0;
                end
                Somma_3 = RSN_3+RSNHR_3+LL_3;
                if Somma_3 >= 2
                    Level_3 = 2;
                else
                    Level_3 = 1;
                end
                
                % --- CALCOLO SOGLIE ASSOLUTE ---
                text = 'Inizio confronto soglie assolute per il sito di Vobarno';
                fprintf(fileID,fmt,text);
                % --- Modulo RSN 1 ---
                if Level_1 == 2
                    % MEMS
                    if AlfaX_Rif1(indexRSN_1(end),1)-AlfaX_1 > 5 % Gradi
                        RSN_1 = 1;
                    elseif ErrRSNLink_Rif1(indexRSN_1(end),1) == 1 && ErrRSNLink_Rif1(indexRSN_1(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                        RSN_1 = 0.5;
                    else
                        RSN_1 = 0;
                    end
                    % Ampolla
                    if AlfaX_HR_Rif1(indexRSNHR_1(end),1)-AlfaX_HR_1 > 5 % Gradi
                        RSNHR_1 = 1;
                    elseif ErrRSNLink_HR_Rif1(indexRSNHR_1(end),1) == 1 && ErrRSNLink_HR_Rif1(indexRSNHR_1(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                        RSNHR_1 = 0.5;
                    else
                        RSNHR_1 = 0;
                    end
                    % Cella di carico
                    if Load_Rif1(indexLL_1(end),1)-Load_1 > 65 % kN
                        LL_1 = 1;
                    elseif ErrLoadLink_Rif1(indexLL_1(end),1) == 1 && ErrLoadLink_Rif1(indexLL_1(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                        LL_1 = 0.5;
                    else
                        LL_1 = 0;
                    end
                    Somma_1_A = RSN_1+RSNHR_1+LL_1;
                    if Somma_1_A >= 2
                        Level_1 = 3;
                    end
                end
                
                % --- Modulo RSN 2 ---
                if Level_2 == 2
                    % MEMS
                    if AlfaX_Rif2(indexRSN_2(end),1)-AlfaX_2 > 5 % Gradi
                        RSN_2 = 1;
                    elseif ErrRSNLink_Rif2(indexRSN_2(end),1) == 1 && ErrRSNLink_Rif2(indexRSN_2(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                        RSN_2 = 0.5;
                    else
                        RSN_2 = 0;
                    end
                    % Ampolla
                    if AlfaX_HR_Rif2(indexRSNHR_2(end),1)-AlfaX_HR_2 > 5 % Gradi
                        RSNHR_2 = 1;
                    elseif ErrRSNLink_HR_Rif2(indexRSNHR_2(end),1) == 1 && ErrRSNLink_HR_Rif2(indexRSNHR_2(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                        RSNHR_2 = 0.5;
                    else
                        RSNHR_2 = 0;
                    end
                    % Cella di carico
                    if Load_Rif2(indexLL_2(end),1)-Load_2 > 65 % kN
                        LL_2 = 1;
                    elseif ErrLoadLink_Rif2(indexLL_2(end),1) == 1 && ErrLoadLink_Rif2(indexLL_2(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                        LL_2 = 0.5;
                    else
                        LL_2 = 0;
                    end
                    Somma_2_A = RSN_2+RSNHR_2+LL_2;
                    if Somma_2_A >= 2
                        Level_2 = 3;
                    end
                end
                
                % --- Modulo RSN 3 ---
                if Level_3 == 2
                    % MEMS
                    if AlfaX_Rif3(indexRSN_3(end),1)-AlfaX_3 > 5 % Gradi
                        RSN_3 = 1;
                    elseif ErrRSNLink_Rif3(indexRSN_3(end),1) == 1 && ErrRSNLink_Rif3(indexRSN_3(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                        RSN_3 = 0.5;
                    else
                        RSN_3 = 0;
                    end
                    % Ampolla
                    if AlfaX_HR_Rif3(indexRSNHR_3(end),1)-AlfaX_HR_3 > 5 % Gradi
                        RSNHR_3 = 1;
                    elseif ErrRSNLink_HR_Rif3(indexRSNHR_3(end),1) == 1 && ErrRSNLink_HR_Rif3(indexRSNHR_3(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                        RSNHR_3 = 0.5;
                    else
                        RSNHR_3 = 0;
                    end
                    % Cella di carico
                    if Load_Rif3(indexLL_3(end),1)-Load_3 > 65 % kN
                        LL_3 = 1;
                    elseif ErrLoadLink_Rif3(indexLL_3(end),1) == 1 && ErrLoadLink_Rif3(indexLL_3(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                        LL_3 = 0.5;
                    else
                        LL_3 = 0;
                    end
                    Somma_3_A = RSN_3+RSNHR_3+LL_3;
                    if Somma_3_A >= 2
                        Level_3 = 3;
                    end
                end
            end
            
            if nTr == 2 || nTr == 3
                text = 'Inizio confronto soglie relative per il sito di Vobarno';
                fprintf(fileID,fmt,text);
                % --- CALCOLO SOGLIE RELATIVE ---
                % --- Modulo RSN 4 ---
                % MEMS
                indexRSN_4 = find(DataRSN_Rif4(:,1)<=datenum(dataTrig)); % Dati precedenti al trigger
                TrigRSN_4 = AlfaX_Rif4(indexRSN_4(end),1)-AlfaX_Rif4(indexRSN_4(end)-1,1);
                TrendRSN_4 = AlfaX_Rif4(indexRSN_4(1:end-1),1);
                if abs(TrigRSN_4) > 3*std(abs(diff(TrendRSN_4)))
                    RSN_4 = 1;
                elseif ErrRSNLink_Rif4(indexRSN_4(end),1) == 1 && ErrRSNLink_Rif4(indexRSN_4(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                    RSN_4 = 0.5;
                else
                    RSN_4 = 0;
                end
                % Ampolla
                indexRSNHR_4 = find(DataRSNHR_Rif4(:,1)<=datenum(dataTrig)); % Dati precedenti al trigger
                TrigRSNHR_4 = AlfaX_HR_Rif4(indexRSNHR_4(end),1)-AlfaX_HR_Rif4(indexRSNHR_4(end)-1,1);
                TrendRSNHR_4 = AlfaX_HR_Rif4(indexRSNHR_4(1:end-1),1);
                if abs(TrigRSNHR_4) > 3*std(abs(diff(TrendRSNHR_4)))
                    RSNHR_4 = 1;
                elseif ErrRSNLink_HR_Rif4(indexRSNHR_4(end),1) == 1 && ErrRSNLink_HR_Rif4(indexRSNHR_4(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                    RSNHR_4 = 0.5;
                else
                    RSNHR_4 = 0;
                end
                % Cella di carico
                indexLL_4 = find(DataLL_Rif4(:,1)<=datenum(dataTrig)); % Dati precedenti al trigger
                TrigLL_4 = Load_Rif4(indexLL_4(end),1)-Load_Rif4(indexLL_4(end)-1,1);
                TrendLL_4 = Load_Rif4(indexLL_4(1:end-1),1);
                if abs(TrigLL_4) > 3*std(abs(diff(TrendLL_4)))
                    LL_4 = 1;
                elseif ErrLoadLink_Rif4(indexLL_4(end),1) == 1 && ErrLoadLink_Rif4(indexLL_4(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                    LL_4 = 0.5;
                else
                    LL_4 = 0;
                end
                Somma_4 = RSN_4+RSNHR_4+LL_4;
                if Somma_4 >= 2
                    Level_4 = 2;
                else
                    Level_4 = 1;
                end
                
                % --- Modulo RSN 5 ---
                % MEMS
                indexRSN_5 = find(DataRSN_Rif5(:,1)<=datenum(dataTrig)); % Dati precedenti al trigger
                TrigRSN_5 = AlfaX_Rif5(indexRSN_5(end),1)-AlfaX_Rif5(indexRSN_5(end)-1,1);
                TrendRSN_5 = AlfaX_Rif5(indexRSN_5(1:end-1),1);
                if abs(TrigRSN_5) > 3*std(abs(diff(TrendRSN_5)))
                    RSN_5 = 1;
                elseif ErrRSNLink_Rif5(indexRSN_5(end),1) == 1 && ErrRSNLink_Rif5(indexRSN_5(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                    RSN_5 = 0.5;
                else
                    RSN_5 = 0;
                end
                % Ampolla
                indexRSNHR_5 = find(DataRSNHR_Rif5(:,1)<=datenum(dataTrig)); % Dati precedenti al trigger
                TrigRSNHR_5 = AlfaX_HR_Rif5(indexRSNHR_5(end),1)-AlfaX_HR_Rif5(indexRSNHR_5(end)-1,1);
                TrendRSNHR_5 = AlfaX_HR_Rif5(indexRSNHR_5(1:end-1),1);
                if abs(TrigRSNHR_5) > 3*std(abs(diff(TrendRSNHR_5)))
                    RSNHR_5 = 1;
                elseif ErrRSNLink_HR_Rif5(indexRSNHR_5(end),1) == 1 && ErrRSNLink_HR_Rif5(indexRSNHR_5(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                    RSNHR_5 = 0.5;
                else
                    RSNHR_5 = 0;
                end
                % Cella di carico
                indexLL_5 = find(DataLL_Rif5(:,1)<=datenum(dataTrig)); % Dati precedenti al trigger
                TrigLL_5 = Load_Rif5(indexLL_5(end),1)-Load_Rif5(indexLL_5(end)-1,1);
                TrendLL_5 = Load_Rif5(indexLL_5(1:end-1),1);
                if abs(TrigLL_5) > 3*std(abs(diff(TrendLL_5)))
                    LL_5 = 1;
                elseif ErrLoadLink_Rif5(indexLL_5(end),1) == 1 && ErrLoadLink_Rif5(indexLL_5(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                    LL_5 = 0.5;
                else
                    LL_5 = 0;
                end
                Somma_5 = RSN_5+RSNHR_5+LL_5;
                if Somma_5 >= 2
                    Level_5 = 2;
                else
                    Level_5 = 1;
                end
                
                % --- CALCOLO SOGLIE ASSOLUTE ---
                text = 'Inizio confronto soglie assolute per il sito di Vobarno';
                fprintf(fileID,fmt,text);
                % --- Modulo RSN 4 ---
                if Level_4 == 2
                    % MEMS
                    if AlfaX_Rif4(indexRSN_4(end),1)-AlfaX_4 > 5 % Gradi
                        RSN_4 = 1;
                    elseif ErrRSNLink_Rif4(indexRSN_4(end),1) == 1 && ErrRSNLink_Rif4(indexRSN_4(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                        RSN_4 = 0.5;
                    else
                        RSN_4 = 0;
                    end
                    % Ampolla
                    if AlfaX_HR_Rif4(indexRSNHR_4(end),1)-AlfaX_HR_4 > 5 % Gradi
                        RSNHR_4 = 1;
                    elseif ErrRSNLink_HR_Rif4(indexRSNHR_4(end),1) == 1 && ErrRSNLink_HR_Rif4(indexRSNHR_4(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                        RSNHR_4 = 0.5;
                    else
                        RSNHR_4 = 0;
                    end
                    % Cella di carico
                    if Load_Rif4(indexLL_4(end),1)-Load_4 > 65 % kN
                        LL_4 = 1;
                    elseif ErrLoadLink_Rif4(indexLL_4(end),1) == 1 && ErrLoadLink_Rif4(indexLL_4(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                        LL_4 = 0.5;
                    else
                        LL_4 = 0;
                    end
                    Somma_4_A = RSN_4+RSNHR_4+LL_4;
                    if Somma_4_A >= 2
                        Level_4 = 3;
                    end
                end
                
                % --- Modulo RSN 5 ---
                if Level_5 == 2
                    % MEMS
                    if AlfaX_Rif5(indexRSN_5(end),1)-AlfaX_5 > 5 % Gradi
                        RSN_5 = 1;
                    elseif ErrRSNLink_Rif5(indexRSN_5(end),1) == 1 && ErrRSNLink_Rif5(indexRSN_5(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                        RSN_5 = 0.5;
                    else
                        RSN_5 = 0;
                    end
                    % Ampolla
                    if AlfaX_HR_Rif5(indexRSNHR_5(end),1)-AlfaX_HR_5 > 5 % Gradi
                        RSNHR_5 = 1;
                    elseif ErrRSNLink_HR_Rif5(indexRSNHR_5(end),1) == 1 && ErrRSNLink_HR_Rif5(indexRSNHR_5(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                        RSNHR_5 = 0.5;
                    else
                        RSNHR_5 = 0;
                    end
                    % Cella di carico
                    if Load_Rif5(indexLL_5(end),1)-Load_5 > 65 % kN
                        LL_5 = 1;
                    elseif ErrLoadLink_Rif5(indexLL_5(end),1) == 1 && ErrLoadLink_Rif5(indexLL_5(end-1),1) == 0 % In errore dopo il trigger, NON in errore prima
                        LL_5 = 0.5;
                    else
                        LL_5 = 0;
                    end
                    Somma_5_A = RSN_5+RSNHR_5+LL_5;
                    if Somma_5_A >= 2
                        Level_5 = 3;
                    end
                end
            end
            AL = 1;
            % --- Solo Trigger ---
            if nTr == 1 && Level_1 == 1 && Level_2 == 1 && Level_3 == 1
                sms = 0;
                Level = 1;
                NodeNum = 1;
                Data = dataTrig;
                Somma = max([Somma_1,Somma_2,Somma_3]);
                desc = ['Punteggio ' num2str(Somma)];
                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} = Level;
                DATAinsert{AL,7} = sms;
                DATAinsert{AL,8} = desc;
                AL = AL+1;
                alert = 1;
            end
            if nTr == 2 && Level_4 == 1 && Level_5 == 1
                sms = 0;
                Level = 1;
                NodeNum = 2;
                Data = dataTrig;
                Somma = max([Somma_4,Somma_5]);
                desc = ['Punteggio ' num2str(Somma)];
                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} = Level;
                DATAinsert{AL,7} = sms;
                DATAinsert{AL,8} = desc;
                AL = AL+1;
                alert = 1;
            end
            if nTr == 3 && Level_1 == 1 && Level_2 == 1 && Level_3 == 1 
                sms = 0;
                Level = 1;
                NodeNum = 1;
                Data = dataTrig;
                Somma = max([Somma_1,Somma_2,Somma_3]);
                desc = ['Punteggio ' num2str(Somma)];
                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} = Level;
                DATAinsert{AL,7} = sms;
                DATAinsert{AL,8} = desc;
                AL = AL+1;
                alert = 1;
            end
            if nTr == 3 && Level_4 == 1 && Level_5 == 1
                sms = 0;
                Level = 1;
                NodeNum = 2;
                Data = dataTrig;
                Somma = max([Somma_4,Somma_5]);
                desc = ['Punteggio ' num2str(Somma)];
                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} = Level;
                DATAinsert{AL,7} = sms;
                DATAinsert{AL,8} = desc;
                AL = AL+1;
                alert = 1;
            end
            % --- Modulo 1 ---
            if Level_1 >= 2
                if Level_1 == 3
                    sms = 1;
                    alarm = 1;
                    SirenON = 1;
                    DataRifALA = datenum(dataTrig);
                else
                    sms = 0;
                    alert = 1;
                    DataRifALE = datenum(dataTrig);
                end
                Level = Level_1;
                Data = dataTrig;
                NodeNum = 1;
                desc = ['Punteggio Livello 2: ' num2str(Somma_1) ', Punteggio Livello 3: ' num2str(Somma_1_A) ];
                DATAinsert{AL,1} = 3; % Allarme tipologia Soglie tradizionali
                DATAinsert{AL,2} = IDcentralina;
                DATAinsert{AL,3} = 'DT0001';
                DATAinsert{AL,4} = NodeNum;
                DATAinsert{AL,5} = Data;
                DATAinsert{AL,6} = Level;
                DATAinsert{AL,7} = sms;
                DATAinsert{AL,8} = desc;
                AL = AL+1;
            end
            % --- Modulo 2 ---
            if Level_2 >= 2
                if Level_2 == 3
                    sms = 1;
                    alarm = 1;
                    SirenON = 1;
                    DataRifALA = datenum(dataTrig);
                else
                    sms = 0;
                    alert = 1;
                    DataRifALE = datenum(dataTrig);
                end
                Level = Level_2;
                Data = dataTrig;
                NodeNum = 1;
                desc = ['Punteggio Livello 2: ' num2str(Somma_2) ', Punteggio Livello 3: ' num2str(Somma_2_A) ];
                DATAinsert{AL,1} = 3; % Allarme tipologia Soglie tradizionali
                DATAinsert{AL,2} = IDcentralina;
                DATAinsert{AL,3} = 'DT0002';
                DATAinsert{AL,4} = NodeNum;
                DATAinsert{AL,5} = Data;
                DATAinsert{AL,6} = Level;
                DATAinsert{AL,7} = sms;
                DATAinsert{AL,8} = desc;
                AL = AL+1;
            end
            % --- Modulo 3 ---
            if Level_3 >= 2
                if Level_3 == 3
                    sms = 1;
                    alarm = 1;
                    SirenON = 1;
                    DataRifALA = datenum(dataTrig);
                else
                    sms = 0;
                    alert = 1;
                    DataRifALE = datenum(dataTrig);
                end
                Level = Level_3;
                Data = dataTrig;
                NodeNum = 1;
                desc = ['Punteggio Livello 2: ' num2str(Somma_3) ', Punteggio Livello 3: ' num2str(Somma_3_A) ];
                DATAinsert{AL,1} = 3; % Allarme tipologia Soglie tradizionali
                DATAinsert{AL,2} = IDcentralina;
                DATAinsert{AL,3} = 'DT0003';
                DATAinsert{AL,4} = NodeNum;
                DATAinsert{AL,5} = Data;
                DATAinsert{AL,6} = Level;
                DATAinsert{AL,7} = sms;
                DATAinsert{AL,8} = desc;
                AL = AL+1;
            end
            % --- Modulo 4 ---
            if Level_4 >= 2
                if Level_4 == 3
                    sms = 1;
                    alarm = 1;
                    SirenON = 1;
                    DataRifALA = datenum(dataTrig);
                else
                    sms = 0;
                    alert = 1;
                    DataRifALE = datenum(dataTrig);
                end
                Level = Level_4;
                Data = dataTrig;
                NodeNum = 1;
                desc = ['Punteggio Livello 2: ' num2str(Somma_4) ', Punteggio Livello 3: ' num2str(Somma_4_A) ];
                DATAinsert{AL,1} = 3; % Allarme tipologia Soglie tradizionali
                DATAinsert{AL,2} = IDcentralina;
                DATAinsert{AL,3} = 'DT0004';
                DATAinsert{AL,4} = NodeNum;
                DATAinsert{AL,5} = Data;
                DATAinsert{AL,6} = Level;
                DATAinsert{AL,7} = sms;
                DATAinsert{AL,8} = desc;
                AL = AL+1;
            end
            % --- Modulo 5 ---
            if Level_5 >= 2
                if Level_5 == 3
                    sms = 1;
                    alarm = 1;
                    SirenON = 1;
                    DataRifALA = datenum(dataTrig);
                else
                    sms = 0;
                    alert = 1;
                    DataRifALE = datenum(dataTrig);
                end
                Level = Level_5;
                Data = dataTrig;
                NodeNum = 1;
                desc = ['Punteggio Livello 2: ' num2str(Somma_5) ', Punteggio Livello 3: ' num2str(Somma_5_A) ];
                DATAinsert{AL,1} = 3; % Allarme tipologia Soglie tradizionali
                DATAinsert{AL,2} = IDcentralina;
                DATAinsert{AL,3} = 'DT0005';
                DATAinsert{AL,4} = NodeNum;
                DATAinsert{AL,5} = Data;
                DATAinsert{AL,6} = Level;
                DATAinsert{AL,7} = sms;
                DATAinsert{AL,8} = desc;
                AL = AL+1;
            end
        end
    elseif Tr == 2 % A27 - Trigger
        if datenum(dataTrig) > DataRifALE
            DataRifALE = datenum(dataTrig);
            sms = 1;
            NodeNum = 1;
            Level = 1;
            Data = dataTrig;
            desc = 'sensore finecorsa';
            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} = Level;
            DATAinsert{AL,7} = sms;
            DATAinsert{AL,8} = desc;
            AL = AL+1;
            alarm = 1;
        end
    elseif Tr == 3 % A27 - Fadalto
        if datenum(dataTrig) > DataRifALE
            DataRifALE = datenum(dataTrig);
            sms = 0;
            NodeNum = 1;
            Level = 1;
            Data = dataTrig;
            comando = ['select `desc` from tools where unit_id = ''' ...
                unitID ''' and name = ''' DTcatena ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            desc = char(curs.Data);
            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} = Level;
            DATAinsert{AL,7} = sms;
            DATAinsert{AL,8} = desc;
            AL = AL+1;
            alarm = 1;
        end
    end
end

NoValue = 1;
% --- Shock Sensor ---
if yesSS == 1
    if SS == 1 % Ne è scattato almeno uno
        [rSS,~] = size(DataSS);
        if rSS > 0
            s = 1;
            while s <= rSS
                if DataSS(s) > DATA_rif % Cerco per ogni Impatto e mi ricordo che esistono 3 letture per impatto
                    Level = 1;
                    if yesRSN == 1
                        % Livello 2 -> Controllo se la prima lettura ha
                        % provocato una rotazione del montante verso valle
                        text = 'Inizio confronto soglie moduli D-Fence';
                        fprintf(fileID,fmt,text);
                        % --- CALCOLO SOGLIE RELATIVE 1° LETTURA ---
                        indexRSN = find(DataRSN_Rif(:,1)<datenum(DataSS(s))); % Dati precedenti al trigger
                        TrigRSN_X = AlfaX_Rif(indexRSN(end)+1,1)-AlfaX_Rif(indexRSN(end),1); % Differenza di inclinazione data dal trigger monte-valle
                        TrigRSN_Y = AlfaY_Rif(indexRSN(end)+1,1)-AlfaY_Rif(indexRSN(end),1); % Differenza di inclinazione laterale data dal trigger
                        % Prendo il trend di dati precedente allo scatto dello Shock Sensor
                        TrendRSN_X = AlfaX_Rif(indexRSN(1:end),1);
                        TrendRSN_Y = AlfaY_Rif(indexRSN(1:end),1);
                        % Scarico eventuali alert già attivati
                        nNodo = num2str(1);
                        type = num2str(3); % tipologia soglie tradizionali
                        comando = ['select type_id, date_time from alarms where unit_name = ''' ...
                            IDcentralina ''' and tool_name = ''' DTcatena ''' and node_num = ''' ...
                            nNodo ''' and date_time >= ''' DataScarico ''' and type_id = ''' type ...
                            ''' order by date_time'];
                        curs = exec(conn,comando);
                        curs = fetch(curs);
                        StoricoAllarmi = curs.Data;
                        [rSA,cSA] = size(StoricoAllarmi);
                        index = [];
                        if cSA > 1
                            I = 1;
                            for ii = 1:rSA
                                check = find(datenum(StoricoAllarmi(ii,2))==DataRSN_Rif);
                                if isempty(check) == 0
                                    index(I) = check;
                                    I = I+1;
                                end
                            end
                            I = I-1; % Numero di indici da eliminare
                        else
                            I = 0;
                        end
                                   
                        AttX = 0;
                        AttY = 0;
                        NUM = 0;
                        if I > 0
                            for i = 1:I
                                dim = size(TrendRSN_X);
                                if index(i)-NUM <= dim(1)
                                    TrendRSN_X(index(i)-NUM)=[];
                                    TrendRSN_Y(index(i)-NUM)=[];
                                    NUM = NUM+1;
                                end
                            end
                        end
                        SogliaX = 10*std(abs(diff(TrendRSN_X)))+mean(abs(diff(TrendRSN_X))); % Soglia di 10 dev.st.
                        SogliaY = 10*std(abs(diff(TrendRSN_Y)))+mean(abs(diff(TrendRSN_X))); % Soglia di 10 dev.st.

                        if abs(TrigRSN_X) > SogliaX || abs(TrigRSN_Y) > SogliaY
                            RSN = 1;
                            if abs(TrigRSN_X) > SogliaX && abs(TrigRSN_Y) > SogliaY
                                AttX = 1;
                                AttY = 1;
                            elseif abs(TrigRSN_X) > SogliaX 
                                AttX = 1;
                            elseif abs(TrigRSN_Y) > SogliaY
                                AttY = 1;
                            end
                        elseif ErrRSNLink_Rif(indexRSN(end)+1,1) == 1 && ErrRSNLink_Rif(indexRSN(end),1) == 0 % In errore dopo il trigger, NON in errore prima
                            RSN = 0.5;
                        else
                            RSN = 0;
                        end
                        Somma = RSN;
                        if Somma >= 1
                            Level = 2;
                            Value_X = abs(TrigRSN_X);
                            Value_Y = abs(TrigRSN_Y);
                        end
                        % --- CALCOLO SOGLIE RELATIVE 3° LETTURA---
                        % Livello 3
                        TrigRSN_X = AlfaX_Rif(indexRSN(end)+1,1)-AlfaX_Rif(indexRSN(end),1);
                        TrigRSN_Y = AlfaY_Rif(indexRSN(end)+1,1)-AlfaY_Rif(indexRSN(end),1);
                        if abs(TrigRSN_X) > SogliaX || abs(TrigRSN_Y) > SogliaY
                            RSN = 1;
                            if abs(TrigRSN_X) > SogliaX && abs(TrigRSN_Y) > SogliaY
                                AttX = 1;
                                AttY = 1;
                            elseif abs(TrigRSN_X) > SogliaX
                                AttX = 1;
                                AttY = 0;
                            elseif abs(TrigRSN_Y) > SogliaY
                                AttX = 0;
                                AttY = 1;
                            end
                        elseif ErrRSNLink_Rif(indexRSN(end)+1,1) == 1 && ErrRSNLink_Rif(indexRSN(end),1) == 0 % In errore dopo il trigger, NON in errore prima
                            RSN = 0.5;
                        else
                            RSN = 0;
                        end
                        Somma = RSN;
                        if Somma >= 1
                            Level = 3;
                            Value_X = abs(TrigRSN_X);
                            Value_Y = abs(TrigRSN_Y);
                        end
                        % --- CALCOLO SOGLIE ASSOLUTE ---
                        % Livello 4 -> Soglie SEL
                        if Level == 3 || Level == 2
                            % MEMS
                            if abs(AlfaX_Rif(indexRSN(end)+1,1)-AlfaX_0) > SEL % Gradi
                                RSN = 1;
                            else
                                RSN = 0;
                            end
                            Somma_S = RSN;
                            if Somma_S >= 1
                                Level = 4;
                                Value_X = abs(TrigRSN_X);
                            end
                        end
                        % Livello 5 -> Soglie MEL
                        if Level == 4
                            % MEMS
                            if abs(AlfaX_Rif(indexRSN(end)+1,1)-AlfaX_0) > MEL % Gradi
                                RSN = 1;
                            else
                                RSN = 0;
                            end
                            Somma_M = RSN;
                            if Somma_M >= 1
                                Level = 5;
                                Value_X = abs(TrigRSN_X);
                            end
                        end
                        % Scrivo
                        if Level == 1 % Shock
                            DataRifALE = DataSS(s);
                            if strcmp(NomeSito,'Sede ASE S.r.l.') == 1
                                sms = 0;
                            else
                                sms = 1;
                            end
                            alert = 1;
                            NodeNum = 2;
                        elseif Level == 2 % Soglie relative Prima lettura
                            DataRifALE = DataSS(s);
                            if strcmp(NomeSito,'Sede ASE S.r.l.') == 1
                                sms = 0;
                            else
                                sms = 1;
                            end
                            alert = 1;
                            NodeNum = 1;
                        elseif Level == 3 % Soglie relative terza lettura
                            DataRifALE = DataSS(s);
                            if strcmp(NomeSito,'Sede ASE S.r.l.') == 1
                                sms = 0;
                            else
                                sms = 1;
                            end
                            alert = 1;
                            NodeNum = 1;
                        elseif Level == 4 % Soglia SEL
                            DataRifALA = DataSS(s);
                            if strcmp(NomeSito,'Sede ASE S.r.l.') == 1
                                sms = 0;
                            else
                                sms = 1;
                            end
                            alarm = 1;
                            NodeNum = 1;
                        elseif Level == 5 % Soglia MEL
                            DataRifALA = DataSS(s);
                            if strcmp(NomeSito,'Sede ASE S.r.l.') == 1
                                sms = 0;
                            else
                                sms = 1;
                            end
                            alarm = 1;
                            NodeNum = 1;
                        end
                        Data = datestr(DataSS(s),'yyyy/mm/dd HH:MM:SS');
                        comando = ['select `desc` from tools where unit_id = ''' ...
                            unitID ''' and name = ''' DTcatena ''' '];
                        curs = exec(conn,comando);
                        curs = fetch(curs);
                        desc = char(curs.Data);
                        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} = Level;
                        DATAinsert{AL,7} = sms;
                        DATAinsert{AL,8} = desc;
                        if Level ~= 1 
                            if Level > 3
                                DATAinsert{AL,9} = Value_X;
                            else
                                if AttX == 1
                                    DATAinsert{AL,9} = Value_X;
                                elseif AttY == 1
                                    DATAinsert{AL,9} = Value_Y;
                                end
                            end
                            NoValue = 0;
                        end
                        AL = AL+1;
                    else
                        DataRifALE = Data(ss);
                        if strcmp(NomeSito,'Sede ASE S.r.l.') == 1
                            sms = 0;
                        else
                            sms = 1;
                        end
                        NodeNum = cell2mat(NodoSS(2));
                        Level = 1;
                        Data = datestr(Data(ss),'yyyy/mm/dd HH:MM:SS');
                        comando = ['select `desc` from tools where unit_id = ''' ...
                            unitID ''' and name = ''' DTcatena ''' '];
                        curs = exec(conn,comando);
                        curs = fetch(curs);
                        desc = char(curs.Data);
                        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} = Level;
                        DATAinsert{AL,7} = sms;
                        DATAinsert{AL,8} = desc;
                        AL = AL+1;
                        alert = 1;
                    end
                end
                s = s+1;
            end
        end
    else
        if yesRSN == 1
            % Anche se non è scattato lo Shock Sensor, controllo se
            % l'ultima lettura si discosta molto dalle precedenti
            text = 'Inizio confronto soglie moduli D-Fence';
            fprintf(fileID,fmt,text);
            % --- CALCOLO SOGLIE RELATIVE ---
            index_RSN = find(DataRSN_Rif>=datenum(datainiRSN)); % cerco i dati nuovi
            [rI,~] = size(index_RSN);
            RSN_X = zeros(rI,1);
            RSN_Y = zeros(rI,1);
            for jj = 1:rI
                if index_RSN(jj) ~= 1
                    RSN_X(jj,1) = AlfaX_Rif(index_RSN(jj),1)-AlfaX_Rif(index_RSN(jj)-1,1);
                    RSN_Y(jj,1) = AlfaY_Rif(index_RSN(jj),1)-AlfaY_Rif(index_RSN(jj)-1,1);
                end
            end
            % Scarico eventuali alert già attivati
            nNodo = num2str(1);
            type = num2str(3); % tipologia soglie tradizionali
            comando = ['select type_id, date_time from alarms where unit_name = ''' ...
                IDcentralina ''' and tool_name = ''' DTcatena ''' and node_num = ''' ...
                nNodo ''' and date_time >= ''' DataScarico ''' and type_id = ''' type ...
                ''' order by date_time'];
            curs = exec(conn,comando);
            curs = fetch(curs);
            StoricoAllarmi = curs.Data;
            [rSA,cSA] = size(StoricoAllarmi);
            index = [];
            if cSA > 1
                I = 1;
                for ii = 1:rSA
                    check = find(datenum(StoricoAllarmi(ii,2))==DataRSN_Rif);
                    if isempty(check) == 0
                        index(I) = check;
                        I = I+1;
                    end
                end
                I = I-1; % Numero di indici da eliminare
            else
                I = 0;
            end            
            for aa = 1:rI
                TrendRSN_X = AlfaX_Rif(1:index_RSN(aa),1);
                TrendRSN_Y = AlfaY_Rif(1:index_RSN(aa),1);
                NUM = 0;
                if I > 0
                    for i = 1:I
                        dim = size(TrendRSN_X);
                        if index(i)-NUM <= dim(1)
                            TrendRSN_X(index(i)-NUM)=[];
                            TrendRSN_Y(index(i)-NUM)=[];
                            NUM = NUM+1;
                        end
                    end
                end
                SogliaX = 10*std(abs(diff(TrendRSN_X)))+mean(abs(diff(TrendRSN_X))); % Soglia di 10 dev.st.
                SogliaY = 10*std(abs(diff(TrendRSN_Y)))+mean(abs(diff(TrendRSN_Y))); % Soglia di 10 dev.st.
                AttX = 0;
                AttY = 0;
                Level = 0;
                % --- Livello 0.1 ---
                if abs(RSN_X(aa)) > SogliaX || abs(RSN_Y(aa)) > SogliaY % L'ultima lettura è fuori trend
                    RSN = 1;
                    if abs(RSN_X(aa)) > SogliaX || abs(RSN_Y(aa)) > SogliaY
                        AttX = 1;
                        AttY = 1;
                    elseif abs(RSN_X(aa)) > SogliaX
                        AttX = 1;
                    elseif abs(RSN_Y(aa)) > SogliaY
                        AttY = 1;
                    end
                elseif ErrRSNLink_Rif(index_RSN(aa),1) == 1 && ErrRSNLink_Rif(index_RSN(aa)-1,1) == 0 % In errore all'ultima lettura ma non in quella prima
                    RSN = 0.5;
                else
                    RSN = 0;
                end
                Somma = RSN;
                if Somma >= 1
                    Level = 0.1;
                    Value_X = abs(RSN_X(aa));
                    Value_Y = abs(RSN_Y(aa));
                end
                % --- CALCOLO SOGLIE ASSOLUTE ---
                % Livello 0.2 -> Soglie SEL
                if Level == 0.1
                    % MEMS
                    if abs(AlfaX_Rif(index_RSN(aa),1)) > SEL % Gradi
                        Level = 0.2;
                        Value_X = abs(RSN_X(aa));
                    end
                end
                % Livello 0.3 -> Soglie MEL
                if Level == 0.1 || Level == 0.2
                    % MEMS
                    if abs(AlfaX_Rif(index_RSN(aa),1)) > MEL % Gradi
                        Level = 0.3;
                        Value_X = abs(RSN_X(aa));
                    end
                end
                % Scrivo
                if DataRSN_Rif(aa) > DATA_rif
                    if Level == 0.1 % Lettura fuori trend
                        DataRifALE = DataRSN_Rif(aa);
                        sms = 0;
                        alert = 1;
                        NodeNum = 1;
                    elseif Level == 0.2 % Soglia SEL
                        DataRifALA = DataRSN_Rif(aa);
                        if strcmp(NomeSito,'Sede ASE S.r.l.') == 1
                            sms = 0;
                        else
                            sms = 1;
                        end
                        alarm = 1;
                        NodeNum = 1;
                    elseif Level == 0.3 % Soglia MEL
                        DataRifALA = DataRSN_Rif(aa);
                        if strcmp(NomeSito,'Sede ASE S.r.l.') == 1
                            sms = 0;
                        else
                            sms = 1;
                        end
                        alarm = 1;
                        NodeNum = 1;
                    end
                end
                if Level > 0
                    Data = datestr(DataRSN_Rif(aa),'yyyy/mm/dd HH:MM:SS');
                    comando = ['select `desc` from tools where unit_id = ''' ...
                        unitID ''' and name = ''' DTcatena ''' '];
                    curs = exec(conn,comando);
                    curs = fetch(curs);
                    desc = char(curs.Data);
                    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} = Level;
                    DATAinsert{AL,7} = sms;
                    DATAinsert{AL,8} = desc;
                    if Level > 0.1
                        DATAinsert{AL,9} = Value_X;
                        NoValue = 0;
                    else
                        if AttX == 1
                            DATAinsert{AL,9} = Value_X;
                            NoValue = 0;
                        elseif AttY == 1
                            DATAinsert{AL,9} = Value_Y;
                            NoValue = 0;
                        end
                    end
                    AL = AL+1;
                end
            end
        else
            DataRifALE = Data(ss);
            sms = 0;
            NodeNum = 2;
            Level = 1;
            Data = datestr(Data(ss),'yyyy/mm/dd HH:MM:SS');
            comando = ['select `desc` from tools where unit_id = ''' ...
                unitID ''' and name = ''' DTcatena ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            desc = char(curs.Data);
            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} = Level;
            DATAinsert{AL,7} = sms;
            DATAinsert{AL,8} = desc;
            AL = AL+1;
            alert = 1;
        end
    end
elseif yesRSN == 1 && yesTrL == 0
    % Anche se non è scattato il trigger, controllo se
    % l'ultima lettura si discosta molto dalle precedenti
    text = 'Inizio confronto soglie moduli D-Fence';
    fprintf(fileID,fmt,text);
    % --- CALCOLO SOGLIE RELATIVE ---
    index_RSN = find(DataRSN_Rif>=datenum(datainiRSN)); % cerco i dati nuovi
    [rI,~] = size(index_RSN);
    RSN_X = zeros(rI,1);
    RSN_Y = zeros(rI,1);
    for jj = 1:rI
        if index_RSN(jj) ~= 1
            RSN_X(jj,1) = AlfaX_Rif(index_RSN(jj),1)-AlfaX_Rif(index_RSN(jj)-1,1);
            RSN_Y(jj,1) = AlfaY_Rif(index_RSN(jj),1)-AlfaY_Rif(index_RSN(jj)-1,1);
        end
    end
    % Scarico eventuali alert già attivati
    nNodo = num2str(1);
    type = num2str(3); % tipologia soglie tradizionali
    comando = ['select type_id, date_time from alarms where unit_name = ''' ...
        IDcentralina ''' and tool_name = ''' DTcatena ''' and node_num = ''' ...
        nNodo ''' and date_time >= ''' DataScarico ''' and type_id = ''' type ...
        ''' order by date_time'];
    curs = exec(conn,comando);
    curs = fetch(curs);
    StoricoAllarmi = curs.Data;
    [rSA,cSA] = size(StoricoAllarmi);
    index = [];
    if cSA > 1
        I = 1;
        for ii = 1:rSA
            check = find(datenum(StoricoAllarmi(ii,2))==DataRSN_Rif);
            if isempty(check) == 0
                index(I) = check;
                I = I+1;
            end
        end
        I = I-1; % Numero di indici da eliminare
    else
        I = 0;
    end
    for aa = 1:rI
        TrendRSN_X = AlfaX_Rif(1:index_RSN(aa),1);
        TrendRSN_Y = AlfaY_Rif(1:index_RSN(aa),1);
        NUM = 0;
        if I > 0
            for i = 1:I
                dim = size(TrendRSN_X);
                if index(i)-NUM <= dim(1)
                    TrendRSN_X(index(i)-NUM)=[];
                    TrendRSN_Y(index(i)-NUM)=[];
                    NUM = NUM+1;
                end
            end
        end
        SogliaX = 10*std(abs(diff(TrendRSN_X)))+mean(abs(diff(TrendRSN_X))); % Soglia di 10 dev.st.
        SogliaY = 10*std(abs(diff(TrendRSN_Y)))+mean(abs(diff(TrendRSN_Y))); % Soglia di 10 dev.st.
        AttX = 0;
        AttY = 0;
        Level = 0;
        % --- Livello 0.1 ---
        if abs(RSN_X(aa)) > SogliaX || abs(RSN_Y(aa)) > SogliaY % L'ultima lettura è fuori trend
            RSN = 1;
            if abs(RSN_X(aa)) > SogliaX || abs(RSN_Y(aa)) > SogliaY 
                AttX = 1;
                AttY = 1;
            elseif abs(RSN_X(aa)) > SogliaX
                AttX = 1;
            elseif abs(RSN_Y(aa)) > SogliaY 
                AttY = 1;
            end
        elseif ErrRSNLink_Rif(index_RSN(aa),1) == 1 && ErrRSNLink_Rif(index_RSN(aa)-1,1) == 0 % In errore all'ultima lettura ma non in quella prima
            RSN = 0.5;
        else
            RSN = 0;
        end
        Somma = RSN;
        if Somma >= 1
            Level = 0.1;
            Value_X = abs(RSN_X(aa));
            Value_Y = abs(RSN_Y(aa));
        end
        % --- CALCOLO SOGLIE ASSOLUTE ---
        % Livello 0.2 -> Soglie SEL
        if Level == 0.1
            % MEMS
            if abs(AlfaX_Rif(index_RSN(aa),1)) > SEL % Gradi
                Level = 0.2;
                Value_X = abs(RSN_X(aa));
            end
        end
        % Livello 0.3 -> Soglie MEL
        if Level == 0.1 || Level == 0.2
            % MEMS
            if abs(AlfaX_Rif(end,1)) > MEL % Gradi
                Level = 0.3;
                Value_X = abs(RSN_X(aa));
            end
        end
        % Scrivo
        if DataRSN_Rif(aa) > DATA_rif
            if Level == 0.1 % Lettura fuori trend
                DataRifALE = DataRSN_Rif(aa);
                sms = 0;
                alert = 1;
                NodeNum = 1;
            elseif Level == 0.2 % Soglia SEL
                DataRifALA = DataRSN_Rif(aa);
                if strcmp(NomeSito,'Sede ASE S.r.l.') == 1
                    sms = 0;
                else
                    sms = 1;
                end
                alarm = 1;
                NodeNum = 1;
            elseif Level == 0.3 % Soglia MEL
                DataRifALA = DataRSN_Rif(aa);
                if strcmp(NomeSito,'Sede ASE S.r.l.') == 1
                    sms = 0;
                else
                    sms = 1;
                end
                alarm = 1;
                NodeNum = 1;
            end
        end
        if Level > 0
            Data = datestr(DataRSN_Rif(aa),'yyyy/mm/dd HH:MM:SS');
            comando = ['select `desc` from tools where unit_id = ''' ...
                unitID ''' and name = ''' DTcatena ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            desc = char(curs.Data);
            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} = Level;
            DATAinsert{AL,7} = sms;
            DATAinsert{AL,8} = desc;
            if Level > 0.1
                DATAinsert{AL,9} = Value_X;
                NoValue = 0;
            else
                if AttX == 1
                    DATAinsert{AL,9} = Value_X;
                    NoValue = 0;
                elseif AttY == 1
                    DATAinsert{AL,9} = Value_Y;
                    NoValue = 0;
                end
            end
            AL = AL+1;
        end
    end
end

% --- G-Flow ---
if yesGF == 1
   % Cerco il trend di date del G-Flow
   Freq = diff(ARRAYdateGF);
   [rF,~] = size(Freq);
   indexALA = [];
   indexALE = [];
   vALA = 1;
   vALE = 1;
   nALE = 1;
   nALA = 1;
   for f = 1:rF
       if Freq(1,1) == 0
           if DatiGFLink(f,4) > 1.3 && ARRAYdateGF(f,1) > DATA_rif
               indexALA(nALA,1) = f;
               ValueALA(vALA,1) = DatiGFLink(f,4);
               alarm = 1;
               nALA = nALA+1;
               vALA = vALA+1;
           elseif ARRAYdateGF(f,1) > DATA_rif
               indexALE(nALE,1) = f;
               ValueAle(vALE,1) = DatiGFLink(f,4);
               alert = 1;
               nALE = nALE+1;
               vALE = vALE+1;
           end
       elseif Freq(f,1) == 0 && Freq(f-1,1) ~= 0
           if DatiGFLink(f,4) > 1.3 && ARRAYdateGF(f,1) > DATA_rif
               indexALA(nALA,1) = f;
               ValueALA(vALA,1) = DatiGFLink(f,4);
               alarm = 1;
               nALA = nALA+1;
               vALA = vALA+1;
           elseif ARRAYdateGF(f,1) > DATA_rif
               indexALE(nALE,1) = f;
               Valueale(vALE,1) = DatiGFLink(f,4);
               alert = 1;
               nALE = nALE+1;
               vALE = vALE+1;
           end
       end
   end
   nTot = nALE-1+nALA-1;
   if nALE-1 > 0 && nALA-1 > 0
       indexG = [indexALA;indexALE];
       Value = [ValueALA;Valueale];
       DataRifALA = ARRAYdateGF(indexALA(end));
       DataRifALE = ARRAYdateGF(indexALE(end));
   elseif nALA-1 > 0
       indexG = indexALA;
       Value = ValueALA;
       DataRifALA = ARRAYdateGF(indexALA(end));
   elseif indexALE-1 > 0
       indexG = indexALE;
       Value = Valueale;
       DataRifALE = ARRAYdateGF(indexALE(end));
   end
   if alarm == 1 || alert == 1
       for t = 1:nTot
           sms = 0;
           NodeNum = 1;
           Data = datestr(ARRAYdateGF(t),'yyyy/mm/dd HH:MM:SS');
           if t <= nALA-1
               Level = 2;
               NoValue = 0;
           else
               Level = 1;
           end
           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} = Level;
           DATAinsert{AL,7} = sms;
           DATAinsert{AL,8} = '';
           DATAinsert{AL,9} = Value(AL);
           AL = AL+1;
       end
   end
end

% --- G-Shock ---
if yesGS == 1
    % Verificare che il GShock sia scattato e poi verificare la relativa
    % accelerazione !!!!!!!!!!!!!
end
        
if alarm == 1 || alert == 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,6}); % livello allarme
        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 ''' 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';
        if NoValue == 0
            colnames = {'type_id','unit_name','tool_name','node_num','date_time',...
                'alarm_level','send_sms','description','registered_value'};
            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)];
        else
            colnames = {'type_id','unit_name','tool_name','node_num','date_time',...
                '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)];
        end
        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,DataRifALE);
fmt = '%.10f \r';
fprintf(fileID,fmt,DataRifALA);
fclose(fileID);

text = ('alert_Levels function worked correctly');
fileID = fopen(FileName,'a');
fmt = '%s \r';
fprintf(fileID,fmt,text);
fclose(fileID);

end
