
#include "queueUart.h"
#include "uart_dev.h"
#include "busIdleDetection.h"

/* 队列中每个消息的大小 */
#define QUEUE_SIZE 4

/* 各个通讯口的队列 */
QueueHandle_t J1_485_Queue = NULL;
QueueHandle_t J2_485_Queue = NULL;
QueueHandle_t J3_485_Queue = NULL;
QueueHandle_t J4_485_Queue = NULL;
QueueHandle_t J5_0_485_Queue = NULL;
QueueHandle_t upward_uart_Queue = NULL;

/* 队列集 */
QueueSetHandle_t uart_Queue = NULL;

/* 查看队列集中是否有数据 */
QueueSetMemberHandle_t xActivatedMember;

/* 通过该结构体接收对应的数据用来发送，结束后通过该结构体，释放数据的内存 */
typedef struct _queueRecvDataInfo {
    queueUartSendInfo *J1_485_data;
    queueUartSendInfo *J2_485_data;
    queueUartSendInfo *J3_485_data;
    queueUartSendInfo *J4_485_data;
    queueUartSendInfo *upward_uart_data;
    queueTimeShareSendInfo *J5_0_485_data;
} queueRecvDataInfo;
static queueRecvDataInfo queueRecvData;

/**
  * @brief  初始化串口发送的队列
  * @param  
  * @retval 
  */
void uartQueueInit(void)
{
    /* 初始化队列 */
    J1_485_Queue = xQueueCreate(5, QUEUE_SIZE);
    // if (NULL == J1_485_Queue) {
    //     log_error("creat J1_485_Queue error\n");
    // }

    J2_485_Queue = xQueueCreate(5, QUEUE_SIZE);
    // if (NULL == J2_485_Queue) {
    //     log_error("creat J2_485_Queue error\n");
    // }
    
    J3_485_Queue = xQueueCreate(5, QUEUE_SIZE);
    // if (NULL == J3_485_Queue) {
    //     log_error("creat J3_485_Queue error\n");
    // }
    
    J4_485_Queue = xQueueCreate(5, QUEUE_SIZE);
    // if (NULL == J4_485_Queue) {
    //     log_error("creat J4_485_Queue error\n");
    // }
    
    J5_0_485_Queue = xQueueCreate(10, QUEUE_SIZE);
    // if (NULL == J5_0_485_Queue) {
    //     log_error("creat J5_0_485_Queue error\n");
    // }
    
    upward_uart_Queue = xQueueCreate(10, QUEUE_SIZE);
    // if (NULL == upward_uart_Queue) {
    //     log_error("creat upward_uart_Queue error\n");
    // }
    

    /* 将队列都放入队列集中 */
    uart_Queue = xQueueCreateSet(6);
    xQueueAddToSet(J1_485_Queue, uart_Queue);
    xQueueAddToSet(J2_485_Queue, uart_Queue);
    xQueueAddToSet(J3_485_Queue, uart_Queue);
    xQueueAddToSet(J4_485_Queue, uart_Queue);
    xQueueAddToSet(J5_0_485_Queue, uart_Queue);
    xQueueAddToSet(upward_uart_Queue, uart_Queue);
}

/**
  * @brief  从队列中取出数据，发送函数
  * @param  
  * @retval 
  */
void uartQueueSend(void)
{
    /* 用于判定是否延时 */
    static uint8_t flag = 0;

    /* 查看队列集中是否有数据 */
    xActivatedMember = xQueueSelectFromSet(uart_Queue, portMAX_DELAY);

    /* 查看Upward_uart5中有无数据 */
    if (!getUartState(g_Upward_uart5_handle)) {
        /* 处理接收到的数据 */
        if (xQueueReceive(upward_uart_Queue, &queueRecvData.upward_uart_data, 0) == pdTRUE) {
            uartInterruptSend(g_Upward_uart5_handle, queueRecvData.upward_uart_data->data
                    , queueRecvData.upward_uart_data->length);
            flag = 1;
        }
    }

    /* 查看J1_485中有无数据 */
    if (!getRs485State(g_J1_uart6_handle)) {
        /* 处理接收到的数据 */
        if (xQueueReceive(J1_485_Queue, &queueRecvData.J1_485_data, 0) == pdTRUE) {
            uartInterruptSend(g_J1_uart6_handle, queueRecvData.J1_485_data->data
                    , queueRecvData.J1_485_data->length);
            flag = 1;
        }
    }

    /* 查看J2_485中有无数据 */
    if (!getRs485State(g_J2_uart7_handle)) {
        /* 处理接收到的数据 */
        if (xQueueReceive(J2_485_Queue, &queueRecvData.J2_485_data, 0) == pdTRUE) {
            uartInterruptSend(g_J2_uart7_handle, queueRecvData.J2_485_data->data
                    , queueRecvData.J2_485_data->length);
            flag = 1;
        }
    }

    /* 查看J3_485中有无数据 */
    if (!getRs485State(g_J3_usart2_handle)) {
        /* 处理接收到的数据 */
        if (xQueueReceive(J3_485_Queue, &queueRecvData.J3_485_data, 0) == pdTRUE) {
            uartInterruptSend(g_J3_usart2_handle, queueRecvData.J3_485_data->data
                    , queueRecvData.J3_485_data->length);
            flag = 1;
        }
    }

    /* 查看J4_485中有无数据 */
    if (!getRs485State(g_J4_uart8_handle)) {
        /* 处理接收到的数据 */
        if (xQueueReceive(J4_485_Queue, &queueRecvData.J4_485_data, 0) == pdTRUE) {
            uartInterruptSend(g_J4_uart8_handle, queueRecvData.J3_485_data->data
                    , queueRecvData.J4_485_data->length);
            flag = 1;
        }
    }
    
    /* 查看J5_0_485中有无数据 */
    if (!getRs485State(g_J5_0_usart3_handle)) {
        /* 处理接收到的数据 */
        if (xQueueReceive(J5_0_485_Queue, &queueRecvData.J5_0_485_data, 0) == pdTRUE) {
            setConnectPort(queueRecvData.J5_0_485_data->connectPort);
            log_info("send J5_0 Data : %s , %d\n", queueRecvData.J5_0_485_data->data, queueRecvData.J5_0_485_data->length);
            uartInterruptSend(g_J5_0_usart3_handle, queueRecvData.J5_0_485_data->data
                    , queueRecvData.J5_0_485_data->length);
            flag = 1;
        }
    }

    /* 没有数据发送则延时 */
    if (!flag) {
        /* 延时三个系统节拍 */
        vTaskDelay(3);
    }
    flag = 0;
}


#ifdef UARTINTERRUPTSEND
/**
  * @brief  数据发送完成后用于清除
  * @param  
  * @retval 
  */
void J1_485_IN_TXE(void)
{
    vPortFree(queueRecvData.J1_485_data);
}


void J2_485_IN_TXE(void)
{
    vPortFree(queueRecvData.J2_485_data);
}


void J3_485_IN_TXE(void)
{
    vPortFree(queueRecvData.J3_485_data);
}


void J4_485_IN_TXE(void)
{
    vPortFree(queueRecvData.J4_485_data);
}


void J5_0_485_IN_TXE(void)
{
    vPortFree(queueRecvData.J5_0_485_data);
}


void Upward_USART_IN_TXE(void)
{
    vPortFree(queueRecvData.upward_uart_data);
}

#endif
