/*
 * AppTaskGps.c
 *
 *  Created on: 2022年2月16日
 *      Author: QiXiang_CHENJIE
 */
#include "AppTaskGps.h"
void strdel(char *str, char c);
uint32 location_handle(char *in1);
void GpsDataDecode(uint8 *msg);
void GpsTask(void *pvParameters)
{
	(void)pvParameters;
	GpsDataQueueHandle = xQueueCreate(1, sizeof(GPSInfo));//长度为1才可以允许覆写
	Dio_WriteChannel(DioConf_DioChannel_PTD1_GPIO_OUT_MCU_GPS_POW_EN, STD_ON);//GPS开机
	uint16 pReadLen = 0;
	uint8 *UartRecvPtr=NULL;
	while(1)
	{
		vTaskDelay(pdMS_TO_TICKS(10));
		UART_Receive_Data(UART_LPUART2,&UartRecvPtr,&pReadLen,1000);
		if(pReadLen>0)
		{
			GpsDataDecode(UartRecvPtr);
			if(UartRecvPtr != NULL)
			{
				free(UartRecvPtr);
			}
			UartRecvPtr = NULL;
		}
	}
}
void GpsDataDecode(uint8 *msg)
{
	//char temp[] = "$GNRMC,082626.000,A,2939.91801,N,10637.09500,E,0.543,30.254,261120,,,A,V*17";
	char *p = NULL;
	char  *p2 = NULL;
	const char *delim = "\n";
	GPSInfo GpsInfoData;
	uint8 index = 0;
	char *databuffer[20];
	uint32 speedtemp;
	uint32 latitude;
	uint32 longitude;
	uint16 direction;
	memset((uint8 *)&GpsInfoData, 0x00, sizeof(GPSInfo));
	p = strtok(msg, delim); //将信息进行分割
	p2 = strtok(NULL, delim);
	p = strtok(p, ","); //只取第1行的信息RMC
	//p = strtok(temp,",");//模拟测试
	if (strcmp(p, "$GNRMC") == 0)
	{
		index = 0;
		while (p)
		{
			databuffer[index] = p;
			p = strtok(NULL, ",");
			index++;
		}
		if (index > 5 && strcmp(databuffer[2], "A") == 0)
		{
			GpsInfoData.locateMark = 0x01; //有效，东经，北纬写定
			strdel(databuffer[3], '.');
			strdel(databuffer[5], '.');
			strdel(databuffer[7], '.');
			speedtemp = atol(databuffer[7]) * 1852 / 1e5; //节换算单位，1节=1.852km每小时
			GpsInfoData.speed[0] = (speedtemp >> 8) & 0xFF;
			GpsInfoData.speed[1] = speedtemp & 0xFF;
			latitude = location_handle(databuffer[3]);
			GpsInfoData.latitude[0] = latitude >> 24;
			GpsInfoData.latitude[1] = latitude >> 16;
			GpsInfoData.latitude[2] = latitude >> 8;
			GpsInfoData.latitude[3] = latitude;
			longitude = location_handle(databuffer[5]);
			GpsInfoData.longitude[0] = longitude >> 24;
			GpsInfoData.longitude[1] = longitude >> 16;
			GpsInfoData.longitude[2] = longitude >> 8;
			GpsInfoData.longitude[3] = longitude;
			if (speedtemp >= 50) //大于5km/h才输出方位
			{
				direction = atol(databuffer[8]);
				GpsInfoData.direction[0] = direction >> 8;
				GpsInfoData.direction[1] = direction;
			}
			else
			{
				GpsInfoData.direction[0] = 0xff;
				GpsInfoData.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
	memset(databuffer, 0x30, 20);
	if (strcmp(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))
		{
			GpsInfoData.satelliteNum = atol(databuffer[7]); //卫星数目写入
			strdel(databuffer[9], '.');
			uint16 alt = 0;
			alt = atol(databuffer[9]) / 10 + 1000;
			GpsInfoData.altitude[0] = alt >> 8; //海拔
			GpsInfoData.altitude[1] = alt;
			strdel(databuffer[2], '.');
			strdel(databuffer[4], '.');
			latitude = location_handle(databuffer[2]);
			GpsInfoData.latitude[0] = latitude >> 24;
			GpsInfoData.latitude[1] = latitude >> 16;
			GpsInfoData.latitude[2] = latitude >> 8;
			GpsInfoData.latitude[3] = latitude;
			longitude = location_handle(databuffer[4]);
			GpsInfoData.longitude[0] = longitude >> 24;
			GpsInfoData.longitude[1] = longitude >> 16;
			GpsInfoData.longitude[2] = longitude >> 8;
			GpsInfoData.longitude[3] = longitude;
		}
	}
	xQueueOverwrite(GpsDataQueueHandle,(uint8 *)&GpsInfoData);
}
void strdel(char *str, char c)
{
	char *p = str;
	while (*str)
	{
		if (*str != c)
			*p++ = *str;
		str++;
	}
	*p = '\0';
}
uint32 location_handle(char *in1)
{
	uint32 location_temp;
	uint32 location_degree;
	uint32 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;
}
