//
#include "NtpDateTime.h"
//

extern CSerialUart UartPC;


CNtpDateTime::CNtpDateTime(void)
{
}
//
// Property
IPAddress CNtpDateTime::GetLocalIPAddress(void)
{
    return WiFi.localIP();
}
//
bool CNtpDateTime::IsConnected(void)
{
    return (WiFi.status() == WL_CONNECTED);
}
//
// Handler
void CNtpDateTime::ConnectWifi(const char* ssid, const char* password)
{
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
  delay(200);
  WiFi.mode(WIFI_MODE_STA);
  WiFi.begin(ssid, password);  
}
//
void CNtpDateTime::DisconnectWifi(void)
{
    WiFi.disconnect(true);
}
//
void CNtpDateTime::SetWifiModeOff(void)
{
    WiFi.mode(WIFI_OFF);
}
//
void CNtpDateTime::Initialise(const char* ntpserver, 
                              long gmtoffset, int daylightoffset)
{
    configTime(gmtoffset, daylightoffset, ntpserver);
    this->GetDateTimeS();
}
//
const char* CNtpDateTime::GetDateTimeS(void)
{
    struct tm DateTime;
    if(getLocalTime(&DateTime))
    {
        sprintf(FBuffer, "%2.2u%2.2u%2.2u%2.2u%2.2u%2.2u", 
                DateTime.tm_year - 100, 1 + DateTime.tm_mon, DateTime.tm_mday, 
                DateTime.tm_hour, DateTime.tm_min, DateTime.tm_sec);
        return FBuffer;
    } 
    return "";
}
//
const char* CNtpDateTime::GetDateS(void)
{
    struct tm DateTime;
    if(getLocalTime(&DateTime))
    {
        sprintf(FBuffer, "%2.2u.%2.2u.20%2.2u", 
                DateTime.tm_mday, 1 + DateTime.tm_mon, DateTime.tm_year - 100);
        return FBuffer;
    } 
    return "";
}
//
const char* CNtpDateTime::GetTimeS(void)
{
    struct tm DateTime;
    if(getLocalTime(&DateTime))
    {
        sprintf(FBuffer, "%2.2u:%2.2u:%2.2u", 
                DateTime.tm_hour, DateTime.tm_min, DateTime.tm_sec);
        return FBuffer;
    } 
    return "";
}
//