New logger & removed old unused files

This commit is contained in:
Martin Bauer
2021-08-10 22:47:47 +02:00
parent 28c85f749c
commit ce12c846f6
18 changed files with 179 additions and 534 deletions

View File

@@ -1,139 +0,0 @@
#include "SessionChunk.h"
template<typename Measurement_T, typename Reader, typename Writer, uint32_t CHUNK_SIZE>
class MeasurementSession {
public:
typedef SessionChunk<Measurement_T, CHUNK_SIZE> Chunk_T;
MeasurementSession()
: currentChunk(&chunks[0]),
otherChunk(&chunks[1]) {}
void init(uint32_t epochStartTime) {
currentChunk = &chunks[0];
otherChunk = &chunks[1];
currentChunk->init(epochStartTime, 0);
otherChunk->init(0, 0);
}
bool addPoint(Measurement_T measurement) {
const bool successful = currentChunk->addPoint(measurement);
if (!successful) {
Serial.println("Starting session rotate");
rotate();
const bool secondInsertSuccess = currentChunk->addPoint(measurement);
assert_msg(secondInsertSuccess, "Session: insertion after rotation failed");
// TODO check that there is place for file - remove old files
}
return true;
}
void finalize() {
if( otherChunkFilled() )
saveChunkToFile(otherChunk);
if( currentChunk->numMeasurements() > 0) {
saveChunkToFile(currentChunk);
}
currentChunk->init(0, 0);
otherChunk->init(0, 0);
}
uint32_t numMeasurements() const {
return currentChunk->getStartIndex() + currentChunk->numMeasurements();
}
template<typename Encoder_T>
void serialize(Encoder_T & encoder, uint32_t startIdx) const
{
const uint32_t lastIdx = currentChunk->getStartIndex() + currentChunk->numMeasurements();
if( lastIdx <= startIdx) {
encoder.template sendArray<Measurement_T>(nullptr, 0);
return;
}
Chunk_T::sendHeader(encoder, currentChunk->getStartTime(), startIdx);
encoder.template sendArrayHeader<Measurement_T>(lastIdx - startIdx);
while(startIdx < lastIdx)
startIdx = serializeChunk(encoder, startIdx);
assert_msg(startIdx == lastIdx, "Not all data was sent");
}
uint32_t getStartTime() const {
return currentChunk->getStartTime();
}
private:
void rotate() {
if( otherChunkFilled() )
saveChunkToFile(otherChunk);
swapChunks();
currentChunk->init(otherChunk->getStartTime(), otherChunk->getStartIndex() + CHUNK_SIZE);
}
bool otherChunkFilled() const {
return otherChunk->numMeasurements() > 0;
}
void swapChunks() {
Chunk_T *tmp = currentChunk;
currentChunk = otherChunk;
otherChunk = tmp;
}
void saveChunkToFile(Chunk_T *chunk) const {
const uint32_t chunkNr = chunk->getStartIndex() / CHUNK_SIZE;
const auto fileName = chunkFileName(chunkNr, chunk->getStartTime());
Serial.print("Writing session to file ");
Serial.println(fileName);
Writer writer(fileName);
chunk->serialize(writer.encoder());
};
template< typename Encoder_T>
uint32_t serializeChunk(Encoder_T & encoder, uint32_t startIdx) const {
assert_msg( startIdx < currentChunk->getStartIndex() + currentChunk->numMeasurements(),
"serializeChunk: invalid startIdx" );
if( startIdx >= currentChunk->getStartIndex() ) {
const auto localStartIdx = startIdx - currentChunk->getStartIndex();
const auto numElements = currentChunk->numMeasurements() - localStartIdx;
assert_msg(numElements <= currentChunk->numMeasurements(), "Internal problem in serializeChunk");
encoder.sendArrayPartialContents( currentChunk->getDataPointer() + localStartIdx, numElements );
return currentChunk->getStartIndex() + currentChunk->numMeasurements();
} else if( startIdx >= otherChunk->getStartIndex() && otherChunkFilled() ) {
encoder.sendArrayPartialContents( otherChunk->getDataPointer(), otherChunk->numMeasurements() );
assert_msg( otherChunk->numMeasurements(), CHUNK_SIZE );
return otherChunk->getStartIndex() + otherChunk->numMeasurements();
} else {
if( encoder.getSizeCountMode() ) {
encoder.template sendArrayPartialContents<Measurement_T>(nullptr, CHUNK_SIZE);
} else {
const uint32_t chunkNr = startIdx / CHUNK_SIZE;
const auto chunkFileNameStr = chunkFileName(chunkNr, currentChunk->getStartTime());
Reader reader(chunkFileNameStr);
reader.seek(Chunk_T::valueOffset());
const uint32_t PART_SIZE = 32;
#ifndef ARDUINO
static_assert((PART_SIZE < CHUNK_SIZE) && (CHUNK_SIZE % PART_SIZE == 0));
#endif
Measurement_T buffer[PART_SIZE];
for(uint32_t i = 0; i < CHUNK_SIZE; i += PART_SIZE)
{
reader.readBytes((char*) buffer, sizeof(Measurement_T) * PART_SIZE);
encoder.template sendArrayPartialContents<Measurement_T>(buffer, PART_SIZE);
}
}
return startIdx + CHUNK_SIZE;
}
}
static String chunkFileName(uint32_t chunkNr, uint32_t startTime) {
return("/dat/" + toString(startTime) + String("_") + toString(chunkNr));
}
Chunk_T chunks[2];
Chunk_T *currentChunk;
Chunk_T *otherChunk;
};

View File

@@ -5,6 +5,7 @@
#include <NTPClient.h>
#include "AutoStartStop.h"
#include "Logger.h"
template <typename SessionT>
class SessionManager
@@ -32,7 +33,8 @@ public:
void enableAutoStop(bool enable) { autoStop_.enable(enable); }
bool autoStartEnabled() const { return autoStart_.enabled(); };
bool autoStopEnabled() const { return autoStop_.enabled(); }
unsigned long getNtpTime() { return timeClient_.getEpochTime(); }
private:
void onMeasurementTaken(MeasurementType measurement);
@@ -69,10 +71,10 @@ void SessionManager<SessionT>::tare()
{
if (measuring_)
stopMeasurements();
Serial.println("Beginning tare");
LOG_INFO("Beginning tare");
scale_.begin(scaleDoutPin_, scaleSckPin_, valueRightShift_);
scale_.tare(CONFIG_TARE_AVG_COUNT);
Serial.println("Finished tare");
LOG_INFO("Finished tare");
}
template <typename SessionT>
@@ -135,7 +137,7 @@ void SessionManager<SessionT>::iteration()
else
{
const long skipped = (cycleDuration / CONFIG_MEASURE_DELAY);
Serial.printf("Measurements skipped: %ld, cycleDuration %ld\n", skipped, cycleDuration);
LOG_WARNING("Measurements skipped: %ld, cycleDuration %ld", skipped, cycleDuration);
for (int i = 0; i < skipped; ++i)
onMeasurementTaken(measurement);
@@ -154,7 +156,7 @@ void SessionManager<SessionT>::onMeasurementTaken(MeasurementType measurement)
bool autoStop = autoStop_.autoStop(measurement);
if (autoStop)
{
Serial.println("Auto stop");
LOG_INFO("Auto stop");
stopMeasurements();
return;
}
@@ -162,7 +164,7 @@ void SessionManager<SessionT>::onMeasurementTaken(MeasurementType measurement)
bool addPointSuccessful = session_.addPoint(measurement);
if (!addPointSuccessful)
{
Serial.println("Maximum time of session reached - stopping");
LOG_INFO("Maximum time of session reached - stopping");
stopMeasurements();
return;
}
@@ -171,7 +173,7 @@ void SessionManager<SessionT>::onMeasurementTaken(MeasurementType measurement)
{
if (autoStart_.autoStart(measurement))
{
Serial.println("Auto start");
LOG_INFO("Auto start");
startMeasurements();
return;
}

View File

@@ -1,6 +1,8 @@
#include "Dtypes.h"
#include "SessionChunk.h"
#include "FilesystemAbstraction.h"
#include "Logger.h"
template <typename Measurement_T, uint32_t MAX_SIZE>
class SimpleMeasurementSession
@@ -37,7 +39,7 @@ public:
if (success && (chunk->numMeasurements() % saveInterval_) == 0)
saveToFileSystem();
if (!success)
Serial.println("Failed to add point");
LOG_WARNING("Failed to add point");
return success;
}
@@ -79,9 +81,9 @@ private:
// todo: check this! free doesn't mean that the file writing actually works ok
// use error codes of write instead? anyway: test it!
Serial.printf("%ld saveToFileSystem start\n", millis());
LOG_INFO("%ld saveToFileSystem start", millis());
deleteUntilBytesFree(CONFIG_SESSION_MAX_SIZE);
Serial.printf(" %ld after deleteUntilBytesFree()\n", millis());
LOG_INFO(" %ld after deleteUntilBytesFree()", millis());
String filename = String(CONFIG_DATA_PATH) + "/" + String(chunk->getStartTime());
if (portablefs::exists(filename.c_str()))
@@ -104,7 +106,7 @@ private:
StreamingMsgPackEncoder<portablefs::File> encoder(&file);
chunk->serialize(encoder);
}
Serial.printf(" %ld saveToFileSystem done\n", millis());
LOG_INFO(" %ld saveToFileSystem done", millis());
}
void deleteUntilBytesFree(size_t requiredSpace)
@@ -132,7 +134,7 @@ private:
}
assert(nextSessionToDelete > 0);
assert(nextSessionToDelete < uint32_t(-1));
Serial.printf("Removing old session %s to make space\n", filenameToDelete.c_str());
LOG_INFO("Removing old session %s to make space", filenameToDelete.c_str());
portablefs::remove(filenameToDelete.c_str());
auto newFreeBytes = portablefs::totalBytes() - portablefs::usedBytes();
assert(newFreeBytes > freeBytes);

View File

@@ -1,90 +0,0 @@
#pragma once
#include "StreamingMsgPackEncoder.h"
#include "FilesystemAbstraction.h"
#ifdef USE_ESP32
struct WriterAdaptor
{
File * f;
void write(const char * ptr, size_t size) {
f->write(reinterpret_cast<const uint8_t *>(ptr), size);
}
};
class SpiffsStorageWriter {
public:
SpiffsStorageWriter(const String &fileName) :
f_(SPIFFS.open(fileName, "w")),
adaptor_{&f_},
encoder_(&adaptor_),
fileName_(fileName)
{
bool success = f_;
Serial.println(success);
}
~SpiffsStorageWriter() {
f_.close();
Serial.println(fileName_);
Serial.println(SPIFFS.exists(fileName_));
}
StreamingMsgPackEncoder<WriterAdaptor> &encoder() { return encoder_; }
private:
File f_;
WriterAdaptor adaptor_;
StreamingMsgPackEncoder<WriterAdaptor> encoder_;
String fileName_;
};
#else
class SpiffsStorageWriter {
public:
SpiffsStorageWriter(const String &fileName) :
f_(SPIFFS.open(fileName, "w")),
encoder_(&f_),
fileName_(fileName)
{
bool success = f_;
Serial.println(success);
}
~SpiffsStorageWriter() {
f_.close();
Serial.println(fileName_);
Serial.println(SPIFFS.exists(fileName_));
}
StreamingMsgPackEncoder<File> &encoder() { return encoder_; }
private:
File f_;
StreamingMsgPackEncoder<File> encoder_;
String fileName_;
};
#endif
class SpiffsStorageReader
{
public:
SpiffsStorageReader(const String &fileName) :
f_(SPIFFS.open(fileName, "r"))
{}
~SpiffsStorageReader() {
f_.close();
}
uint32_t readBytes(char *buffer, size_t length) {
return f_.readBytes(buffer, length);
}
bool seek(uint32_t pos) {
return f_.seek(pos);
}
private:
File f_;
};

View File

@@ -60,7 +60,7 @@ public:
{
auto len = strlen_P(s);
if( len >= 255 ) {
Serial.println(F("ERROR: StreamingMsgPackEncoder::string255 - string too long"));
LOG_WARNING("ERROR: StreamingMsgPackEncoder::string255 - string too long");
return;
}
byte castedLen = (byte)(len);