Data Gator
Hardware and software documentation for the Data Gator project.
Loading...
Searching...
No Matches
SDReader Class Reference

SDReader provides an interface for opening and reading data from files created using the SDLogger library. More...

#include <SDReader.hpp>

Collaboration diagram for SDReader:

Public Member Functions

 SDReader ()
 Constructor initializes SD card connection and nothing more.
 
 SDReader (std::string filename)
 Constructor initializes SD card and set default file to open.
 
 SDReader (std::string prefix, int month, int day, int year, std::string filetype)
 Constructor initializes and sets the file name structure.
 
void set_filename (string filename)
 Manually set the filename to access.
 
std::string read_line ()
 Read until next newline character into buffer and return as a string.
 
void read_entry_range_from_files (TimeStamp epoch, TimeStamp terminus, vector< string > topic_filter, int page_length=5, string prefix="log", string filetype="csv")
 Retrieve all data within the specified time range, potentially accessing multiple files.
 
void read_entry_range (File f, TimeStamp epoch, TimeStamp terminus, vector< string > topic_filter, int page_length)
 Access a single file to retrieve data in time range and publish via MQTT.
 
File * open_file ()
 Open the specified file and get the file pointer.
 
File * open_file (string filename)
 Open the specified file from path provided.
 
void close_file ()
 Close the open file.
 

Private Member Functions

bool initialize_sd_card ()
 Initialize connection to SD card and return false if no connection established.
 
bool topic_filter_match (vector< string > filter, string target)
 Check if any of the filters match the target string return true if one of them does or filter is "", else return false.
 
string build_json_page (string filename, long int epoch, long int terminus, vector< string > &data)
 Compile collected data into a JSON "page" which can then be published via the MQTT interface.
 
int calculate_page_size (vector< string > &page)
 Calculate the size, in bytes, of the current page.
 

Private Attributes

std::string filename = ""
 
std::string separator = ";"
 
SDCard sd
 
bool file_open = false
 
File fp
 

Detailed Description

SDReader provides an interface for opening and reading data from files created using the SDLogger library.

Constructor & Destructor Documentation

◆ SDReader() [1/2]

SDReader::SDReader ( std::string  filename)
inline

Constructor initializes SD card and set default file to open.

Parameters
[in]filenameThe default file to open and read data from.

◆ SDReader() [2/2]

SDReader::SDReader ( std::string  prefix,
int  month,
int  day,
int  year,
std::string  filetype 
)
inline

Constructor initializes and sets the file name structure.

Parameters
[in]prefixWhat is this file for? Example: log.
[in]monthWhat month did this file start collecting data in?
[in]dayThe day starting at?
[in]yearWhat year is it?
[in]filetypeWhat filetype/extension is this?

Member Function Documentation

◆ build_json_page()

string SDReader::build_json_page ( string  filename,
long int  epoch,
long int  terminus,
vector< string > &  data 
)
private

Compile collected data into a JSON "page" which can then be published via the MQTT interface.

Parameters
[in]filenameThe name of the file this page came from.
[in]epochThe beginning timestamp of the range this data was collected from.
[in]terminusThe end timestamp of the range this data was collected from.
[in]dataThe vector of data entries collected in this page.
Returns
A single JSON object string which contains fields: file name, epoch, terminus, and data.

◆ calculate_page_size()

int SDReader::calculate_page_size ( vector< string > &  page)
private

Calculate the size, in bytes, of the current page.

If debugging is enabled over serial port, then the page size is printed to the serial port.

Parameters
[in]pageReference to the "page" of collected results.
Returns
An integer (bytes) representing the size of the page.

◆ initialize_sd_card()

bool SDReader::initialize_sd_card ( )
private

Initialize connection to SD card and return false if no connection established.

Returns
A boolean value: true if initialization successful, false otherwise.

◆ read_entry_range()

void SDReader::read_entry_range ( File  f,
TimeStamp  epoch,
TimeStamp  terminus,
vector< string topic_filter,
int  page_length 
)

Access a single file to retrieve data in time range and publish via MQTT.

Collect all log entries in a file that fall within the time range and match a topic in topic filter. Collected entries are counted as entries in a "page". Entries are added to the "page" until page_length is reached. At this point the entries are uploaded via MQTT to the broker.

This process is repeated until the end of the file is reached or the entries no longer fall within the time range.

Parameters
[in]fFile object to read from.
[in]epochThe beginning of the time range to collect entries from.
[in]terminusThe end of the time range to collect entries from.
[in]topic_filterA vector list of topic strings, which if matching, should be collected.
[in]page_lengthThe maximum number of results to include in this page.

◆ read_entry_range_from_files()

void SDReader::read_entry_range_from_files ( TimeStamp  epoch,
TimeStamp  terminus,
vector< string topic_filter,
int  page_length = 5,
string  prefix = "log",
string  filetype = "csv" 
)

Retrieve all data within the specified time range, potentially accessing multiple files.

Collect all log entries within date range included by the topic filter. This search is conducted on all files in the SD card file system. Once a file is found which meets the criteria of time range, prefix, and filetype, read_entry_range() is used to parse the file's entries for ones that match time range and topic filter untill the end of the file has been reached. Entries are uploaded to MQTT broker in pages which are limited in length by page_length.

This process is repeated until all files in the file system have been checked.

Parameters
[in]epochThe beginning of the time range to collect data from.
[in]terminusThe end of the time range to collect data from.
[in]topic_filterThe vector of topics to collect in a page.
[in]page_lengthThe maximum length of the page to construct.
[in]prefixOnly collect data from files whose prefix matches, for example only collect from log files.
[in]filetypeMatch file type, this defaults to csv.

◆ read_line()

string SDReader::read_line ( )

Read until next newline character into buffer and return as a string.

Returns
The next line from the file as a string.

◆ set_filename()

void SDReader::set_filename ( string  filename)
inline

Manually set the filename to access.

Parameters
[in]filenameThe name of the file to open. Should be a path if not in the root directory.

◆ topic_filter_match()

bool SDReader::topic_filter_match ( vector< string filter,
string  target 
)
private

Check if any of the filters match the target string return true if one of them does or filter is "", else return false.

Parameters
[in]filter
[in]target
Returns
true if match found, false otherwise.

The documentation for this class was generated from the following files: