Data Gator
Hardware and software documentation for the Data Gator project.
Loading...
Searching...
No Matches
logger.hpp
Go to the documentation of this file.
1
16#ifndef LOGGER_HPP
17#define LOGGER_HPP
18
19extern int reset_count;
20
32void log_data(std::string topic, std::string message){
33
34
35 bool wifi_connected = WiFi.status() == WL_CONNECTED;
36
37 if(wifi_connected){
38 // log to MQTT
39 MQTTMailer instance = MQTTMailer::getInstance();
40 instance.mailMessage(&mqtt_client, topic, message);
41 Serial.println("\t-> logging to MQTT");
42 }
43
44 // logging available set when initializing SDLogger in
45 // setup
47 Serial.println("\t-> logging to SD card");
48
49 std::string time;
50
52 // use absolute time from NTP
53 time = tsp->get_timestamp();
54 log_to_sd_file(logger, time, topic, message);
56
57 }else{
58 // use relative timestamp and offset
59 // calculate the offset to use
60 int log_offset = cache_retrieve_log_offset();
61 Serial.printf("[DEBUG] log offset:%i, reset_count:%i, tsp offset:%i\n", log_offset, reset_count, tsp->get_offset());
62
63 int new_offset;
64 // prevent reset_count wrap from breaking
65 // relative time keeping
66 if(log_offset > reset_count){
67 // offset has exceeded MAX_COUNT and wrapped
68 // so make the new offset the old offset + net distance
69 // between log_offset and reset_count
70 new_offset = tsp->get_offset() + (MAX_COUNT - log_offset) + reset_count;
71
72 }else{
73 // new offset is cached offset + distance between
74 // current reset count and previous marker
75 new_offset = tsp->get_offset() + (reset_count - log_offset);
76
77 }
78
79 // parse the date and time from previous time stamp
80 //
81 std::string date = std::to_string(tsp->get_month()) + "-" +
82 std::to_string(tsp->get_day()) + "-" +
83 std::to_string(tsp->get_year());
84
85 std::string clock_time = std::to_string(tsp->get_hours()) + ":" +
86 std::to_string(tsp->get_minutes()) + ":" +
87 std::to_string(tsp->get_seconds());
88
89 std::string offset = std::to_string(new_offset);
90
91 time = tsb->get_date_time(date, clock_time, offset);
92 // save the timestamp with updated offset
94
95 log_to_sd_file(logger, time, topic, message);
96
98 }
99
100 Serial.printf("[DEBUG] logging \'%s\' | \'%s\' | \'%s\'\n", time.c_str(), topic.c_str(), message.c_str());
101
102 }else{
103 Serial.println("[ERROR] no SD card connected when logging");
104 }
105}
106
107#endif
Converts MQTTMail objects into viable MQTT messages so that they can be published.
Definition MQTTMailer.hpp:60
void mailMessage(PubSubClient *mqtt_client, std::string topic, std::string message, bool serial_debug=true)
Publish an MQTT message to a topic hosted by a specific broker.
Definition MQTTMailer.cpp:31
#define MAX_COUNT
Definition config.hpp:21
void log_data(std::string topic, std::string message)
Definition logger.hpp:32
int reset_count
number of resets retrieved for NVS
Definition scheduler.hpp:31
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 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
bool logging_available
is some logging interface available?
Definition logging_util.cpp:35
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
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
PubSubClient mqtt_client
MQTT client object for logging.