From a5134c27b6c4b2f5e59fa4ce6e92002ed18d2588 Mon Sep 17 00:00:00 2001 From: annhypen Date: Sat, 7 Feb 2026 17:06:21 -0800 Subject: [PATCH] added a feature to save buffer data to runtime directory when it's X% full --- client/CMakeLists.txt | 2 +- client/include/client/DataBuffer.hpp | 8 +++-- client/include/client/buffer_base.hpp | 1 + client/include/client/data_logger.hpp | 3 ++ client/include/common/panorama_defines.hpp | 4 +-- client/src/DataBuffer.cpp | 34 +++++++++++++--------- client/src/data_logger.cpp | 4 +++ client/src/example.json | 26 +++++++++++++---- client/src/main.cpp | 2 +- scripts/example.json | 11 +++---- tools/client.py | 13 --------- 11 files changed, 61 insertions(+), 47 deletions(-) diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index a65b54e1..58c8f3c5 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -53,7 +53,7 @@ target_include_directories(panorama-client # SQLite3 - Required for config database find_package(SQLite3 REQUIRED) -target_link_libraries(panorama-client PRIVATE SQLite::SQLite3) +target_link_libraries(panorama-client SQLite::SQLite3) if(WIN32) target_link_libraries(panorama-client PRIVATE ws2_32) diff --git a/client/include/client/DataBuffer.hpp b/client/include/client/DataBuffer.hpp index e05935fb..6f56e3ac 100644 --- a/client/include/client/DataBuffer.hpp +++ b/client/include/client/DataBuffer.hpp @@ -3,13 +3,14 @@ #include #include #include +#include #include "common/panorama_defines.hpp" #include "client/buffer_base.hpp" -#include +#include "client/data_logger.hpp" class DataBuffer : public BufferBase { public: - DataBuffer(); + DataBuffer(const std::string& logFilePath); ~DataBuffer(); // --------------------------- @@ -60,11 +61,12 @@ class DataBuffer : public BufferBase { std::string toStringAll(); - void exportBuffer(); + void exportBuffer(std::string exportPath); private: // Raw buffer storing incoming data std::list buffer_; + std::string logFilePath_; // --------------------------- // JSON HELPER LOGIC diff --git a/client/include/client/buffer_base.hpp b/client/include/client/buffer_base.hpp index d50ea151..81b5a1fe 100644 --- a/client/include/client/buffer_base.hpp +++ b/client/include/client/buffer_base.hpp @@ -99,4 +99,5 @@ class BufferBase { std::list buffer_; mutable std::mutex mutex_; int MAX_BUFFER_SIZE = 5; + int FLUSH_THRESHOLD = 50; //percentage }; diff --git a/client/include/client/data_logger.hpp b/client/include/client/data_logger.hpp index 3f3a2e0e..e3166997 100644 --- a/client/include/client/data_logger.hpp +++ b/client/include/client/data_logger.hpp @@ -22,6 +22,9 @@ class DataLogger { // Close the log file void close(); + std::string getLogFilePath(); + + private: std::string logFilePath_; std::ofstream logFile_; diff --git a/client/include/common/panorama_defines.hpp b/client/include/common/panorama_defines.hpp index 82fa6b44..8880cd4c 100644 --- a/client/include/common/panorama_defines.hpp +++ b/client/include/common/panorama_defines.hpp @@ -4,7 +4,7 @@ typedef struct { float data; // actual value std::time_t timestamp; // date recorded - const char * dataunit; // e.g. "kPa", "mL" - const char * datatype; // e.g. "temperature", "sound" + std::string dataunit; // e.g. "kPa", "mL" + std::string datatype; // e.g. "temperature", "sound" } buffer_data_t; \ No newline at end of file diff --git a/client/src/DataBuffer.cpp b/client/src/DataBuffer.cpp index 5d9a4273..eeed0fa2 100644 --- a/client/src/DataBuffer.cpp +++ b/client/src/DataBuffer.cpp @@ -1,10 +1,11 @@ #include "client/DataBuffer.hpp" #include -#include -DataBuffer::DataBuffer() { - // TODO: any initialization if needed +DataBuffer::DataBuffer(const std::string& logFilePath) + : logFilePath_(logFilePath) { + // TODO: initialize buffer if needed } + DataBuffer::~DataBuffer() { // TODO: clean things up if needed } @@ -17,11 +18,17 @@ void DataBuffer::writeData(buffer_data_t jsonChunk) { // std::cout << "[DataBuffer] a: '" << jsonChunk.a << "' a_data: " << jsonChunk.a_data << std::endl; // std::cout << "[DataBuffer] b: '" << jsonChunk.b << "' b_data: " << jsonChunk.b_data << std::endl; // std::cout << "[DataBuffer] Buffer size before write: " << size() << std::endl; + + //get the runtime directory path from DataLogger to export buffer if it exceeds threshold + + //DataLogger logger; + //std::string exportPath = logger.getLogFilePath(); write(jsonChunk); - if (size() > MAX_BUFFER_SIZE) { + + if ((int)size() > FLUSH_THRESHOLD * MAX_BUFFER_SIZE / 100) { popFront(); - exportBuffer(); + exportBuffer(logFilePath_); } // std::cout << "[DataBuffer] Buffer size after write: " << size() << std::endl; // std::cout << buffer_.size(); @@ -105,16 +112,11 @@ void DataBuffer::parseAll(/* std::vector &out */) { std::string DataBuffer::toString(const buffer_data_t& buffer_item) { //convert one struct of buffer_ into string - bool hasUnit = buffer_item.dataunit != '\0'; std::string temp = "{"; temp = temp + "\"datatype\": \"" + buffer_item.datatype + "\", \"data\": " + std::to_string(buffer_item.data) + ", "; - - if (hasUnit) { - temp = temp + "\"dataunit\": \"" + buffer_item.dataunit + "\", "; - } - + temp = temp + "\"dataunit\": \"" + buffer_item.dataunit + "\", "; temp = temp + "\"timestamp\": " + std::to_string(buffer_item.timestamp); temp += "}"; @@ -128,7 +130,7 @@ std::string DataBuffer::toStringAll() { std::string res = ""; int c = 0; for (buffer_data_t buffer_item : readAll()) { - if (c == size() - 1) { + if (c == (int)size() - 1) { res += toString(buffer_item) + "\n"; } else { res += toString(buffer_item) + ",\n"; @@ -140,11 +142,15 @@ std::string DataBuffer::toStringAll() { return res; } -void DataBuffer::exportBuffer() { +void DataBuffer::exportBuffer(std::string exportPath) { //Export the entire buffer (make a local JSON file under client/src/) - FILE* fp = fopen("./example.json", "w"); + + std::string exportFile = exportPath + "/exported_buffer.json"; + + FILE* fp = fopen(exportFile.c_str(), "w"); if (!fp) { std::cerr << "Could not open file for writing exported buffer." << std::endl; + std::cerr << "logFilePath: " << exportPath << std::endl; return; } diff --git a/client/src/data_logger.cpp b/client/src/data_logger.cpp index 6789bca3..c20125dc 100644 --- a/client/src/data_logger.cpp +++ b/client/src/data_logger.cpp @@ -78,3 +78,7 @@ void DataLogger::close() { logFile_.close(); } } + +std::string DataLogger::getLogFilePath() { + return logFilePath_; +} diff --git a/client/src/example.json b/client/src/example.json index 30469db6..a5fc51ba 100644 --- a/client/src/example.json +++ b/client/src/example.json @@ -1,6 +1,20 @@ -{ - "cats": "maybe", - "dogs": { - "dat": "okay" - } -} \ No newline at end of file +[ + { + "data": 23.5, + "timestamp": 1706966400, + "dataunit": "°C", + "datatype": "temperature" + }, + { + "data": 101.3, + "timestamp": 1706966460, + "dataunit": "kPa", + "datatype": "pressure" + }, + { + "data": 55.0, + "timestamp": 1706966520, + "dataunit": "dB", + "datatype": "sound" + } +] diff --git a/client/src/main.cpp b/client/src/main.cpp index f67f562b..8c93f493 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -132,7 +132,7 @@ class PanoramaClient : public wxApp { } // --- Create DataBuffer --- - dataBuffer_ = std::make_shared(); + dataBuffer_ = std::make_shared(runtimeDir + "/data"); // --- Create and start TCP client on separate thread --- tcpClient_ = std::make_unique("127.0.0.1", 3000, model_, dataBuffer_, dataLogger_); diff --git a/scripts/example.json b/scripts/example.json index 83060fbb..306db949 100644 --- a/scripts/example.json +++ b/scripts/example.json @@ -1,7 +1,4 @@ -{ - "cats": [ - {"type": "Bernese", "age": 10, "color": "black"}, - {"type": "Calico", "age": 2, "color": "orange"}, - {"type": "Mountain", "age": 4, "color": "grey"} - ] -} \ No newline at end of file +[ +{"datatype": "light", "data": 0.200000, "dataunit": "nm", "timestamp": 1770504589}, +{"datatype": "temperature", "data": 29.500000, "dataunit": "celsius", "timestamp": 1770504589} +] \ No newline at end of file diff --git a/tools/client.py b/tools/client.py index 0568e61d..1af2d86d 100644 --- a/tools/client.py +++ b/tools/client.py @@ -1,4 +1,3 @@ -<<<<<<< Updated upstream import requests import numpy as np import matplotlib.pyplot as plt @@ -28,18 +27,6 @@ def noisy_data(start_val, end_val, num_points, noise_standard_deviation): # plt.show() response = requests.get("http127.0.0.1:8080") -======= -#!/usr/bin/env python3 -""" -esp32_client.py ---------------- -A simple TCP client that connects to the ESP32 emulator (server) -and prints telemetry data as it is received. - -Usage: - python esp32_client.py --host localhost --port 7000 -""" ->>>>>>> Stashed changes import socket import argparse