swimtracker-firmware/firmware/lib/session/SessionManager.h

182 lines
4.9 KiB
C
Raw Normal View History

2020-06-21 16:03:00 +02:00
// NTP headers
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include "AutoStartStop.h"
2021-08-10 22:47:47 +02:00
#include "Logger.h"
2020-06-21 16:03:00 +02:00
template <typename SessionT>
class SessionManager
{
public:
2020-06-25 22:01:53 +02:00
using MeasurementType = typename SessionT::MeasurementType;
2020-06-21 16:03:00 +02:00
SessionManager();
void begin(int scaleDoutPin, int scaleSckPin, uint8_t tareAvgCount, int valueRightShift, MeasurementType autoStartMinTh, MeasurementType autoStartMaxTh, uint32_t autoStartMeasuresBetweenPeaks,
MeasurementType autoStopTh, uint32_t autoStopNumMeasures);
2020-06-21 16:03:00 +02:00
void tare();
long tareOffset() const { return scale_.offset(); };
int valueRightShift() const { return scale_.valueRightShift(); }
2020-06-21 16:03:00 +02:00
void startMeasurements();
void stopMeasurements();
bool isMeasuring() const { return measuring_; }
SessionT &session() { return session_; }
void iteration();
2020-09-03 15:23:15 +02:00
void enableAutoStart(bool enable) { autoStart_.enable(enable); }
void enableAutoStop(bool enable) { autoStop_.enable(enable); }
bool autoStartEnabled() const { return autoStart_.enabled(); };
bool autoStopEnabled() const { return autoStop_.enabled(); }
2021-08-10 22:47:47 +02:00
unsigned long getNtpTime() { return timeClient_.getEpochTime(); }
2020-06-21 16:03:00 +02:00
private:
void onMeasurementTaken(MeasurementType measurement);
2020-06-21 16:03:00 +02:00
WiFiUDP ntpUDP_;
NTPClient timeClient_;
Scale scale_;
2020-06-21 16:03:00 +02:00
//MockScale scale;
SessionT session_;
bool measuring_;
long lastCallTime_;
int scaleDoutPin_;
int scaleSckPin_;
uint8_t tareAvgCount_;
int valueRightShift_;
2023-08-28 11:55:39 +02:00
AutoStart<MeasurementType> autoStart_;
AutoStop<MeasurementType> autoStop_;
2020-06-21 16:03:00 +02:00
};
// ------------------------------------------------------------------------------------------------
template <typename SessionT>
SessionManager<SessionT>::SessionManager()
2020-06-21 16:03:00 +02:00
: timeClient_(ntpUDP_, "pool.ntp.org"),
measuring_(false),
lastCallTime_(0)
2020-06-21 16:03:00 +02:00
{
}
template <typename SessionT>
void SessionManager<SessionT>::tare()
{
if (measuring_)
stopMeasurements();
2021-08-10 22:47:47 +02:00
LOG_INFO("Beginning tare");
scale_.begin(scaleDoutPin_, scaleSckPin_, valueRightShift_);
2020-06-21 16:03:00 +02:00
scale_.tare(CONFIG_TARE_AVG_COUNT);
2021-08-10 22:47:47 +02:00
LOG_INFO("Finished tare");
2020-06-21 16:03:00 +02:00
}
template <typename SessionT>
void SessionManager<SessionT>::begin(int scaleDoutPin, int scaleSckPin, uint8_t tareAvgCount, int valueRightShift,
MeasurementType autoStartMinTh, MeasurementType autoStartMaxTh, uint32_t autoStartMeasuresBetweenPeaks,
MeasurementType autoStopTh, uint32_t autoStopNumMeasures)
2020-06-21 16:03:00 +02:00
{
2020-09-03 15:23:15 +02:00
if(measuring_)
stopMeasurements();
scaleDoutPin_ = scaleDoutPin;
scaleSckPin_ = scaleSckPin;
tareAvgCount_ = tareAvgCount;
2021-07-27 14:38:14 +02:00
valueRightShift_ = valueRightShift;
2020-06-21 16:03:00 +02:00
timeClient_.begin();
timeClient_.update();
session_.init(timeClient_.getEpochTime());
autoStart_.begin(autoStartMinTh, autoStartMaxTh, autoStartMeasuresBetweenPeaks);
autoStop_.begin(autoStopTh, autoStopNumMeasures);
2020-06-21 16:03:00 +02:00
}
template <typename SessionT>
void SessionManager<SessionT>::startMeasurements()
{
if (measuring_ == true)
return;
2020-06-21 16:03:00 +02:00
measuring_ = true;
lastCallTime_ = 0;
session_.init(timeClient_.getEpochTime());
}
template <typename SessionT>
void SessionManager<SessionT>::stopMeasurements()
{
if (measuring_ == false)
return;
2020-06-21 16:03:00 +02:00
session_.finalize();
measuring_ = false;
}
template <typename SessionT>
void SessionManager<SessionT>::iteration()
{
MeasurementType measurement = -1;
bool measurementDone = false;
while (!measurementDone)
measurementDone = scale_.measure(measurement);
onMeasurementTaken(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);
2021-08-10 22:47:47 +02:00
LOG_WARNING("Measurements skipped: %ld, cycleDuration %ld", skipped, cycleDuration);
for (int i = 0; i < skipped; ++i)
onMeasurementTaken(measurement);
delay(CONFIG_MEASURE_DELAY * (skipped + 1) - cycleDuration);
}
}
lastCallTime_ = millis();
}
template <typename SessionT>
void SessionManager<SessionT>::onMeasurementTaken(MeasurementType measurement)
{
if (measuring_)
{
bool autoStop = autoStop_.autoStop(measurement);
if (autoStop)
{
2021-08-10 22:47:47 +02:00
LOG_INFO("Auto stop");
stopMeasurements();
return;
}
bool addPointSuccessful = session_.addPoint(measurement);
if (!addPointSuccessful)
{
2021-08-10 22:47:47 +02:00
LOG_INFO("Maximum time of session reached - stopping");
stopMeasurements();
return;
}
}
else
{
if (autoStart_.autoStart(measurement))
{
2021-08-10 22:47:47 +02:00
LOG_INFO("Auto start");
startMeasurements();
return;
}
}
}