#define BLINKER_WIFI
#include <Arduino.h>
#include <Blinker.h>
#include <Ticker.h>

char auth[] = "fef9287848c3";
char ssid[] = "webserver";
char pswd[] = "www.360xyz.cn";

#define D1 32
#define D2 33
#define D3 25
#define D4 26
#define D5 27
#define D6 14
#define D7 12
#define D8 13
#define D9 15
#define D10 2
bool bl1 = false;
bool bl2 = false;
bool bl3 = false;
bool bl4 = false;
bool bl5 = false;
bool bl6 = false;
bool bl7 = false;
bool bl8 = false;
bool bl9 = false;
bool bl10 = false;

BlinkerButton Button1("btn-ho0");
BlinkerButton Button2("btn-3xq");
BlinkerButton Button3("btn-qsi");
BlinkerButton Button4("btn-qg3");
BlinkerButton Button5("btn-88b");
BlinkerButton Button6("btn-gjj");
BlinkerButton Button7("btn-fpd");
BlinkerButton Button8("btn-hca");
BlinkerButton Button9("btn-n33");
BlinkerButton Button10("btn-5lk");
void setup() {
  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);
  pinMode(D4, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D6, OUTPUT);
  pinMode(D7, OUTPUT);
  pinMode(D8, OUTPUT);
  pinMode(D9, OUTPUT);
  pinMode(D10, OUTPUT);
  digitalWrite(D1, LOW);
  digitalWrite(D2, LOW);
  digitalWrite(D3, LOW);
  digitalWrite(D4, LOW);
  digitalWrite(D5, LOW);
  digitalWrite(D6, LOW);
  digitalWrite(D7, LOW);
  digitalWrite(D8, LOW);
  digitalWrite(D9, LOW);
  digitalWrite(D10, LOW);
  Serial.begin(115200);
  Blinker.begin(auth, ssid, pswd);
  Button1.attach(button1_callback);
  Button2.attach(button2_callback);
  Button3.attach(button3_callback);
  Button4.attach(button4_callback);
  Button5.attach(button5_callback);
  Button6.attach(button6_callback);
  Button7.attach(button7_callback);
  Button8.attach(button8_callback);
  Button9.attach(button9_callback);
  Button10.attach(button10_callback);
  Blinker.attachData(dataRead);
}

void loop() {
  Blinker.run();

  if (bl1 == true && bl10 == true) {
    digitalWrite(D1, HIGH);
    digitalWrite(D10, HIGH);
    delay(1000);
    digitalWrite(D1, LOW);
    digitalWrite(D10, LOW);
  } else if (bl2 == true && bl10 == true) {
    digitalWrite(D2, HIGH);
    digitalWrite(D10, HIGH);
    delay(1000);
    digitalWrite(D2, LOW);
    digitalWrite(D10, LOW);
  } else {
    if (bl1 == true) {
      digitalWrite(D1, HIGH);
      delay(1000);
      digitalWrite(D1, LOW);
    }
    if (bl2 == true) {
      digitalWrite(D2, HIGH);
      delay(1000);
      digitalWrite(D2, LOW);
    }
    if (bl10 == true) {
      digitalWrite(D10, HIGH);
      delay(1000);
      digitalWrite(D10, LOW);
    }
  }
}
void button1_callback(const String& state) {
  //BLINKER_LOG("get button state: ", state);
  if (bl1 == false) {
    bl1 = true;
    bl2=false;
    digitalWrite(D2, LOW);
    Button2.color("#000000");
    Button2.text("已停");
    Button2.print();
    Button1.color("#00FF00");
    Button1.text("左转");
  } else {
    bl1 = false;
    Button1.color("#000000");
    Button1.text("已停");
  }
  Button1.print();
}
void button2_callback(const String& state) {
  if (bl2 == false) {
    bl2 = true;
    bl1=false;
    Button1.color("#000000");
    Button1.text("已停");
    Button1.print();
    digitalWrite(D1, LOW);
    Button2.color("#00FF00");
    Button2.text("右转");
  } else {
    bl2 = false;
    Button2.color("#000000");
    Button2.text("已停");
  }
  Button2.print();
}

void button3_callback(const String& state) {
  if (bl3 == false) {
    bl3 = true;
    digitalWrite(D3, HIGH);
    Button3.color("#00FF00");
    Button3.text("已开");
  } else {
    bl3 = false;
    digitalWrite(D3, LOW);
    Button3.color("#000000");
    Button3.text("已关");
  }
  Button3.print();
}
void button4_callback(const String& state) {
  if (bl4 == false) {
    bl4 = true;
    digitalWrite(D4, HIGH);
    Button4.color("#00FF00");
    Button4.text("已开");
  } else {
    bl4 = false;
    digitalWrite(D4, LOW);
    Button4.color("#000000");
    Button4.text("已关");
  }
  Button4.print();
}
void button5_callback(const String& state) {
  //BLINKER_LOG("get button state: ", state);
}
void button6_callback(const String& state) {
  //BLINKER_LOG("get button state: ", state);
}
void button7_callback(const String& state) {
  //BLINKER_LOG("get button state: ", state);
}
void button8_callback(const String& state) {
  //BLINKER_LOG("get button state: ", state);
}
void button9_callback(const String& state) {
  //BLINKER_LOG("get button state: ", state);
}
void button10_callback(const String& state) {
  if (bl10 == false) {
    bl10 = true;
    Button10.color("#00FF00");
    Button10.text("闪烁中");
  } else {
    bl10 = false;
    Button10.color("#000000");
    Button10.text("已关");
  }
  Button10.print();
}
void dataRead(const String& data) {
  BLINKER_LOG("Blinker readString: ", data);
}
