function checkUnit(IDcentralina,conn,FileName)

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

FileNameUnit = ['' IDcentralina '-Deadline.txt'];
if isfile(FileNameUnit) == 0 % NON Esiste    
    outdat = fopen(FileNameUnit,'wt+');
    fileID_site = fopen(FileNameUnit,'a');
    text = now;
    fmt = '%f \r';
    fprintf(fileID_site,fmt,text);
    fclose(fileID_site);
end

% Scarico dati di riferimento
FileNameUnit = ['' IDcentralina '-Deadline.txt'];
Data_Rif = importdata(FileNameUnit);
[rA,~] = size(Data_Rif);
if rA == 0
    Data_Rif = 0;
end

% Cerco la data di scadenza della centralina
comando = ['select duedate from units where name = ''' IDcentralina ''' '];
curs = exec(conn,comando);
curs = fetch(curs);
Date = curs.Data;
Check = cell2mat(Date);
Info = 0;
if strcmp(Check,'null') == 1|| strcmp(Check,'No Data') == 1
else
    if now <= datenum(Date) && datenum(Date) < now+30 % fra 1 giorno e 1 Mese
        if datenum(Data_Rif) > datenum(Date)-30 
           mail = 0; % la mail è già stata inviata
        else
            mail = 1;
        end
    elseif now > datenum(Date) && datenum(Data_Rif) < datenum(Date) % centralina scaduta
        mail = 1;
        Info = 1;
        desc = 'Scaduto';
    else
        mail = 0;
    end
    
    if mail == 1
        DATAinsert = cell(1,6);
        sms = 0;
        Data = datestr(now,'yyyy-mm-dd HH:MM:SS');
        DATAinsert{1,1} = 5; % Allarme scadenza centralina
        DATAinsert{1,2} = IDcentralina;
        DATAinsert{1,3} = Data;
        DATAinsert{1,4} = sms;
        if Info == 1
            DATAinsert{1,5} = desc;
        end
        
        if Info == 1
            comando = ['select id, type_id, date_time from alarms where unit_name = ''' ...
                IDcentralina ''' and date_time = ''' Data ''' and type_id = ''' DATAinsert{1,1} ...
                ''' and description like ''' desc ''' order by date_time'];
        else
            comando = ['select id, type_id, date_time from alarms where unit_name = ''' ...
                IDcentralina ''' and date_time = ''' Data ''' and type_id = ''' DATAinsert{1,1} ...
                ''' order by date_time'];
        end
        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 Info == 1
            colnames = {'type_id','unit_name','date_time','send_sms','description'};
            data = [DATAinsert(1,1),DATAinsert(1,2),DATAinsert(1,3),DATAinsert(1,4),DATAinsert(1,5)];
        else
            colnames = {'type_id','unit_name','date_time','send_sms'};
            data = [DATAinsert(1,1),DATAinsert(1,2),DATAinsert(1,3),DATAinsert(1,4)];
        end
        if idElabData == 0 % Scrivo
            fastinsert(conn,tablename,colnames,data);
            text = ['ALERT was written in the DB by checkUnit function for date: ''' Data ''' '];
        end
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
    end

    outdat = fopen(FileNameUnit,'wt+');
    fileID_site = fopen(FileNameUnit,'a');
    text = now;
    fmt = '%f \r';
    fprintf(fileID_site,fmt,text);
    fclose(fileID_site);
end

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

end