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

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

//豸
#include "esp8266.h"

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

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

//ͼƬ
#include "image_2k.h"

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


#define DEVID	"6580246"

#define APIKEY	"iUZqKW6xIjpGngggBH=66VCGzqg="


extern unsigned char esp8266_buf[128];


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

	unsigned char *dataPtr;
	
	unsigned char status = 1;
	
	UsartPrintf(USART_DEBUG, "OneNet_DevLink\r\n"
                        "DEVID: %s,     APIKEY: %s\r\n"
                        , DEVID, APIKEY);

	if(EDP_PacketConnect1(DEVID, APIKEY, 256, &edpPacket) == 0)		//devid  apikeyװЭ
	{
		ESP8266_SendData(edpPacket._data, edpPacket._len);			//ϴƽ̨
		
		dataPtr = ESP8266_GetIPD(250);								//ȴƽ̨Ӧ
		if(dataPtr != NULL)
		{
			if(EDP_UnPacketRecv(dataPtr) == CONNRESP)
			{
				switch(EDP_UnPacketConnectRsp(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:	ʧܣ豸IDȨʧ\r\n");break;
					case 3:UsartPrintf(USART_DEBUG, "WARN:	ʧܣʧ\r\n");break;
					case 4:UsartPrintf(USART_DEBUG, "WARN:	ʧܣûIDȨʧ\r\n");break;
					case 5:UsartPrintf(USART_DEBUG, "WARN:	ʧܣδȨ\r\n");break;
					case 6:UsartPrintf(USART_DEBUG, "WARN:	ʧܣȨЧ\r\n");break;
					case 7:UsartPrintf(USART_DEBUG, "WARN:	ʧܣδ\r\n");break;
					case 8:UsartPrintf(USART_DEBUG, "WARN:	ʧܣ豸ѱ\r\n");break;
					case 9:UsartPrintf(USART_DEBUG, "WARN:	ʧܣظ\r\n");break;
					
					default:UsartPrintf(USART_DEBUG, "ERR:	ʧܣδ֪\r\n");break;
				}
			}
		}
		
		EDP_DeleteBuffer(&edpPacket);								//ɾ
	}
	else
		UsartPrintf(USART_DEBUG, "WARN:	EDP_PacketConnect Failed\r\n");
	
	return status;
	
}

unsigned char OneNet_FillBuf(char *buf)
{
	
	char text[16];
	
	memset(text, 0, sizeof(text));
	
	strcpy(buf, "{");
	
	memset(text, 0, sizeof(text));
	sprintf(text, "\"Red_Led\":%d,", led_status.Led4Sta);
	strcat(buf, text);
	
	memset(text, 0, sizeof(text));
	sprintf(text, "\"Green_Led\":%d,", led_status.Led5Sta);
	strcat(buf, text);
	
	memset(text, 0, sizeof(text));
	sprintf(text, "\"Yellow_Led\":%d,", led_status.Led6Sta);
	strcat(buf, text);
	
	memset(text, 0, sizeof(text));
	sprintf(text, "\"Blue_Led\":%d", led_status.Led7Sta);
	strcat(buf, text);
	
	strcat(buf, "}");
	
	return strlen(buf);

}

//==========================================================
//	ƣ	OneNet_SendData_EDPType2
//
//	ܣ	ϴݵƽ̨
//
//	ڲ	devid豸ID(ƼΪNULL)
//				pictureͼƬ
//				pic_lenͼƬݳ
//
//	ز	
//
//	˵		ǵ豸ʱʹ豸͸ģʽ
//				ϴͼƬǣǿҽdevidֶΪգƽ̨ὫͼƬ·豸
//==========================================================
#define PKT_SIZE 1024
void OneNet_SendData_Picture(char *devid, const char* picture, unsigned int pic_len)
{
	
	EDP_PACKET_STRUCTURE edpPacket = {NULL, 0, 0, 0};					//Э

	char type_bin_head[] = "{\"ds_id\":\"pic\"}";						//ͼƬͷ
	unsigned char *pImage = (unsigned char *)picture;
	
	if(EDP_PacketSaveData(devid, pic_len, type_bin_head, kTypeBin, &edpPacket) == 0)
	{	
		ESP8266_Clear();
		
		UsartPrintf(USART_DEBUG, "Send %d Bytes\r\n", edpPacket._len);
		ESP8266_SendData(edpPacket._data, edpPacket._len);				//ϴݵƽ̨
		
		EDP_DeleteBuffer(&edpPacket);									//ɾ
		
		UsartPrintf(USART_DEBUG, "image len = %d\r\n", pic_len);
		
		while(pic_len > 0)
		{
			DelayXms(300);												//ͼʱʱһ㣬һʱ
			
			if(pic_len >= PKT_SIZE)
			{
				ESP8266_SendData(pImage, PKT_SIZE);						//ڷͷƬ
				
				pImage += PKT_SIZE;
				pic_len -= PKT_SIZE;
			}
			else
			{
				ESP8266_SendData(pImage, (unsigned short)pic_len);		//ڷһƬ
				pic_len = 0;
			}
		}
		
		UsartPrintf(USART_DEBUG, "image send ok\r\n");
	}
	else
		UsartPrintf(USART_DEBUG, "EDP_PacketSaveData Failed\r\n");

}

//==========================================================
//	ƣ	OneNet_SendData
//
//	ܣ	ϴݵƽ̨
//
//	ڲ	typeݵĸʽ
//
//	ز	
//
//	˵		
//==========================================================
void OneNet_SendData(void)
{
	
	OneNet_SendData_Picture(NULL, Array, sizeof(Array));
	
}

//==========================================================
//	ƣ	OneNet_RevPro
//
//	ܣ	ƽ̨ݼ
//
//	ڲ	dataPtrƽ̨ص
//
//	ز	
//
//	˵		
//==========================================================
void OneNet_RevPro(unsigned char *cmd)
{
	
	EDP_PACKET_STRUCTURE edpPacket = {NULL, 0, 0, 0};	//Э
	
	char *cmdid_devid = NULL;
	char *req = NULL;
	unsigned char type = 0;
	
	short result = 0;

	char *dataPtr = NULL;
	char numBuf[10];
	int num = 0;
	
	type = EDP_UnPacketRecv(cmd);
	switch(type)										//жpushdata·
	{
		case SAVEACK:
			
			if(cmd[3] == MSG_ID_HIGH && cmd[4] == MSG_ID_LOW)
			{
				UsartPrintf(USART_DEBUG, "Tips:	Send %s\r\n", cmd[5] ? "Err" : "Ok");
			}
			else
				UsartPrintf(USART_DEBUG, "Tips:	Message ID Err\r\n");
			
		break;
			
		default:
			result = -1;
		break;
	}
	
	ESP8266_Clear();									//ջ
	
	if(result == -1)
		return;
	
	dataPtr = strchr(req, '}');							//'}'
	
	if(dataPtr != NULL)									//ҵ
	{
		dataPtr++;
		
		while(*dataPtr >= '0' && *dataPtr <= '9')		//жǷ·
		{
			numBuf[num++] = *dataPtr++;
		}
		numBuf[num] = 0;
		
		num = atoi((const char *)numBuf);				//תΪֵʽ
		
		
	}
	
	if(type == CMDREQ && result == 0)						//  ɹ
	{
		EDP_FreeBuffer(cmdid_devid);						//ͷڴ
		EDP_FreeBuffer(req);
															//ظ
		ESP8266_SendData(edpPacket._data, edpPacket._len);	//ϴƽ̨
		EDP_DeleteBuffer(&edpPacket);						//ɾ
	}

}
