More cleanup
This commit is contained in:
@@ -4,112 +4,24 @@
|
||||
#include "SwimTrackerConfig.h"
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <WiFiUdp.h> // for NTP
|
||||
#include <NTPClient.h> // for NTP
|
||||
|
||||
// Own libs
|
||||
#include "MockScale.h"
|
||||
#include "Scale.h"
|
||||
#include "MeasurementSession.h"
|
||||
#include "SessionManager.h"
|
||||
#include "SpiffsStorage.h"
|
||||
#include "DeviceInfoLog.h"
|
||||
#include "SimpleMeasurementSession.h"
|
||||
#include "EspHttp.h"
|
||||
#include "WebDAV.h"
|
||||
|
||||
WiFiUDP ntpUDP;
|
||||
NTPClient timeClient(ntpUDP, "pool.ntp.org");
|
||||
|
||||
using Session_T = SimpleMeasurementSession<uint16_t, CONFIG_SESSION_MAX_SIZE>;
|
||||
|
||||
template <typename Session_T>
|
||||
class SessionManager
|
||||
{
|
||||
public:
|
||||
SessionManager() : measuring_(false), lastCallTime_(0)
|
||||
{
|
||||
}
|
||||
|
||||
void begin()
|
||||
{
|
||||
Serial.println("Beginning tare");
|
||||
scale.begin(CONFIG_SCALE_DOUT_PIN, CONFIG_SCALE_SCK_PIN);
|
||||
scale.tare(CONFIG_TARE_AVG_COUNT);
|
||||
Serial.println("Finished tare");
|
||||
session.init(timeClient.getEpochTime());
|
||||
Serial.println("Finished session init");
|
||||
}
|
||||
|
||||
void startMeasurements()
|
||||
{
|
||||
measuring_ = true;
|
||||
lastCallTime_ = 0;
|
||||
session.init(timeClient.getEpochTime());
|
||||
}
|
||||
|
||||
void stopMeasurements()
|
||||
{
|
||||
measuring_ = false;
|
||||
session.finalize();
|
||||
}
|
||||
|
||||
bool isMeasuring() const
|
||||
{
|
||||
return measuring_;
|
||||
}
|
||||
|
||||
void iteration()
|
||||
{
|
||||
if (!measuring_)
|
||||
return;
|
||||
|
||||
uint16_t measurement = -1;
|
||||
scale.measure(measurement);
|
||||
bool addPointSuccessful = session.addPoint(measurement);
|
||||
if(!addPointSuccessful) {
|
||||
Serial.println("Maximum time of session reached - stopping");
|
||||
stopMeasurements();
|
||||
return;
|
||||
}
|
||||
Serial.print("Measurement: ");
|
||||
Serial.println(measurement);
|
||||
if (lastCallTime_ != 0)
|
||||
{
|
||||
const long cycleDuration = millis() - lastCallTime_;
|
||||
if (cycleDuration <= CONFIG_MEASURE_DELAY)
|
||||
{
|
||||
delay(CONFIG_MEASURE_DELAY - cycleDuration);
|
||||
}
|
||||
else
|
||||
{
|
||||
const long skipped = (cycleDuration / CONFIG_MEASURE_DELAY);
|
||||
Serial.printf("Warning: measurements skipped: %ld, cycleDuration %ld", skipped, cycleDuration);
|
||||
|
||||
for (int i = 0; i < skipped; ++i)
|
||||
session.addPoint(measurement);
|
||||
|
||||
delay(CONFIG_MEASURE_DELAY * (skipped + 1) - cycleDuration);
|
||||
}
|
||||
}
|
||||
lastCallTime_ = millis();
|
||||
}
|
||||
|
||||
Session_T &getSession() { return session; }
|
||||
|
||||
private:
|
||||
Scale<CONFIG_VALUE_DIVIDER> scale;
|
||||
//MockScale scale;
|
||||
Session_T session;
|
||||
bool measuring_;
|
||||
long lastCallTime_;
|
||||
};
|
||||
|
||||
SessionManager<Session_T> sessionManager;
|
||||
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 Session_T>
|
||||
void httpSetup(SessionManager<Session_T> *sessionManager)
|
||||
template <typename SessionT>
|
||||
void httpSetup(SessionManager<SessionT> *sessionManager)
|
||||
{
|
||||
auto cbStartSession = [sessionManager](httpd_req_t *req) {
|
||||
httpd_resp_send(req, "Session started", -1);
|
||||
@@ -131,7 +43,7 @@ void httpSetup(SessionManager<Session_T> *sessionManager)
|
||||
result += "{ \"session\": ";
|
||||
if (sessionManager->isMeasuring())
|
||||
{
|
||||
const auto &session = sessionManager->getSession();
|
||||
const auto &session = sessionManager->session();
|
||||
result += "{ \"started\":" + String(session.getStartTime()) + ", ";
|
||||
result += "\"num_measurements\":" + String(session.numMeasurements()) + "},\n";
|
||||
}
|
||||
@@ -149,18 +61,18 @@ void httpSetup(SessionManager<Session_T> *sessionManager)
|
||||
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->getSession().getStartTime();
|
||||
auto sessionId = sessionManager->session().getStartTime();
|
||||
uint32_t startIdx = getUrlQueryParameter(req, "startIdx", 0);
|
||||
Serial.printf("Data request, start index: %d", startIdx);
|
||||
|
||||
@@ -172,13 +84,13 @@ void httpSetup(SessionManager<Session_T> *sessionManager)
|
||||
//data
|
||||
StreamingMsgPackEncoder<DummyWriter> encoderToDetermineSize(nullptr);
|
||||
encoderToDetermineSize.setSizeCountMode(true);
|
||||
sessionManager->getSession().serialize(encoderToDetermineSize, startIdx);
|
||||
sessionManager->session().serialize(encoderToDetermineSize, startIdx);
|
||||
auto totalSize = encoderToDetermineSize.getContentLength();
|
||||
|
||||
char *buf = (char *)malloc(totalSize);
|
||||
CopyWriter copyWriter((uint8_t *)buf);
|
||||
StreamingMsgPackEncoder<CopyWriter> encoder(©Writer);
|
||||
sessionManager->getSession().serialize(encoder, startIdx);
|
||||
sessionManager->session().serialize(encoder, startIdx);
|
||||
httpd_resp_send(req, buf, totalSize);
|
||||
free(buf);
|
||||
};
|
||||
@@ -207,25 +119,19 @@ void setup()
|
||||
while (!Serial)
|
||||
{
|
||||
}
|
||||
Serial.println(" ");
|
||||
Serial.println("----- New start -----");
|
||||
Serial.println("Starting SwimTracker Firmware");
|
||||
|
||||
// File system
|
||||
bool spiffsResult = SPIFFS.begin(true);
|
||||
Serial.printf("Spiffs begin %d\n", spiffsResult);
|
||||
|
||||
printDeviceInfo();
|
||||
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);
|
||||
#ifdef PLATFORM_ESP32
|
||||
WiFi.setHostname(CONFIG_HOSTNAME);
|
||||
#else
|
||||
WIFI.hostname(CONFIG_HOSTNAME);
|
||||
#endif
|
||||
Serial.print(F("\n\n"));
|
||||
Serial.println(F("Waiting for WIFI connection..."));
|
||||
int connectCounter = 0;
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
@@ -234,23 +140,18 @@ void setup()
|
||||
connectCounter += 1;
|
||||
if (connectCounter > 5)
|
||||
{
|
||||
Serial.println("Couldn't obtain WIFI - trying begin() again");
|
||||
Serial.println("Couldn't obtain WIFI - retrying..");
|
||||
WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD);
|
||||
}
|
||||
}
|
||||
Serial.print(F("Connected to WiFi. IP:"));
|
||||
Serial.print("Connected to WiFi. IP:");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
// NTP
|
||||
timeClient.begin();
|
||||
timeClient.update();
|
||||
|
||||
// Session
|
||||
sessionManager.begin();
|
||||
|
||||
// HTTP & Websocket server
|
||||
httpSetup(&sessionManager);
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
|
||||
Reference in New Issue
Block a user