/*
 * uart_rcv.c
 *
 *  Created on: 2022年4月20日
 *      Author: dell
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "onechip_include_header.h"




#define ENV_ADJ_THRESHOLD 27.0


#define DEBUG_BUF_SIZE (512)	// 调试串口缓冲区长度
#define BUF_SIZE (512)	// 调试串口缓冲区长度

enum SLAVE_MCU_Ctr_cmd_type
{
    SLAVE_MCU_CMD_PARAM = 0x01,
    SLAVE_MCU_CMD_HEAT = 0x02,
    SLAVE_MCU_CMD_LIGHT_CTR= 0X03,
    SLAVE_MCU_CMD_QRSCANNER_CTR= 0X04,    
    SLAVE_MCU_CMD_FAN= 0X05,
    SLAVE_MCU_CMD_CARD_ST= 0X06,
	SLAVE_MCU_CMD_PID = 0x20,
};
enum SLAVE_MCU_Ctr_UP_type
{
	SLAVE_MCU_UP_HEAT = 0x10,
	SLAVE_MCU_UP_QR_PARAM= 0X11,
	SLAVE_MCU_UP_QR = 0x12,
	SLAVE_MCU_INFO,					//异常信息
};




uint8_t g_uart2_rcv_start = 0;
uint8_t g_uart2_rcv_complete_flg = 0;
uint8_t g_uart2_rx_buf[DEBUG_BUF_SIZE] = {0};
uint8_t g_uart2_tx_buf[BUF_SIZE] = {0};
uint16_t g_uart2_rx_len = 0;

str_hole_info_t hole1 = {0};
str_hole_info_t hole2 = {0};
str_hole_info_t hole3 = {0};
str_hole_info_t hole4 = {0};
str_hole_info_t hole5 = {0};
str_hole_info_t hole6 = {0};
str_hole_info_t hole7 = {0};
str_hole_info_t hole8 = {0};
str_hole_info_t hole9 = {0};
str_hole_info_t hole10 = {0};
str_card_param_info_t m_str_card_param_info = {0};
unsigned int haveheaderflag = 0;
uint32_t qr_check_delay = QR_CHECH_STATE_FIRST;  /*1s定时器里面，大于0，每s减1*/
extern void QRparam_upload(str_card_param_info_t *p_str_card_param_info);
extern uint8_t sys_info[40];

uint32_t received_len;


void svanner_receive_Rec_Start(UART_HandleTypeDef *huart){
		__HAL_UART_CLEAR_IDLEFLAG(huart);
	  memset((char *)g_uart2_rx_buf,0,DEBUG_BUF_SIZE);
		HAL_UART_Receive_DMA(huart, g_uart2_rx_buf, DEBUG_BUF_SIZE);	
	__HAL_UART_ENABLE_IT(huart, UART_IT_IDLE);
}


void svanner_receive_Rec_Stop(UART_HandleTypeDef *huart){
	HAL_UART_DMAStop(huart);
}

/********************************************************************
 *                         bcd_to_hex()
 *
 *描述：BCD转HEX
 *参数：无
 *返回：无
 *其他：无
 *--------------------------------------------------------------------
 *记录：
 ********************************************************************/
uint8_t bcd_to_hex(uint8_t val)
{
	return ((val / 10) << 4) + val % 10;
}


void svanner_receive_Rec_IDLE(UART_HandleTypeDef *huart)
{
	if ((__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) != RESET))
	{
		__HAL_UART_CLEAR_IDLEFLAG(huart);
		received_len		= DEBUG_BUF_SIZE - __HAL_DMA_GET_COUNTER(huart->hdmarx);
		 HAL_UART_DMAStop(huart); 

		HAL_UART_Receive_DMA(huart, g_uart2_rx_buf, DEBUG_BUF_SIZE);
		
		sysinf.updateQR = false;	
	}
}


void svanner_receive_analysis(void)
{
	if(received_len){	
		Van_Device_Printf(VAN_LOG_CRITICAL, "rec QR inif %d \n\r",received_len);
		unsigned filter = 0;
		/*while('{' != g_uart2_rx_buf[filter] && filter<received_len){
			filter++;
		}*/
		if ('{' == g_uart2_rx_buf[0]){
			if(0 == parse_qrcode_info(&m_str_card_param_info,  (char *)&g_uart2_rx_buf[0])){
				haveheaderflag = QR_REC_STATE_SENDPRAM; 	
				memcpy(&g_uart2_rx_buf[g_uart2_rx_len],  sysinf.SN, 7);
				g_uart2_rx_len += 7;
				qr_check_delay = 0;				
				if((sysinf.psmMaster.state == SW_Scan_Qrcode || sysinf.psmMaster.state == SW_Chip_Id  )){
					svanner_receive_Rec_Stop(sysinf.scannerUart);
					sysinf.updateQR = true;
					data_send_to_pc(PC_CMD_QRCODE,(char *)g_uart2_rx_buf, g_uart2_rx_len);	
				}else{
#ifdef ONECHIP_MINI_PLUSE
				if((sysinf.psmSlave.state == SW_Scan_Qrcode || sysinf.psmSlave.state == SW_Chip_Id  )){
					{

					}
#endif
				}

				Van_Device_Printf(VAN_LOG_CRITICAL, "unpack json ok \n\r ");
			}
			else{
				Van_Device_Printf(VAN_LOG_CRITICAL, "unpack json error \n\r %s",g_uart2_rx_buf);
			}
		}
		else{
				Van_Device_Printf(VAN_LOG_CRITICAL, "header error \n\r %s",g_uart2_rx_buf);
			}
		
		received_len = 0;	
	  memset((char *)g_uart2_rx_buf,0,DEBUG_BUF_SIZE);
	}
	return;
}


int parse_qrcode_info(str_card_param_info_t *info, char *qrcode_str)
{
	int id = 0;
	double Tt = 0;
	double Tt1 = 0;
	
	if (info == 0)
	{
		return -1;
	}

	cJSON *root = cJSON_Parse(qrcode_str);
	if (root == NULL)
	{   /*消息不合格*/
		return -1;
	}
	cJSON *item = cJSON_GetObjectItem(root, "p");
	if (item == NULL)
	{   /*消息不合格*/
		cJSON_Delete(root);
		return -1;
	}
	char *pid = cJSON_GetStringValue(item);
	//    printf("pid=%s \r\n", pid);
	memcpy(info->pid, pid, 10);

	item = cJSON_GetObjectItem(root, "t");
	if (item == NULL)
	{   /*消息不合格*/
		cJSON_Delete(root);
		return -1;
	}
	double t = item->valuedouble;
	//    printf("t=%f \r\n", t);
	info->temp[0] = (uint8_t)t;
	info->temp[1] = (uint8_t)((t -(uint8_t)t)* 100);

	// 获取数组item
	item = cJSON_GetObjectItem(root, "e");
	if (item == NULL)
	{   /*消息不合格*/
		cJSON_Delete(root);
		return -1;
	}
	int test_arry_size = cJSON_GetArraySize(item);

	for (int i = 0; i < test_arry_size; i++)
	{
		// 打印数组里的所有item
		cJSON *sub_array = cJSON_GetArrayItem(item, i);

		cJSON *sub = cJSON_GetObjectItem(sub_array, "i");
		id = sub->valueint;
		//        printf("id=%d \r\n", id);

		if(id > 0)
		{
			sub = cJSON_GetObjectItemCaseSensitive(sub_array, "T");
			Tt = sub->valuedouble;
			//        printf("Tt=%f \r\n", Tt);

			sub = cJSON_GetObjectItemCaseSensitive(sub_array, "t");
			Tt1 = sub->valuedouble;
			//        printf("Tt1=%f \r\n", Tt1);
		}
		else
		{
			Tt = 0;
			Tt1 = 0;
		}

		info->hole_info[i].item = id;
		info->hole_info[i].TtMax[0] = (uint8_t)Tt;
		info->hole_info[i].TtMax[1] = (uint8_t)((Tt -(uint8_t)Tt)* 100);
		info->hole_info[i].TtMin[0] = (uint8_t)Tt1;
		info->hole_info[i].TtMin[1] = (uint8_t)((Tt1 -(uint8_t)Tt1)* 100);
	}
	// 记得删除json
	cJSON_Delete(root);
	return 0;
}

