  *  body:
  *    json data from yunba service
  */ 
+#include <ArduinoJson.h>
 
 #define HEADER_LEN 4
 #define BUF_LEN 256
 #define FLAG_CHAR 0xaa
+#define FIRST_PIN 5
+
+#define CHILD_NUM 3
 
 const char *g_appkey = "5697113d4407a3cd028abead";
 const char *g_topic = "smart_office";

int g_step = 1; // 1: recv header, 2 recv body
uint16_t g_body_len = 0;
uint16_t g_recv_len = 0;

void recv_header() {
  while (Serial.available() >= HEADER_LEN) {

}

void handle_msg() {
  StaticJsonBuffer<128> jsonBuffer;

  JsonObject& root = jsonBuffer.parseObject((char *)g_buf);
  if (!root.success()) {
    Serial.println("parseObject");
    return;
  }

  if (strcmp(root["devid"], g_devid) != 0) {
    Serial.println("not my devid");
    return;
  }

  if (strcmp(root["cmd"], "switch_set") == 0) {
    int j = min(CHILD_NUM, root["status"].size());
    for (int i = 0; i < j; i++) {
      uint8_t st = root["status"][i];
      set_status(i, st);
    }
  } else if (strcmp(root["cmd"], "switch_get") == 0) {
    report_status();
  }
}

void set_status(int index, char st) {
  if (st != 0)
    st = 1;

  Serial.print("set:");
  Serial.print(index);
  Serial.print(st);

  if (g_status[index] != st) {
    g_status[index] = st;
  }
   
  if (st == 0) {
    //Serial.println(0);
    digitalWrite(FIRST_PIN + index, LOW);
  } else {
    //Serial.println(1);
    digitalWrite(FIRST_PIN + index, HIGH);
  }
}

void init_status() {
  
}

void send_msg() {
  g_buf[0] = FLAG_CHAR;
  g_buf[1] = 1; // 1: upstream
  g_buf[2] = ((uint8_t *)&g_body_len)[1];
  g_buf[3] = ((uint8_t *)&g_body_len)[0];

  Serial.write(g_buf, g_body_len + HEADER_LEN);
///  Serial.flush();
}

void report_status() {
  StaticJsonBuffer<128> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();

  root["devid"] = g_devid;

  JsonArray& st = root.createNestedArray("status");
  for (int i = 0; i < CHILD_NUM; i++) {
    st.add(g_status[i]);
  }
  g_body_len = root.printTo((char *)g_buf + HEADER_LEN, BUF_LEN - HEADER_LEN);

  send_msg();
}

void handle_input() {
