function [Mail,Users_Alert,Users_SMS,Users_Report,activeIT,activeEN,... sms,siteID,NomeSito,ini_CoV] = Users_Def(IDcentralina,conn,FileName) text = 'Users function started'; fileID = fopen(FileName,'a'); fmt = '%s \r'; fprintf(fileID,fmt,text); % A partire dalla centralina, risalgo al site ID comando = ['select site_id from units where name like ''' IDcentralina ''' ']; curs = exec(conn,comando); curs = fetch(curs); siteID = num2str(cell2mat(curs.Data)); % --- Cerco gli utenti --- % - Definisco gli Users % A partire dal site ID, risalgo al company ID comando = ['select company_id, name, lat, lon, alarm_email from sites where id like ''' siteID ''' ']; curs = exec(conn,comando); curs = fetch(curs); Site = curs.Data; Company_id = num2str(cell2mat(Site(1,1))); Email_Alarm_Site = Site(1,5); NomeSito = Site(1,2); % A partire dal company ID, mi scarico le info relative a Send Email Alarm comando = ['select send_alarm from companies where id like ''' Company_id ''' ']; curs = exec(conn,comando); curs = fetch(curs); Mail = cell2mat(curs.Data); if Mail == 1 % Significa che invio Mail alla Company del Sito % La Mail la mando SOLO alla mail indicata nel campo del Sito (per quanto riguarda la company cliente) if isempty(cell2mat(Email_Alarm_Site)) == 0 if strcmp(char(Email_Alarm_Site),'null') == 0 US = Email_Alarm_Site; check = cell2mat(strfind(US,',')); if isempty(check)==1 % A partire dal company ID, risalgo agli utenti e alla loro mail di % quella company che sono admin comando = ['select fname, lname, language_id, info, phone, sms_alarm, report, report_cov, '... 'allertamento from users where email like '''... char(US) ''' ']; curs = exec(conn,comando); curs = fetch(curs); Info = curs.Data; % Cerco la lingua dell'unico destinatario [~,cC] = size(Info); if cC > 1 Users = Info(1:2); Users(3) = US; Users(4:5) = Info(3:4); Users(6) = Info(5); Users(7:10) = Info(6:9); else Users = []; end else [~,cC] = size(check); Users = cell(cC+1,6); u = 1; ini = 1; for i=1:cC+1 utente = char(US); % A partire dal company ID, risalgo agli utenti e alla loro mail di % quella company che sono admin if i == cC+1 comando = ['select fname, lname, language_id, info, phone, sms_alarm, report, report_cov, '... 'allertamento, email from users where email like ''' utente(ini:end) ''' ']; else comando = ['select fname, lname, language_id, info, phone, sms_alarm, report, report_cov, '... 'allertamento, email from users where email like ''' utente(ini:check(i)-1) ''' ']; end curs = exec(conn,comando); curs = fetch(curs); Info = curs.Data; % Cerco la lingua dell'unico destinatario [~,cI] = size(Info); if cI ~= 1 Users(u,1:2) = Info(1:2); Users(u,3) = Info(10); Users(u,4:5) = Info(3:4); Users(u,6) = Info(5); Users(u,7:10) = Info(6:9); u = u+1; end if i ~= cC+1 ini = check(i)+1; end end Users = Users(1:u-1,:); end else % Company Manager Role = ' 2 '; % A partire dal company ID, risalgo agli utenti e alla loro mail di % quella company che sono admin comando = ['select fname, lname, email, language_id, info, phone, sms_alarm, report, report_cov, '... 'allertamento from users where company_id like ''' Company_id ''' and role_id = ''' Role ''' ']; curs = exec(conn,comando); curs = fetch(curs); Users = curs.Data; end else % Company Manager Role = ' 2 '; % A partire dal company ID, risalgo agli utenti e alla loro mail di % quella company che sono admin comando = ['select fname, lname, email, language_id, info, phone, sms_alarm, report, report_cov, '... 'allertamento from users where company_id like ''' Company_id ''' and role_id = ''' Role ''' ']; curs = exec(conn,comando); curs = fetch(curs); Users = curs.Data; end else Users = []; end % Elimino gli utenti che non possono essere allertati [rU,cU] = size(Users); al = 1; sms = 1; rpt = 1; Users_Alert = cell(1,1); Users_SMS = cell(1,1); Users_Report = cell(1,1); if cU > 1 for u = 1:rU if cell2mat(Users(u,10)) == 1 [rUA,cUA] = size(Users_Alert); if rUA == 1 && cUA == 1 clear Users_Alert end Users_Alert(al,:) = Users(u,:); al = al+1; if cell2mat(Users(u,7)) == 1 [rUA,cUA] = size(Users_SMS); if rUA == 1 && cUA == 1 clear Users_SMS end Users_SMS(sms,:) = Users(u,:); sms = sms+1; end end if cell2mat(Users(u,8)) == 1 [rUA,cUA] = size(Users_Report); if rUA == 1 && cUA == 1 clear Users_Report end Users_Report(rpt,:) = Users(u,:); rpt = rpt+1; end end end ini_CoV = al; % Cerco se allertare o meno i Cone of Visions comando = ['select conn_path from units where name like ''' IDcentralina ''' ']; curs = exec(conn,comando); curs = fetch(curs); Auth = curs.Data; % Se pari a 1 autorizzo i Cone of Visions, altrimenti no if strcmp(char(Auth),'Yes') Mail = 1; % Cone of vision % A partire dal site ID, risalgo al USER ID degli utenti nel % cone-of-vision per quel sito comando = ['select user_id from coneofvisions where site_id like ''' siteID ''' ']; curs = exec(conn,comando); curs = fetch(curs); CoV = cell2mat(curs.Data); % A partire dal USER ID, risalgo agli utenti e alla loro email se sono company manager [rC, ~] = size(CoV); if strcmp(char(CoV(1,:)),'No Data') % almeno un utente da cercare nel database Users_CoV = []; else ii = 1; for i=1:rC Utente = num2str(CoV(i,1)); Role = ' 2 '; comando = ['select fname, lname, email, language_id, info, phone, sms_alarm, report, report_cov, '... 'allertamento from users where id = ''' Utente ''' and role_id = ''' Role ''' ']; curs = exec(conn,comando); curs = fetch(curs); UC = curs.Data; [~,cUC] = size(UC); if cUC ~= 1 Users_CoV(ii,:) = UC; ii = ii+1; end end end [rUC,cUC] = size(Users_CoV); [rUA,cUA] = size(Users_Alert); if cUC > 1 for u = 1:rUC if cell2mat(Users_CoV(u,10)) == 1 && cell2mat(Users_CoV(u,9)) == 1 salta = 0; for cov = 1:rUA if cUA > 1 A = strfind(char(Users_CoV(u,3)),char(Users_Alert(cov,3))); if A == 1 salta = 1; break end end end if salta == 0 if cUA > 1 Users_Alert(al,:) = Users_CoV(u,:); else Users_Alert = Users_CoV(u,:); end al = al+1; if cell2mat(Users_CoV(u,7)) == 1 Users_SMS(sms,:) = Users_CoV(u,:); sms = sms+1; end end end if cell2mat(Users_CoV(u,9)) == 1 salta = 0; for cov = 1:rUA if cUA > 1 A = strfind(char(Users_CoV(u,3)),char(Users_Alert(cov,3))); if A == 1 salta = 1; break end end end if salta == 0 [rUA,cUA] = size(Users_Report); if rUA == 1 && cUA == 1 clear Users_Report end Users_Report(rpt,:) = Users_CoV(u,:); rpt = rpt+1; end end end end else Users_CoV = []; end [rU,cU] = size(Users); [rC,cC] = size(Users_CoV); if cC > 1 && rC > 0 && cU > 1 % comando ha trovato informazioni e le ha scritte in Users Users(rU+1:rU+rC,1:10) = Users_CoV; elseif cC > 1 && rC > 0 && cU == 0 Users = Users_CoV; end [rU,~] = size(Users); if rU == 0 Mail = 0; end % Controllo che la centralina non sia scaduta comando = ['select duedate from units where name = ''' IDcentralina ''' ']; curs = exec(conn,comando); curs = fetch(curs); Date = curs.Data; Check = cell2mat(Date); if strcmp(Check,'null') || strcmp(Check,'No Data') % La data di scadenza centralina non è impostata sms = 1; else if datenum(Date) < now % Centralina scaduta sms = 0; Users_Alert = []; Users_SMS = []; Users_Report = []; else sms = 1; end end % A chi mando la mail activeIT = 0; activeEN = 0; [~,c1] = size(Users_Alert); [~,c2] = size(Users_Report); if c1 == 1 && c2 == 1 Users_Lang = []; elseif c1 == 1 Users_Lang = Users_Report; elseif c2 == 1 Users_Lang = Users_Alert; else Users_Lang = [Users_Alert; Users_Report]; end [rU,cU] = size(Users_Lang); if cU >1 % sono presenti utenti a cui inviare la mail for i = 1:rU if Users_Lang{i,4} == 2 activeIT = 1; else activeEN = 1; end end end text = 'Users function worked correctly'; fprintf(fileID,fmt,text); fclose(fileID); end