184 lines
6.3 KiB
C++
184 lines
6.3 KiB
C++
|
|
// Arduino & ESP headers
|
|
#include "Dtypes.h"
|
|
#include "SwimTrackerConfig.h"
|
|
|
|
#include <WiFi.h>
|
|
|
|
// Own libs
|
|
#include "MockScale.h"
|
|
#include "Scale.h"
|
|
#include "MeasurementSession.h"
|
|
#include "SessionManager.h"
|
|
#include "SpiffsStorage.h"
|
|
#include "SimpleMeasurementSession.h"
|
|
#include "EspHttp.h"
|
|
#include "WebDAV.h"
|
|
|
|
using Session_T = SimpleMeasurementSession<MeasurementT, CONFIG_SESSION_MAX_SIZE>;
|
|
SessionManager<Session_T> sessionManager(CONFIG_SCALE_DOUT_PIN, CONFIG_SCALE_SCK_PIN, CONFIG_TARE_AVG_COUNT);
|
|
|
|
EspHttp espHttpServer;
|
|
|
|
template <typename SessionT>
|
|
void httpSetup(SessionManager<SessionT> *sessionManager)
|
|
{
|
|
auto cbStartSession = [sessionManager](httpd_req_t *req) {
|
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
httpd_resp_send(req, "Session started", -1);
|
|
sessionManager->startMeasurements();
|
|
Serial.println("Started session");
|
|
};
|
|
auto cbStopSession = [sessionManager](httpd_req_t *req) {
|
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
httpd_resp_send(req, "Session stopped", -1);
|
|
sessionManager->stopMeasurements();
|
|
Serial.println("Stopped session");
|
|
};
|
|
auto cbRestart = [](httpd_req_t *req) {
|
|
Serial.println("Restarted requested");
|
|
ESP.restart();
|
|
};
|
|
auto cbTare = [sessionManager](httpd_req_t *req) {
|
|
Serial.println("Tare");
|
|
sessionManager->tare();
|
|
};
|
|
auto cbStatus = [sessionManager](httpd_req_t *req) {
|
|
String result;
|
|
result.reserve(512);
|
|
httpd_resp_set_hdr(req, "Content-Type", "application/json");
|
|
|
|
// session
|
|
{
|
|
result += "{ \"session\": ";
|
|
if (sessionManager->isMeasuring())
|
|
{
|
|
const auto &session = sessionManager->session();
|
|
result += "{ \"started\":" + String(session.getStartTime()) + ", ";
|
|
result += "\"num_measurements\":" + String(session.numMeasurements()) + "},\n";
|
|
}
|
|
else
|
|
result += "{},\n";
|
|
}
|
|
// scale
|
|
{
|
|
const String tareOffset(sessionManager->tareOffset());
|
|
const String divider(CONFIG_VALUE_DIVIDER);
|
|
result += "\"scale\": { \"tare_offset\": " + tareOffset + ", \"divider\":" + divider + "},\n";
|
|
}
|
|
// flash
|
|
{
|
|
const String usedBytes(portablefs::usedBytes());
|
|
const String freeBytes(portablefs::totalBytes() - portablefs::usedBytes());
|
|
result += "\"file_system\": { \"used\": " + usedBytes + ", \"free\":" + freeBytes + "},\n";
|
|
}
|
|
// RAM
|
|
{
|
|
const String freeBytes(ESP.getFreeHeap());
|
|
const String usedBytes(ESP.getHeapSize() - ESP.getFreeHeap());
|
|
result += "\"ram\": { \"used\": " + usedBytes + ", \"free\":" + freeBytes + "},\n";
|
|
}
|
|
// PSRAM
|
|
{
|
|
const String freeBytes(ESP.getFreePsram());
|
|
const String usedBytes(ESP.getPsramSize() - ESP.getFreePsram());
|
|
result += "\"psram\": { \"used\": " + usedBytes + ", \"free\":" + freeBytes + "}\n";
|
|
}
|
|
result += "}";
|
|
httpd_resp_send(req, result.c_str(), result.length());
|
|
};
|
|
auto cbGetData = [sessionManager](httpd_req_t *req) {
|
|
auto sessionId = sessionManager->session().getStartTime();
|
|
uint32_t startIdx = getUrlQueryParameter(req, "startIdx", 0);
|
|
//Serial.printf("Data request, start index: %d\n", startIdx);
|
|
if (startIdx >= sessionManager->session().numMeasurements())
|
|
{
|
|
httpd_resp_send_404(req);
|
|
return;
|
|
}
|
|
|
|
//headers
|
|
httpd_resp_set_hdr(req, "Content-Type", "application/x-msgpack");
|
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
String contentDisp = "attachment; filename=\"" + String(sessionId) + ".st\"";
|
|
httpd_resp_set_hdr(req, "content-disposition", contentDisp.c_str());
|
|
|
|
//data
|
|
StreamingMsgPackEncoder<DummyWriter> encoderToDetermineSize(nullptr);
|
|
encoderToDetermineSize.setSizeCountMode(true);
|
|
sessionManager->session().serialize(encoderToDetermineSize, startIdx);
|
|
auto totalSize = encoderToDetermineSize.getContentLength();
|
|
|
|
char *buf = (char *)malloc(totalSize);
|
|
CopyWriter copyWriter((uint8_t *)buf);
|
|
StreamingMsgPackEncoder<CopyWriter> encoder(©Writer);
|
|
sessionManager->session().serialize(encoder, startIdx);
|
|
httpd_resp_send(req, buf, totalSize);
|
|
free(buf);
|
|
};
|
|
|
|
espHttpServer.start();
|
|
espHttpServer.on("/api/session/start", HTTP_GET, cbStartSession);
|
|
espHttpServer.on("/api/session/start", HTTP_POST, cbStartSession);
|
|
|
|
espHttpServer.on("/api/session/stop", HTTP_GET, cbStopSession);
|
|
espHttpServer.on("/api/session/stop", HTTP_POST, cbStopSession);
|
|
|
|
espHttpServer.on("/api/session/data", HTTP_GET, cbGetData);
|
|
espHttpServer.on("/api/status", HTTP_GET, cbStatus);
|
|
espHttpServer.on("/api/tare", HTTP_GET, cbTare);
|
|
espHttpServer.on("/api/restart", HTTP_GET, cbRestart);
|
|
auto webdav = webdavHandler("/webdav/", "/dat");
|
|
espHttpServer.on("/webdav/*?", HTTP_GET, webdav);
|
|
espHttpServer.on("/webdav/*?", HTTP_PROPFIND, webdav);
|
|
espHttpServer.on("/webdav/*?", HTTP_DELETE, webdav);
|
|
Serial.println("HTTP setup done");
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
// Serial
|
|
Serial.begin(115200);
|
|
while (!Serial)
|
|
{
|
|
}
|
|
Serial.println("Starting SwimTracker Firmware");
|
|
|
|
// File system
|
|
bool spiffsResult = SPIFFS.begin(true);
|
|
if (!spiffsResult)
|
|
Serial.println("Failed to mount/format SPIFFS file system");
|
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
// WiFi
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD);
|
|
WiFi.setHostname(CONFIG_HOSTNAME);
|
|
Serial.println(F("Waiting for WIFI connection..."));
|
|
int connectCounter = 0;
|
|
while (WiFi.status() != WL_CONNECTED)
|
|
{
|
|
delay(1000);
|
|
connectCounter += 1;
|
|
if (connectCounter > 5)
|
|
{
|
|
Serial.println("Couldn't obtain WIFI - retrying..");
|
|
WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD);
|
|
}
|
|
}
|
|
Serial.print("Connected to WiFi. IP:");
|
|
Serial.println(WiFi.localIP());
|
|
|
|
// Session
|
|
sessionManager.begin();
|
|
|
|
// HTTP & Websocket server
|
|
httpSetup(&sessionManager);
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
sessionManager.iteration();
|
|
}
|