diff --git a/src/SpecialMenus/Route/menuRoute.cpp b/src/SpecialMenus/Route/menuRoute.cpp index c60196b..772c23a 100644 --- a/src/SpecialMenus/Route/menuRoute.cpp +++ b/src/SpecialMenus/Route/menuRoute.cpp @@ -83,18 +83,13 @@ void MenuRoute::init() { this->mainMenu = new Menu; MenuRoutePoints* pointsMenu = new MenuRoutePoints(this->route); - Menu* importMenu = new Menu; - Menu* exportMenu = new Menu; - Menu* clearMenu = new Menu; this->mainMenu->setLcd(this->lcd); pointsMenu->setLcd(this->lcd); - importMenu->setLcd(this->lcd); - exportMenu->setLcd(this->lcd); - clearMenu->setLcd(this->lcd); MenuIntInput* importWrapper = new MenuIntInput(1, new MenuRouteWrapper(this, &MenuRoute::importRoute)); MenuIntInput* exportWrapper = new MenuIntInput(1, new MenuRouteWrapper(this, &MenuRoute::exportRoute)); + MenuIntInput* deleteWrapper = new MenuIntInput(1, new MenuRouteWrapper(this, &MenuRoute::deleteRoute)); MenuActionRoute* clearWrapper = new MenuActionRoute(this, &MenuRoute::clearRoute); importWrapper->setLcd(this->lcd); @@ -107,15 +102,16 @@ void MenuRoute::init() { exportWrapper->setPrintParentMenu(false); exportWrapper->setEntry(0, "Export Route"); - MenuAction* pointsAction = new MenuAction("Points", pointsMenu); - MenuAction* importAction = new MenuAction("Import", importWrapper); - MenuAction* exportAction = new MenuAction("Export", exportWrapper); - MenuAction* clearAction = new MenuAction("Clear", clearWrapper); + deleteWrapper->setLcd(this->lcd); + deleteWrapper->setMinMax(0, 100); + deleteWrapper->setPrintParentMenu(false); + deleteWrapper->setEntry(0, "Delete Route"); - this->mainMenu->addEntry(pointsAction); - this->mainMenu->addEntry(importAction); - this->mainMenu->addEntry(exportAction); - this->mainMenu->addEntry(clearAction); + this->mainMenu->addEntry(new MenuAction("Points", pointsMenu)); + this->mainMenu->addEntry(new MenuAction("Clear", clearWrapper)); + this->mainMenu->addEntry(new MenuAction("Import", importWrapper)); + this->mainMenu->addEntry(new MenuAction("Export", exportWrapper)); + this->mainMenu->addEntry(new MenuAction("Delete", deleteWrapper)); } void MenuRoute::importRoute(uint8_t routeNumber) { @@ -236,6 +232,52 @@ void MenuRoute::exportRoute(uint8_t routeNumber) { http.end(); } +void MenuRoute::deleteRoute(uint8_t routeNumber) { + this->blockInput = true; + + String lineOne = ""; + String lineTwo = ""; + + if (WiFi.status() != WL_CONNECTED) { + lineOne = "Not connected to"; + lineTwo = "the WiFi."; + this->print(lineOne, lineTwo); + this->blockInput = false; + return; + } + + if (ESP.getMaxAllocHeap() < JSON_DOCUMENT_SIZE_ROUTE) { + lineOne = "Not enough mem"; + lineTwo = "for Json obj"; + this->print(lineOne, lineTwo); + this->blockInput = false; + return; + } + + WiFiClient client; + HTTPClient http; + DynamicJsonDocument doc(JSON_DOCUMENT_SIZE_ROUTE); + + String host = "http://rover.kleiax.de/api/"; + host.concat(routeNumber); + http.begin(client, host); + int httpResponseCode = http.sendRequest("DELETE"); + + lineOne = "Delete complete"; + lineTwo = "Code: "; + + if (httpResponseCode == 202) + deserializeJson(doc, http.getStream()); + else + lineOne = "HTTP Error"; + + lineTwo.concat(httpResponseCode); + this->print(lineOne, lineTwo); + + this->blockInput = false; + http.end(); +} + void MenuRoute::clearRoute(uint8_t none) { this->route->clear(); this->print("Currente route", "deleted..."); diff --git a/src/SpecialMenus/Route/menuRoute.h b/src/SpecialMenus/Route/menuRoute.h index ebfc50e..b8cdae8 100644 --- a/src/SpecialMenus/Route/menuRoute.h +++ b/src/SpecialMenus/Route/menuRoute.h @@ -109,6 +109,8 @@ class MenuRoute : public MenuControl { */ void exportRoute(uint8_t routeNumber); + void deleteRoute(uint8_t routeNumber); + /** * @brief Delete the current Route. *