161 lines
3.1 KiB
C++
161 lines
3.1 KiB
C++
|
|
#include "DomoEspConfig.h"
|
|
#include "DomoEspSensorManager.h"
|
|
|
|
|
|
DomoEspManager::DomoEspManager()
|
|
{
|
|
clienteMqtt.setClient(espClient);
|
|
n=0;
|
|
tiempo=0;
|
|
suscrito=false;
|
|
tiempo_sens=0;
|
|
SetTimeRefres(15);
|
|
}
|
|
void DomoEspManager::SetTimeRefres(int seg)
|
|
{
|
|
incre_tsens=seg*1000;
|
|
}
|
|
void DomoEspManager::inicia(DomoEspConfig* cnf)
|
|
{
|
|
conf=cnf;
|
|
#ifdef DEBUG_PS
|
|
Serial.begin(conf->velocidadPortSerie);
|
|
delay(100);
|
|
Serial.println("");
|
|
Serial.println("Iniciando");
|
|
#endif
|
|
#ifdef DEBUG_PS
|
|
Serial.println("Inicia Wifi");
|
|
#endif
|
|
wifi.inicia(&espClient, conf->ssidWifi, conf->keyWifi, conf->ideEsp);
|
|
#ifdef DEBUG_PS
|
|
Serial.println("Inicia Mqtt");
|
|
#endif
|
|
mqtt.inicia(&clienteMqtt,conf->ideEsp, conf->hostMQTT, conf->portMQTT, this);
|
|
|
|
#ifdef DEBUG_PS
|
|
Serial.println("Configura Sensores");
|
|
#endif
|
|
conf->inicia(this);
|
|
|
|
|
|
//pasar funcion de configuracion de añadir sensores
|
|
for(int i=0; i<n; i++)
|
|
{
|
|
sensores[i]->inicia();
|
|
}
|
|
}
|
|
|
|
void DomoEspManager::loop()
|
|
{
|
|
if(!wifi.loop())
|
|
{
|
|
suscrito=false;
|
|
return;
|
|
}
|
|
|
|
if(!mqtt.loop())
|
|
{
|
|
suscrito=false;
|
|
return;
|
|
}
|
|
|
|
if(!suscrito)
|
|
{
|
|
for(int i=0; i<n; i++)
|
|
{
|
|
sensores[i]->SubscribeMqtt(this);
|
|
}
|
|
suscrito=true;
|
|
}
|
|
int tiempoAux=0;
|
|
if(MqttUtiles::pasa_incre(&tiempo_sens, incre_tsens))
|
|
{
|
|
|
|
tiempo=(tiempo+1)%2;
|
|
tiempoAux=tiempo+1;
|
|
#ifdef DEBUG_PS
|
|
Serial.print("Procesa Tiempo: ");
|
|
Serial.print(tiempoAux);
|
|
Serial.println("------------ ");
|
|
#endif
|
|
}
|
|
|
|
for(int i=0; i<n; i++)
|
|
{
|
|
sensores[i]->procesa(this, tiempoAux);
|
|
}
|
|
#ifdef DEBUG_PS
|
|
if(tiempoAux>0)
|
|
Serial.println("----------------------------- ");
|
|
#endif
|
|
}
|
|
|
|
void DomoEspManager::OnMqtt(char* topic, char* payload)
|
|
{
|
|
char buf[MAXTOPICVAR];
|
|
strcpy(buf, topic);
|
|
int nt=strlen(buf);
|
|
if(nt<4)
|
|
{
|
|
#ifdef DEBUG_PS
|
|
Serial.print("se descarta por topic corto:");
|
|
Serial.println(buf);
|
|
#endif
|
|
return;
|
|
}
|
|
int tipo=Topic::NO_RECONOCIDO;
|
|
if(!strcmp("/get", &buf[nt-4]))
|
|
tipo=Topic::GET;
|
|
else if(!strcmp("/set", &buf[nt-4]))
|
|
tipo=Topic::SET;
|
|
else if(!strcmp("/pul", &buf[nt-4]))
|
|
tipo=Topic::PUL;
|
|
|
|
if(tipo!=Topic::NO_RECONOCIDO)
|
|
buf[nt-4]=0;
|
|
#ifdef DEBUG_PS
|
|
Serial.print("OnMqtt ");
|
|
Serial.print(topic);
|
|
Serial.print(" -> ");
|
|
Serial.println(payload);
|
|
#endif
|
|
for(int i=0; i<n; i++)
|
|
{
|
|
sensores[i]->OnMqtt(this, buf, payload, tipo);
|
|
}
|
|
|
|
}
|
|
|
|
void DomoEspManager::SubscribeMqtt(PubSubClient *client_mqtt)
|
|
{
|
|
for(int i=0; i<n; i++)
|
|
{
|
|
sensores[i]->SubscribeMqtt(this);
|
|
}
|
|
}
|
|
|
|
void DomoEspManager::MqttSend(char* topic, char* payload)
|
|
{
|
|
clienteMqtt.publish(topic, payload);
|
|
}
|
|
void DomoEspManager::MqttSubs(char* topic)
|
|
{
|
|
clienteMqtt.subscribe(topic);
|
|
}
|
|
|
|
void DomoEspManager::Add(DomoEspSensorReceiver* sensor)
|
|
{
|
|
if(n<MAXSENS)
|
|
{
|
|
sensores[n]=sensor;
|
|
n++;
|
|
}
|
|
else
|
|
{
|
|
#ifdef DEBUG_PS
|
|
Serial.println("----------------------ERROR NO ENTRAN MAS SENSORES---------------- ");
|
|
#endif
|
|
}
|
|
} |