Files
matlab-python/RSN/checkBattery.m

138 lines
3.9 KiB
Matlab
Executable File

function checkBattery(Batteria,Batteria_LoRa,IDcentralina,Unit,Mail,conn,FileName)
text = 'checkBattery function started';
fileID = fopen(FileName,'a');
fmt = '%s \r';
fprintf(fileID,fmt,text);
fclose(fileID);
if strcmp(Unit,'G801') == 1 || strcmp(Unit,'G802') == 1 || strcmp(Unit,'G201') == 1 ...
|| strcmp(Unit,'G301') == 1 % G801, G802, G201, G301
Batt = 12;
Threshold = 11;
Dato = cellstr('12 V');
elseif strcmp(Unit,'D2W') == 1
Batt = 6;
Threshold = 5.6;
Dato = cellstr('6 V');
elseif strcmp(Unit,'Nesa') == 1
Batt = 3.4;
Threshold = 3;
Dato = cellstr('3.4 V');
elseif strcmp(Unit,'Nesa evolution') == 1
Batt = 12.2;
Threshold = 11.8;
Dato = cellstr('12.2 V');
elseif strcmp(Unit,'CRX1000') == 1
Batt = 12;
Threshold = 10;
Dato = cellstr('12 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
if strcmp(Unit,'Nesa evolution') == 1
Level = str2double(cell2mat(Batteria(end,2)));
else
Level = cell2mat(Batteria(end,3));
end
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;
if strcmp(Unit,'Nesa evolution') == 1
Data = datestr(datenum(Batteria(end,1)),'yyyy-mm-dd HH:MM:SS');
else
Data = datestr(strjoin([Batteria(end,1) Batteria(end,2)]),'yyyy-mm-dd HH:MM:SS');
end
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