#include "nettimer.h"
#include "../network_mgr/net_ctrl.h"
#include "../network_mgr/sx1268/lora.h"


OS_TMR netsend_timer; // 5分钟的定时器
OS_TMR netsend_hour_timer; // 24小时的定时器



timeout_t g_timeOut;
timeout_t g_usmTimeOut; // 4G模块的连接超时状态

/*-----------------------------------------------------
 * 一分钟一次
 * ----------------------------------------------------*/
void netsend_msg_timer(void *p_tmr, void *p_arg)
{
    uint8_t i=0,j=0,k=0;
    static uint32_t _tick = 0;
    static uint32_t minutes = 0;
    static uint8_t timer = 1;//,timer1 = 1;

    if(g_runData.bUpdate!=0) return;

    minutes++;
    g_runData.timeSendStatus++;

    _tick++;
    if(_tick>=timer){
        g_runData.bsendgatewayfirmMsg = 1;
        _tick = 0;
        if(timer ==1) timer = 3;
        else if(timer==3) timer = 10;
        else if(timer == 10) timer = 60;
        else timer = 60;
        if((downlink_config.config_flag&0x01) == CONFIG_WRITE){
            g_runData.bRead0x58 = 1;
            g_readCmd58.nodeCur = 0;
            g_readCmd58.stauts[0] = 0;
        }
    }

    if(sys_net.net_hdl && ((downlink_config.config_flag&0x01) == CONFIG_WRITE)){

        for(i=0;i<downlink_config.collect_num;i++)
        {
            for(j=0;j<downlink_config.collect_conf[i].tax_num;j++){
                for(k=0;k<downlink_config.collect_conf[i].tax_conf[j].gun_num;k++){
                    g_runData.daStatus[i][j][k]++;
                }
            }
        }

        if(!g_data4G.bconnect4G) { // 判断4G模块 mqtt是否连接
            if(g_usmTimeOut.flag == 0) {
                g_data4G.flag++;
                if(g_data4G.flag==1) timeout_setValue(&g_usmTimeOut,3*60*1000);
                else if(g_data4G.flag==2) timeout_setValue(&g_usmTimeOut,5*60*1000);
                else {
                    g_data4G.flag = 3;
                    timeout_setValue(&g_usmTimeOut,10*60*1000);
                }
                timeout_start(&g_usmTimeOut);
                printf("start g_usmTimer\r\n");
            }
            printf("g_dat4G.error = %d\r\n",g_data4G.errorTime);
            g_data4G.errorTime++;
            if(g_data4G.errorTime>=60){ // mqtt 一个小时没有连接上，则芯片复位
                 NVIC_SystemReset();//复位
            }
        }
        else {
            g_data4G.errorTime = 0;
            g_data4G.flag = 0;
            timeout_setValue(&g_usmTimeOut,60*1000);
            timeout_stop(&g_usmTimeOut);
        }
    }


}

void netsend_hour_msg_timer(void *p_tmr, void *p_arg)
{
    g_runData.bRead0x58 = 1;
    g_readCmd58.stauts[0] = 0;
    printf("netsend time start %d \r\n",TickCounter);
}

void nettimer_init(void)
{
    OS_ERR err;

    OSTmrCreate(&netsend_timer, "netsend_tmr", NETSEND_MSG_PERIOD, NETSEND_MSG_PERIOD, OS_OPT_TMR_PERIODIC, netsend_msg_timer, NULL, &err);
    OSTmrStart(&netsend_timer, &err);

   // OSTmrCreate(&netsend_hour_timer, "netsend_hour_tmr", NETSEND_HOUR_PERIOD, NETSEND_HOUR_PERIOD, OS_OPT_TMR_PERIODIC, netsend_hour_msg_timer, NULL, &err);
   // OSTmrStart(&netsend_hour_timer, &err);
}


void net_check_status(void)
{
    uint8_t sendbuff[128];
    uint8_t sendlen;
//    uint16_t type;
    static uint8_t index = 0;
    static uint8_t bsend = 0,bFirstSend = 0;
    if(g_runData.timeSendStatus < 3) return;

    if((downlink_config.config_flag&0x01) != CONFIG_WRITE) { //没有下发配置信息时
        memset(sendbuff,0,sizeof(sendbuff));
        uplink_tax_statusmcmd_0x2002(sendbuff,&sendlen,DEVICE_GATEWAY,0); //网关的状态信息上报
        tax_net_send(sendbuff,sendlen,FIRST_TYPE_STATUS,COLL_UP_STATUS_CMD2002);
        g_loraCollMsg.lora_sendCount = 0;
        g_loraCollMsg.lora_rcvCount = 0;
        g_runData.timeSendStatus = 0;

        return;
    }

    if(bsend==0){
        memset(sendbuff,0,sizeof(sendbuff));
        uplink_tax_statusmcmd_0x2002(sendbuff,&sendlen,DEVICE_GATEWAY,0); //网关的状态信息上报
        tax_net_send(sendbuff,sendlen,FIRST_TYPE_STATUS,COLL_UP_STATUS_CMD2002);
        g_loraCollMsg.lora_sendCount = 0;
        g_loraCollMsg.lora_rcvCount = 0;
        bsend = 1;
        if(bFirstSend==0){
            g_runData.timeSendStatus = 0;
            bFirstSend = 1;
            bsend    = 0;
        }
    }
    else {
        memset(sendbuff,0,sizeof(sendbuff));
        uplink_tax_statusmcmd_0x2002(sendbuff,&sendlen,DEVICE_COLLECT,index); //采集器的状态信息上报
        tax_net_send(sendbuff,sendlen,FIRST_TYPE_STATUS,COLL_UP_STATUS_CMD2002);
        printf("net send collect status collect = %d, timer = %010d\r\n",index,TickCounter);
        g_runData.collrunMsg[index].lorastatus = 0;
        g_loraCollMsg.info[index].rcvNum = 0;
        g_loraCollMsg.info[index].sendNum = 0;
        index++;
        if(index>=downlink_config.collect_num){
            index = 0;
            g_runData.timeSendStatus = 0;
            bsend = 0;
        }

    }
}
