#include "oiltank.h"
#include "../network_mgr/net_proc.h"
#include "nettimer.h"
//uint8_t oil_buff[1024] = {0};

#define DATA_LEN  450 //479
int oiltank_operation(uint8_t uart)
{

   // uint8_t rcv_buf[1024]= {0};
    uint8_t send_buf[128]= {0};
    uint32_t i, rcv_size = 0, send_size = YB_SS160_SEND_SIZE;
    uint32_t remainSize = 0;
    oiltankDataCmd0x2021 pmsg;
    RS232_YB_SS160_SEND *p_tank_tx = (RS232_YB_SS160_SEND *)send_buf;
    static uint8_t flag = 0, bread = 0,bfirstread = 1;
    OS_ERR      err;

    if(g_runData.bUpdate!=0) return 0;
		
    uart = OILTANK_UART;
   // memset(oil_buff,0,sizeof(oil_buff));
    if((g_runData.bread==1) && (bread == 0)) { // 服务器读取的数据
        p_tank_tx->SOH = 0x01;
        strcpy((char *)p_tank_tx->FCN_CODE, "i20100");
        p_tank_tx->EXT = 0x03;

        data_dump("server read", send_buf, send_size);
        uart_msg_send(uart, (char *)p_tank_tx, send_size);//
        //uart_msg_send(UART2_ID, (char *)p_tank_tx, send_size);//
        timeout_start(&g_timeOut);
        flag = 2;
        bread = 1;
    }
    else
    {
        if((timeout_isOut(&g_timeOut)==1) || (bfirstread == 1)){ // 液位仪定时读取
             timeout_stop(&g_timeOut);
            if(g_oilReadMsg.bInit==0x05){
                memcpy(send_buf,g_oilReadMsg.param,g_oilReadMsg.paraLen);
            }
            else {
               p_tank_tx->SOH = 0x01;
               strcpy((char *)p_tank_tx->FCN_CODE, "i20100");
               p_tank_tx->EXT = 0x03;
            }

           data_dump("YB_SS160 uart 5 oiltank send", send_buf, send_size);
           uart_msg_send(uart, (char *)p_tank_tx, send_size);//
           //uart_msg_send(UART2_ID, (char *)p_tank_tx, send_size);
           timeout_start(&g_timeOut);
           flag = 2;
           bfirstread = 0;
        }
        else{
            if(timeout_isOut(&g_uart4Time)==1){
        //        rcv_size = uart_blocking_read_len((char *)oil_buff, UART4_ID);//透传口
                if(rcv_size){
                   set_led_status(0,1);
                   data_dump("uart 4 recv", g_oilreadDa.oil_buff, rcv_size);
                   uart_msg_send(uart, (char *)g_oilreadDa.oil_buff, rcv_size);//
                   flag = 1;
                }
            }
        }
    }

    if((g_oilreadDa.bfinish == 1 && g_oilreadDa.len!=0)) {
        data_dump("oil_buff", g_oilreadDa.oil_buff, g_oilreadDa.len);
        rcv_size = g_oilreadDa.len;
        g_oilreadDa.bfinish = 0;
        g_oilreadDa.len = 0;
        g_oilreadDa.len1 = 0; // 收到有结束符的了，不再处理没有结束符的
        printf("test_01\n");
    }
    else if( timeout_isOut(&g_uart5Time)==1 && g_oilreadDa.len1!=0){
        data_dump("oil_buff_01", g_oilreadDa.oil_buff, g_oilreadDa.len1);
         rcv_size = g_oilreadDa.len1;
        g_oilreadDa.bfinish = 0;
        g_oilreadDa.len = 0;
        g_oilreadDa.len1 = 0;
         printf("test_02\n");

    }
    else {
        if(timeout_isOut(&g_uart5Time)==0) return 0;
    }

    set_led_status(4,1);
   // rcv_size = uart_blocking_read_len((char *)oil_buff, uart); //接收液位仪返回的数据

    if(rcv_size) {
        set_led_status(4,0);
      //  if(flag==1) uart_msg_send(UART4_ID, (char *)oil_buff, rcv_size);// 发送给加油站的控制机
        printf("\r\n rcv_size = %d,flag = %d \r\n",rcv_size,flag);
        if(flag==1) {
            set_led_status(0,0);
            if(g_runData.bsendOilData<READ_TIMER) return 0;
        }
					
        pmsg.sn = downlink_config.gateway_id;
        if(flag != 2) return 0;  // 液位仪控制器产生的指令，不再向服务器上报
        if(rcv_size<=DATA_LEN){
             printf("test_03\n");
            if((flag == 2) || (g_runData.bread==1)){ //(g_runData.bsendOilData>=READ_TIMER)
                pmsg.len = rcv_size;
                pmsg.frameNo = 0xFF;
                memset(pmsg.rcv_buf,0,sizeof(pmsg.rcv_buf));
                memcpy(pmsg.rcv_buf,g_oilreadDa.oil_buff,rcv_size);
                tax_net_send((uint8_t*)&pmsg,rcv_size+7,0x03,SECOND_TYPE_DATA_OIL);
                printf("send oiltank data to server\r\n");
                g_runData.bsendOilData = 0;
                g_runData.bread = 0;
                bread = 0;
            }
        }
        else {
             printf("test_04  bsend = %d\n",g_runData.bsendOilData);
            if((flag == 2) || (g_runData.bread==1)){//g_runData.bsendOilData>=READ_TIMER
                pmsg.len = DATA_LEN;
                pmsg.frameNo = 0x82;
                memset(pmsg.rcv_buf,0,sizeof(pmsg.rcv_buf));
                memcpy(pmsg.rcv_buf,g_oilreadDa.oil_buff,DATA_LEN);
              //  data_dump("YB_SS160 oiltank send_01", pmsg.rcv_buf, DATA_LEN);
                printf("send oiltank data the first\r\n");
                tax_net_send((uint8_t*)&pmsg,DATA_LEN+7,0x03,SECOND_TYPE_DATA_OIL);
                OSTimeDlyHMSM(0, 0, 1, 0, OS_OPT_TIME_DLY, &err);

                pmsg.frameNo = 0x01;
                remainSize = rcv_size-DATA_LEN;
                pmsg.len = remainSize;
                memset(pmsg.rcv_buf,0,sizeof(pmsg.rcv_buf));
                memcpy(pmsg.rcv_buf,g_oilreadDa.oil_buff+DATA_LEN,remainSize);
                printf("send oiltank data the second\r\n");
              //  data_dump("YB_SS160 oiltank send_02", pmsg.rcv_buf, remainSize);
                tax_net_send((uint8_t*)&pmsg,remainSize+7,0x03,SECOND_TYPE_DATA_OIL);
                g_runData.bsendOilData = 0;
                g_runData.bread = 0;
                bread = 0;
                timeout_start(&g_timeOut);
            }
        }
    }

    return 0;
}
