
#include "DomoEspManager.h"
DomoEspManager::DomoEspManager()
{
  vars.config(NULL, NULL);
  vars.AddListenner(&sens);
  vars.AddListenner(&des);
  sens.Config(&vars);
  suscrito=false;
  
}

void DomoEspManager::inicia( LiquidCrystal *lcd, PubSubClient *mqttClient, WiFiClient* espClient, DomoConfig* conf)
{
  #ifdef DEBUG_PS
     Serial.println("Inicia Wifi");
     #endif
  wifi.inicia(espClient, conf->ssidWifi, conf->keyWifi);
  #ifdef DEBUG_PS
     Serial.println("Inicia Mqtt");
     #endif
  mqtt.inicia(mqttClient,conf->ideEsp, conf->hostMQTT, conf->portMQTT, this);
#ifdef DEBUG_PS
     Serial.println("Configura Sensores");
     #endif
  //pasar funcion de configuracion de añadir sensores
  
  des.inicia(&vars);
 conf->ConfigGen(&sens, &vars, &lcdm, &des);
  #ifdef DEBUG_PS
     Serial.println("Inicia variables");
     #endif
  vars.inicia(mqttClient);
  
    #ifdef DEBUG_PS
     Serial.println("Inicia Sensores");
     #endif
  sens.inicia();
    #ifdef DEBUG_PS
     Serial.println("Iniciado esp");
     #endif
   #if CON_LCD
   
  lcdm.inicia(&vars, lcd, conf->lcd.colum, conf->lcd.lines);
  
  #endif
  
}

void DomoEspManager::loop()
{

  if(!wifi.loop())
    {
      suscrito=false;
      return;
    }

  if(!mqtt.loop())
  {
     suscrito=false;
    return;
  }
    
  if(!suscrito)
  {
    vars.Suscribe();
    suscrito=true;
  }
  
  sens.loop();
  vars.loop();
  des.loop();
   #if CON_LCD
  lcdm.loop();
  #endif
}

void DomoEspManager::OnMqtt(char* topic, char* payload)
{
   vars.OnMqtt(topic, payload);
   des.OnMqtt(topic, payload);
}

void DomoEspManager::SubscribeMqtt(PubSubClient *client_mqtt)
{
  
}
