add route import and export
This commit is contained in:
@@ -24,6 +24,7 @@ lib_deps =
|
||||
br3ttb/PID@^1.2.1
|
||||
marcoschwartz/LiquidCrystal_I2C@^1.1.4
|
||||
marian-craciunescu/ESP32Ping@^1.7
|
||||
bblanchon/ArduinoJson@^6.20.0
|
||||
upload_port = COM3
|
||||
|
||||
[platformio]
|
||||
|
||||
@@ -31,18 +31,22 @@ void MenuRoute::printMenu() {
|
||||
|
||||
|
||||
void MenuRoute::down() {
|
||||
if (blockInput) return;
|
||||
this->mainMenu->down();
|
||||
}
|
||||
|
||||
void MenuRoute::up() {
|
||||
if (blockInput) return;
|
||||
this->mainMenu->up();
|
||||
}
|
||||
|
||||
void MenuRoute::right() {
|
||||
if (blockInput) return;
|
||||
this->mainMenu->right();
|
||||
}
|
||||
|
||||
void MenuRoute::left() {
|
||||
if (blockInput) return;
|
||||
if (this->mainMenu->isInSubmenu())
|
||||
this->mainMenu->left();
|
||||
else
|
||||
@@ -50,10 +54,12 @@ void MenuRoute::left() {
|
||||
}
|
||||
|
||||
void MenuRoute::yes() {
|
||||
if (blockInput) return;
|
||||
this->mainMenu->yes();
|
||||
}
|
||||
|
||||
void MenuRoute::no() {
|
||||
if (blockInput) return;
|
||||
if (this->mainMenu->isInSubmenu())
|
||||
this->mainMenu->no();
|
||||
else
|
||||
@@ -85,3 +91,117 @@ void MenuRoute::init() {
|
||||
this->mainMenu->addEntry(exportAction);
|
||||
}
|
||||
|
||||
void MenuRoute::importRoute() {
|
||||
this->blockInput = true;
|
||||
// ask user for route number!
|
||||
uint8_t routeNumber = 1;
|
||||
|
||||
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);
|
||||
|
||||
http.begin(client, "rover.kleiax.de");
|
||||
int httpResponseCode = http.GET();
|
||||
|
||||
lineOne = "HTTP Response";
|
||||
lineTwo = "Code: ";
|
||||
|
||||
if (httpResponseCode > 0)
|
||||
deserializeJson(doc, http.getStream());
|
||||
else
|
||||
lineOne = "HTTP Error";
|
||||
|
||||
lineTwo.concat(httpResponseCode);
|
||||
this->print(lineOne, lineTwo);
|
||||
|
||||
if (httpResponseCode <= 0)
|
||||
return;
|
||||
|
||||
uint16_t totalPoints = doc["amountPoints"];
|
||||
|
||||
this->route->clear();
|
||||
for (uint16_t i = 0; i < totalPoints; i++) {
|
||||
Point::Coordinates coords;
|
||||
coords.lat = doc["points"][i][0].as<double>();
|
||||
coords.lon = doc["points"][i][1].as<double>();
|
||||
this->route->addPointToRoute(Point(coords));
|
||||
}
|
||||
|
||||
this->blockInput = false;
|
||||
http.end();
|
||||
}
|
||||
|
||||
void MenuRoute::exportRoute() {
|
||||
this->blockInput = true;
|
||||
// ask user for route number!
|
||||
uint8_t routeNumber = 1;
|
||||
uint16_t totalPoints = this->route->getRouteInfo().totalPoints;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
DynamicJsonDocument doc(JSON_DOCUMENT_SIZE_ROUTE);
|
||||
doc["number"] = routeNumber;
|
||||
doc["amountPoints"] = totalPoints;
|
||||
|
||||
Point::Coordinates coords = this->route->startRoute().getCoordinates();
|
||||
doc["points"][0][0] = coords.lat;
|
||||
doc["points"][0][1] = coords.lon;
|
||||
for (uint16_t i = 1; i < totalPoints; i++) {
|
||||
coords = this->route->getNextPoint().getCoordinates();
|
||||
doc["points"][i][0] = coords.lat;
|
||||
doc["points"][i][1] = coords.lon;
|
||||
}
|
||||
String jsonData;
|
||||
serializeJson(doc, jsonData);
|
||||
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
|
||||
http.begin(client, "rover.kleiax.de");
|
||||
http.addHeader("Content-Type", "application/json");
|
||||
int httpResponseCode = http.POST(jsonData);
|
||||
|
||||
lineOne = "HTTP Response";
|
||||
lineTwo = "Code: ";
|
||||
lineTwo.concat(httpResponseCode);
|
||||
this->print(lineOne, lineTwo);
|
||||
this->blockInput = false;
|
||||
|
||||
http.end();
|
||||
}
|
||||
|
||||
@@ -12,11 +12,18 @@
|
||||
#ifndef MENU_ROUTE_H
|
||||
#define MENU_ROUTE_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <WiFi.h>
|
||||
#include <HTTPClient.h>
|
||||
|
||||
#include "route.h"
|
||||
#include "menu.h"
|
||||
#include "menuControl.h"
|
||||
#include "menuRoutePoints.h"
|
||||
|
||||
#define JSON_DOCUMENT_SIZE_ROUTE 4096
|
||||
|
||||
class MenuRoute : public MenuControl {
|
||||
public:
|
||||
MenuRoute(Route* route);
|
||||
@@ -33,10 +40,14 @@ class MenuRoute : public MenuControl {
|
||||
private:
|
||||
void init();
|
||||
|
||||
void importRoute();
|
||||
void exportRoute();
|
||||
|
||||
Route* route;
|
||||
Menu* mainMenu;
|
||||
|
||||
bool isInit = false;
|
||||
bool blockInput = false;
|
||||
};
|
||||
|
||||
#endif // MENU_ROUTE_H
|
||||
|
||||
Reference in New Issue
Block a user