#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模块的连接超时状态

timeout_t g_uart4Time; // 串口4的接收中断计时
timeout_t g_uart5Time; // 串口5的接收中断计时
timeout_t g_uart2Time; // 串口2的接收中断计时

/*-----------------------------------------------------
 * 一分钟一次
 * ----------------------------------------------------*/
void netsend_msg_timer(void *p_tmr, void *p_arg)
{
    uint8_t i=0,j=0,k=0;
    static uint8_t _tick = 0;
    uint8_t sendbuff[160] = {0};
    uint8_t len = 0;
    if(g_runData.bUpdate!=0) return;

    if(sys_net.net_hdl && downlink_config.config_flag == CONFIG_WRITE){
        _tick++;
        g_runData.timeSendStatus = 1;
        printf("netsend hour start %d \r\n",TickCounter);

        if(!g_data4G.bconnect4G) { // 判断4G模块 mqtt是否连接
            g_data4G.errorTime++;
            if(g_data4G.errorTime>=60){ // mqtt 一个小时没有连接上，则芯片复位
                 NVIC_SystemReset();//复位
            }
        }
        else g_data4G.errorTime = 0;
        g_runData.bsendOilData++;
        g_runData.bAutoReadOilData++;
        g_runData.bsendGatewaySttus++;
        g_runData.bsendGatewayVersion++;
        if(g_runData.loraConnectTime>=1) g_runData.loraConnectTime++;
    }
		
//		 uplink_tax_statusmcmd_0x2002(sendbuff,&len,DEVICE_OILL,0);
//        tax_net_send(sendbuff,len,FIRST_TYPE_STATUS,COLL_UP_STATUS_CMD2002);
//    if(_tick>=3){
//			memset(sendbuff,0,sizeof(sendbuff));
//        uplink_tax_statusmcmd_0x2001(sendbuff,&len,DEVICE_OILL,0);
//        tax_net_send(sendbuff,len,0x02,0x2001);
//        _tick = 0;
//    }
}

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_timer, "netsend_tmr", NETSEND_MSG_PERIOD, NETSEND_MSG_PERIOD, OS_OPT_TMR_PERIODIC, netsend_msg_timer, NULL, &err);
//    OSTmrStart(&netsend_timer, &err);
}

