function checkSIM(IDcentralina,conn,FileName)

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

comando = ['select sim_card_id from units where name = ''' IDcentralina ''' '];
curs = exec(conn,comando);
curs = fetch(curs);
sim_ID = cell2mat(curs.Data);

if isnan(sim_ID) == 0
    if sim_ID ~= 0
        comando = ['select name, duedate from sim_cards where id = ''' num2str(sim_ID) ''' '];
        curs = exec(conn,comando);
        curs = fetch(curs);
        SIM = curs.Data;
        Nome = char(SIM(1));
        
        FileNameSIM = ['' Nome '-Deadline.txt'];
        if isfile(FileNameSIM) == 0 % NON Esiste
            outdat = fopen(FileNameSIM,'wt+');
            fileID_site = fopen(FileNameSIM,'a');
            text = now;
            fmt = '%f \r';
            fprintf(fileID_site,fmt,text);
            fclose(fileID_site);
        end
        
        % Scarico dati di riferimento
        FileNameSIM = ['' IDcentralina '-Deadline.txt'];
        Data_Rif = importdata(FileNameSIM);
        [rA,~] = size(Data_Rif);
        if rA == 0
            Data_Rif = 0;
        end
        
        % Controllo la data di scadenza della SIM
        Date = SIM(2);
        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) % sim 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} = 7; % Allarme scadenza SIM
                DATAinsert{1,2} = Nome;
                DATAinsert{1,3} = Data;
                DATAinsert{1,4} = sms;
                if Info == 1
                    DATAinsert{1,5} = desc;
                end
                
                % Cerco se il dato è già presente
                if Info == 1
                    comando = ['select id, type_id, date_time from alarms where tool_name = ''' ...
                        Nome ''' 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 tool_name = ''' ...
                        Nome ''' 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','tool_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','tool_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 checkSIM function for date: ''' Data ''' '];
                end
                fileID = fopen(FileName,'a');
                fmt = '%s \r';
                fprintf(fileID,fmt,text);
                fclose(fileID);
            end
            outdat = fopen(FileNameSIM,'wt+');
            fileID_site = fopen(FileNameSIM,'a');
            text = now;
            fmt = '%f \r';
            fprintf(fileID_site,fmt,text);
            fclose(fileID_site);
        end
    end
end

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

end