#include <SPI.h>



#include <PubSubClient.h>
#include <Wire.h>

#include <EEPROM.h>
#include "MqttUtiles.h"
//**************************************************************************************************************************************************

bool MqttUtiles::pasa_incre( unsigned long *tt, unsigned long incre)
{
  unsigned long t=millis();
  if(t<*tt)
  {
    *tt=t;
    return true;
  }
  if((*tt+incre)>=t)
    return false;
 *tt=t;
  return true;
}

bool MqttUtiles::pasa_incre( volatile unsigned long *tt, unsigned long incre)
{
  unsigned long t=millis();
  if(t<*tt)
  {
    *tt=t;
    return true;
  }
  if((*tt+incre)>=t)
    return false;
 *tt=t;
  return true;
}
//**************************************************************************************************************************************************
WifiManager::WifiManager()
{
    
}
  
void WifiManager::inicia( WiFiClient *espclient,char *ssid, char* key)
{
   espClient= espclient;
   strcpy(nred, ssid);
   strcpy(pass, key);
}

bool WifiManager::loop()
{
    if((WiFi.status() == WL_CONNECTED))
     {
      return true;
     }
     #ifdef DEBUG_PS
     Serial.println("Conectando wifi");
     Serial.println(nred);
     Serial.println(pass);
     #endif
    WiFi.mode(WIFI_STA);
    WiFi.begin(nred, pass);

    for(int i=0; i<100 && WiFi.status() != WL_CONNECTED; i++)
    {
      delay(500);
         #ifdef DEBUG_PS
      Serial.print(".");
     #endif
    }
     #ifdef DEBUG_PS
     Serial.println("");
     if(WiFi.status() == WL_CONNECTED)
      Serial.println("Wifi Conectado");
      else
           Serial.println("No se pudo conectar");
     #endif
    return WiFi.status() == WL_CONNECTED;
}
//**************************************************************************************************************************************************
MqttManager::MqttManager()
{
  
}

void MqttManager::inicia(PubSubClient *mqttClient,char *ideEsp, char *host, int port, MqttReceiver* classReceiver)
{
  strcpy(idEsp,ideEsp);
  Mqttlistener=classReceiver;
  client_mqtt=mqttClient;
  client_mqtt->setServer(host, port);
  client_mqtt->setCallback(MqttManager::OnMqtt);
}

bool MqttManager::loop()
{
   if(client_mqtt->loop())
    return true;
    #ifdef DEBUG_PS
     if(WiFi.status() == WL_CONNECTED)
      Serial.println("Conectando a broker");
     #endif
   if(client_mqtt->connect(idEsp))
  {
    #ifdef DEBUG_PS
     if(WiFi.status() == WL_CONNECTED)
      Serial.println("Conectado a broker");
     #endif
     subscribe_mqtt();
     return true;
     
  }
}
void MqttManager::subscribe_mqtt()
{
  if(Mqttlistener==NULL)
    return;
   Mqttlistener->SubscribeMqtt(client_mqtt);
}
//auxiliar------------------
void MqttManager::OnMqtt(char* topic, byte* payload, unsigned int length)
{
  if(Mqttlistener==NULL)
  return;
   int i;
  char buf[32];
  i=31;
  if(i>length)
    i=length;
  memcpy(buf, payload, i);
  buf[i]=0;
  Mqttlistener->OnMqtt(topic, buf);
  
}

//**************************************************************************************************************************************************
void Ceprom_manager::fin_grabacion()
{
   EEPROM.commit();
}

Ceprom_manager::Ceprom_manager()
{
  nb=0;
}

void Ceprom_manager::leeb(byte *p)
{
  *p=EEPROM.read(nb++);
}

void Ceprom_manager::grabab(byte p)
{
  EEPROM.write(nb++, p);
}

void Ceprom_manager::graba_st(char *st)
{
  for(int i=strlen(st); i>0; i--)
  {
    Ceprom_manager::grabab(*st);
    st++;
  }
}

void Ceprom_manager::cursor(int pos)
{
  nb=pos;
}
