Data Gator
Hardware and software documentation for the Data Gator project.
Loading...
Searching...
No Matches
mqtt_util.hpp
Go to the documentation of this file.
1
17#include <ArduinoJson.h>
18#include <string>
19#include "SDReader.hpp"
20
31void process_command(string command, string message){
32
33 if(command == "get_time_range"){
34 // convert message to json structure
35 StaticJsonDocument<256> doc;
36 deserializeJson(doc, message);
37
38 int page_size;
39 JsonArray topic_filter_ja = {};
40 string time_range;
41
42 if(!doc.containsKey("page_size")){
43 if(USB_DEBUG) Serial.println("[ERROR] MQTT command \'get_time_range\' missing key \'page_size\' so using default size of 20");
44 page_size = 20;
45
46 }else{
47 page_size = doc["page_size"];
48 }
49
50 if(!doc.containsKey("time_range")){
51 if(USB_DEBUG) Serial.println("[ERROR] MQTT command \'get_time_range\' missing key \'time_range\' so quitting");
52 return;
53
54 }else{
55 time_range = doc["time_range"].as<string>();
56 }
57
58 if(!doc.containsKey("topic_filter")){
59 if(USB_DEBUG) Serial.println("[ERROR] MQTT command \'get_time_range\' missing key \'topic_filter\' so using default \"\"");
60 topic_filter_ja.add("");
61 }else{
62 topic_filter_ja = doc["topic_filter"];
63 }
64
65 // empty topic filter vector
66 vector<string> topic_filter_v = {};
67
68 // convert topic filter jason array to vector
69 for(JsonVariant n : topic_filter_ja){
70 topic_filter_v.push_back(n.as<string>());
71 }
72
73 // parse time range string for epochs
74 int ampersand_pos = time_range.find("&");
75 string epoch = time_range.substr(0, ampersand_pos);
76 string terminus = time_range.substr(ampersand_pos+1);
77
78 TimeStamp* ep = NULL;
79 TimeStamp* term = NULL;
80
81 // convert strings to TimeStamp objects
82 if(epoch.find("T") != string::npos){
83 ep = new TimeStamp(epoch);
84 }else{
85 ep = new TimeStamp(stoi(epoch));
86 }
87
88 if(terminus.find("T") != string::npos){
89 term = new TimeStamp(terminus);
90 }else{
91 term = new TimeStamp(stoi(terminus));
92 }
93
94
95
96 Serial.println("[DEBUG] pulling data range");
97 /*
98 if(USB_DEBUG){
99 Serial.print("[DEBUG] process command has ");
100 double perc = ESP.getFreeHeap();
101 Serial.print(perc);
102 Serial.println(" free bytes");
103 }
104 */
105 // read data from files and upload via MQTT
106 SDReader sdr;
108 *ep,
109 *term,
110 topic_filter_v,
111 page_size);
112
113 }else{
114
115 if(USB_DEBUG) {
116 Serial.print("[DEBUG] unkown command \'");
117 Serial.print(command.c_str());
118 Serial.println("\'");
119 }
120
121 }
122
123 if(USB_DEBUG) Serial.println("[DEBUG] finished processing MQTT command");
124}
125
126
146void callback(char* topic, byte* message, unsigned int length){
147 std::string msg_str;
148 for(int i = 0; i < length; i++){
149 msg_str += (char)message[i];
150 }
151
152 Serial.printf("[MQTT] Received \'%s\'|\'%s\'\n", topic, msg_str.c_str());
153
154 // TODO: check if something needs to be done when receiving MQTT packets
155 // parse command
156 string topic_s = topic;
157 int mac_id_pos = topic_s.find("/", 15);
158 string command = topic_s.substr(14, mac_id_pos - 14);
159
160 // parse mac id
161 string mac_id = topic_s.substr(mac_id_pos + 1);
162
163 // check if MAC is match before processing command
164 if(WiFi.macAddress().c_str() == mac_id){
165 process_command(command, msg_str);
166 }
167
168
169}
Defines the utility class SDReader which is used to read from SD card files.
SDReader provides an interface for opening and reading data from files created using the SDLogger lib...
Definition SDReader.hpp:31
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.
Definition SDReader.cpp:167
Definition TimeStamp.hpp:14
void callback(char *topic, byte *message, unsigned int length)
Called when MQTT message is passed to the device by the broker.
Definition mqtt_util.hpp:146
void process_command(string command, string message)
Process a command execute it.
Definition mqtt_util.hpp:31
const bool USB_DEBUG
USB serial debugging enabled.
Definition main.cpp:62