Files
matlab-python/Tilt/checkBattery.m

219 lines
6.8 KiB
Matlab
Executable File

function checkBattery(Batteria,Batteria_LoRa,IDcentralina,DTcatena,Unit,Mail,conn,FileName)
text = 'checkBattery function started';
fileID = fopen(FileName,'a');
fmt = '%s \r';
fprintf(fileID,fmt,text);
fclose(fileID);
% Centralina
if strcmp(Unit,'GEI G801') == 1 || strcmp(Unit,'GEI G802') == 1 || strcmp(Unit,'GEI G201') == 1 ...
|| strcmp(Unit,'GEI G301') == 1 % G801, G802, G201, G301
Batt = 12;
Threshold = 11;
Dato = cellstr('12 V');
elseif strcmp(Unit,'GEI 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,'Campbell CRX1000') == 1 || strcmp(Unit,'Campbell CR500') == 1
Batt = 12;
Threshold = 10;
Dato = cellstr('12 V');
elseif strcmp(Unit,'SISGEO WR-GW-868') == 1
Batt = 5;
Threshold = 4.5;
Dato = cellstr('5 V');
elseif strcmp(Unit,'SwissMetNet') == 1
Batt = 12;
Threshold = -2;
Dato = cellstr('12 V');
end
% Modulo LoRa
[rL] = size(Batteria_LoRa);
if rL(2)>1
if isempty(Batteria_LoRa) == 0
if strcmp(Unit,'GEI G802') == 1
Threshold_LoRa = 6.5;
Dato_LoRa = cellstr('7.2 V');
elseif strcmp(Unit,'SISGEO WR-GW-868') == 1
Threshold_LoRa = 3;
Dato_LoRa = cellstr('5 V');
end
else
Threshold_LoRa = [];
Dato_LoRa = [];
end
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
Data_Rif = 0;
elseif rA == 1
Data_Rif = 0;
else
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;
Centralina = 0;
LoRa = 0;
% Check Centralina
if Level < Threshold
if Level > 0
if now-Data_Rif > 1 % || Level < Livello_Rif
Data_Rif = now;
activate = 1;
Centralina = 1;
end
end
end
% Check Modulo LoRa
if isnan(cell2mat(Batteria_LoRa(end,3))) == 0
Level_LoRa = cell2mat(Batteria_LoRa(end,3));
if Level_LoRa < Threshold_LoRa
if Level_LoRa > 0
if now-Data_Rif > 1
Data_Rif = now;
activate = 1;
LoRa = 1;
end
end
end
end
if activate == 0
Mail = 0;
end
if Mail == 1
sms = 0;
% Centralina
if Centralina == 1
DATAinsert = cell(1,6);
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
desc = Dato;
DATAinsert{1,1} = 2; % Allarme tipologia batteria
DATAinsert{1,2} = IDcentralina;
DATAinsert{1,3} = Data;
DATAinsert{1,4} = Level;
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 = ''' Level ''' 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
end
if LoRa == 1
DATAinsert = cell(1,7);
Data = datestr(strjoin([Batteria_LoRa(end,1) Batteria_LoRa(end,2)]),'yyyy-mm-dd HH:MM:SS');
desc = Dato_LoRa;
DATAinsert{1,1} = 2; % Allarme tipologia batteria
DATAinsert{1,2} = IDcentralina;
DATAinsert{1,3} = DTcatena;
DATAinsert{1,4} = Data;
DATAinsert{1,5} = Level_LoRa;
DATAinsert{1,6} = sms;
DATAinsert{1,7} = desc;
% Cerco se il dato è già presente
comando = ['select id, type_id, date_time from alarms where unit_name = ''' ...
IDcentralina ''' and tool_name = ''' DTcatena ''' and date_time = ''' Data ...
''' and type_id = ''' DATAinsert{1,1} ''' and registered_value = ''' Level ...
''' 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','tool_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),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
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 = Level;
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