#include "GSMMannager.h"
// #include "SoftwareSerial.h"
// uint32_t AIR_AutoBaud();

const char apn[]      = "CMNET";
const char gprsUser[] = "";
const char gprsPass[] = "";

HardwareSerial *MySerial;
GSMMannger::GSMMannger(int serialnumber, int rx, int tx)
{   
    pinMode(37, OUTPUT); //pin32控制sim800C电源 高电平上电   低电平断电
    digitalWrite(37, HIGH);
    vTaskDelay(1000*10);
    // digitalWrite(37, LOW);

    MySerial=new HardwareSerial(serialnumber);

    modem = new TinyGsm(*MySerial);
    MySerial->begin(115200, SERIAL_8N1, rx, tx);
    client = new TinyGsmClient(*modem);
    // !!!!!!!!!!! �ϵ�
    // Serial.println("Wait...");
    write_log(log_path,"Wait...",10);

    // Serial.println("Wait...1");
    write_log(log_path,"Wait...1",10);
    vTaskDelay(1800);
    // Set your reset, enable, power pins here
    // !!!!!!!!!!!

    vTaskDelay(5000);
// Set GSM module baud rate
//   MySerial->readString();
    int banddd=TinyGsmAutoBaud(*MySerial, rx, tx, 9600,460800);
    if (banddd==0)
    {
        // Serial.println("fail to connect sim800  reboot");
        write_log(log_path,"fail to connect sim800  reboot",10);
        esp_restart();
        /* code */
    }
    
    // Serial.println("band is" + String(banddd));
    write_log(log_path,"band is" + String(banddd),10);
    vTaskDelay(6000);
    modem->init();

    String modemInfo = modem->getModemInfo();
    if (modemInfo=="")
    {
        // Serial.println("fail to getmode reboot");
        write_log(log_path,"fail to getmode reboot",10);
        esp_restart();
        /* code */
    }
    // Serial.print("Modem Info: ");
    write_log(log_path,"Modem Info: ",10);

    //以后用4G再
    // SerialMon.println(modemInfo);
    write_log(log_path,modemInfo,10);
    if (GSM_PIN && modem->getSimStatus() != 3)
    {
        modem->simUnlock(GSM_PIN);
    }
    // SerialMon.print("Waiting for network...");
    write_log(log_path,"Waiting for network...",10);
    if (!modem->waitForNetwork())
    {
        // SerialMon.println(" fail");
        write_log(log_path,"fail",10);
        vTaskDelay(10000);
        return;
    }
    // SerialMon.println(" success");
    write_log(log_path,"success",10);

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

    if (modem->isGprsConnected())
    {
        // SerialMon.println("GPRS connected");
        write_log(log_path,"GPRS connected",10);
    }
    modem->NTPServerSync();
}




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();
            vTaskDelay(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();
                vTaskDelay(10000);
                return;
            }
            if (modem->isGprsConnected())
            {
                SerialMon.println("GPRS reconnected");
            }
        }
    }
}

