/**
	************************************************************
	************************************************************
	************************************************************
	*	ļ 	onenet.c
	*
	*	ߣ 		ż
	*
	*	ڣ 		2017-05-08
	*
	*	汾 		V1.1
	*
	*	˵ 		onenetƽ̨ݽӿڲ
	*
	*	޸ļ¼	V1.0Эװж϶ͬһļҲͬЭӿڲͬ
	*				V1.1ṩͳһӿڹӦòʹãݲͬЭļװЭصݡ
	************************************************************
	************************************************************
	************************************************************
**/

//Ƭͷļ
#include "stm32f10x.h"

//豸
#include "m6312.h"

//Эļ
#include "onenet.h"
#include "mqttkit.h"

//Ӳ
#include "usart.h"
#include "delay.h"
#include "adxl362.h"

//C
#include <string.h>
#include <stdio.h>


#define PROID		"77247"

#define AUTH_INFO	"test"

#define DEVID		"5616839"


extern unsigned char esp8266_buf[128];


//==========================================================
//	ƣ	OneNet_DevLink
//
//	ܣ	onenet
//
//	ڲ	
//
//	ز	1-ɹ	0-ʧ
//
//	˵		onenetƽ̨
//==========================================================
_Bool OneNet_DevLink(void)
{
	
	MQTT_PACKET_STRUCTURE mqttPacket = {NULL, 0, 0, 0};					//Э

	unsigned char *dataPtr;
	
	_Bool status = 1;
	
	UsartPrintf(USART_DEBUG, "OneNet_DevLink\r\n"
							"PROID: %s,	AUIF: %s,	DEVID:%s\r\n"
                        , PROID, AUTH_INFO, DEVID);
	
	if(MQTT_PacketConnect(PROID, AUTH_INFO, DEVID, 256, 0, MQTT_QOS_LEVEL0, NULL, NULL, 0, &mqttPacket) == 0)
	{
		M6312_SendData(mqttPacket._data, mqttPacket._len);				//ϴƽ̨
		
		dataPtr = M6312_GetIPD(250);									//ȴƽ̨Ӧ
		if(dataPtr != NULL)
		{
			if(MQTT_UnPacketRecv(dataPtr) == MQTT_PKT_CONNACK)
			{
				switch(MQTT_UnPacketConnectAck(dataPtr))
				{
					case 0:UsartPrintf(USART_DEBUG, "Tips:	ӳɹ\r\n");status = 0;break;
					
					case 1:UsartPrintf(USART_DEBUG, "WARN:	ʧܣЭ\r\n");break;
					case 2:UsartPrintf(USART_DEBUG, "WARN:	ʧܣǷclientid\r\n");break;
					case 3:UsartPrintf(USART_DEBUG, "WARN:	ʧܣʧ\r\n");break;
					case 4:UsartPrintf(USART_DEBUG, "WARN:	ʧܣû\r\n");break;
					case 5:UsartPrintf(USART_DEBUG, "WARN:	ʧܣǷ(tokenǷ)\r\n");break;
					
					default:UsartPrintf(USART_DEBUG, "ERR:	ʧܣδ֪\r\n");break;
				}
			}
		}
		
		MQTT_DeleteBuffer(&mqttPacket);								//ɾ
	}
	else
		UsartPrintf(USART_DEBUG, "WARN:	MQTT_PacketConnect Failed\r\n");
	
	return status;
	
}

unsigned char OneNet_FillBuf(char *buf)
{
	
	char text[64];
	
	memset(text, 0, sizeof(text));
	
	strcpy(buf, "{\"datastreams\":[");
	
	memset(text, 0, sizeof(text));
	sprintf(text, "{\"id\":\"Xg\",\"datapoints\":[{\"value\":%0.2f}]},", adxl362Info.x);
	strcat(buf, text);
	
	memset(text, 0, sizeof(text));
	sprintf(text, "{\"id\":\"Yg\",\"datapoints\":[{\"value\":%0.2f}]},", adxl362Info.y);
	strcat(buf, text);
	
	memset(text, 0, sizeof(text));
	sprintf(text, "{\"id\":\"Zg\",\"datapoints\":[{\"value\":%0.2f}]}", adxl362Info.z);
	strcat(buf, text);
	
	strcat(buf, "]}");
	
	return strlen(buf);

}

//==========================================================
//	ƣ	OneNet_SendData
//
//	ܣ	ϴݵƽ̨
//
//	ڲ	typeݵĸʽ
//
//	ز	
//
//	˵		
//==========================================================
void OneNet_SendData(void)
{
	
	MQTT_PACKET_STRUCTURE mqttPacket = {NULL, 0, 0, 0};												//Э
	
	char buf[256];
	
	short body_len = 0, i = 0;
	
	UsartPrintf(USART_DEBUG, "Tips:	OneNet_SendData-MQTT\r\n");
	
	memset(buf, 0, sizeof(buf));
	
	body_len = OneNet_FillBuf(buf);																	//ȡǰҪ͵ܳ
	
	if(body_len)
	{
		if(MQTT_PacketSaveData(DEVID, body_len, NULL, 1, &mqttPacket) == 0)							//
		{
			for(; i < body_len; i++)
				mqttPacket._data[mqttPacket._len++] = buf[i];
			
			M6312_SendData(mqttPacket._data, mqttPacket._len);										//ϴݵƽ̨
			UsartPrintf(USART_DEBUG, "Send %d Bytes\r\n", mqttPacket._len);
			
			MQTT_DeleteBuffer(&mqttPacket);															//ɾ
		}
		else
			UsartPrintf(USART_DEBUG, "WARN:	MQTT_NewBuffer Failed\r\n");
	}
	
}

//==========================================================
//	ƣ	OneNet_RevPro
//
//	ܣ	ƽ̨ݼ
//
//	ڲ	dataPtrƽ̨ص
//
//	ز	
//
//	˵		
//==========================================================
void OneNet_RevPro(unsigned char *cmd)
{
	
	MQTT_PACKET_STRUCTURE mqttPacket = {NULL, 0, 0, 0};								//Э
	
	char *req_payload = NULL;
	char *cmdid_topic = NULL;
	
	unsigned short req_len = 0;
	
	unsigned char type = 0;
	
	short result = 0;

	char *dataPtr = NULL;
	char numBuf[10];
	int num = 0;
	
	type = MQTT_UnPacketRecv(cmd);
	switch(type)
	{
		case MQTT_PKT_CMD:															//·
			
			result = MQTT_UnPacketCmd(cmd, &cmdid_topic, &req_payload, &req_len);	//topicϢ
			if(result == 0)
			{
				UsartPrintf(USART_DEBUG, "cmdid: %s, req: %s, req_len: %d\r\n", cmdid_topic, req_payload, req_len);
				
				if(MQTT_PacketCmdResp(cmdid_topic, req_payload, &mqttPacket) == 0)	//ظ
				{
					UsartPrintf(USART_DEBUG, "Tips:	Send CmdResp\r\n");
					
					M6312_SendData(mqttPacket._data, mqttPacket._len);				//ظ
					MQTT_DeleteBuffer(&mqttPacket);									//ɾ
				}
			}
		
		break;
			
		case MQTT_PKT_PUBACK:														//PublishϢƽ̨ظAck
		
			if(MQTT_UnPacketPublishAck(cmd) == 0)
				UsartPrintf(USART_DEBUG, "Tips:	MQTT Publish Send OK\r\n");
			
		break;
		
		default:
			result = -1;
		break;
	}
	
	M6312_Clear();										//ջ
	
	if(result == -1)
		return;
	
	dataPtr = strchr(req_payload, '}');					//'}'

	if(dataPtr != NULL && result != -1)					//ҵ
	{
		dataPtr++;
		
		while(*dataPtr >= '0' && *dataPtr <= '9')		//жǷ·
		{
			numBuf[num++] = *dataPtr++;
		}
		numBuf[num] = 0;
		
		num = atoi((const char *)numBuf);				//תΪֵʽ
	}

	if(type == MQTT_PKT_CMD || type == MQTT_PKT_PUBLISH)
	{
		MQTT_FreeBuffer(cmdid_topic);
		MQTT_FreeBuffer(req_payload);
	}

}
