webapi returns an actual status message

This commit is contained in:
2019-08-02 21:57:29 +03:00
parent 27a6887d92
commit cd93c7b6e1
3 changed files with 26 additions and 4 deletions

View File

@@ -3,12 +3,13 @@
#include <Arduino.h> #include <Arduino.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#include <ArduinoJson.h>
class WebAPI { class WebAPI {
private: private:
void handleNotFound(); void handleNotFound();
void handlePutCommand(); void handlePutCommand();
String getStatusJson();
public: public:
WebAPI(); WebAPI();
void init(); void init();

View File

@@ -16,4 +16,6 @@ monitor_speed = 115200
board_build.f_cpu = 80000000L board_build.f_cpu = 80000000L
upload_resetmethod = nodemcu upload_resetmethod = nodemcu
upload_port=/dev/ttyUSB0 upload_port=/dev/ttyUSB0
lib_deps = ArduinoJson@6.11

View File

@@ -5,17 +5,36 @@ ESP8266WebServer server(80);
WebAPI::WebAPI() {} WebAPI::WebAPI() {}
String WebAPI::getStatusJson() {
StaticJsonDocument<256> doc;
Status status = executor.status();
doc["commandQueue"] = queueManager.execQueueSize();
doc["eggDia"] = status.eggDia;
doc["eggLen"] = status.eggLen;
doc["feedrate"] = status.feedrate;
doc["mmE"] = status.mmE;
doc["mmS"] = status.mmS;
doc["pEng"] = status.pEng;
doc["xLim"] = status.xLim;
String out;
serializeJson(doc, out);
return out;
}
void WebAPI::handleNotFound() { void WebAPI::handleNotFound() {
server.send(404, "text/plain", "File Not Found\n\n"); server.send(404, "text/plain", "File Not Found\n\n");
} }
void WebAPI::handlePutCommand() { void WebAPI::handlePutCommand() {
queueManager.putCommand(server.arg("plain").c_str()); queueManager.putCommand(server.arg("plain").c_str());
server.send(200, "text/plain", String(queueManager.execQueueSize())); server.send(200, "text/plain", getStatusJson());
} }
void WebAPI::init() { void WebAPI::init() {
server.on("/putCommand", HTTP_POST, std::bind(&WebAPI::handlePutCommand, this)); server.on("/putCommand", HTTP_POST,
std::bind(&WebAPI::handlePutCommand, this));
server.onNotFound(std::bind(&WebAPI::handleNotFound, this)); server.onNotFound(std::bind(&WebAPI::handleNotFound, this));
server.begin(); server.begin();
} }