#include "app_gps.h"
#include "FreeRTOS.h"
#include "queue.h"
#include "stdlib.h"
#include "app_protocol.h"

extern gps_info_t gps_info;

QueueHandle_t gps_data_queue;
uint16_t usart3_rx_status;
uint8_t  usart3_rx[1500];
	
void strdel(char *str, char c);
uint32_t location_handle(char *in1);
void GpsDataDecode(uint8_t *msg);


void GpsTask(void *pvParameters)
{
	(void)pvParameters;
	gps_data_queue = xQueueCreate(1, sizeof(gps_info_t));//长度为1才可以允许覆写
 
	while(1)
	{
		if(usart3_rx_status&0x8000)//串口3收到数据
		{
			//printf("recieve gps data:%s\r\n",usart3_rx);
			GpsDataDecode(usart3_rx);
			usart3_rx_status = 0;
			memset(usart3_rx,0,sizeof(usart3_rx));			
		}			
	 
		vTaskDelay(pdMS_TO_TICKS(100));
	}
}



void GpsDataDecode(uint8_t *msg)
{	
	/*
	$GNRMC,021732.00,A,2939.88641,N,10637.05509,E,0.424,,290822,,,A,V*18
	$GNGGA,021732.00,2939.88641,N,10637.05509,E,1,14,2.91,245.6,M,,M,,*5B
	$GNGSA,A,3,05,11,12,20,02,25,,,,,,,4.41,2.91,3.31,1*0A
	$GNGSA,A,3,13,29,08,05,35,,,,,,,,4.41,2.91,3.31,4*0C
	$GNGSA,A,3,11,24,25,,,,,,,,,,4.41,2.91,3.31,3*08
	$GPGSV,3,1,11,02,43,316,41,05,46,239,28,11,56,006,11,12,40,255,41,0*63
	$GPGSV,3,2,11,20,75,313,33,25,23,295,36,19,,,26,42,39,126,,0*5D
	$GPGSV,3,3,11,50,35,121,,40,24,248,36,41,46,221,29,0*50
	$GBGSV,2,1,08,05,28,245,29,08,59,335,35,13,58,310,35,29,62,346,36,0*7C
	$GBGSV,2,2,08,35,16,312,33,09,01,178,,10,54,201,,30,46,099,,0*7A
	$GAGSV,1,1,04,11,38,313,32,24,30,227,14,25,25,288,22,10,,,32,0*4E
	$GNVTG,,T,,M,0.424,N,0.785,K,A*35 
	*/
	//char temp[] = "$GNRMC,082626.000,A,2939.91801,N,10637.09500,E,0.543,30.254,261120,,,A,V*17";
	char temp[] = "$GNRMC,021732.00,A,2939.88641,N,10637.05509,E,0.424,,290822,,,A,V*18";
	char temp1[] ="$GNGGA,021732.00,2939.88641,N,10637.05509,E,1,14,2.91,245.6,M,,M,,*5B";

	char *p = NULL;
	char *p2 = NULL;
	const char *delim = "\n";
	gps_info_t gps_info_data;
	uint8_t index = 0;
	char *databuffer[20];
	uint32_t speedtemp;
	uint32_t latitude;
	uint32_t longitude;
	uint32_t direction;
	memset((uint8_t *)&gps_info_data, 0x00, sizeof(gps_info_t));
	p = strtok((char*)msg, delim); //将信息进行分割
	p2 = strtok(NULL, delim);
	p = strtok(p, ","); //只取第1行的信息RMC

  //memcpy(p,temp,strlen(temp));测试

	if (strstr(p,"$GNRMC") != 0)
	{
		index = 0;
		while (p)
		{
			databuffer[index] = p;
			p = strtok(NULL, ",");
			index++;
		}
		if (index > 5 && strcmp(databuffer[2], "A") == 0)
		{
			gps_info_data.locate_mark = 0x01; //有效，东经，北纬写定
			strdel(databuffer[3], '.');
			strdel(databuffer[5], '.');
			//printf("databuffer[7]=%s\r\n",databuffer[7]);
			strdel(databuffer[7], '.');
			//printf("databuffer[7]=%s\r\n",databuffer[7]);
			speedtemp = atol(databuffer[7]) * 1852 / 1e5; //节换算单位，1节=1.852km每小时
			//printf("speedtemp=%d\r\n",speedtemp);
			gps_info_data.speed[0] = (speedtemp >> 8) & 0xFF;
			gps_info_data.speed[1] = speedtemp & 0xFF;
			latitude = location_handle(databuffer[3]);
		
			gps_info_data.latitude[0] = latitude >> 24;
			gps_info_data.latitude[1] = latitude >> 16;
			gps_info_data.latitude[2] = latitude >> 8;
			gps_info_data.latitude[3] = latitude;
			longitude = location_handle(databuffer[5]);
			
			gps_info_data.longitude[0] = longitude >> 24;
			gps_info_data.longitude[1] = longitude >> 16;
			gps_info_data.longitude[2] = longitude >> 8;
			gps_info_data.longitude[3] = longitude;
			if (speedtemp >= 50) //大于5km/h才输出方位
			{
				direction = atol(databuffer[8]);
				gps_info_data.direction[0] = direction >> 8;
				gps_info_data.direction[1] = direction;
			}
			else
			{
				gps_info_data.direction[0] = 0xff;
				gps_info_data.direction[1] = 0xff;
			}
//			if (speedtemp >= 30 && speedtemp <= 1500 && BattWorkStateDelay == 0x01)
//			{
//				AppDataInfo.appDataModify = true;
//				AppDataInfo.AccMileage = speedtemp / 36 + AppDataInfo.AccMileage;
//				if (AppDataInfo.AccMileage >= 0xfffffffe)
//				{
//					AppDataInfo.AccMileage = 0;
//				}
//			}累计里程的累加
		}
//		GpsInfoData.AccMileage[0] = AppDataInfo.AccMileage >> 24;
//		GpsInfoData.AccMileage[1] = AppDataInfo.AccMileage >> 16;
//		GpsInfoData.AccMileage[2] = AppDataInfo.AccMileage >> 8;
//		GpsInfoData.AccMileage[3] = AppDataInfo.AccMileage;
	}
	p2 = strtok(p2, ","); //只取第2行的信息GGA
	memcpy(p2,temp,strlen(temp));///////////////////////////////

	memset(databuffer, 0x30, 20);
	if (strstr(p2, "$GNGGA") != 0)
	{
		index = 0;
		while (p2)
		{
			databuffer[index] = p2;
			p2 = strtok(NULL, ",");
			index++;
		}
		if (index > 9 && (strcmp(databuffer[6], "1") == 0 || strcmp(databuffer[6], "2") == 0))
		{
			gps_info_data.satellite_num = atol(databuffer[7]); //卫星数目写入
			strdel(databuffer[9], '.');
			uint16_t alt = 0;
			alt = atol(databuffer[9]) / 10 + 1000;
			gps_info_data.altitude[0] = alt >> 8; //海拔
			gps_info_data.altitude[1] = alt;
			strdel(databuffer[2], '.');
			strdel(databuffer[4], '.');
			latitude = location_handle(databuffer[2]);
			gps_info_data.latitude[0] = latitude >> 24;
			gps_info_data.latitude[1] = latitude >> 16;
			gps_info_data.latitude[2] = latitude >> 8;
			gps_info_data.latitude[3] = latitude;
			longitude = location_handle(databuffer[4]);
			gps_info_data.longitude[0] = longitude >> 24;
			gps_info_data.longitude[1] = longitude >> 16;
			gps_info_data.longitude[2] = longitude >> 8;
			gps_info_data.longitude[3] = longitude;
		}
	}
	xQueueOverwrite(gps_data_queue,(uint8_t *)&gps_info_data);
}


void strdel(char *str, char c)
{
	char *p = str;
	while (*str)
	{
		if (*str != c)
			*p++ = *str;
		str++;
	}
	*p = '\0';
}
uint32_t location_handle(char *in1)
{
	uint32_t location_temp;
	uint32_t location_degree;
	uint32_t location_min;
	location_temp = atol(in1);
	location_degree = location_temp / (1e7);
	location_degree = location_degree * (1e6);
	location_min = location_temp - location_degree * 10;
	location_min = location_min / 6;
	location_temp = location_degree + location_min;
	return location_temp;
}



