diff --git a/firmware/.gitignore b/firmware/.gitignore index 89cc49c..001bf10 100644 --- a/firmware/.gitignore +++ b/firmware/.gitignore @@ -3,3 +3,5 @@ .vscode/c_cpp_properties.json .vscode/launch.json .vscode/ipch +/.cache +compile_commands.json diff --git a/firmware/deploy-firmware.py b/firmware/deploy-firmware.py index 146789f..33601e2 100644 --- a/firmware/deploy-firmware.py +++ b/firmware/deploy-firmware.py @@ -121,8 +121,8 @@ if __name__ == "__main__": with open(version_file_name, "w") as version_file: print(version, file=version_file) - subprocess.run(["scp", firmware_file, "root@server:/volumes/swimtracker-update"]) - subprocess.run(["scp", version_file_name, "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, "core@server:/docker/web/volumes/static-sites/swimtracker-update"]) os.unlink(firmware_file) os.unlink(version_file_name) diff --git a/firmware/lib/logging/Logger.cpp b/firmware/lib/logging/Logger.cpp index fc1ef3d..7c72de7 100644 --- a/firmware/lib/logging/Logger.cpp +++ b/firmware/lib/logging/Logger.cpp @@ -23,24 +23,6 @@ Logger::Logger() data_ = (char *)heap_caps_malloc(LOG_SIZE, MALLOC_CAP_SPIRAM); totalSize_ = LOG_SIZE; 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)); } diff --git a/firmware/lib/logging/Logger.h b/firmware/lib/logging/Logger.h index 59ad028..2666f81 100644 --- a/firmware/lib/logging/Logger.h +++ b/firmware/lib/logging/Logger.h @@ -17,19 +17,19 @@ Logger::getInstance()->log(__VA_ARGS__); \ } + + class Logger { public: - using NtpTimeT = unsigned long; - - static constexpr int HEADER_SIZE = sizeof(NtpTimeT) + sizeof(millis()); + using TimeT = unsigned long; ~Logger(); template inline bool log(const char *formatStr, Args &&...args) { - const auto time = millis(); + const TimeT time = millis(); if (totalSize_ - currentSize_ <= sizeof(time)) return false; @@ -40,7 +40,8 @@ public: const auto spaceLeft = totalSize_ - currentSize_; auto charsWritten = snprintf(&data_[currentSize_], spaceLeft, formatStr, std::forward(args)...); - Serial.println(&data_[currentSize_]); + + //Serial.println(&data_[currentSize_]); if (charsWritten < spaceLeft) { @@ -53,14 +54,39 @@ public: static Logger *getInstance(); static void init(); - static void setNtpTime(NtpTimeT time); - const size_t totalSize() const { return totalSize_; } const size_t currentSize() const { return currentSize_; } 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(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: + Logger(); char *data_; diff --git a/firmware/lib/session/SessionManager.h b/firmware/lib/session/SessionManager.h index 65ee357..223a898 100644 --- a/firmware/lib/session/SessionManager.h +++ b/firmware/lib/session/SessionManager.h @@ -52,8 +52,8 @@ private: uint8_t tareAvgCount_; int valueRightShift_; - AutoStart autoStart_; - AutoStop autoStop_; + AutoStart autoStart_; + AutoStop autoStop_; }; // ------------------------------------------------------------------------------------------------ diff --git a/firmware/platformio.ini b/firmware/platformio.ini index 42e5145..3659787 100644 --- a/firmware/platformio.ini +++ b/firmware/platformio.ini @@ -32,7 +32,7 @@ lib_deps = HX711@0.7.4 ArduinoJson https://github.com/gilmaimon/ArduinoWebsockets.git -src_filter = +<*> - +build_src_filter = +<*> - board_build.partitions = partitions_custom.csv board_build.embed_txtfiles = certificate.pem @@ -41,4 +41,4 @@ board_build.embed_txtfiles = platform = native test_ignore = test_embedded build_flags = -g -DPLATFORM_NATIVE -src_filter = +<*> - \ No newline at end of file +build_src_filter = +<*> - \ No newline at end of file diff --git a/firmware/src/LoggingAPI.h b/firmware/src/LoggingAPI.h new file mode 100644 index 0000000..b121f9d --- /dev/null +++ b/firmware/src/LoggingAPI.h @@ -0,0 +1,54 @@ +#include "Dtypes.h" +#include "MessageCodes.h" +#include "Logger.h" +#include "SwimTrackerConfig.h" + +#include +#include + + +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 + 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; +}; + diff --git a/firmware/src/MessageCodes.h b/firmware/src/MessageCodes.h index 00e78ff..2b592aa 100644 --- a/firmware/src/MessageCodes.h +++ b/firmware/src/MessageCodes.h @@ -15,6 +15,7 @@ enum class MessageCode : uint8_t WIFI_STATE_RESPONSE = 8, WIFI_SCAN_RESPONSE = 9, APP_LAYER_PING = 10, + LOG_UPDATE = 11, // from frontend to device START_SESSION = 128, @@ -25,4 +26,6 @@ enum class MessageCode : uint8_t WIFI_STATE_SET = 133, WIFI_STATE_GET = 134, WIFI_TRIGGER_SCAN = 135, + LOG_STREAMING_START = 136, + LOG_STREAMING_STOP = 137 }; diff --git a/firmware/src/WebsocketServer.h b/firmware/src/WebsocketServer.h index f7e69ce..6f4a160 100644 --- a/firmware/src/WebsocketServer.h +++ b/firmware/src/WebsocketServer.h @@ -3,8 +3,10 @@ #include "Dtypes.h" #include "UserDB.h" #include "MessageCodes.h" +#include "SwimTrackerConfig.h" #include +#include #include diff --git a/firmware/src/firmware_main.cpp b/firmware/src/firmware_main.cpp index 5140147..793576c 100644 --- a/firmware/src/firmware_main.cpp +++ b/firmware/src/firmware_main.cpp @@ -460,7 +460,6 @@ void setup() mdnsSetup(configuredHostname); sessionManagerSetup(); - Logger::setNtpTime(sessionManager.getNtpTime()); sessionManager.tare(); // HTTP & Websocket server