diff --git a/.gitignore b/.gitignore index 7a46a89..24a2a71 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ -.ipynb_checkpoints -__pycache__ -*.st -/espidf -/venv -/hardware/case/generated +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch +venv +hardware/case/generated/ +/example-data \ No newline at end of file diff --git a/firmware/lib/logging/Logger.h b/firmware/lib/logging/Logger.h index e0f041a..59ad028 100644 --- a/firmware/lib/logging/Logger.h +++ b/firmware/lib/logging/Logger.h @@ -22,6 +22,8 @@ class Logger public: using NtpTimeT = unsigned long; + static constexpr int HEADER_SIZE = sizeof(NtpTimeT) + sizeof(millis()); + ~Logger(); template @@ -53,6 +55,11 @@ public: 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_; } + private: Logger(); diff --git a/firmware/src/firmware_main.cpp b/firmware/src/firmware_main.cpp index d68a0f7..5140147 100644 --- a/firmware/src/firmware_main.cpp +++ b/firmware/src/firmware_main.cpp @@ -21,13 +21,11 @@ #include "UserDB.h" #include "Logger.h" - // Api #include "WebsocketServer.h" #include "SessionAPI.h" #include "WifiAPI.h" - using Session_T = SimpleMeasurementSession; SessionManager sessionManager; @@ -380,6 +378,12 @@ void httpSetup(SessionManager *sessionManager, WifiManager *wifiManage prefs.putBool("hostname", CONFIG_HOSTNAME + getIdSuffix()); httpd_resp_send(req, "OK", -1); }; + auto cbLoggingGet = [](httpd_req_t *req) + { + const auto logger = Logger::getInstance(); + httpd_resp_set_hdr(req, "Content-Type", "application/octet-stream"); + httpd_resp_send(req, logger->data(), logger->currentSize()); + }; espHttpServer.start(); @@ -387,34 +391,29 @@ void httpSetup(SessionManager *sessionManager, WifiManager *wifiManage espHttpServer.on("/api/wifi", HTTP_POST, cbWifiPost); espHttpServer.on("/api/restart", HTTP_GET, cbRestart); - if (!wifiManager->inProvisioningMode()) - { - espHttpServer.on("/api/session/start", HTTP_GET, cbStartSession); - espHttpServer.on("/api/session/start", HTTP_POST, cbStartSession); + espHttpServer.on("/api/session/start", HTTP_GET, cbStartSession); + espHttpServer.on("/api/session/start", HTTP_POST, cbStartSession); - espHttpServer.on("/api/session/stop", HTTP_GET, cbStopSession); - espHttpServer.on("/api/session/stop", HTTP_POST, cbStopSession); + espHttpServer.on("/api/session/stop", HTTP_GET, cbStopSession); + espHttpServer.on("/api/session/stop", HTTP_POST, cbStopSession); - espHttpServer.on("/api/session/data", HTTP_GET, cbGetData); - espHttpServer.on("/api/status", HTTP_GET, cbStatus); - espHttpServer.on("/api/tare", HTTP_GET, cbTare); - espHttpServer.on("/api/firmwareupdate", HTTP_GET, cbFirmwareUpdate); + espHttpServer.on("/api/session/data", HTTP_GET, cbGetData); + espHttpServer.on("/api/status", HTTP_GET, cbStatus); + espHttpServer.on("/api/tare", HTTP_GET, cbTare); + espHttpServer.on("/api/firmwareupdate", HTTP_GET, cbFirmwareUpdate); - espHttpServer.on("/api/settings", HTTP_GET, cbSettingsGet); - espHttpServer.on("/api/settings", HTTP_POST, cbSettingsPost); - espHttpServer.on("/api/settings", HTTP_DELETE, cbSettingsDelete); + espHttpServer.on("/api/settings", HTTP_GET, cbSettingsGet); + espHttpServer.on("/api/settings", HTTP_POST, cbSettingsPost); + espHttpServer.on("/api/settings", HTTP_DELETE, cbSettingsDelete); - auto webdav = webdavHandler("/webdav/", "/dat"); - espHttpServer.on("/webdav/*?", HTTP_GET, webdav); - espHttpServer.on("/webdav/*?", HTTP_PROPFIND, webdav); - espHttpServer.on("/webdav/*?", HTTP_DELETE, webdav); - espHttpServer.on("/webdav/*?", HTTP_OPTIONS, webdav); - LOG_INFO("HTTP setup done"); - } - else - { - LOG_INFO("HTTP setup with limited API in provisioning mode"); - } + espHttpServer.on("/api/log", HTTP_GET, cbLoggingGet); + + auto webdav = webdavHandler("/webdav/", "/dat"); + espHttpServer.on("/webdav/*?", HTTP_GET, webdav); + espHttpServer.on("/webdav/*?", HTTP_PROPFIND, webdav); + espHttpServer.on("/webdav/*?", HTTP_DELETE, webdav); + espHttpServer.on("/webdav/*?", HTTP_OPTIONS, webdav); + LOG_INFO("HTTP setup done"); } void mdnsSetup(const String &fullHostname) @@ -443,7 +442,7 @@ void setup() LOG_WARNING("Failed to mount/format SPIFFS file system"); ESP_ERROR_CHECK(esp_event_loop_create_default()); - + auto spiffsSetupTimeSecs = (millis() - millisBeforeSpiffsInit) / 1000; userStorage.init(); diff --git a/hardware/case/swim_tracker_case.FCStd b/hardware/case/swim_tracker_case.FCStd index 71bbb0f..53ad99d 100644 Binary files a/hardware/case/swim_tracker_case.FCStd and b/hardware/case/swim_tracker_case.FCStd differ diff --git a/python-mock/plot.py b/python-mock/plot.py index 8012dd8..8d31799 100644 --- a/python-mock/plot.py +++ b/python-mock/plot.py @@ -3,6 +3,7 @@ import numpy as np from msgpack.fallback import unpackb import requests import array +import sys def ext_hook(code, data): @@ -50,6 +51,6 @@ def analyze(data, max_size=8*1024): print("Time range {}, dense {} [minutes]".format(time_range_seconds / 60, dense_time_range_seconds / 60)) #from_network() -res = from_file('real_data/1593335527.st') +res = from_file(sys.argv[1]) print(res) #analyze(res) diff --git a/websocket_protocol_draft.md b/websocket_protocol_draft.md new file mode 100644 index 0000000..cd71712 --- /dev/null +++ b/websocket_protocol_draft.md @@ -0,0 +1,68 @@ +Web Socket Protocol Draft +========================== + +Requirements: + - fast when necessary (not even msgpack overhead) + - sending live data updates + - easy otherwise, based on msgpack + - sending existing measured data + - + + +Message Format +-------------- + +- 1byte "opcode" (if lower than threshold, probably 8, pure binary msg, otherwise msgpack) +- msgpack data, or custom binary data + + + + + + +```js + +class WebsocketConnection +{ + sendMessage(opcode, optionalData); // optional data is packed via msgpck + registerMessageReceive(func, singleShot); + + request(opcode, data, onReplyFunc); // with futures, async + // register a single shot receive that calls the "onReplyFunc", or promise + +}; + +``` + + +```cpp + +class WebsocketInterface +{ +public: + virtual bool onMessageReceived(websockets::WebsocketsClient &client) + { + + } + +protected: + WebsocketServer * server_; +} + +class SessionWebsocketInterface +{ +public: + +} + + +class WebsocketServer +{ + void sendToAll(char * data, size_t size); + + void registerInterface(WebsocketInterface *); +}; + +``` + +can this be done with static polymorphism as well? does std::tuple and std::apply work?! -> test \ No newline at end of file