nettimer.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "nettimer.h"
  2. #include "../network_mgr/net_ctrl.h"
  3. #include "../network_mgr/sx1268/lora.h"
  4. OS_TMR netsend_timer; // 5分钟的定时器
  5. OS_TMR netsend_hour_timer; // 24小时的定时器
  6. timeout_t g_timeOut;
  7. timeout_t g_usmTimeOut; // 4G模块的连接超时状态
  8. /*-----------------------------------------------------
  9. * 一分钟一次
  10. * ----------------------------------------------------*/
  11. void netsend_msg_timer(void *p_tmr, void *p_arg)
  12. {
  13. uint8_t i=0,j=0,k=0;
  14. static uint32_t _tick = 0;
  15. static uint32_t minutes = 0;
  16. static uint8_t timer = 1;//,timer1 = 1;
  17. if(g_runData.bUpdate!=0) return;
  18. minutes++;
  19. g_runData.timeSendStatus++;
  20. _tick++;
  21. if(_tick>=timer){
  22. g_runData.bsendgatewayfirmMsg = 1;
  23. _tick = 0;
  24. if(timer ==1) timer = 3;
  25. else if(timer==3) timer = 10;
  26. else if(timer == 10) timer = 60;
  27. else timer = 60;
  28. if((downlink_config.config_flag&0x01) == CONFIG_WRITE){
  29. g_runData.bRead0x58 = 1;
  30. g_readCmd58.nodeCur = 0;
  31. g_readCmd58.stauts[0] = 0;
  32. }
  33. }
  34. if(sys_net.net_hdl && ((downlink_config.config_flag&0x01) == CONFIG_WRITE)){
  35. for(i=0;i<downlink_config.collect_num;i++)
  36. {
  37. for(j=0;j<downlink_config.collect_conf[i].tax_num;j++){
  38. for(k=0;k<downlink_config.collect_conf[i].tax_conf[j].gun_num;k++){
  39. g_runData.daStatus[i][j][k]++;
  40. }
  41. }
  42. }
  43. if(!g_data4G.bconnect4G) { // 判断4G模块 mqtt是否连接
  44. if(g_usmTimeOut.flag == 0) {
  45. g_data4G.flag++;
  46. if(g_data4G.flag==1) timeout_setValue(&g_usmTimeOut,3*60*1000);
  47. else if(g_data4G.flag==2) timeout_setValue(&g_usmTimeOut,5*60*1000);
  48. else {
  49. g_data4G.flag = 3;
  50. timeout_setValue(&g_usmTimeOut,10*60*1000);
  51. }
  52. timeout_start(&g_usmTimeOut);
  53. printf("start g_usmTimer\r\n");
  54. }
  55. printf("g_dat4G.error = %d\r\n",g_data4G.errorTime);
  56. g_data4G.errorTime++;
  57. if(g_data4G.errorTime>=60){ // mqtt 一个小时没有连接上,则芯片复位
  58. NVIC_SystemReset();//复位
  59. }
  60. }
  61. else {
  62. g_data4G.errorTime = 0;
  63. g_data4G.flag = 0;
  64. timeout_setValue(&g_usmTimeOut,60*1000);
  65. timeout_stop(&g_usmTimeOut);
  66. }
  67. }
  68. }
  69. void netsend_hour_msg_timer(void *p_tmr, void *p_arg)
  70. {
  71. g_runData.bRead0x58 = 1;
  72. g_readCmd58.stauts[0] = 0;
  73. printf("netsend time start %d \r\n",TickCounter);
  74. }
  75. void nettimer_init(void)
  76. {
  77. OS_ERR err;
  78. OSTmrCreate(&netsend_timer, "netsend_tmr", NETSEND_MSG_PERIOD, NETSEND_MSG_PERIOD, OS_OPT_TMR_PERIODIC, netsend_msg_timer, NULL, &err);
  79. OSTmrStart(&netsend_timer, &err);
  80. // OSTmrCreate(&netsend_hour_timer, "netsend_hour_tmr", NETSEND_HOUR_PERIOD, NETSEND_HOUR_PERIOD, OS_OPT_TMR_PERIODIC, netsend_hour_msg_timer, NULL, &err);
  81. // OSTmrStart(&netsend_hour_timer, &err);
  82. }
  83. void net_check_status(void)
  84. {
  85. uint8_t sendbuff[128];
  86. uint8_t sendlen;
  87. // uint16_t type;
  88. static uint8_t index = 0;
  89. static uint8_t bsend = 0,bFirstSend = 0;
  90. if(g_runData.timeSendStatus < 3) return;
  91. if((downlink_config.config_flag&0x01) != CONFIG_WRITE) { //没有下发配置信息时
  92. memset(sendbuff,0,sizeof(sendbuff));
  93. uplink_tax_statusmcmd_0x2002(sendbuff,&sendlen,DEVICE_GATEWAY,0); //网关的状态信息上报
  94. tax_net_send(sendbuff,sendlen,FIRST_TYPE_STATUS,COLL_UP_STATUS_CMD2002);
  95. g_loraCollMsg.lora_sendCount = 0;
  96. g_loraCollMsg.lora_rcvCount = 0;
  97. g_runData.timeSendStatus = 0;
  98. return;
  99. }
  100. if(bsend==0){
  101. memset(sendbuff,0,sizeof(sendbuff));
  102. uplink_tax_statusmcmd_0x2002(sendbuff,&sendlen,DEVICE_GATEWAY,0); //网关的状态信息上报
  103. tax_net_send(sendbuff,sendlen,FIRST_TYPE_STATUS,COLL_UP_STATUS_CMD2002);
  104. g_loraCollMsg.lora_sendCount = 0;
  105. g_loraCollMsg.lora_rcvCount = 0;
  106. bsend = 1;
  107. if(bFirstSend==0){
  108. g_runData.timeSendStatus = 0;
  109. bFirstSend = 1;
  110. bsend = 0;
  111. }
  112. }
  113. else {
  114. memset(sendbuff,0,sizeof(sendbuff));
  115. uplink_tax_statusmcmd_0x2002(sendbuff,&sendlen,DEVICE_COLLECT,index); //采集器的状态信息上报
  116. tax_net_send(sendbuff,sendlen,FIRST_TYPE_STATUS,COLL_UP_STATUS_CMD2002);
  117. printf("net send collect status collect = %d, timer = %010d\r\n",index,TickCounter);
  118. g_runData.collrunMsg[index].lorastatus = 0;
  119. g_loraCollMsg.info[index].rcvNum = 0;
  120. g_loraCollMsg.info[index].sendNum = 0;
  121. index++;
  122. if(index>=downlink_config.collect_num){
  123. index = 0;
  124. g_runData.timeSendStatus = 0;
  125. bsend = 0;
  126. }
  127. }
  128. }