function [ATTIVA,FIG,FIG_ENG] = report_RSNHR(rRSNHR,m,NodoRSNHRLink,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_RSNHR 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 h = 1:rRSNHR(m,1)
        NodeNum = num2str(NodoRSNHRLink(h,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_HR = cell2mat(Dati(:,3)); % asse x assoluti
            AngY_HR = cell2mat(Dati(:,4)); % asse y assoluti
            TempRSNHR = cell2mat(Dati(:,5)); % temp
            [d,~] = size(AngX_HR);
            for ind = 2:d
                VarAngX_HR(ind,1) = AngX_HR(ind,1)-AngX_HR(ind-1,1); % variazioni asse x
                VarAngY_HR(ind,1) = AngY_HR(ind,1)-AngY_HR(ind-1,1); % variazione asse y
            end
            
            % grafico inclinazione-tempo asse X,Y NELL'ULTIMO MESE
            figure(1)
            yyaxis left
            RSNHR_X = plot(Date,VarAngX_HR,'-','LineWidth',2);
            hold on
            RSNHR_Y = plot(Date,VarAngY_HR,'-','Color','red','LineWidth',2);
            hold off
            yyaxis right
            RSNHR_Temp = plot(Date,TempRSNHR,':','LineWidth',1.2);
            title('Rockfall Safety Network - RSN Link HR');
            xlabel('Data [gg/mm/aaaa]');
            lgd = legend('Asse X','Asse Y','Temperatura','Location','south');
            lgd.NumColumns = 3;
            gra = gca;
            gra.XAxis.MinorTick = 'on';
            datetick('x','dd/mm/yyyy','keepticks','keeplimits');
            xtickangle(12);
            yyaxis left
            if max(VarAngX_HR) > 1 ||  max(VarAngY_HR) > 1
                if min(VarAngX_HR) < -1 ||  max(VarAngY_HR) < -1
                    ylim([-inf inf]);
                else
                    ylim([-1 inf]);
                end
            elseif min(VarAngX_HR) < -1 ||  max(VarAngY_HR) < -1
                if max(VarAngX_HR) > 1 ||  max(VarAngY_HR) > 1
                    ylim([-inf inf]);
                else
                    ylim([-inf 1]);
                end
            else
                ylim([-1 1]);
            end
            set(gca,'YColor','k');
            ylabel(['Variazione di inclinazione [' char(176) ']','Color','k']);
            yyaxis right
            if max(VarAngX_HR) > 1 ||  max(VarAngY_HR) > 1
                if min(VarAngX_HR) < -1 ||  max(VarAngY_HR) < -1
                else
                    ylim([min(TempRSNHR)-7 inf]);
                end
            elseif min(VarAngX_HR) < -1 ||  max(VarAngY_HR) < -1
                if max(VarAngX_HR) > 1 ||  max(VarAngY_HR) > 1
                else
                    ylim([-inf max(TempRSNHR)+7]);
                end
            end
            ylabel(['Temperatura [' char(176) 'C]','Color','k']);
            set(gca,'YColor','k');
            grid on
            grid minor
            
            TempName = char(strcat('Report',siteID,'-', char(toolrif),'-RSNHR.png'));
            saveas(RSNHR_Temp,TempName);
            ChartRSNHR = Image(TempName);
            ChartRSNHR.Style = {ScaleToFit};
            ChartRSNHR_cap = Paragraph(['Fig. ' num2str(FIG) ' - Variazione dell''inclinazione'...
                ' in direzione X (perpendicolarmente rispetto alla barriera) e Y (trasversalmente alla barriera)'...
                ' registrata dal sensore elettrolitico, integrato nel '...
                'modulo BPM, durante il periodo temporale di riferimento']);
            FIG = FIG+1;
            ChartRSNHR_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 elettrolitico incluso'...
                ' nel Rockfall Safety Network - RSN Link HR, relativamente agli assi strumentali'...
                ' X (perpendicolare alla barriera) e Y (trasversale alla barriera). ']);
            testo2 = Paragraph(['In questo periodo di tempo, l''ampolla elettrolitica ha'...
                ' rilevato una variazione di inclinazione massima lungo l''asse X '...
                'pari a ' num2str(round(max(abs(VarAngX_HR)),2), '%0.2f')...
                char(176) '. Per quanto riguarda l''asse Y, la massima variazione risulta '...
                ' pari a ' num2str(round(max(abs(VarAngY_HR)),2), '%0.2f') '' char(176) ' di inclinazione.']);
            testo.HAlign = 'justify';
            testo2.HAlign = 'justify';
            if titolo == 1
                sezRSNHR = Paragraph('RSN Link HR');
                sezRSNHR.Style = {FontSize(Font_tools),Bold(1),Italic(1)};
                add(DT,sezRSNHR);
            end
            add(DT,testo);
            add(DT,testo2);
            add(DT,ChartRSNHR);
            add(DT,ChartRSNHR_cap);
            add(DT,br);
            close(figure(1));
            
            if activeEN == 1
                % grafico inclinazione-tempo asse X,Y NELL'ULTIMO MESE
                figure(1)
                yyaxis left
                RSNHR_X = plot(Date,VarAngX_HR,'-','LineWidth',2);
                hold on
                RSNHR_Y = plot(Date,VarAngY_HR,'-','Color','red','LineWidth',2);
                hold off
                yyaxis right
                RSNHR_Temp = plot(Date,TempRSNHR,':','LineWidth',1.2);
                title('Rockfall Safety Network - RSN Link HR');
                xlabel('Data [gg/mm/aaaa]');
                lgd = legend('X axis','Y axis','Temperature','Location','south');
                lgd.NumColumns = 3;
                gra = gca;
                gra.XAxis.MinorTick = 'on';
                datetick('x','dd/mm/yyyy','keepticks','keeplimits');
                xtickangle(12);
                yyaxis left
                if max(VarAngX_HR) > 1 ||  max(VarAngY_HR) > 1
                    if min(VarAngX_HR) < -1 ||  max(VarAngY_HR) < -1
                        ylim([-inf inf]);
                    else
                        ylim([-1 inf]);
                    end
                elseif min(VarAngX_HR) < -1 ||  max(VarAngY_HR) < -1
                    if max(VarAngX_HR) > 1 ||  max(VarAngY_HR) > 1
                        ylim([-inf inf]);
                    else
                        ylim([-inf 1]);
                    end
                else
                    ylim([-1 1]);
                end
                set(gca,'YColor','k');
                ylabel(['Tilt variation [' char(176) ']','Color','k']);
                yyaxis right
                if max(VarAngX_HR) > 1 ||  max(VarAngY_HR) > 1
                    if min(VarAngX_HR) < -1 ||  max(VarAngY_HR) < -1
                    else
                        ylim([min(TempRSNHR)-7 inf]);
                    end
                elseif min(VarAngX_HR) < -1 ||  max(VarAngY_HR) < -1
                    if max(VarAngX_HR) > 1 ||  max(VarAngY_HR) > 1
                    else
                        ylim([-inf max(TempRSNHR)+7]);
                    end
                end
                ylabel(['Temperature [' char(176) 'C]','Color','k']);
                set(gca,'YColor','k');
                grid on
                grid minor
                
                TempName = char(strcat('Report',siteID,'-', char(toolrif),'-RSNHR_ENG.png'));
                saveas(RSNHR_Temp,TempName);
                ChartRSNHR_ENG = Image(TempName);
                ChartRSNHR_ENG.Style = {ScaleToFit};
                ChartRSNHR_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 electrolytic sensor '...
                    'integrated in the BPM module']);
                FIG_ENG = FIG_ENG+1;
                ChartRSNHR_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 electrolytic sensor integrated in the Rockfall Safety Network - RSNHR 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_HR)),2), '%0.2f')...
                    char(176) '. For what concerns the Y axis, the maximum tilt variation amounts to '...
                    num2str(round(max(abs(VarAngY_HR)),2), '%0.2f') '' char(176) '.']);
                testo.HAlign = 'justify';
                testo2.HAlign = 'justify';
                if titolo == 1
                    sezRSNHR_ENG = Paragraph('RSN Link HR');
                    sezRSNHR_ENG.Style = {FontSize(Font_tools),Bold(1),Italic(1)};
                    add(DT_ENG,sezRSNHR_ENG);
                end
                add(DT_ENG,testo);
                add(DT_ENG,testo2);
                add(DT_ENG,ChartRSNHR);
                add(DT_ENG,ChartRSNHR_cap);
                add(DT_ENG,br);
                close(figure(1));
            end
            titolo = 0;
            %pulisco variabili da riutilizzare per catena successiva
            clear AngX_HR;
            clear AngY_HR;
            clear VarAngX_HR;
            clear VarAngY_HR;
            clear TempRSNHR;
        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_RSNHR function executed correctly';
fprintf(fileID,fmt,text);
fclose(fileID);

end