CppGenerate/include/data_logger.hpp

154 lines
4.0 KiB
C++
Raw Permalink Normal View History

2026-04-17 09:17:56 +00:00
#ifndef HSM_DATA_LOGGER_HPP
#define HSM_DATA_LOGGER_HPP
#include <cstdint>
#include <string>
#include <vector>
#include <memory>
/**
* @brief GJB需求
*/
struct HSM_Record_t {
uint32_t timestamp; // UTC时间戳
float altitude; // 测量高度(单位:米)
char unit; // 单位标识符:'m' 表示米,'f' 表示英尺
uint8_t status; // 状态码0=正常, 1=异常, 2=校准中
HSM_Record_t() : timestamp(0), altitude(0.0f), unit('m'), status(0) {}
HSM_Record_t(uint32_t ts, float alt, char u = 'm', uint8_t s = 0)
: timestamp(ts), altitude(alt), unit(u), status(s) {}
};
/**
* @brief
*
*
* 1.
* 2.
* 3. CSV/JSON格式导出
* 4.
*/
class DataLogger {
public:
/**
* @brief
* @param max_records 10000024
*/
explicit DataLogger(size_t max_records = 100000);
/**
* @brief
*/
~DataLogger();
/**
* @brief
* @param record
* @return
*/
bool addRecord(const HSM_Record_t& record);
/**
* @brief
* @param timestamp
* @param altitude
* @param unit
* @param status
* @return
*/
bool addRecord(uint32_t timestamp, float altitude, char unit = 'm', uint8_t status = 0);
/**
* @brief
* @return
*/
size_t getRecordCount() const;
/**
* @brief
* @param index
* @return nullptr
*/
const HSM_Record_t* getRecord(size_t index) const;
/**
* @brief
* @param count
* @return
*/
std::vector<HSM_Record_t> getRecentRecords(size_t count) const;
/**
* @brief
* @param start_time
* @param end_time
* @return
*/
std::vector<HSM_Record_t> getRecordsByTimeRange(uint32_t start_time, uint32_t end_time) const;
/**
* @brief CSV文件
* @param filename
* @return
*/
bool exportToCSV(const std::string& filename) const;
/**
* @brief JSON文件
* @param filename
* @return
*/
bool exportToJSON(const std::string& filename) const;
/**
* @brief
*/
void clear();
/**
* @brief
* @return
*/
bool isFull() const;
/**
* @brief
* @return
*/
size_t getAvailableSpace() const;
/**
* @brief
* @return
*/
size_t getCapacity() const { return capacity_; }
private:
std::unique_ptr<HSM_Record_t[]> buffer_; // 数据缓冲区
size_t capacity_; // 缓冲区容量
size_t count_; // 当前记录数
size_t head_; // 环形缓冲区头指针
size_t tail_; // 环形缓冲区尾指针
/**
* @brief
* @param current
* @return
*/
size_t nextIndex(size_t current) const;
/**
* @brief CSV内容
* @return CSV格式字符串
*/
std::string generateCSV() const;
/**
* @brief JSON内容
* @return JSON格式字符串
*/
std::string generateJSON() const;
};
#endif // HSM_DATA_LOGGER_HPP