Worked on logging + some cleanup

This commit is contained in:
Martin Bauer 2023-08-28 11:55:39 +02:00
parent dffab21a1c
commit 900d3c8262
10 changed files with 100 additions and 32 deletions

2
firmware/.gitignore vendored
View File

@ -3,3 +3,5 @@
.vscode/c_cpp_properties.json .vscode/c_cpp_properties.json
.vscode/launch.json .vscode/launch.json
.vscode/ipch .vscode/ipch
/.cache
compile_commands.json

View File

@ -121,8 +121,8 @@ if __name__ == "__main__":
with open(version_file_name, "w") as version_file: with open(version_file_name, "w") as version_file:
print(version, file=version_file) print(version, file=version_file)
subprocess.run(["scp", firmware_file, "root@server:/volumes/swimtracker-update"]) subprocess.run(["scp", firmware_file, "core@server:/docker/web/volumes/static-sites/swimtracker-update"])
subprocess.run(["scp", version_file_name, "root@server:/volumes/swimtracker-update"]) subprocess.run(["scp", version_file_name, "core@server:/docker/web/volumes/static-sites/swimtracker-update"])
os.unlink(firmware_file) os.unlink(firmware_file)
os.unlink(version_file_name) os.unlink(version_file_name)

View File

@ -23,24 +23,6 @@ Logger::Logger()
data_ = (char *)heap_caps_malloc(LOG_SIZE, MALLOC_CAP_SPIRAM); data_ = (char *)heap_caps_malloc(LOG_SIZE, MALLOC_CAP_SPIRAM);
totalSize_ = LOG_SIZE; totalSize_ = LOG_SIZE;
currentSize_ = 0; currentSize_ = 0;
// write header placeholder
const auto millisPlaceholder = 0;
memcpy(&data_[currentSize_], &millisPlaceholder, sizeof(millisPlaceholder));
currentSize_ += sizeof(millisPlaceholder);
NtpTimeT ntpPlaceholder = 0;
memcpy(&data_[currentSize_], &ntpPlaceholder, sizeof(ntpPlaceholder));
currentSize_ += sizeof(ntpPlaceholder);
}
void Logger::setNtpTime(NtpTimeT ntpTime)
{
auto data = getInstance()->data_;
const auto millisTime = millis();
memcpy(&data[0], &millisTime, sizeof(millisTime));
memcpy(&data[sizeof(millisTime)], &ntpTime, sizeof(ntpTime));
} }

View File

@ -17,19 +17,19 @@
Logger::getInstance()->log(__VA_ARGS__); \ Logger::getInstance()->log(__VA_ARGS__); \
} }
class Logger class Logger
{ {
public: public:
using NtpTimeT = unsigned long; using TimeT = unsigned long;
static constexpr int HEADER_SIZE = sizeof(NtpTimeT) + sizeof(millis());
~Logger(); ~Logger();
template <class... Args> template <class... Args>
inline bool log(const char *formatStr, Args &&...args) inline bool log(const char *formatStr, Args &&...args)
{ {
const auto time = millis(); const TimeT time = millis();
if (totalSize_ - currentSize_ <= sizeof(time)) if (totalSize_ - currentSize_ <= sizeof(time))
return false; return false;
@ -40,7 +40,8 @@ public:
const auto spaceLeft = totalSize_ - currentSize_; const auto spaceLeft = totalSize_ - currentSize_;
auto charsWritten = snprintf(&data_[currentSize_], spaceLeft, formatStr, auto charsWritten = snprintf(&data_[currentSize_], spaceLeft, formatStr,
std::forward<Args>(args)...); std::forward<Args>(args)...);
Serial.println(&data_[currentSize_]);
//Serial.println(&data_[currentSize_]);
if (charsWritten < spaceLeft) if (charsWritten < spaceLeft)
{ {
@ -53,14 +54,39 @@ public:
static Logger *getInstance(); static Logger *getInstance();
static void init(); static void init();
static void setNtpTime(NtpTimeT time);
const size_t totalSize() const { return totalSize_; } const size_t totalSize() const { return totalSize_; }
const size_t currentSize() const { return currentSize_; } const size_t currentSize() const { return currentSize_; }
const char * data() const { return data_; } const char * data() const { return data_; }
// Iteration
class iterator
{
public:
using TimeT = unsigned long;
iterator(const char * ptr) : current_position_(ptr) {}
TimeT time_millis() const {
return *reinterpret_cast<const TimeT*>(current_position_);
}
const char * message() const {
return current_position_ + sizeof(TimeT);
}
void operator++(){
current_position_ += sizeof(TimeT) + strlen(message()) + 1;
}
private:
const char * current_position_;
};
iterator begin() const { return {data_}; }
iterator end() const { return {data_ + currentSize_}; }
private: private:
Logger(); Logger();
char *data_; char *data_;

View File

@ -52,8 +52,8 @@ private:
uint8_t tareAvgCount_; uint8_t tareAvgCount_;
int valueRightShift_; int valueRightShift_;
AutoStart<MeasurementT> autoStart_; AutoStart<MeasurementType> autoStart_;
AutoStop<MeasurementT> autoStop_; AutoStop<MeasurementType> autoStop_;
}; };
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------

View File

@ -32,7 +32,7 @@ lib_deps =
HX711@0.7.4 HX711@0.7.4
ArduinoJson ArduinoJson
https://github.com/gilmaimon/ArduinoWebsockets.git https://github.com/gilmaimon/ArduinoWebsockets.git
src_filter = +<*> -<native_main.cpp> build_src_filter = +<*> -<native_main.cpp>
board_build.partitions = partitions_custom.csv board_build.partitions = partitions_custom.csv
board_build.embed_txtfiles = board_build.embed_txtfiles =
certificate.pem certificate.pem
@ -41,4 +41,4 @@ board_build.embed_txtfiles =
platform = native platform = native
test_ignore = test_embedded test_ignore = test_embedded
build_flags = -g -DPLATFORM_NATIVE build_flags = -g -DPLATFORM_NATIVE
src_filter = +<*> -<firmware_main.cpp> build_src_filter = +<*> -<firmware_main.cpp>

54
firmware/src/LoggingAPI.h Normal file
View File

@ -0,0 +1,54 @@
#include "Dtypes.h"
#include "MessageCodes.h"
#include "Logger.h"
#include "SwimTrackerConfig.h"
#include <ArduinoWebsockets.h>
#include <ArduinoJson.h>
class LoggingAPI
{
public:
void onClientConnect(websockets::WebsocketsClient &client) {}
bool handleMessage(websockets::WebsocketsClient &client, MessageCode code, const char *payload, size_t size) {
switch(code) {
case MessageCode::LOG_STREAMING_START:
running_ = true;
return true;
case MessageCode::LOG_STREAMING_STOP:
running_ = false;
return true;
default:
return false;
}
}
template <typename TServer>
void iteration(TServer &server)
{
const Logger * logger = Logger::getInstance();
if(running_)
{
StaticJsonDocument<1024> data;
for (int i = 0; i < MAX_WEBSOCKET_CONNECTIONS; ++i)
{
auto &c = server.client(i);
if (c.available())
{
}
}
}
lastOffset_ = logger->currentSize();
}
private:
bool running_ = false;
size_t lastOffset_ = 0;
};

View File

@ -15,6 +15,7 @@ enum class MessageCode : uint8_t
WIFI_STATE_RESPONSE = 8, WIFI_STATE_RESPONSE = 8,
WIFI_SCAN_RESPONSE = 9, WIFI_SCAN_RESPONSE = 9,
APP_LAYER_PING = 10, APP_LAYER_PING = 10,
LOG_UPDATE = 11,
// from frontend to device // from frontend to device
START_SESSION = 128, START_SESSION = 128,
@ -25,4 +26,6 @@ enum class MessageCode : uint8_t
WIFI_STATE_SET = 133, WIFI_STATE_SET = 133,
WIFI_STATE_GET = 134, WIFI_STATE_GET = 134,
WIFI_TRIGGER_SCAN = 135, WIFI_TRIGGER_SCAN = 135,
LOG_STREAMING_START = 136,
LOG_STREAMING_STOP = 137
}; };

View File

@ -3,8 +3,10 @@
#include "Dtypes.h" #include "Dtypes.h"
#include "UserDB.h" #include "UserDB.h"
#include "MessageCodes.h" #include "MessageCodes.h"
#include "SwimTrackerConfig.h"
#include <ArduinoWebsockets.h> #include <ArduinoWebsockets.h>
#include <ArduinoJson.h>
#include <type_traits> #include <type_traits>

View File

@ -460,7 +460,6 @@ void setup()
mdnsSetup(configuredHostname); mdnsSetup(configuredHostname);
sessionManagerSetup(); sessionManagerSetup();
Logger::setNtpTime(sessionManager.getNtpTime());
sessionManager.tare(); sessionManager.tare();
// HTTP & Websocket server // HTTP & Websocket server