#include "GSMMannager.h"
HardwareSerial *MySerial;
GSMMannger::GSMMannger(int serialnumber, int rx, int tx)
{
    m_serialnumber = serialnumber;
    m_rx = rx;
    m_tx = tx;
   
}

bool GSMMannger::setpu()
{
     MySerial=new HardwareSerial(m_serialnumber);
       Serial.println("Wait...123123");
    modem = new TinyGsm(*MySerial);
    MySerial->begin(115200, SERIAL_8N1, m_rx, m_tx);
    client = new TinyGsmClient(*modem);
    // !!!!!!!!!!! �ϵ�
    Serial.println("Wait...");

    #if ARDUINO_USB_CDC_ON_BOOT
        pinMode(37, OUTPUT); //pin32控制sim800C电源 高电平上电   低电平断电
    digitalWrite(37, LOW);
    delay(100);
    digitalWrite(37, HIGH);
    #else


    pinMode(37, OUTPUT); //pin32控制sim800C电源 高电平上电   低电平断电
    digitalWrite(37, HIGH);
    delay(100);
    digitalWrite(37, LOW);
    #endif
    Serial.println("Wait...1");
    delay(1800);
    // Set your reset, enable, power pins here
    // !!!!!!!!!!!

    delay(5000);
    // Set GSM module baud rate
 //   MySerial->readString();
    int banddd=TinyGsmAutoBaud(*MySerial, m_rx, m_tx, 9600,460800);
    if (banddd==0)
    {
        Serial.println("fail to connect sim800  reboot");
        return false;
        /* code */
    }
    
    Serial.println("band is" + String(banddd));
    delay(6000);
    modem->init();

    String modemInfo = modem->getModemInfo();
    if (modemInfo=="")
    {
        Serial.println("fail to getmode reboot");
        return false;
        /* code */
    }
    Serial.print("Modem Info: ");
    
    SerialMon.println(modemInfo);
    if (GSM_PIN && modem->getSimStatus() != 3)
    {
        modem->simUnlock(GSM_PIN);
    }
    SerialMon.print("Waiting for network...");
    if (!modem->waitForNetwork())
    {
        SerialMon.println(" fail");
        delay(10000);
        return false;
    }
    SerialMon.println(" success");

    if (modem->isNetworkConnected())
    {
        SerialMon.println("Network connected");
    }
    SerialMon.print(F("Connecting to "));
    SerialMon.print(apn);
    if (!modem->gprsConnect(apn, gprsUser, gprsPass))
    {
        SerialMon.println(" fail");
        delay(10000);
        return false;
    }
    SerialMon.println(" success");

    if (modem->isGprsConnected())
    {
        SerialMon.println("GPRS connected");
    }
    modem->NTPServerSync();
    return true;
}
String GSMMannger::I2toS(int n)
{
    if (n>=10)
    {
        return String(n);
    } else{
        return "0"+ String(n);
    }
}
String GSMMannger::GetDataAndTime() {
    //String str=modem->getGSMDateTime(DATE_FULL);
    if(modem->getNetworkTime(&year,&month,&day,&hour,&minute, &second,&timezone))
    {
    String date=String(year)+"-"+I2toS(month)+"-"+I2toS(day)+" "+I2toS(hour)+":"+I2toS(minute)+":"+I2toS(second);
    return date;}
    return "-1";
}

void GSMMannger::loop()
{
    
    if (!modem->isNetworkConnected())
    {
       
        SerialMon.println("Network disconnected");
        if (!modem->waitForNetwork(20000L, true))
        {
            SerialMon.println(" re wait For Network fail");
            esp_restart();
            delay(10000);
            return;
        }
        if (modem->isNetworkConnected())
        {
            SerialMon.println("Network re-connected");
        }

        // and make sure GPRS/EPS is still connected
        if (!modem->isGprsConnected())
        {
            SerialMon.println("GPRS disconnected!");
            SerialMon.print(F("Connecting to "));
            SerialMon.print(apn);
            if (!modem->gprsConnect(apn, gprsUser, gprsPass))
            {
                SerialMon.println(" set apn gprsuser gprspass fail");
                esp_restart();
                delay(10000);
                return;
            }
            if (modem->isGprsConnected())
            {
                SerialMon.println("GPRS reconnected");
            }
        }
    }
}


