function [ATTIVA,FIG,FIG_ENG] = report_RSN(rRSN,m,NodoRSNLink,toolrif,unitrif,datarif,...
    Font_caption,Font_tools,siteID,DT,DT_ENG,FIG,FIG_ENG,br,status,ATTIVA,activeEN,conn,FileName)

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

import mlreportgen.dom.*
import mlreportgen.report.*


% Make sure DOM is compilable
makeDOMCompilable()

if status(m,1) == 1
    if ATTIVA == 1
        testo = Paragraph('Catena non ancora installata o inattiva.');
        ATTIVA = 0;
        add(DT,testo);
        if activeEN == 1
            testo = Paragraph('Array not installed or inactive.');
            add(DT_ENG,testo);
        end
    end
elseif status(m,1) == 3
    if ATTIVA == 1
        testo = Paragraph(['Monitoraggio completato. Invitiamo a visitare la '...
            'piattaforma web per la visione dei dati storici.']);
        ATTIVA = 0;
        add(DT,testo);
        if activeEN == 1
            testo = Paragraph(['Monitoring completed. Please log into the web-based platform '...
                'to access the complete dataset.']);
            add(DT_ENG,testo);
        end
    end
else
    titolo = 1;
    for b = 1:rRSN(m,1)
        NodeNum = num2str(NodoRSNLink(b,m));
        comando = ['select EventDate, EventTime, AlfaX, AlfaY, T_node from ElabDataView where EventDate >= ''' ...
            datarif ''' and  ToolNameID = ''' char(toolrif) ''' and UnitName = ''' char(unitrif) ...
            ''' and NodeNum = ''' NodeNum ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        Dati = curs.Data;
        % Modifico il formato di data e ora
        [rD,rC] = size(Dati);
        if rC ~= 1
            T = [cell2mat(Dati(:,1)) repmat(' ', [rD,1]) cell2mat(Dati(:,2))];
            Date = datenum(T); % Data
            AngX = cell2mat(Dati(:,3)); % asse x assoluti
            AngY = cell2mat(Dati(:,4)); % asse y assoluti
            TempRSN = cell2mat(Dati(:,5)); % temp
            [d,~] = size(AngX);
            for ind = 2:d
                VarAngX(ind,1) = AngX(ind,1)-AngX(ind-1,1); % variazioni asse x
                VarAngY(ind,1) = AngY(ind,1)-AngY(ind-1,1); % variazione asse y
            end
            
            % grafico inclinazione-tempo asse X,Y NELL'ULTIMO MESE
            figure(1)
            yyaxis left
            RSN_X = plot(Date,VarAngX,'-','LineWidth',2);
            hold on
            RSN_Y = plot(Date,VarAngY,'-','Color','red','LineWidth',2);
            hold off
            yyaxis right
            RSN_Temp = plot(Date,TempRSN,':','LineWidth',1.2);
            title('Rockfall Safety Network - RSN Link');
            xlabel('Data [gg/mm/aaaa]');
            h = gca;
            h.XAxis.MinorTick = 'on';
            datetick('x','dd/mm/yyyy','keepticks','keeplimits');
            xtickangle(12);
            lgd = legend('Asse X','Asse Y','Temperatura','Location','south');
            lgd.NumColumns = 3;
            yyaxis left
            if max(VarAngX) > 1 ||  max(VarAngY) > 1
                if min(VarAngX) < -1 ||  max(VarAngY) < -1
                    ylim([-inf inf]);
                else
                    ylim([-1 inf]);
                end
            elseif min(VarAngX) < -1 ||  max(VarAngY) < -1
                if max(VarAngX) > 1 ||  max(VarAngY) > 1
                    ylim([-inf inf]);
                else
                    ylim([-inf 1]);
                end
            else
                ylim([-1 1]);
            end
            ylabel(['Variazione di inclinazione [' char(176) ']','Color','k']);
            set(gca,'YColor','k');
            yyaxis right
            if max(VarAngX) > 1 ||  max(VarAngY) > 1
                if min(VarAngX) < -1 ||  max(VarAngY) < -1
                    ylim([-inf inf]);
                else
                    ylim([min(TempRSN)-7 inf]);
                end
            elseif min(VarAngX) < -1 ||  max(VarAngY) < -1
                if max(VarAngX) > 1 ||  max(VarAngY) > 1
                    ylim([-inf inf]);
                else
                    ylim([-inf max(TempRSN)+7]);
                end
            else
            end
            ylabel(['Temperatura [' char(176) 'C]','Color','k']);
            set(gca,'YColor','k');
            grid on
            grid minor
            
            TempName = char(strcat('Report',siteID,'-', char(toolrif),'-RSN.png'));
            saveas(RSN_Y,TempName);
            ChartRSN = Image(TempName);
            ChartRSN.Style = {ScaleToFit};
            ChartRSN_cap = Paragraph(['Fig. ' num2str(FIG) ' - Variazione dell''inclinazione'...
                ' in direzione X (perpendicolarmente rispetto alla barriera) e Y (trasversalmente alla barriera)'...
                ' registrata dal sensore MEMS, integrato nel '...
                'modulo BPM, durante il periodo temporale di riferimento']);
            FIG = FIG+1;
            ChartRSN_cap.Style = {HAlign('justify'),FontSize(Font_caption),Italic(1),Color('midnightblue')};
            
            testo = Paragraph(['I grafici seguenti riportano le variazioni di inclinazione misurate '...
                'nel periodo temporale di riferimento dal sensore MEMS incluso nel Rockfall Safety Network - RSN Link'...
                ', relativamente agli assi strumentali X (perpendicolare alla barriera) e Y (trasversale alla barriera). ']);
            testo2 = Paragraph(['In questo periodo di tempo, il sensore ha rilevato una variazione di inclinazione massima lungo l''asse X '...
                'pari a ' num2str(round(max(abs(VarAngX)),2), '%0.2f')...
                char(176) '. Per quanto riguarda l''asse Y, la massima variazione risulta '...
                ' pari a ' num2str(round(max(abs(VarAngY)),2), '%0.2f') '' char(176) ' di inclinazione.']);
            testo.HAlign = 'justify';
            testo2.HAlign = 'justify';
            if titolo == 1
                sezRSN = Paragraph('RSN Link');
                sezRSN.Style = {FontSize(Font_tools),Bold(1),Italic(1)};
                add(DT,sezRSN);
            end
            add(DT,testo);
            add(DT,testo2);
            add(DT,ChartRSN);
            add(DT,ChartRSN_cap);
            add(DT,br);
            close(figure(1));
            
            if activeEN == 1
                % grafico inclinazione-tempo asse X,Y NELL'ULTIMO MESE
                figure(11)
                yyaxis left
                RSN_X = plot(Date,VarAngX,'-','LineWidth',2);
                hold on
                RSN_Y = plot(Date,VarAngY,'-','Color','red','LineWidth',2);
                hold off
                yyaxis right
                RSN_Temp = plot(Date,TempRSN,':','LineWidth',1.2);
                title('Rockfall Safety Network - RSN Link');
                xlabel('Date [dd/mm/yyyy]');
                h = gca;
                h.XAxis.MinorTick = 'on';
                datetick('x','dd/mm/yyyy','keepticks','keeplimits');
                xtickangle(12);
                lgd = legend('X axis','Y axis','Temperature','Location','south');
                lgd.NumColumns = 3;
                yyaxis left
                if max(VarAngX) > 1 ||  max(VarAngY) > 1
                    if min(VarAngX) < -1 ||  max(VarAngY) < -1
                        ylim([-inf inf]);
                    else
                        ylim([-1 inf]);
                    end
                elseif min(VarAngX) < -1 ||  max(VarAngY) < -1
                    if max(VarAngX) > 1 ||  max(VarAngY) > 1
                        ylim([-inf inf]);
                    else
                        ylim([-inf 1]);
                    end
                else
                    ylim([-1 1]);
                end
                ylabel(['Tilt variation [' char(176) ']','Color','k']);
                set(gca,'YColor','k');
                yyaxis right
                if max(VarAngX) > 1 ||  max(VarAngY) > 1
                    if min(VarAngX) < -1 ||  max(VarAngY) < -1
                        ylim([-inf inf]);
                    else
                        ylim([min(TempRSN)-7 inf]);
                    end
                elseif min(VarAngX) < -1 ||  max(VarAngY) < -1
                    if max(VarAngX) > 1 ||  max(VarAngY) > 1
                        ylim([-inf inf]);
                    else
                        ylim([-inf max(TempRSN)+7]);
                    end
                else
                end
                ylabel(['Temperature [' char(176) 'C]','Color','k']);
                set(gca,'YColor','k');
                grid on
                grid minor
                
                TempName = char(strcat('Report',siteID,'-', char(toolrif),'-RSN_ENG.png'));
                saveas(RSN_Y,TempName);
                ChartRSN_ENG = Image(TempName);
                ChartRSN_ENG.Style = {ScaleToFit};
                ChartRSN_ENG_cap = Paragraph(['Fig. ' num2str(FIG_ENG) ' - Tilt variation along '...
                    'X and Y directions (perpendicular and parallel to the barrier, respectively) '...
                    'recorded during the reference time period by the MEMS sensor '...
                    'integrated in the BPM module']);
                FIG_ENG = FIG_ENG+1;
                ChartRSN_ENG_cap.Style = {HAlign('justify'),FontSize(Font_caption),Italic(1),Color('midnightblue')};
                
                testo = Paragraph(['The following graphs display the tilt variation recorded during the '...
                    'reference time period by the MEMS sensor integrated in the Rockfall Safety Network - RSN Link'...
                    ', referring to instrumental axes X (perpendicular to the barrier) and Y (parallel to the barrier). ']);
                testo2 = Paragraph(['During the reference time period, the sensors measured a maximum tilt variation along the X axis '...
                    'equal to ' num2str(round(max(abs(VarAngX)),2), '%0.2f')...
                    char(176) '. For what concerns the Y axis, the maximum tilt variation amounts to '...
                    num2str(round(max(abs(VarAngY)),2), '%0.2f') '' char(176) '.']);
                testo.HAlign = 'justify';
                testo2.HAlign = 'justify';
                if titolo == 1
                    sezRSN_ENG = Paragraph('RSN Link');
                    sezRSN_ENG.Style = {FontSize(Font_tools),Bold(1),Italic(1)};
                    add(DT_ENG,sezRSN_ENG);
                end
                add(DT_ENG,testo);
                add(DT_ENG,testo2);
                add(DT_ENG,ChartRSN_ENG);
                add(DT_ENG,ChartRSN_ENG_cap);
                add(DT_ENG,br);
                close(figure(11));
            end
            titolo = 0;
            % pulisco variabili da riutilizzare per catena successiva
            clear AngX;
            clear AngY;
            clear VarAngX;
            clear VarAngY;
            clear TempRSN;
        else
            if ATTIVA == 1
                testo = Paragraph('Non sono presenti dati relativi al periodo temporale considerato.');
                ATTIVA = 0;
                add(DT,testo);
                if activeEN == 1
                    testo = Paragraph('No data available for the reference time period.');
                    add(DT_ENG,testo);
                end
            end
        end
    end
end

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

end