Use name for endpoints
This commit is contained in:
parent
e45991de2f
commit
0e659a6f97
1 changed files with 18 additions and 28 deletions
46
espower.ino
46
espower.ino
|
@ -130,26 +130,20 @@ void setup() {
|
|||
// create HTTP server
|
||||
server.on("/", handleMainpage);
|
||||
for (int i = 0; i < numofsockets; i++) {
|
||||
server.on("/" + String(sockets[i].channel), handleSocket);
|
||||
server.on("/" + String(sockets[i].name), handleSocket);
|
||||
}
|
||||
server.begin();
|
||||
Serial.println("HTTP server started");
|
||||
}
|
||||
|
||||
|
||||
void handleSocket() {
|
||||
String data = server.arg("plain");
|
||||
String channelArg = server.uri().substring(1);
|
||||
String idArg = server.arg("id");
|
||||
String nameArg = server.uri().substring(1);
|
||||
int socketIdx = findSocketIndex(nameArg);
|
||||
String result;
|
||||
|
||||
char channel[5];
|
||||
strncpy(channel, channelArg.c_str(), 5);
|
||||
|
||||
char id[5];
|
||||
strncpy(id, idArg.c_str(), 5);
|
||||
|
||||
Serial.println(channel);
|
||||
Serial.println(id);
|
||||
Serial.println(nameArg);
|
||||
|
||||
if (data != "") {
|
||||
Serial.println("change request");
|
||||
|
@ -158,19 +152,14 @@ void handleSocket() {
|
|||
|
||||
if (json["active"] == "true") {
|
||||
Serial.println("on");
|
||||
result = setSocket(channel, id, true);
|
||||
result = setSocket(socketIdx, true);
|
||||
} else if (json["active"] == "false") {
|
||||
Serial.println("off");
|
||||
result = setSocket(channel, id, false);
|
||||
result = setSocket(socketIdx, false);
|
||||
}
|
||||
} else {
|
||||
Serial.println("status request");
|
||||
if (server.arg("id") != "") {
|
||||
result = generateStatusJson(channel, id);
|
||||
} else {
|
||||
Serial.println("ERROR: no id");
|
||||
result = "{ error: \"no id\" }";
|
||||
}
|
||||
result = generateStatusJson(socketIdx);
|
||||
}
|
||||
server.send(200, "text/json", result);
|
||||
|
||||
|
@ -178,17 +167,18 @@ void handleSocket() {
|
|||
Serial.println("");
|
||||
}
|
||||
|
||||
|
||||
void handleMainpage() {
|
||||
String mainpage = website(header, body);
|
||||
server.send(200, "text/html", mainpage);
|
||||
}
|
||||
|
||||
String setSocket(const char* channel, const char* id, bool newStatus) {
|
||||
|
||||
String setSocket(int socketIdx, bool newStatus) {
|
||||
PowerSocket socket;
|
||||
bool oldStatus = false;
|
||||
bool success = false;
|
||||
|
||||
int socketIdx = findSocketIndex(channel, id);
|
||||
if (socketIdx >= 0) {
|
||||
success = true;
|
||||
socket = sockets[socketIdx];
|
||||
|
@ -211,12 +201,11 @@ String setSocket(const char* channel, const char* id, bool newStatus) {
|
|||
return generateChangeJson(success, String(oldStatus), String(socket.powered));
|
||||
}
|
||||
|
||||
int findSocketIndex(const char* channel, const char* id) {
|
||||
|
||||
int findSocketIndex(String name) {
|
||||
for (int socketIdx = 0; socketIdx < numofsockets; socketIdx++) {
|
||||
PowerSocket canidateSocket = sockets[socketIdx];
|
||||
if (strncmp(canidateSocket.channel, channel, 5) == 0 &&
|
||||
strncmp(canidateSocket.id, id, 5) == 0)
|
||||
{
|
||||
if (canidateSocket.name == name) {
|
||||
return socketIdx;
|
||||
}
|
||||
}
|
||||
|
@ -224,6 +213,7 @@ int findSocketIndex(const char* channel, const char* id) {
|
|||
return -1;
|
||||
}
|
||||
|
||||
|
||||
String generateChangeJson(bool success, String oldStatus, String newStatus) {
|
||||
String status = "";
|
||||
if (success) {
|
||||
|
@ -234,9 +224,9 @@ String generateChangeJson(bool success, String oldStatus, String newStatus) {
|
|||
return retJson;
|
||||
}
|
||||
|
||||
String generateStatusJson(const char* channel, const char* id) {
|
||||
int idx = findSocketIndex(channel, id);
|
||||
PowerSocket socket = sockets[idx];
|
||||
|
||||
String generateStatusJson(int socketIdx) {
|
||||
PowerSocket socket = sockets[socketIdx];
|
||||
String status = "{ \"status\" : " + String(socket.powered) + " }";
|
||||
return status;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue