function alarms = Siren(siteID,yesTuL,yesRL,yesTLH,yesTLHRH,yesPCL,yesPCLHR,...
    yesCrL,yes3DCrL,yes2DCrL,yesEL,yes3DEL,yesWEL,yesMPBEL,datainiTuL,tempoiniTuL,...
    datainiRL,tempoiniRL,datainiTLH,tempoiniTLH,datainiTLHRH,tempoiniTLHRH,...
    datainiPCL,tempoiniPCL,datainiPCLHR,tempoiniPCLHR,datainiCrL,tempoiniCrL,...
    dataini3DCrL,tempoini3DCrL,dataini2DCrL,tempoini2DCrL,...
    datainiEL,tempoiniEL,dataini3DEL,tempoini3DEL,...
    datainiWEL,tempoiniWEL,datainiMPBEL,tempoiniMPBEL,ARRAYdate_TuL,ARRAYdate_RL,...
    ARRAYdate_TLH,ARRAYdate_TLHRH,ARRAYdate_PCL,ARRAYdate_PCLHR,ARRAYdate_CrL,...
    ARRAYdate_3DCrL,ARRAYdate_2DCrL,ARRAYdate_EL,ARRAYdate_3DEL,ARRAYdate_WEL,ARRAYdate_MPBEL,...
    conn,date,time,FileName)

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

text = 'Siren function started';
fprintf(fileID,fmt,text);

% Scarico l'allarme relativo al sito
comando = ['select id, name, ctrltype_id from ctrltools where site_id = ''' siteID ''' ']; 
curs = exec(conn,comando);
curs = fetch(curs);
alarms = curs.Data;
[rA,cA] = size(alarms);

sirena = 0;
if rA > 0 && cA > 1
    for aa = 1:rA
        if cell2mat(alarms(aa,3)) == 1
            sirena = 1;
            break
        end
    end
end

if cA ~= 1 && sirena == 1
    if yesTuL == 1 
       ARRAYdateAL = ARRAYdate_TuL;
    elseif yesRL == 1 
           ARRAYdateAL = ARRAYdate_RL;
    elseif yesTLH == 1
           ARRAYdateAL = ARRAYdate_TLH;
    elseif yesTLHRH == 1
           ARRAYdateAL = ARRAYdate_TLHRH;
    elseif yesPCL == 1
           ARRAYdateAL = ARRAYdate_PCL;
    elseif yesPCLHR == 1
           ARRAYdateAL = ARRAYdate_PCLHR;
    elseif yesCrL == 1
           ARRAYdateAL = ARRAYdate_CrL;
    elseif yes3DCrL == 1
           ARRAYdateAL = ARRAYdate_3DCrL;
    elseif yes2DCrL == 1
           ARRAYdateAL = ARRAYdate_2DCrL;
    elseif yesEL == 1
           ARRAYdateAL = ARRAYdate_EL;
    elseif yes3DEL == 1
           ARRAYdateAL = ARRAYdate_3DEL;
    elseif yesWEL == 1
           ARRAYdateAL = ARRAYdate_WEL;
    elseif yesMPBEL == 1
           ARRAYdateAL = ARRAYdate_MPBEL;
    end
    [ListaDate,~] = size(ARRAYdateAL);
    val_AL = zeros(ListaDate,rA);
    StoricoAllarmi = zeros(ListaDate,rA);
    % Attivazione della Sirena al superamento di una determinata soglia
    
    if yesTuL == 1
       dataini = datainiTuL;
       tempoini = tempoiniTuL;
    elseif yesRL == 1
       dataini = datainiRL;
       tempoini = tempoiniRL;
    elseif yesTLH == 1
       dataini = datainiTLH;
       tempoini = tempoiniTLH;
    elseif yesTLHRH == 1
       dataini = datainiTLHRH;
       tempoini = tempoiniTLHRH;
    elseif yesPCL == 1
       dataini = datainiPCL;
       tempoini = tempoiniPCL;
    elseif yesPCLHR == 1
       dataini = datainiPCLHR;
       tempoini = tempoiniPCLHR;
    elseif yesCrL == 1
        dataini = datainiCrL;
        tempoini = tempoiniCrL;
    elseif yes3DCrL == 1
        dataini = dataini3DCrL;
        tempoini = tempoini3DCrL;
    elseif yes2DCrL == 1
        dataini = dataini2DCrL;
        tempoini = tempoini2DCrL;
    elseif yesEL == 1
        dataini = datainiEL;
        tempoini = tempoiniEL;
    elseif yes3DEL == 1
        dataini = dataini3DEL;
        tempoini = tempoini3DEL;
    elseif yesWEL == 1
        dataini = datainiWEL;
        tempoini = tempoiniWEL;
    elseif yesMPBEL == 1
        dataini = datainiMPBEL;
        tempoini = tempoiniMPBEL;
    end
    dateCheck = [date ' ' time];
    if dataini < datenum(dateCheck)
        dataini = date;
        tempoini = time;
    else
        dataini = datestr(dataini,'yyyy-mm-dd HH:MM:SS');
    end
    
    Control = 1;
    while Control == 1
        for nn = 1:rA
            % Scarico i dati storici di attivazione dell'allarme
            comando = ['select EventDate, EventTime, X from ELABDATACTRL where EventDate = ''' ...
                dataini ''' and EventTime >= ''' tempoini ''' and CtrlToolName = '''...
                char(alarms(nn,2)) ''' and NodeNum = 1 '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            AlarmTot_1 = curs.Data; % numero cumulato di attivazioni prima della lettura in elaborazione
            
            comando = ['select EventDate, EventTime, X from ELABDATACTRL where EventDate > ''' ...
                dataini ''' and CtrlToolName = ''' char(alarms(nn,2)) ''' and NodeNum = 1 '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            AlarmTot_2 = curs.Data; % numero cumulato di attivazioni prima della lettura in elaborazione
            
            [r1,c1] = size(AlarmTot_1);
            [r2,c2] = size(AlarmTot_2);
            if r1 == 1 && c1 == 1
                if r2 == 1 && c2 == 1
                    if datenum(dataini) < datenum(dateCheck)
                       AlarmTot(1,1) = cellstr(date);
                       AlarmTot(1,2) = cellstr(time);
                       AlarmTot(1,3) = num2cell(0);
                       Control = 0;
                    else
                        dataini = dataini-1;
                        break
                    end
                else
                    AlarmTot = AlarmTot_2;
                    Control = 0;
                end
            else
                Control = 0;
                if r2 == 1 && c2 == 1
                    AlarmTot = AlarmTot_1;
                else
                    AlarmTot = [AlarmTot_1; AlarmTot_2];
                end
            end
            [rA,~] = size(AlarmTot);
            Data = [cell2mat(AlarmTot(:,1)) repmat(' ', [rA,1]) cell2mat(AlarmTot(:,2))];
            Data = datenum(Data);
            Var = find(Data < datenum(ARRAYdateAL(1,1)));
            if isempty(Var) == 1
                StoricoAllarmi(:,nn) = cumsum(val_AL);
            else
                StoricoAllarmi(:,nn) = cumsum(val_AL)+cell2mat(AlarmTot(Var(end),3));
            end
            
            DATAinsert = cell(ListaDate,6);
            % Creo i dati da scrivere
            for ii=1:ListaDate
                DATAinsert{ii,1} = alarms(nn,2);
                DATAinsert{ii,2} = datestr(ARRAYdateAL(ii),'yyyy-mm-dd');
                DATAinsert{ii,3} = datestr(ARRAYdateAL(ii),'HH:MM:SS');
                DATAinsert{ii,4} = val_AL(ii,nn);
                DATAinsert{ii,5} = StoricoAllarmi(ii,nn);
                DATAinsert{ii,6} = 1;
            end
            idElabData = zeros(ListaDate,1); % Contiene gli id delle date delle quali ho già dati
            cc = 1; % contatore
            % Cerco su DB se i dati sono da aggiornare o scrivere
            for ii=1:ListaDate
                dString = datestr(ARRAYdateAL(ii),'yyyy-mm-dd');
                tString = datestr(ARRAYdateAL(ii),'HH:MM:SS');
                
                comando = ['select idElabDataCtrl from ELABDATACTRL where CtrlToolName = '''...
                    char(alarms(nn,2)) ''' and NodeNum = 1 and EventDate = ''' ...
                    dString ''' and EventTime = ''' tString ''' '];
                curs = exec(conn,comando);
                curs = fetch(curs);
                idDate = curs.Data;
                if  strcmp(idDate,'No Data')
                    idElabData(cc,1) = 0; % 0 indica che il dato non è presente su DB
                else
                    idElabData(cc,1) = cell2mat(idDate);
                end
                cc = cc+1;
                % Scrivo o Aggiorno i dati
                idData = idElabData(ii,1);
                tablename = 'ELABDATACTRL';
                if  idData == 0 % Se la data non è su DB (=0), scrivo i dati
                    colnames = {'CtrlToolName','EventDate','EventTime','XShift','X','NodeNum',};
                    data = [DATAinsert(ii,1),DATAinsert(ii,2),DATAinsert(ii,3),...
                        DATAinsert(ii,4),DATAinsert(ii,5),DATAinsert(ii,6)];
                    fastinsert(conn,tablename,colnames,data);
                else % altrimenti li aggiorno
                    colnames = {'XShift','X'};
                    data = [DATAinsert(ii,4),DATAinsert(ii,5)];
                    whereclause = ['WHERE CtrlToolName = ''' char(alarms(nn,2)) ...
                        ''' and NodeNum = 1 and EventDate = ''' cell2mat(DATAinsert(ii,2))...
                        ''' and EventTime = ''' cell2mat(DATAinsert(ii,3)) ''' '];
                    update(conn,tablename,colnames,data,whereclause)
                end
            end
            text = ['Data of Control Tools ' char(alarms(nn,2)) ' uploaded in the DB correctly'];
            fprintf(fileID,fmt,text);
        end
    end
end

text = 'Siren function executed correctly';
fprintf(fileID,fmt,text);
fclose(fileID);
end

 