-
Notifications
You must be signed in to change notification settings - Fork 1
/
lamp_api.cpp
79 lines (64 loc) · 1.74 KB
/
lamp_api.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "lamp_api.h"
#include <ESP8266WebServer.h>
void LampAPI::setup(ESP8266WebServer *server, LampController *controller) {
this->server = server;
this->controller = controller;
server->on("/setLamp", [&]() {
handleLamp();
});
server->on("/setAntiNoise", [&]() {
handleAntiNoise();
});
server->on("/getState", [&]() {
handleGetState();
});
}
void LampAPI::handleLamp() {
String t_state = server->arg("lampState");
Serial.println(t_state);
if (t_state == "ON")
controller->setState(true);
else if (t_state == "OFF")
controller->setState(false);
else if (t_state == "TOGGLE")
controller->toggleLamp();
handleGetState();
}
void LampAPI::handleAntiNoise() {
String q = server->arg("q");
if (q == "ON")
controller->setAntiNoise(true);
else if (q == "OFF")
controller->setAntiNoise(false);
else if (q == "PULSE") {
String len = server->arg("len");
controller->setAntiNoisePulseLen(atoi(len.c_str()));
}
handleGetState();
}
void LampAPI::handleGetState() {
String response = "{";
if (controller->getState()) {
response += "\"lamp\": \"ON\"";
} else {
response += "\"lamp\": \"OFF\"";
}
if (controller->getSwitchState()) {
response += ", \"switch\": \"ON\"";
} else {
response += ", \"switch\": \"OFF\"";
}
if (controller->getAntiNoise()) {
response += ", \"antiNoise\": \"ON\"";
} else {
response += ", \"antiNoise\": \"OFF\"";
}
response += ", \"antiNoisePulseLen\":";
response += controller->getAntiNoisePulseLen();
response += ", \"switchLastChange\": ";
response += controller->getSwitchLastChange();
response += ", \"systemTime\": ";
response += millis();
response += "}";
server->send(200, "application/json", response);
}