Data Gator
Hardware and software documentation for the Data Gator project.
Loading...
Searching...
No Matches
logging_util.cpp
Go to the documentation of this file.
1
27#include <Preferences.h>
28#include <NTPClient.h>
29#include <TimeStamp.hpp> // functions for formatting time stamps
30#include <SDLogger.hpp>
31#include <SDReader.hpp>
32
33// interface to NVM access
34extern Preferences gator_prefs;
35bool logging_available = false;
42std::string time_stamp_to_cache = "";
49// SDLogger
51// TimeStamp
54
55// uSD Utilities
56//bool is_sd_attached(void); // check if there is an sd card connected
57
58// TimeStamp Utilities
59std::string cache_retrieve_timestamp(); // return the last timestamp as a string
60void cache_timestamp(std::string timestamp); // save the current timestamp to NVM
61 //
62// Logging Utilities
63bool init_data_logger(); // perform all initialization that MUST happen before logging
64std::string get_log_filename(TimeStampParser*); // build the log filename from timestamp
65void log_to_sd_file(std::string filename,
66 std::string time,
67 std::string topic,
68 std::string message); // log an mqtt message to the sd card
69
81 Serial.println("[DEBUG] initializing new SDLogger");
82 // initialize SDLogger
83 logger = new SDLogger();
84
85 // check if the uSD card is connected
87 return false;
88 }
89
90 // retrieve latest timestamp
91 std::string ts = cache_retrieve_timestamp();
92 std::string fn = "";
93 Serial.printf("[DEBUG] retrieved cached timestamp -> \'%s\'\n", ts.c_str());
94 if(ts != "" && !absolute_timestamp_available){
95 // 1. there was a cached timestamp
96 // 2. no absolute timestamp from NTP available
97 // therefore,
98 // use cached timestamp
99 tsp = new TimeStampParser(ts);
100 // generate log filename
101 fn = get_log_filename(tsp);
102
104 // use absolute timestamp to initialize
105 tsp = new TimeStampParser(tsb->get_date_time());
106 Serial.printf("[DEBUG] using absolute timestamp -> \'%s\'\n", tsp->get_timestamp().c_str());
107 fn = get_log_filename(tsp);
108
109 }else{
110 // 1. no cached timestamp
111 // 2. no absolute timestamp
112 // therefore,
113 // set to default log file name
114 fn = "/log";
115 // set timestamp to default value
116 tsp = new TimeStampParser("00-00-00T00:00:00+0");
117 Serial.printf("[DEBUG] using default timestamp -> \'%s\'\n", tsp->get_timestamp().c_str());
118 }
119
120 // ready to rock!
121 logger->set_filename(fn + ".csv");
122
123 return true;
124}
125
135 if(tsp == NULL) return "";
136
137 std::string filename = "/log_" + std::to_string(tsp->get_month()) +
138 "-" + std::to_string(tsp->get_day()) +
139 "-" + std::to_string(tsp->get_year());
140
141 return filename;
142}
143
150void cache_timestamp(std::string timestamp){
151 // cache string
152 gator_prefs.putString("timestamp", timestamp.c_str());
153}
154
160void cache_log_offset(int log_offset){
161 gator_prefs.putInt("log_offset", log_offset);
162}
163
174 if(!gator_prefs.isKey("timestamp")){
175 // return empty string, because it doesn't
176 // exist
177 Serial.println("[WARNING] key \'timestamp\' doesn't exist");
178 gator_prefs.putString("timestamp", "00-00-00T00:00:00+0");
179 return "";
180 }
181
182 return gator_prefs.getString("timestamp", "").c_str();
183}
184
191 if(!gator_prefs.isKey("log_offset")){
192 Serial.println("[WARNING] key \'log_offset\' doesn't exist");
193 gator_prefs.putInt("log_offset", 0);
194 return 0;
195 }
196
197 return gator_prefs.getInt("log_offset", 0);
198}
199
200
209void log_to_sd_file(std::string filename, std::string time, std::string topic, std::string message){
210 std::string log_fn = filename;
211 if(DEBUG){
212 Serial.printf("[SDLogger] logging to \'%s\n\'", log_fn);
213 }
214
215 logger = new SDLogger(log_fn);
216
217 // Write headers if file doesn't already exist
218 if(!logger->exists(filename)){
219
220 std::vector<std::string> header_fields;
221 header_fields.push_back("TIME");
222 header_fields.push_back("MQTT TOPIC");
223 header_fields.push_back("MQTT MESSAGE");
224
225 logger->write_header(header_fields);
226
227 }
228
229 // generate timestamp
230 std::string ts = tsb->get_date_time();
231
232 // log as absolute, trust user to have
233 // generated the timestamp they wanted
234 logger->log_absolute_mqtt(time, topic, message);
235
236 //logger->close_card();
237}
238
253void log_to_sd_file(SDLogger* l, std::string time, std::string topic, std::string message){
254 /*
255 if(DEBUG){
256 Serial.printf("[SDLogger] logging to \'%s\n\'", log_fn);
257 }
258 */
259
260 if(!l->exists()){
261 std::vector<std::string> header_fields;
262 header_fields.push_back("TIME");
263 header_fields.push_back("MQTT TOPIC");
264 header_fields.push_back("MQTT MESSAGE");
265
266 l->write_header(header_fields);
267
268 }
269
270 // log as absolute, trust user to have
271 // generated the timestamp they wanted
272 l->log_absolute_mqtt(time, topic, message);
273}
Implements an interface for using SPI, SDLogger, for reading and writing to files on an SD card.
Defines the utility class SDReader which is used to read from SD card files.
Creates an interface for writing data to a log file on an SD card.
Definition SDLogger.hpp:30
bool initialize_sd_card()
Call before using SD card interface. Initializes connection to SD card.
Definition SDLogger.cpp:37
void log_absolute_mqtt(std::string time, std::string mqtt_topic, std::string mqtt_message)
Append a line to the target csv file containing an absolute time stamp, mqtt topic,...
Definition SDLogger.cpp:161
bool exists(std::string fn)
File with name fn exists in SD card's filesystem.
Definition SDLogger.hpp:116
void set_filename(std::string)
Set the filename to write to the easiest way possible.
Definition SDLogger.cpp:59
void write_header(std::vector< std::string > fields)
Writes a header line to a file using append to prevent overwriting valuable data.
Definition SDLogger.cpp:197
Definition TimeStamp.hpp:91
Definition TimeStamp.hpp:117
#define DEBUG
Definition config.hpp:17
TimeStampParser * tsp
parses timestamp strings
Definition logging_util.cpp:53
bool logged_relative_data
relatively accurate timestamp used, last exact timestamp + # of minutes/resets elapsed
Definition logging_util.cpp:39
std::string cache_retrieve_timestamp()
Definition logging_util.cpp:173
std::string time_stamp_to_cache
the formatted timestamp to write to non-volatile memory for next reset
Definition logging_util.cpp:42
SDLogger * logger
reads and writes to files
Definition logging_util.cpp:50
std::string get_log_filename(TimeStampParser *)
Definition logging_util.cpp:134
bool logging_available
is some logging interface available?
Definition logging_util.cpp:35
void cache_timestamp(std::string timestamp)
Definition logging_util.cpp:150
TimeStampBuilder * tsb
builds timestamp strings
Definition logging_util.cpp:52
void log_to_sd_file(std::string filename, std::string time, std::string topic, std::string message)
Definition logging_util.cpp:209
void cache_log_offset(int log_offset)
Definition logging_util.cpp:160
int cache_retrieve_log_offset(void)
Definition logging_util.cpp:190
bool absolute_timestamp_available
is an exact/accurate timestamp available?
Definition logging_util.cpp:37
Preferences gator_prefs
Reference to non-volatile-storage on ESP32.
Definition main.cpp:67
bool init_data_logger()
Initialize logging interfaces.
Definition logging_util.cpp:80