Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions client/include/client/DataBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
#include <string>
#include <vector>
#include <mutex>
#include <list>
#include "common/panorama_defines.hpp"
#include "client/buffer_base.hpp"
#include <list>
#include "client/data_logger.hpp"

class DataBuffer : public BufferBase<buffer_data_t> {
public:
DataBuffer();
DataBuffer(const std::string& logFilePath);
~DataBuffer();

// ---------------------------
Expand Down Expand Up @@ -60,11 +61,12 @@ class DataBuffer : public BufferBase<buffer_data_t> {

std::string toStringAll();

void exportBuffer();
void exportBuffer(std::string exportPath);

private:
// Raw buffer storing incoming data
std::list<buffer_data_t> buffer_;
std::string logFilePath_;

// ---------------------------
// JSON HELPER LOGIC
Expand Down
1 change: 1 addition & 0 deletions client/include/client/buffer_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,5 @@ class BufferBase {
std::list<T> buffer_;
mutable std::mutex mutex_;
int MAX_BUFFER_SIZE = 5;
int FLUSH_THRESHOLD = 50; //percentage
};
3 changes: 3 additions & 0 deletions client/include/client/data_logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class DataLogger {
// Close the log file
void close();

std::string getLogFilePath();


private:
std::string logFilePath_;
std::ofstream logFile_;
Expand Down
4 changes: 2 additions & 2 deletions client/include/common/panorama_defines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
34 changes: 20 additions & 14 deletions client/src/DataBuffer.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@

#include "client/DataBuffer.hpp"
#include <iostream>
#include <iostream>

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
}
Expand All @@ -18,11 +19,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();
Expand Down Expand Up @@ -106,16 +113,11 @@ void DataBuffer::parseAll(/* std::vector<ParsedData> &out */) {

std::string DataBuffer::toString(const buffer_data_t& buffer_item) {
//convert one struct of buffer_ into string
bool hasUnit = buffer_item.dataunit != nullptr && buffer_item.dataunit[0] != '\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 += "}";
Expand All @@ -129,7 +131,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";
Expand All @@ -141,11 +143,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;
}

Expand Down
4 changes: 4 additions & 0 deletions client/src/data_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ void DataLogger::close() {
logFile_.close();
}
}

std::string DataLogger::getLogFilePath() {
return logFilePath_;
}
26 changes: 20 additions & 6 deletions client/src/example.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
{
"cats": "maybe",
"dogs": {
"dat": "okay"
}
}
[
{
"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"
}
]
2 changes: 1 addition & 1 deletion client/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class PanoramaClient : public wxApp {
}

// --- Create DataBuffer ---
dataBuffer_ = std::make_shared<DataBuffer>();
dataBuffer_ = std::make_shared<DataBuffer>(runtimeDir + "/data");

// --- Create and start TCP client on separate thread ---
tcpClient_ = std::make_unique<TcpClient>("127.0.0.1", 3000, model_, dataBuffer_, dataLogger_);
Expand Down
11 changes: 4 additions & 7 deletions scripts/example.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"cats": [
{"type": "Bernese", "age": 10, "color": "black"},
{"type": "Calico", "age": 2, "color": "orange"},
{"type": "Mountain", "age": 4, "color": "grey"}
]
}
[
{"datatype": "light", "data": 0.200000, "dataunit": "nm", "timestamp": 1770504589},
{"datatype": "temperature", "data": 29.500000, "dataunit": "celsius", "timestamp": 1770504589}
]
13 changes: 0 additions & 13 deletions tools/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<<<<<<< Updated upstream
import requests
import numpy as np
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -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
Expand Down