#define BLINKER_WIFI
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Blinker.h>
#include <FS.h>

String flash_file = "/false.txt";  //被读取的文件位置和名称
String flash_bl = "/bl.txt";
bool flash_bool = false;

char auth[] = "b07664fc1910";
char ssid[] = "webserver";
char pswd[] = "www.360xyz.cn";
bool heating = false;  //加热状态  true 正在加热  false 加热关闭
bool water = false;    //加水状态  true 正在加水  false 加水关闭
float waterT = 0.01;   //预设定水温

// 定义数据线引脚 D1
#define ONE_WIRE_BUS 5
#define D5 14  //加热开关
#define D6 12  //上水开关

// 初始化 OneWire 总线对象
OneWire oneWire(ONE_WIRE_BUS);

// 初始化 DallasTemperature 对象
DallasTemperature sensors(&oneWire);

BlinkerNumber Number1("num-8sr");  //水温
BlinkerButton Button1("btn-x2p");  //刷新水温

BlinkerNumber Number2("num-yc4");  //预热水温
BlinkerButton Button2("btn-s73");  //刷新预热水温

BlinkerButton Button3("btn-8ft");  //手动加热开关
BlinkerButton Button4("btn-bx8");  //上水开关

BlinkerButton Button5("btn-4dx");  //刷新手动开关状态
BlinkerButton Button6("btn-ems");  //刷新上水开关状态

BlinkerText Text1("tex-qyx");  //显示加热开关或上水 状态

void setup(void) {
  pinMode(D5, OUTPUT);
  pinMode(D6, OUTPUT);
  digitalWrite(D5, LOW);
  digitalWrite(D6, 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);  //刷新上水开关状态
  Blinker.attachData(dataRead);

  SPIFFS.begin();
  //是否存在数据
  if (SPIFFS.exists(flash_file) == false) {
    File dataFile = SPIFFS.open(flash_file, "w");
    dataFile.println("27.00");
    waterT = 27.00;
    dataFile.close();
  } else {
    SPIFFS.begin();
    File dataFile = SPIFFS.open(flash_file, "r");
    String result = dataFile.readString();
    result.trim();  //清理字符两边的空格
    waterT = result.toFloat();
  }

  if (SPIFFS.exists(flash_bl) == false) {
    File dataFile = SPIFFS.open(flash_bl, "w");
    dataFile.println("false");
    dataFile.close();
  } else {
    SPIFFS.begin();
    File dataFile = SPIFFS.open(flash_bl, "r");
    String result = dataFile.readString();
    result.trim();  //清理字符两边的空格
    if (result == "true") {
      digitalWrite(D5, HIGH);
      heating = true;
      flash_bool = true;
    }
  }

  // 初始化DS18B20传感器
  sensors.begin();
}

void loop(void) {
  Blinker.run();

  // 获取温度数据
  sensors.requestTemperatures();
  // 读取温度
  float temperature = sensors.getTempCByIndex(0);
  if (temperature >= waterT) {
    digitalWrite(D5, LOW);
    heating = false;
    if (flash_bool == false) {
      SPIFFS.begin();
      File dataFile = SPIFFS.open(flash_bl, "w");
      dataFile.println("false");
      dataFile.close();
      flash_bool = true;
    }

    Serial.println("达到设定温度 关闭加热");
  }

  delay(1000);
}
//刷新水温
void button1_callback(const String& state) {
  //BLINKER_LOG("get button state: ", state);
  sensors.requestTemperatures();
  float temperature = sensors.getTempCByIndex(0);
  Number1.print(temperature);
  Text1.print(temperature);
}
//刷新预热水温
void button2_callback(const String& state) {
  //BLINKER_LOG("get button state: ", state);
  SPIFFS.begin();
  File dataFile = SPIFFS.open(flash_file, "r");
  String result = dataFile.readString();
  result.trim();  //清理字符两边的空格
  Number2.print(result.toFloat());
  Serial.print(result);
}
//手动加热开关
void button3_callback(const String& state) {
  //BLINKER_LOG("get button state: ", state);
  SPIFFS.begin();
  if (heating == false) {
    heating = true;
    digitalWrite(D5, HIGH);
    if (flash_bool == false) {
      File dataFile = SPIFFS.open(flash_bl, "w");
      dataFile.println("true");
      dataFile.close();
      flash_bool = true;
    }

    Text1.print("正在加热...");
    Button3.color("#00FF00");
    Button3.text("开");
  } else {
    heating = false;
    digitalWrite(D5, LOW);

    if (flash_bool == true) {
      File dataFile = SPIFFS.open(flash_bl, "w");
      dataFile.println("false");
      dataFile.close();
      flash_bool = false;
    }

    Text1.print("加热已经关闭");

    Button3.color("#000000");
    Button3.text("关");
  }
  Button3.print();
}
//上水开关
void button4_callback(const String& state) {
  // BLINKER_LOG("get button state: ", state);
  if (water == false) {
    water = true;
    digitalWrite(D6, HIGH);
    Text1.print("正在加水...");

    Button4.color("#00FF00");
    Button4.text("开");
  } else {
    water = false;
    digitalWrite(D6, LOW);
    Text1.print("加水已经关闭");

    Button4.color("#000000");
    Button4.text("关");
  }
  Button4.print();
}
//刷新手动加热开关状态
void button5_callback(const String& state) {
  //BLINKER_LOG("get button state: ", state);
  if (heating == true) {

    Text1.print("正在加热...");
    Button3.color("#00FF00");
    Button3.text("开");
  } else {
    Button3.color("#000000");
    Button3.text("关");
    Text1.print("加热已经关闭");
  }
  Button3.print();
}
//刷新上水开关状态
void button6_callback(const String& state) {
  //BLINKER_LOG("get button state: ", state);
  if (water == true) {
    Button4.color("#00FF00");
    Button4.text("开");
    Text1.print("正在加水...");
  } else {
    Button4.color("#000000");
    Button4.text("关");
    Text1.print("加水已经关闭");
  }
  Button4.print();
}

// 如果未绑定的组件被触发，则会执行其中内容
void dataRead(const String& data) {
  BLINKER_LOG("Blinker readString: ", data);
  float tmp = data.toFloat();

  if (tmp == 0) {
    Text1.print("失败,只能数字");
  } else {
    File dataFile = SPIFFS.open(flash_file, "w");
    dataFile.println(data);
    waterT = tmp;
    dataFile.close();
    Text1.print("预制温度 " + data);
  }
}
