More cleanup
This commit is contained in:
118
firmware/lib/session/SessionManager.h
Normal file
118
firmware/lib/session/SessionManager.h
Normal file
@@ -0,0 +1,118 @@
|
||||
|
||||
// NTP headers
|
||||
#include <NTPClient.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include <NTPClient.h>
|
||||
|
||||
template <typename SessionT>
|
||||
class SessionManager
|
||||
{
|
||||
public:
|
||||
SessionManager(int scaleDoutPin, int scaleSckPin, uint8_t tareAvgCount);
|
||||
|
||||
void begin();
|
||||
|
||||
void tare();
|
||||
void startMeasurements();
|
||||
void stopMeasurements();
|
||||
bool isMeasuring() const { return measuring_; }
|
||||
SessionT &session() { return session_; }
|
||||
|
||||
void iteration();
|
||||
|
||||
private:
|
||||
WiFiUDP ntpUDP_;
|
||||
NTPClient timeClient_;
|
||||
|
||||
Scale<CONFIG_VALUE_DIVIDER> scale_;
|
||||
//MockScale scale;
|
||||
SessionT session_;
|
||||
bool measuring_;
|
||||
long lastCallTime_;
|
||||
|
||||
int scaleDoutPin_;
|
||||
int scaleSckPin_;
|
||||
uint8_t tareAvgCount_;
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
template <typename SessionT>
|
||||
SessionManager<SessionT>::SessionManager(int scaleDoutPin, int scaleSckPin, uint8_t tareAvgCount)
|
||||
: timeClient_(ntpUDP_, "pool.ntp.org"),
|
||||
measuring_(false),
|
||||
lastCallTime_(0),
|
||||
scaleDoutPin_(scaleDoutPin),
|
||||
scaleSckPin_(scaleSckPin),
|
||||
tareAvgCount_(tareAvgCount)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename SessionT>
|
||||
void SessionManager<SessionT>::tare()
|
||||
{
|
||||
Serial.println("Beginning tare");
|
||||
scale_.begin(scaleDoutPin_, scaleSckPin_);
|
||||
scale_.tare(CONFIG_TARE_AVG_COUNT);
|
||||
Serial.println("Finished tare");
|
||||
}
|
||||
|
||||
template <typename SessionT>
|
||||
void SessionManager<SessionT>::begin()
|
||||
{
|
||||
timeClient_.begin();
|
||||
timeClient_.update();
|
||||
tare();
|
||||
session_.init(timeClient_.getEpochTime());
|
||||
}
|
||||
|
||||
template <typename SessionT>
|
||||
void SessionManager<SessionT>::startMeasurements()
|
||||
{
|
||||
measuring_ = true;
|
||||
lastCallTime_ = 0;
|
||||
session_.init(timeClient_.getEpochTime());
|
||||
}
|
||||
|
||||
template <typename SessionT>
|
||||
void SessionManager<SessionT>::stopMeasurements()
|
||||
{
|
||||
session_.finalize();
|
||||
measuring_ = false;
|
||||
}
|
||||
|
||||
template <typename SessionT>
|
||||
void SessionManager<SessionT>::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;
|
||||
}
|
||||
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();
|
||||
}
|
||||
@@ -13,6 +13,7 @@ public:
|
||||
: chunk(nullptr), saveInterval_(saveInterval)
|
||||
{
|
||||
}
|
||||
|
||||
~SimpleMeasurementSession()
|
||||
{
|
||||
if (chunk != nullptr)
|
||||
@@ -21,7 +22,7 @@ public:
|
||||
|
||||
void init(uint32_t epochStartTime)
|
||||
{
|
||||
if (chunk == nullptr)
|
||||
if (chunk == nullptr)
|
||||
{
|
||||
// psram allocation doesn't seem to work in constructor
|
||||
chunk = (ChunkT *)heap_caps_malloc(sizeof(ChunkT), MALLOC_CAP_SPIRAM);
|
||||
@@ -35,15 +36,15 @@ public:
|
||||
bool success = chunk->addPoint(measurement);
|
||||
if (success && (chunk->numMeasurements() % saveInterval_) == 0)
|
||||
saveToFileSystem();
|
||||
if(!success)
|
||||
if (!success)
|
||||
Serial.println("Failed to add point");
|
||||
//Serial.printf("Add point %d success %d\n", measurement, success);
|
||||
return success;
|
||||
}
|
||||
|
||||
void finalize()
|
||||
{
|
||||
saveToFileSystem();
|
||||
if (numMeasurements() > 0)
|
||||
saveToFileSystem();
|
||||
chunk->init(0, 0);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user