Use name for endpoints

This commit is contained in:
sqozz 2018-03-11 01:05:17 +01:00
parent e45991de2f
commit 0e659a6f97
1 changed files with 18 additions and 28 deletions

View File

@ -130,26 +130,20 @@ void setup() {
// create HTTP server // create HTTP server
server.on("/", handleMainpage); server.on("/", handleMainpage);
for (int i = 0; i < numofsockets; i++) { for (int i = 0; i < numofsockets; i++) {
server.on("/" + String(sockets[i].channel), handleSocket); server.on("/" + String(sockets[i].name), handleSocket);
} }
server.begin(); server.begin();
Serial.println("HTTP server started"); Serial.println("HTTP server started");
} }
void handleSocket() { void handleSocket() {
String data = server.arg("plain"); String data = server.arg("plain");
String channelArg = server.uri().substring(1); String nameArg = server.uri().substring(1);
String idArg = server.arg("id"); int socketIdx = findSocketIndex(nameArg);
String result; String result;
char channel[5]; Serial.println(nameArg);
strncpy(channel, channelArg.c_str(), 5);
char id[5];
strncpy(id, idArg.c_str(), 5);
Serial.println(channel);
Serial.println(id);
if (data != "") { if (data != "") {
Serial.println("change request"); Serial.println("change request");
@ -158,19 +152,14 @@ void handleSocket() {
if (json["active"] == "true") { if (json["active"] == "true") {
Serial.println("on"); Serial.println("on");
result = setSocket(channel, id, true); result = setSocket(socketIdx, true);
} else if (json["active"] == "false") { } else if (json["active"] == "false") {
Serial.println("off"); Serial.println("off");
result = setSocket(channel, id, false); result = setSocket(socketIdx, false);
} }
} else { } else {
Serial.println("status request"); Serial.println("status request");
if (server.arg("id") != "") { result = generateStatusJson(socketIdx);
result = generateStatusJson(channel, id);
} else {
Serial.println("ERROR: no id");
result = "{ error: \"no id\" }";
}
} }
server.send(200, "text/json", result); server.send(200, "text/json", result);
@ -178,17 +167,18 @@ void handleSocket() {
Serial.println(""); Serial.println("");
} }
void handleMainpage() { void handleMainpage() {
String mainpage = website(header, body); String mainpage = website(header, body);
server.send(200, "text/html", mainpage); server.send(200, "text/html", mainpage);
} }
String setSocket(const char* channel, const char* id, bool newStatus) {
String setSocket(int socketIdx, bool newStatus) {
PowerSocket socket; PowerSocket socket;
bool oldStatus = false; bool oldStatus = false;
bool success = false; bool success = false;
int socketIdx = findSocketIndex(channel, id);
if (socketIdx >= 0) { if (socketIdx >= 0) {
success = true; success = true;
socket = sockets[socketIdx]; 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)); 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++) { for (int socketIdx = 0; socketIdx < numofsockets; socketIdx++) {
PowerSocket canidateSocket = sockets[socketIdx]; PowerSocket canidateSocket = sockets[socketIdx];
if (strncmp(canidateSocket.channel, channel, 5) == 0 && if (canidateSocket.name == name) {
strncmp(canidateSocket.id, id, 5) == 0)
{
return socketIdx; return socketIdx;
} }
} }
@ -224,6 +213,7 @@ int findSocketIndex(const char* channel, const char* id) {
return -1; return -1;
} }
String generateChangeJson(bool success, String oldStatus, String newStatus) { String generateChangeJson(bool success, String oldStatus, String newStatus) {
String status = ""; String status = "";
if (success) { if (success) {
@ -234,9 +224,9 @@ String generateChangeJson(bool success, String oldStatus, String newStatus) {
return retJson; return retJson;
} }
String generateStatusJson(const char* channel, const char* id) {
int idx = findSocketIndex(channel, id); String generateStatusJson(int socketIdx) {
PowerSocket socket = sockets[idx]; PowerSocket socket = sockets[socketIdx];
String status = "{ \"status\" : " + String(socket.powered) + " }"; String status = "{ \"status\" : " + String(socket.powered) + " }";
return status; return status;
} }