#include "yunRGB.h"
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "mem.h"
#include "user_interface.h"
#include "espconn.h"
#include "gpio.h"
#include "uart.h"
#include "pwm.h"

/******************************************************************************
*此区域为编辑区,请需要添加设计的请到此区域编辑!
*******************************************************************************/

/************************乐鑫云三维数据起始************************************/
void cyctimer_star(void);
void  cyctimeractiv(void) ;
void  gettimerto(void *arg)  ;
int linkled=4;
char masterkey[]="4e56266f2502936e0378ea6a985dc74a5bec4280";
os_timer_t  cyctimer;
 os_timer_t  gettimer;
#define RESPONSE_FRAME  "{\"status\": 200, \"datapoint\": {\"x\": %s,\"y\": %s,\"z\": %s,\"k\": %s}, \"nonce\": %d, \"deliver_to_device\": true}\n"
#define FIRST_FRAME     "{\"path\": \"/v1/device/identify\", \"method\": \"GET\",\"meta\": {\"Authorization\": \"token %s\"}}\n"
#define BEACON_FRAME    "{\"path\": \"/v1/ping/\", \"method\": \"POST\",\"meta\": {\"Authorization\": \"token %s\"}}\n"
#define RPC_RESPONSE_FRAME  "{\"status\": 200, \"nonce\": %d, \"deliver_to_device\": true}\n"
#define TIMER_FRAME     "{\"body\": {}, \"get\":{\"is_humanize_format_simple\":\"true\"},\"meta\": {\"Authorization\": \"Token %s\"},\"path\": \"/v1/device/timers/\",\"post\":{},\"method\": \"GET\"}\n"
#include "yuntimer.h"
/******************************************************************************/
//TCP客户端起始
LOCAL struct espconn tcpclient1;
static ip_addr_t host_ip;
int RR='0';//红色
int GG='0';//绿色
int BB='0';//蓝色
int WW='0';//白色
void ICACHE_FLASH_ATTR
tcpclient1_recv(void *arg, char *pdata, unsigned short len)//接收函数
{int notice;
  char x[10];
 char y[10];
 char z[10];
 char k[10];
   char *pbuf = (char *)os_zalloc(2048);
os_timer_disarm (&cyctimer);//停止心跳包
  struct espconn *pespconn = (struct espconn *)arg;
  uart0_tx_buffer(pdata, len);
  notice=atoi(jsonval(pdata,"\"nonce\""));
  if(notice>0){
os_sprintf(x, "%d",GG);//X为第一路占空比
os_sprintf(y, "%d",RR);//Y为第二路占空比
os_sprintf(z, "%d",BB);//z为第三路占空比
os_sprintf(k, "%d",WW);//K为第四路占空比
    os_sprintf(pbuf, RESPONSE_FRAME, x,y,z,k, notice);//终端请求发送数据点x,y,z,k
    espconn_sent(pespconn , pbuf, strlen(pbuf));  
    }

   if(strstr(pdata,"datapoint")&&strstr(pdata,"\"x\"")){strcpy(x, jsonval(pdata,"\"x\""));//处理数据点X,处理第一路占空比
/**********************************处理区***************************************/

pwm_set_freq(100) ;
RR=atoi(x);
pwm_set_duty(GG,0) ;
pwm_set_duty(RR,1) ;
pwm_set_duty(BB,2) ;
pwm_set_duty(WW,3) ;
pwm_start();
/******************************************************************************/
     } 

   if(strstr(pdata,"datapoint")&&strstr(pdata,"\"y\"")){strcpy(y, jsonval(pdata,"\"y\""));//处理数据点Y,处理第二路占空比
/**********************************处理区***************************************/

pwm_set_freq(100) ;
GG=atoi(y);
pwm_set_duty(GG,0) ;
pwm_set_duty(RR,1) ;
pwm_set_duty(BB,2) ;
pwm_set_duty(WW,3) ;
pwm_start();
/******************************************************************************/
     } 

   if(strstr(pdata,"datapoint")&&strstr(pdata,"\"z\"")){strcpy(z, jsonval(pdata,"\"z\""));//处理数据点Z,处理第三路占空比
/**********************************处理区***************************************/

pwm_set_freq(100) ;
BB=atoi(z);
pwm_set_duty(GG,0) ;
pwm_set_duty(RR,1) ;
pwm_set_duty(BB,2) ;
pwm_set_duty(WW,3) ;
pwm_start();
/******************************************************************************/
     } 
   if(strstr(pdata,"datapoint")&&strstr(pdata,"\"k\"")){strcpy(k, jsonval(pdata,"\"k\""));//处理数据点K,,处理第四路占空比
/**********************************处理区***************************************/

pwm_set_freq(100) ;
WW=atoi(k);
pwm_set_duty(GG,0) ;
pwm_set_duty(RR,1) ;
pwm_set_duty(BB,2) ;
pwm_set_duty(WW,3) ;
pwm_start();
/******************************************************************************/
     } 
 if(strstr(pdata,"weekday_timestamp_mod")){timer_start(pdata);}  //获取定时并运行定时列表 


     if(strstr(pdata,"/v1/device/timers/"))//手机定时后，重建定时列表
      { os_timer_disarm (&gettimer) ;
os_timer_setfn(&gettimer, (os_timer_func_t *)gettimerto, pespconn);
os_timer_arm(&gettimer,2000,0);  
   }

if(strstr(pdata,"device")){
    os_sprintf(pbuf, TIMER_FRAME , masterkey);
    espconn_sent(pespconn , pbuf, strlen(pbuf));  //发送获取定时列表请求
     }   

  os_free(pbuf);
os_timer_setfn(&cyctimer, (os_timer_func_t *)cyctimeractiv, NULL);
os_timer_arm(&cyctimer,50000,1);  //重启心跳包  

}

static void ICACHE_FLASH_ATTR
tcpclient1_discon_cb(void *arg)//断开回调
{
  struct espconn *pespconn = (struct espconn *)arg;
GPIO_OUTPUT_SET(linkled, 1);
  os_timer_disarm (&cyctimer) ;
  cyctimer_star();
    //uart0_sendStr("Unlink\r\n");

  }

static void ICACHE_FLASH_ATTR
tcpclient1_sent_cb(void *arg)//发送回调
{
struct espconn *pespconn = (struct espconn *)arg;
  
}



static void ICACHE_FLASH_ATTR
tcpclient1_connect_cb(void *arg) //连接回调
{  uint8 bssid[6];
   char *pbuf = (char *)os_zalloc(2048);
  struct espconn *pespconn = (struct espconn *)arg;
  //os_printf("tcp tcpclient1 connect\r\n");
 uart0_sendStr("连接成功\n");
  GPIO_OUTPUT_SET(linkled, 0);
  espconn_regist_disconcb(pespconn, tcpclient1_discon_cb);
  espconn_regist_recvcb(pespconn,tcpclient1_recv);
  espconn_regist_sentcb(pespconn, tcpclient1_sent_cb);
os_timer_disarm (&cyctimer) ;

os_sprintf(pbuf, FIRST_FRAME, masterkey);  
espconn_sent(pespconn , pbuf, strlen(pbuf));  
  os_free(pbuf); 
  os_timer_setfn(&cyctimer, (os_timer_func_t *)cyctimeractiv, NULL);
os_timer_arm(&cyctimer,50000,1);
}

static void ICACHE_FLASH_ATTR
tcpclient1_recon_cb(void *arg, sint8 errType)//重连回调,或连接失败回调
{
     uart0_sendStr("Fail\r\n");
  GPIO_OUTPUT_SET(linkled, 1);
  os_timer_disarm (&cyctimer) ;
  cyctimer_star();
   // espconn_connect(pespconn);
  }




 LOCAL void ICACHE_FLASH_ATTR
 tcpclient1_init(){
   uint32_t ip = 0;
  char ipTemp[128]="115.29.202.58";
  ip = ipaddr_addr(ipTemp);
tcpclient1.type = ESPCONN_TCP;
tcpclient1.proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
tcpclient1.proto.tcp->local_port = espconn_port();
tcpclient1.proto.tcp->remote_port = 8000;
os_memcpy(tcpclient1.proto.tcp->remote_ip, &ip, 4);
 espconn_regist_connectcb(&tcpclient1, tcpclient1_connect_cb); //连接回调
 espconn_regist_reconcb(&tcpclient1, tcpclient1_recon_cb);//重连回调或连接结果回调

espconn_connect(&tcpclient1);
      
   
  }
//TCP客户端结尾
/******************************************************************************/
/******************************************************************************/

void ICACHE_FLASH_ATTR
cyctimer_exe(void) 
{
//时钟周期执行区

tcpclient1_init();


//时钟周期执行区
}

void ICACHE_FLASH_ATTR
cyctimeractiv(void) 
{
//时钟周期执行区
   char *pbuf = (char *)os_zalloc(2048);
os_sprintf(pbuf, BEACON_FRAME, masterkey);
espconn_sent(&tcpclient1, pbuf, strlen(pbuf));  //发送心跳包
os_free(pbuf);
//时钟周期执行区
}

void ICACHE_FLASH_ATTR
gettimerto(void *arg) 
{
//时钟周期执行区
 char *pbuf = (char *)os_zalloc(2048);
   struct espconn *pespconn = (struct espconn *)arg;
   os_sprintf(pbuf, TIMER_FRAME , masterkey);
   espconn_sent(pespconn , pbuf, strlen(pbuf)); 
 os_free(pbuf);
//时钟周期执行区
}

void ICACHE_FLASH_ATTR
cyctimer_star(void) //启用时钟
{
os_timer_setfn(&cyctimer, (os_timer_func_t *)cyctimer_exe, NULL);
os_timer_arm(&cyctimer,1000,1);
}
/******************************************************************************/


/******************************************************************************
*编辑区结尾,下面是唯一个子程序,也叫接口程序,请不要动其位置!
*******************************************************************************/
void ICACHE_FLASH_ATTR
yunserves_init(void)//云服务调用接口
{
gpiorun(linkled);
gpiorun(14);
cyctimer_star();

}

/************************乐鑫云三维数据结束************************************/


/******************************************************************************
*编辑区结尾,下面是唯一个子程序,也叫接口程序,请不要动其位置!
*******************************************************************************/
void ICACHE_FLASH_ATTR
yunRGB_init(void)
{

pwm_init(100, "0000");

yunserves_init();

}
