diff options
author | Anton Šijanec <sijanecantonluka@gmail.com> | 2019-12-14 20:55:18 +0100 |
---|---|---|
committer | Anton Šijanec <sijanecantonluka@gmail.com> | 2019-12-14 20:55:18 +0100 |
commit | 7eaa0674d83351828a2e235752d21f160b5fe9af (patch) | |
tree | 2c2412b3f5a67410fe2c878f2f7cbf19232f0f76 /f015_apihandler.ino | |
parent | first commit (diff) | |
download | sijaneciot-7eaa0674d83351828a2e235752d21f160b5fe9af.tar sijaneciot-7eaa0674d83351828a2e235752d21f160b5fe9af.tar.gz sijaneciot-7eaa0674d83351828a2e235752d21f160b5fe9af.tar.bz2 sijaneciot-7eaa0674d83351828a2e235752d21f160b5fe9af.tar.lz sijaneciot-7eaa0674d83351828a2e235752d21f160b5fe9af.tar.xz sijaneciot-7eaa0674d83351828a2e235752d21f160b5fe9af.tar.zst sijaneciot-7eaa0674d83351828a2e235752d21f160b5fe9af.zip |
Diffstat (limited to 'f015_apihandler.ino')
-rw-r--r-- | f015_apihandler.ino | 234 |
1 files changed, 234 insertions, 0 deletions
diff --git a/f015_apihandler.ino b/f015_apihandler.ino new file mode 100644 index 0000000..12c9044 --- /dev/null +++ b/f015_apihandler.ino @@ -0,0 +1,234 @@ +void handleTest() { + server.send(200, "text/plain", "200: OK"); +} + +bool preveriprijavo() { + if(!server.authenticate(program_ime, readfile("/403/webgeslo.txt").c_str())){ + server.requestAuthentication(); + return false; + } else { + return true; + } +} +void handleRoot() { + if (!preveriprijavo()) return; + server.sendHeader("Location", "/www/index.html"); + server.send(303); +} +void handleRename() { + if (!preveriprijavo()) return; + if (!server.hasArg("i") || server.arg("i") == NULL || !server.hasArg("v") || server.arg("v") == NULL) { + server.send(400, "text/html", fihr(400, "obvezni argument: i(z) in v (pot datoteke, npr. /www/index.html)")); + return; + } + if(SPIFFS.rename(server.arg("i"), server.arg("v"))) { + server.send(200, "text/html", fihr(200, "uspešno premaknjeno!")); + } else { + server.send(500, "text/html", fihr(500, "premikanje ni uspelo!")); + } +} +void handleTms() { // timeset + if (!preveriprijavo()) return; + if (!server.hasArg("s") || server.arg("s") == NULL) { + server.send(400, "text/html", fihr(400, "obvezni argument: s(ec) ter neobvezni: h(r), m(in), s(ec), d(ay), (m)o(nth), y(ear) (ce je prisoten le argument s, nastavimo kot unix timestamp, drugace kot mhsdmy)")); + return; + } + if (!server.hasArg("m") || server.arg("m") == NULL) { + setTime(String(server.arg("s")).toInt()); + server.send(200, "text/html", fihr(200, "ura nastavljena!")); + } else { + if(server.hasArg("h") && server.arg("h") != NULL && server.hasArg("m") && server.arg("m") != NULL && server.hasArg("s") && server.arg("s") != NULL && + server.hasArg("d") && server.arg("d") != NULL && server.hasArg("o") && server.arg("o") != NULL && server.hasArg("y") && server.arg("y") != NULL) { + setTime(String(server.arg("h")).toInt(), String(server.arg("m")).toInt(), String(server.arg("s")).toInt(), String(server.arg("d")).toInt(), + String(server.arg("o")).toInt(), String(server.arg("y")).toInt()); + server.send(200, "text/html", fihr(200, "ura zamenjana!")); + } else { + server.send(400, "text/html", fihr(400, "ce ze delas ura, minuta, sekunda, dan, mesec, leto; morajo biti prisotni vsi argumenti (hmsdoy)")); + } + } +} +void handlePin() { + if (!preveriprijavo()) return; + if (!server.hasArg("p") || server.arg("p") == NULL) { + server.send(400, "text/html", fihr(400, "obvezni argument: p(in) ter neobvezni: v(rednost) (0-1 za digital, 0-1023 za a) (glede na to ce je prisoten spremeni pinMode)")); + return; + } + if (!server.hasArg("v") && server.arg("v") != NULL) { + pinMode(pin2gpio(server.arg("p")), OUTPUT); + if(server.arg("p").substring(0, 1) == "a" || server.arg("p").substring(0, 1) == "A" || server.arg("v").toInt() > 1) { + analogWrite(pin2gpio(server.arg("p")), server.arg("v").toInt()); + } else { + digitalWrite(pin2gpio(server.arg("p")), server.arg("v").toInt()); + } + } else { + pinMode(pin2gpio(server.arg("p")), OUTPUT); + delay(1); // idk + if(server.arg("p").substring(0, 1) == "a") { + analogRead(pin2gpio(server.arg("p"))); + } else { + digitalRead(pin2gpio(server.arg("p"))); + } + } +} +void handleRes() { + if (!preveriprijavo()) return; + if (!server.hasArg("k") || server.arg("k") == NULL) { + server.send(400, "text/html", fihr(400, "obvezni argument: k(aj), vrednost je lahko: availheap")); + return; + } + server.send(200, "text/plain", String(ESP.getFreeHeap())); +} +void handleSetAP() { + if (!preveriprijavo()) return; + if (!server.hasArg("s") || server.arg("s") == NULL) { + server.send(400, "text/html", fihr(400, "obvezni argument: s(sid) ter neobvezni: p(ass), c(hannel), h(idden-bool) in m(AC address npr. EEFABC286787 APja)")); + return; + } + WiFi.softAPdisconnect(true); + if(server.hasArg("m") && server.arg("m") != NULL) { + long unsigned int mac; uint8_t macar[6]; + mac = strtoul(server.arg("m").c_str(), NULL, 16); + memcpy(macar, (char*)&mac, 6); + wifi_set_macaddr(STATION_IF, macar); + } + if (server.hasArg("h") && server.arg("h") != NULL) { + writefile("/403/hostname.txt", String(server.arg("h"))); + } + writefile("/403/wifi-ap.txt", String(server.arg("s"))+","+String(server.arg("p"))+","+String(server.arg("c"))+","+String(readfile("/403/hostname.txt"))); + WiFi.softAP(server.arg("s").c_str(), server.arg("p").c_str(), server.arg("c").toInt(), readfile("/403/hostname.txt").toInt()); + server.send(200, "text/html", fihr(200, "AP nastavljena")); +} +void handleSetSTA() { + if (!preveriprijavo()) return; + if (!server.hasArg("s") || server.arg("s") == NULL) { + server.send(400, "text/html", fihr(400, "obvezni argument: s(sid) ter neobvezni: p(ass), h(ostname) in m(AC address npr. EEFABC286787 STAja)")); + return; + } + WiFi.disconnect(true); + if(server.hasArg("m") && server.arg("m") != NULL) { + long unsigned int mac; uint8_t macar[6]; + mac = strtoul(server.arg("m").c_str(), NULL, 16); + memcpy(macar, (char*)&mac, 6); + wifi_set_macaddr(SOFTAP_IF, macar); + } + if (server.hasArg("h") && server.arg("h") != NULL) writefile("/403/hostname.txt", String(server.arg("h"))); + WiFi.hostname(readfile("/403/hostname.txt").c_str()); + WiFi.begin(server.arg("s").c_str(), server.arg("p").c_str()); + server.send(200, "text/html", fihr(200, "STA nastavljena")); +} +void handleGetID() { + if (!preveriprijavo()) return; + if (!server.hasArg("k") || server.arg("k") == NULL) { + server.send(400, "text/html", fihr(400, "obvezni argument: k(aj s(ta MAC)/a(p MAC)/i(p)/c(hipId)/h(ostname)). prebere ID, za pisanje MACa glejte /api/sta in /api/ap")); + return; + } + if(server.arg("k").startsWith("s")) { + server.send(200, "text/plain", String(WiFi.macAddress())); + } else if (server.arg("k").startsWith("a")) { + server.send(200, "text/plain", String(WiFi.softAPmacAddress())); + } else if (server.arg("k").startsWith("i")) { + server.send(200, "text/plain", WiFi.localIP().toString()); + } else if (server.arg("k").startsWith("c")) { + server.send(200, "text/plain", String(ESP.getChipId())); + } else { + server.send(200, "text/plain", String(readfile("/403/hostname.txt"))); + } +} +void handlePWM() { + if (!preveriprijavo()) return; + if (server.hasArg("v") && server.arg("v") != NULL) { + writefile("/403/pwmfreq.txt", String(server.arg("v"))); + analogWriteFreq(String(server.arg("v")).toInt()); + server.send(200, "text/html", fihr(200, "PWM nastavljena")); + } else { + server.send(400, "text/html", fihr(400, "obvezni argument: v(rednost v Hz)")); + } +} +void handleRestart() { + if (!preveriprijavo()) return; + ESP.restart(); +} +void handleDeepSleep() { + if (!preveriprijavo()) return; + if (!server.hasArg("m") || server.arg("m") == NULL) { + server.send(400, "text/html", fihr(400, "obvezni argument: m(ikrosekund)")); + return; + } + ESP.deepSleep(String(server.arg("m")).toInt()); +} +void handleUpdate() { + if (!preveriprijavo()) return; + int vraat; + if (!server.hasArg("g") || server.arg("g") == NULL || !server.hasArg("p") || server.arg("p") == NULL) { + server.send(400, "text/html", fihr(400, "obvezna argumenta: g(ostiteljev IP) in p(ot /do/datoteke.bin), neobvezen: v(rata HTTP (privzeta 80))")); + return; + } + if(String(server.arg("v")).toInt() > 0 && String(server.arg("v")).toInt() < 65536) { + vraat = String(server.arg("v")).toInt(); + } else { + vraat = 80; + } + t_httpUpdate_return ret = ESPhttpUpdate.update(server.arg("g"), vraat, server.arg("p"), String(verzija[0])+"."+String(verzija[1])+"."+String(verzija[2])); + switch(ret) { + case HTTP_UPDATE_FAILED: + server.send(500, "text/html", fihr(500, "Sistemska napaka: programska posodobitev: ni uspela")); + break; + case HTTP_UPDATE_NO_UPDATES: + server.send(304, "text/html", fihr(304, "Nespremenjeno: programska posodobitev: ni potrebna, verjetno ni nove verzije")); + break; + case HTTP_UPDATE_OK: + server.send(200, "text/html", fihr(200, "OK: programska posodobitev: uspela, po ponovnem zagonu se bo uporabila")); + break; + } +} +void handleUnixTimestamp() { + if (!preveriprijavo()) return; + server.send(200, "text/plain", String(now())); +} +void handleNewPassword() { + if (!preveriprijavo()) return; + if (!server.hasArg("g") || server.arg("g") == NULL) { + server.send(400, "text/html", fihr(400, "obvezen argument: g(eslo-novo)")); + return; + } + writefile("/403/webgeslo.txt", String(server.arg("g"))); +} +void handleFileUpload() { + if (!preveriprijavo()) return; + HTTPUpload& upload = server.upload(); + if(upload.status == UPLOAD_FILE_START){ + String filename = upload.filename; + if(!filename.startsWith("/")) filename = "/"+filename; + if (razhroscevanje) Serial.print("(razhroscevanje) -> handleFileUpload Name: "); Serial.println(filename); + fsUploadFile = SPIFFS.open(filename, "w"); // Open the file for writing in SPIFFS (create if it doesn't exist) + filename = String(); + } else if(upload.status == UPLOAD_FILE_WRITE){ + if(fsUploadFile) + fsUploadFile.write(upload.buf, upload.currentSize); // Write the received bytes to the file + } else if(upload.status == UPLOAD_FILE_END){ + if(fsUploadFile) { // If the file was successfully created + fsUploadFile.close(); // Close the file again + Serial.print("handleFileUpload Size: "); Serial.println(upload.totalSize); + server.send(200, "text/html", fihr(200, "prenos uspel")); + } else { + server.send(500, "text/html", fihr(500, "izdelava datoteke ni uspela")); + } + } +} +bool handleFileRead(String path) { // send the right file to the client (if it exists) + if (!preveriprijavo()) return false; + // Serial.println("handleFileRead: " + path); // 2hc + if (path.endsWith("/")) path += "index.html"; // If a folder is requested, send the index file + if(path.indexOf("/403/") >= 0) { + server.send(403, "text/html", fihr(403, "prepovedano")); + return true; + } + String contentType = getContentType(path); // Get the MIME type + if (SPIFFS.exists(path)) { // If the file exists + File file = SPIFFS.open(path, "r"); // Open it + size_t sent = server.streamFile(file, contentType); // And send it to the client + file.close(); // Then close the file again + return true; + } + return false; // If the file doesn't exist, return false +} |