
#include "StatusManager.h"

StatusManager::StatusManager(int pinLedSD = -1, int pinLedGPS = -1, int pinLedSMS = -1)
{
  flagStatus = 0;
  timerStatus = 0;
  ledSD = pinLedSD;
  if (ledSD >= 0)
  {
    pinMode(ledSD, OUTPUT);
    digitalWrite (ledSD, LOW);
  }
  ledSMS = pinLedSMS;
  if (ledSMS >= 0)
  {
    pinMode(ledSMS, OUTPUT);
    digitalWrite (ledSMS, LOW);
  }
  ledGPS = pinLedGPS;
  if (ledGPS >= 0)
  {
    pinMode(ledGPS, OUTPUT);
    digitalWrite (ledGPS, LOW);
  }

}
void StatusManager::EmpiezaInicio()
{
  /*if(ledSD>=0)
    {
    digitalWrite (ledSD, HIGH);
    }*/
  if (ledSMS >= 0)
  {
    digitalWrite (ledSMS, HIGH);
  }
  if (ledGPS >= 0)
  {
    digitalWrite (ledGPS, HIGH);
  }

}
void StatusManager::FinInicio()
{
  if (ledSD >= 0)
  {
    digitalWrite (ledSD, LOW);
  }
  if (ledSMS >= 0)
  {
    digitalWrite (ledSMS, LOW);
  }
  if (ledGPS >= 0)
  {
    digitalWrite (ledGPS, LOW);
  }

}
void StatusManager::SetStatusSD(bool ok)
{
  if (ok)
    flagStatus = flagStatus | FLAG_SD;
  else
    flagStatus = flagStatus & ~FLAG_SD;
}

void StatusManager::SetStatusGPS(bool ok)
{
  if (ok)
    flagStatus = flagStatus | FLAG_GPS;
  else
    flagStatus = flagStatus & ~FLAG_GPS;
}

void StatusManager::SetStatusSMS(bool ok)
{
  if (ok)
    flagStatus = flagStatus | FLAG_SMS;
  else
    flagStatus = flagStatus & ~FLAG_SMS;
}


void StatusManager::Procesa()
{
  unsigned long incremento = TIMER_REPORTE_STATUS; //5 seg
  if (Utiles::isTime(&timerStatus, incremento))
  {
    for (int estatus = 0; estatus < 3; estatus++)
    {
      int pin = -1;
      bool enciende = false;
      switch (estatus)
      {
        case (0): //sd
          pin = ledSD;
          enciende = GetStatusSD();

          break;
        case (1): //gps
          pin = ledGPS;
          enciende = GetStatusGPS();
          break;
        case (2): //sms
          pin = ledSMS;
          enciende = GetStatusSMS();
          break;
        default:
          pin = -1;

          break;
      }
      parpadeoLed( pin, enciende);
    }
  }

}

bool StatusManager::GetStatusSD()
{
  return (flagStatus & FLAG_SD) == FLAG_SD;
}

bool StatusManager::GetStatusGPS()
{
  return (flagStatus & FLAG_GPS) == FLAG_GPS;
}

bool StatusManager::GetStatusSMS()
{
  return (flagStatus & FLAG_SMS) == FLAG_SMS;
}

void StatusManager::parpadeoLed(int pled, bool enciende)
{
  if (pled < 0)
    return;
  if (!enciende)
  {
    digitalWrite(pled, LOW);
    delay(TIMER_ON_LED);
    delay(TIMER_ON_LED);
    return;
  }
  digitalWrite(pled, HIGH);
  delay(TIMER_ON_LED);
  digitalWrite(pled, LOW);

}
