function checkBattery(Batteria,IDcentralina,Mail,conn,FileName) text = 'checkBattery function started'; fileID = fopen(FileName,'a'); fmt = '%s \r'; fprintf(fileID,fmt,text); fclose(fileID); comando = ['select type_id from units where name = ''' IDcentralina ''' ']; curs = exec(conn,comando); curs = fetch(curs); Type = cell2mat(curs.Data); if Type == 1 || Type == 2 || Type == 3 || Type == 4 % G801, G802, G201, G301 Batt = 12; Threshold = 11; Dato = cellstr('12 V'); elseif Type == 5 % D2W Batt = 6; Threshold = 5.6; Dato = cellstr('6 V'); elseif Type == 7 % NESA Batt = 3.4; Threshold = 3; Dato = cellstr('3.4 V'); end FileNameBattery = strcat(IDcentralina,'-Batteria.txt'); if isfile(FileNameBattery) == 0 % NON Esiste outdat = fopen(FileNameBattery,'wt+'); fileID_site = fopen(FileNameBattery,'a'); fmt = '%f \r'; fprintf(fileID_site,fmt,Batt); fmt = '%.10f \r'; text = now; fprintf(fileID_site,fmt,text); fclose(fileID_site); end % Scarico dati di riferimento FileNameBattery = ['' IDcentralina '-Batteria.txt']; Dati = importdata(FileNameBattery); [rA,~] = size(Dati); if rA == 0 Livello_Rif = 0; Data_Rif = 0; elseif rA == 1 Livello_Rif = Dati(1,1); Data_Rif = 0; else Livello_Rif = Dati(1,1); Data_Rif = Dati(2,1); end Level = cell2mat(Batteria(end,3)); activate = 0; if Level < Threshold if now-Data_Rif > 1 || Level < Livello_Rif Data_Rif = now; activate = 1; end end if activate == 0 Mail = 0; end if Mail == 1 DATAinsert = cell(1,6); sms = 0; desc = Dato; Data = datestr(strjoin([Batteria(end,1) Batteria(end,2)]),'yyyy-mm-dd HH:MM:SS'); Livello = cell2mat(Batteria(end,3)); DATAinsert{1,1} = 2; % Allarme tipologia batteria DATAinsert{1,2} = IDcentralina; DATAinsert{1,3} = Data; DATAinsert{1,4} = Livello; DATAinsert{1,5} = sms; DATAinsert{1,6} = desc; % Cerco se il dato è già presente comando = ['select id, type_id, date_time from alarms where unit_name = ''' ... IDcentralina ''' and date_time = ''' Data ''' and type_id = ''' DATAinsert{1,1} ... ''' and registered_value = ''' Livello ''' and description like ''' desc ''' order by date_time']; 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'; colnames = {'type_id','unit_name','date_time','battery_level','send_sms','description'}; data = [DATAinsert(1,1),DATAinsert(1,2),DATAinsert(1,3),DATAinsert(1,4),... DATAinsert(1,5),DATAinsert(1,6)]; if idElabData == 0 % Scrivo fastinsert(conn,tablename,colnames,data); text = ['ALERT was written in the DB by checkBattery function for date: ''' Data ''' ']; end fileID = fopen(FileName,'a'); fmt = '%s \r'; fprintf(fileID,fmt,text); fclose(fileID); end outdat = fopen(FileNameBattery,'wt+'); fileID_site = fopen(FileNameBattery,'a'); text_B = cell2mat(Batteria(end,3)); fmt = '%f \r'; fprintf(fileID_site,fmt,text_B); fmt = '%.10f \r'; text = Data_Rif; fprintf(fileID_site,fmt,text); fclose(fileID_site); text = 'checkBattery function worked correctly'; fileID = fopen(FileName,'a'); fmt = '%s \r'; fprintf(fileID,fmt,text); fclose(fileID); end