156 lines
3.8 KiB
C++
156 lines
3.8 KiB
C++
#ifdef NODE_FERMENTER
|
|
|
|
#include "http.h"
|
|
#include "config.h"
|
|
#include "pid.h"
|
|
#include "Program.h"
|
|
|
|
void httpHistory(AsyncWebServerRequest* request) {
|
|
AsyncResponseStream* stream = request->beginResponseStream("text/plain");
|
|
constexpr int size = std::size(history);
|
|
const History* h = history;
|
|
for (int i = 0; i < size; i++) {
|
|
stream->printf("%d/%d/%d\n", h->target, h->temperature, h->heater);
|
|
h = (h - history + size - 1) % size + history;
|
|
}
|
|
request->send(stream);
|
|
}
|
|
|
|
void httpStatus(AsyncWebServerRequest* request) {
|
|
JsonDocument json;
|
|
json["pid"]["p"] = pid.p;
|
|
json["pid"]["i"] = pid.i;
|
|
json["pid"]["d"] = pid.d;
|
|
json["pid"]["target"] = pid.getTarget();
|
|
json["temperature"] = temperature.getValue();
|
|
json["heater"]["percent"] = heater.getPercent();
|
|
json["heater"]["powerW"] = heater.getPercent() / 100.0 * HEATER_POWER_W;
|
|
|
|
AsyncResponseStream* stream = request->beginResponseStream("application/json");
|
|
serializeJson(json, *stream);
|
|
request->send(stream);
|
|
}
|
|
|
|
void httpTargetAdd(AsyncWebServerRequest* request) {
|
|
const auto param = request->getParam("delta");
|
|
if (param == nullptr) {
|
|
Log.error("Missing parameter: delta (1)");
|
|
return;
|
|
}
|
|
|
|
const auto string = param->value();
|
|
if (string == nullptr) {
|
|
Log.error("Missing parameter: delta (2)");
|
|
return;
|
|
}
|
|
|
|
const auto delta = string.toDouble();
|
|
if (isnan(delta)) {
|
|
Log.error("Missing parameter: delta (3)");
|
|
return;
|
|
}
|
|
|
|
addTarget(delta);
|
|
|
|
httpStatus(request);
|
|
}
|
|
|
|
void httpConfigSet(AsyncWebServerRequest* request) {
|
|
const auto keyParam = request->getParam("key");
|
|
if (keyParam == nullptr) {
|
|
Log.error("Missing parameter: key (1)");
|
|
return;
|
|
}
|
|
|
|
const auto keyString = keyParam->value();
|
|
if (keyString == nullptr) {
|
|
Log.error("Missing parameter: key (2)");
|
|
return;
|
|
}
|
|
|
|
const auto valueParam = request->getParam("value");
|
|
if (valueParam == nullptr) {
|
|
Log.error("Missing parameter: value (1)");
|
|
return;
|
|
}
|
|
|
|
const auto valueString = valueParam->value();
|
|
if (valueString == nullptr) {
|
|
Log.error("Missing parameter: value (2)");
|
|
return;
|
|
}
|
|
|
|
const auto value = valueString.toDouble();
|
|
if (isnan(value)) {
|
|
Log.error("Missing parameter: value (3)");
|
|
return;
|
|
}
|
|
|
|
if (keyString.equals("p")) {
|
|
pid.p = value;
|
|
} else if (keyString.equals("i")) {
|
|
pid.i = value;
|
|
} else if (keyString.equals("d")) {
|
|
pid.d = value;
|
|
} else if (keyString.equals("target")) {
|
|
pid.setTarget(value);
|
|
} else {
|
|
request->send(400, "text/plain", "unknown key");
|
|
return;
|
|
}
|
|
config.markDirty();
|
|
|
|
httpStatus(request);
|
|
}
|
|
|
|
void httpProgramLoad(AsyncWebServerRequest* request) {
|
|
const auto nameParam = request->getParam("name");
|
|
if (nameParam == nullptr) {
|
|
Log.error("Missing parameter: name (1)");
|
|
return;
|
|
}
|
|
|
|
const auto nameString = nameParam->value();
|
|
if (nameString == nullptr) {
|
|
Log.error("Missing parameter: name (2)");
|
|
return;
|
|
}
|
|
|
|
program.wantedName = nameString;
|
|
request->send(200);
|
|
}
|
|
|
|
void httpProgramStart(AsyncWebServerRequest* request) {
|
|
program.start();
|
|
request->send(200);
|
|
}
|
|
|
|
void httpProgramStop(AsyncWebServerRequest* request) {
|
|
program.stop();
|
|
request->send(200);
|
|
}
|
|
|
|
void httpProgramPause(AsyncWebServerRequest* request) {
|
|
program.pause();
|
|
request->send(200);
|
|
}
|
|
|
|
void httpProgramResume(AsyncWebServerRequest* request) {
|
|
program.resume();
|
|
request->send(200);
|
|
}
|
|
|
|
void httpSetup2() {
|
|
server.on("/history", httpHistory);
|
|
server.on("/status", httpStatus);
|
|
server.on("/target/add", httpTargetAdd);
|
|
server.on("/config/set", httpConfigSet);
|
|
server.on("/program/load", httpProgramLoad);
|
|
server.on("/program/start", httpProgramStart);
|
|
server.on("/program/stop", httpProgramStop);
|
|
server.on("/program/pause", httpProgramPause);
|
|
server.on("/program/resume", httpProgramResume);
|
|
}
|
|
|
|
#endif
|