#include "includes.h"
SUT_MESS sutMess;
GPS_DEF gpsInfo;
const unsigned short GPS_TimeTable[GPS_TABLE_NUM]={0,5,10,15,30,60,120,300};
void gpsDataInit(void){
	memset((unsigned char *)&sutMess, 0, sizeof(SUT_MESS));
	memset((unsigned char *)&gpsInfo, 0, sizeof(GPS_DEF));
}
unsigned short getGPSTimeValue(unsigned char index){
	return GPS_TimeTable[index];
}	
static unsigned char gpsCheckCnt=1;//默认开机查询一次
void gpsCheckInfo(void){//查询GPS信息
	gpsCheckCnt++;
}
/*控制查询GPS状态
dly:接口被调用的频率,单位ms*/
void gpsProCtl(int dly){
	static unsigned short cnt=0;
	if(sutApp.gtMode!=0) return;//GT模式后不再查询

	if(getAppObjStatus(ASLEEP_POC)==0) return;//POC休眠时,不发指令
	if(++cnt<(5*1000/dly) && gpsCheckCnt==0) return;
	cnt=0;
	//以下5秒操作一次
	msgAtSend("AT+CGPS\r\n");
	gpsCheckCnt=0;
}

/*
+CGPS:bf,ba,gf,[Lat],[Long]
bf:0 部标关闭 1 部标打开
ba:0 部标未鉴权 1 部标已鉴权
gf:0 GPS关闭 1 GPS打开
Lat: 已定位才有,定位纬度 度.度(已放大1000000)
Long: 已定位才有,定位经度 度.度(已放大1000000)
msg=bf,ba,gf,[Lat],[Long]
*/
void proGpsMsg(char *msg){
	int i;
	unsigned char needCloseGps=0;
	unsigned char needOpenGps=0;
	unsigned char dotNum=0,lach=0;
	unsigned char bblF=0,bbAuth=0,gpsF=0;
	unsigned int Lat=0,Long=0;
	//获取部标开关状态
	bblF=atoi(msg);
	for(i=0;i<strlen(msg);i++){
		if(msg[i]==',') dotNum++;
		if(lach==','){
			if(dotNum==1) bbAuth=atoi(&msg[i]);
			else if(dotNum==2) gpsF=atoi(&msg[i]);
			else if(dotNum==3) Lat=atoi(&msg[i]);
			else if(dotNum==4) Long=atoi(&msg[i]);
		}
		lach=msg[i];
	}
	//优先根据gpsEnable打开或关闭GPS
	if(newPara.gpsEnable==0){
		needCloseGps=1;
	}else{
		//根据GPS上传间隔,打开/关闭GPS
		if(newPara.gpsTimeIndex<GPS_TABLE_NUM){
			if(GPS_TimeTable[newPara.gpsTimeIndex]==0) needCloseGps=1;
			else needOpenGps=1;
		}
	}
	if(needCloseGps!=0 && gpsF!=0){
		MSG_INFO(1, "Shut GPS");
		msgAtSend("AT+SGPS=0\r\n");
		gpsCheckInfo();//检查下是否成功
	}
	if(needOpenGps!=0 && gpsF==0){
		MSG_INFO(1, "Open GPS");
		msgAtSend("AT+SGPS=1\r\n");
		gpsCheckInfo();//检查下是否成功
	}
	//根据bubiaoEnable打开或关闭GPS
	if(newPara.bubiaoEnable==0){
		if(bblF!=0){
			MSG_INFO(1, "Shut BUBIAO");
			msgAtSend("AT+BUBIAO=0\r\n");
			gpsCheckInfo();//检查下是否成功			
		}
	}else{
		if(bblF==0){
			MSG_INFO(1, "Open BUBIAO");
			msgAtSend("AT+BUBIAO=1\r\n");
			gpsCheckInfo();//检查下是否成功
		}
	}
	//更新值
	gpsInfo.bblF=bblF;
	gpsInfo.bbAuth=bbAuth;
	gpsInfo.Lat=Lat;
	gpsInfo.Long=Long;
	gpsInfo.update++;
	if(Lat==0 && Long==0) gpsInfo.gpsLocated=0;
	else gpsInfo.gpsLocated=1;
}