Skip to content

Commit

Permalink
comments documentation done
Browse files Browse the repository at this point in the history
  • Loading branch information
vaibhavambastha committed Nov 30, 2024
1 parent a9bef97 commit fa49c78
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 55 deletions.
31 changes: 21 additions & 10 deletions src/network_systems/lib/sailbot_db/inc/sailbot_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ class SailbotDB
*/
bool storeNewSensors(const Polaris::Sensors & sensors_pb, RcvdMsgInfo new_info);

/**
* @brief Write new sensor data to the database
*
* @param global_pb Protobuf GlobalPath object
* @param timestamp Timestamp for data
*
* @return true if successful
* @return false on failure
*/
bool storeNewGlobalPath(const Polaris::GlobalPath & global_pb, const std::string & timestamp);

/**
Expand All @@ -108,6 +117,17 @@ class SailbotDB
bool storeNewGlobalPath(
const Polaris::GlobalPath & global_path_pb, const std::string & timestamp, mongocxx::client & client);

/**
* @brief Write iridium response data to the database
*
* @param response OK or FAILED
* @param error MO message error code
* @param message message given by rockblock server
* @param timestamp transmission time <year - 2000>-<month>-<day> <hour>:<minute>:<second>
* @return true if successful
* @return false on failure
*/
bool storeIridiumResponse(
const std::string & response, const std::string & error, const std::string & message,
const std::string & timestamp);
Expand All @@ -117,6 +137,7 @@ class SailbotDB
*
* @param response OK or FAILED
* @param error MO message error code
* @param message message given by rockblock server
* @param timestamp transmission time <year - 2000>-<month>-<day> <hour>:<minute>:<second>
* @param client mongocxx::client instance for the current thread
*
Expand Down Expand Up @@ -209,14 +230,4 @@ class SailbotDB
*/
bool storeWindSensors(
const ProtoList<Polaris::Sensors::Wind> & wind_pb, const std::string & timestamp, mongocxx::client & client);

// /**
// * @brief Builds global path document by extracting waypoint information and adding it to document
// *
// * @param global_path_doc_arr bstream document builder array for global path
// * @param waypoints global path waypoints: <latitude: decimal>, <longitude: decimal>
// *
// */
// auto buildGlobalPathDoc(
// auto global_path_doc_arr, const ProtoList<Polaris::Waypoint>& waypoints);
};
16 changes: 14 additions & 2 deletions src/network_systems/lib/sailbot_db/inc/util_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class UtilDB : public SailbotDB
*/
std::pair<Polaris::Sensors, SailbotDB::RcvdMsgInfo> genRandData(const std::tm & tm);

/**
* @brief Generate random global path data
*
* @param tm Timestamp returned by getTimestamp() (with any modifications made to it)
* @return std::pair<Polaris::GlobalPath, std::string>
*/
std::pair<Polaris::GlobalPath, std::string> genGlobalData(const std::tm & tm);

/**
Expand All @@ -71,10 +77,11 @@ class UtilDB : public SailbotDB
std::span<Polaris::GlobalPath> expected_globalpath, std::span<std::string> expected_timestamp);

/**
* @brief Query the database and check that the Iridium response, error code, and timestamp are correct
* @brief Query the database and check that the Iridium response, error code, message, and timestamp are correct
*
* @param expected_response
* @param expected_error
* @param expected_message
* @param expected_timestamp
*/
bool verifyDBWrite_IridiumResponse(
Expand Down Expand Up @@ -106,7 +113,7 @@ class UtilDB : public SailbotDB
*
* @param tracker FailureTracker that gets if any unexpected results are dumped
* @param expected_num_docs Expected number of documents. tracker is updated if there's a mismatch
* @return std::tuple{Vector of dumped responses, Vector of dumped error codes, Vector of dumped timestamps}
* @return std::tuple{Vector of dumped responses, Vector of dumped error codes, Vector of dumped messages, Vector of dumped timestamps}
*/
std::tuple<std::vector<std::string>, std::vector<std::string>, std::vector<std::string>, std::vector<std::string>>
dumpIridiumResponse(utils::FailTracker & tracker, size_t expected_num_docs = 1);
Expand Down Expand Up @@ -156,5 +163,10 @@ class UtilDB : public SailbotDB
*/
void genRandPathData(Polaris::Sensors::Path & path_data);

/**
* @brief generate random global path data
*
* @param global_path_data Global path data to modify
*/
void genGlobalPathData(Polaris::GlobalPath & global_path_data);
};
13 changes: 0 additions & 13 deletions src/network_systems/lib/sailbot_db/src/sailbot_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ bool SailbotDB::storeNewGlobalPath(
global_path_doc_arr = global_path_doc_arr << bstream::open_document << "latitude" << waypoint.latitude()
<< "longitude" << waypoint.longitude() << bstream::close_document;
}
// global_path_doc_arr = buildGlobalPathDoc(global_path_doc_arr, waypoints);
DocVal global_path_doc = global_path_doc_arr << bstream::close_array << "timestamp" << timestamp
<< bstream::finalize;
return static_cast<bool>(global_path_coll.insert_one(global_path_doc.view()));
Expand All @@ -215,21 +214,9 @@ bool SailbotDB::storeIridiumResponse(
mongocxx::database db = client[db_name_];
mongocxx::collection iridium_response_coll = db[COLLECTION_IRIDIUM_RESPONSE];

// Print the contents of the collection before insertion
// std::cout << "Collection contents before insertion:\n";
// for (auto doc : iridium_response_coll.find({})) {
// std::cout << bsoncxx::to_json(doc) << "\n";
// }
DocVal iridium_response_doc = bstream::document{} << "response" << response << "error" << error << "timestamp"
<< timestamp << "message" << message << bstream::finalize;

// Print the contents of the collection after insertion
// std::cout << "\nCollection contents after insertion:\n";
// for (auto doc : iridium_response_coll.find({})) {
// std::cout << bsoncxx::to_json(doc) << "\n";
// }

// return static_cast<bool>(iridium_response_coll.insert_one(iridium_response_doc.view()));
return static_cast<bool>(iridium_response_coll.insert_one(iridium_response_doc.view()));
}

Expand Down
2 changes: 0 additions & 2 deletions src/network_systems/lib/sailbot_db/test/test_sailbot_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,3 @@ TEST_F(TestSailbotDB, TestStoreGlobalPath)

EXPECT_TRUE(g_test_db.verifyDBWrite_GlobalPath(expected_global_path_data, expected_global_path_timestamp));
}

//testing
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,12 @@ void HTTPServer::doPost()
beast::ostream(res_.body()) << "Server does not support sensors POST requests of type: " << content_type;
}
} else if (req_.target() == remote_transceiver::targets::GLOBAL_PATH) {
// TODO(): Allow POST global path

std::shared_ptr<HTTPServer> self = shared_from_this();
std::string json_str = beast::buffers_to_string(req_.body().data()); //JSON Parsing
std::stringstream ss(json_str);
boost::property_tree::ptree json_tree;
boost::property_tree::read_json(ss, json_tree);
std::string timestamp = json_tree.get<std::string>("timestamp");
std::cout << "timestamp: " << timestamp << std::endl;
std::string timestamp = json_tree.get<std::string>("timestamp");
Polaris::GlobalPath global_path;
int num_waypoints = 0;
for (const auto & waypoint : json_tree.get_child("waypoints")) {
Expand All @@ -216,7 +213,6 @@ void HTTPServer::doPost()
global_waypoint->set_longitude(lon);
global_waypoint->set_latitude(lat);
num_waypoints++;
std::cout << "actual waypoint latlon: " << lat << " " << lon << std::endl;
}
global_path.set_num_waypoints(num_waypoints);
std::string data;
Expand All @@ -239,7 +235,6 @@ void HTTPServer::doPost()
std::string readBuffer;

curl = curl_easy_init();
// Sample URL we are using: http://localhost:8100/?data=thisistestdata&ec=B&imei=300434065264590&username=myuser

std::string EC = "B";
std::string IMEI = "300434065264590";
Expand Down Expand Up @@ -267,7 +262,6 @@ void HTTPServer::doPost()
if (res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
} else {
std::cout << "Read Buffer: " << readBuffer << std::endl;
std::stringstream ss(readBuffer);

std::string response;
Expand All @@ -278,10 +272,6 @@ void HTTPServer::doPost()
std::getline(ss, error, ',');
std::getline(ss, message, ',');

std::cout << "response: " << response << std::endl;
std::cout << "error: " << error << std::endl;
std::cout << "message: " << message << std::endl;

if (!self->db_.storeIridiumResponse(response, error, message, timestamp)) { //important
std::cerr << "Error, failed to store data received at:\n" << timestamp << std::endl;
} else {
Expand All @@ -294,8 +284,6 @@ void HTTPServer::doPost()
}

curl_global_cleanup();
// Create a post request to rockblock http pot request URL (how to send a post request to a url/endpoint)

} else {
doNotFound();
}
Expand Down Expand Up @@ -421,5 +409,3 @@ http::response<http::dynamic_body> http_client::post_response_body(

return res;
}

//OK, 10, message
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/address.hpp>
#include <boost/beast/http/status.hpp>
#include <boost/property_tree/json_parser.hpp> //JSON parser
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <chrono>
#include <cstddef>
Expand Down Expand Up @@ -240,7 +240,6 @@ TEST_F(TestRemoteTransceiver, rockblockWebServerExample)
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
// EXPECT_TRUE(false);
} else {
std::cout << "Response data: " << readBuffer << std::endl;
EXPECT_EQ("FAILED,11,No RockBLOCK with this IMEI found on your account", readBuffer);
}

Expand All @@ -261,24 +260,17 @@ TEST_F(TestRemoteTransceiver, TestPostGlobalPath)

std::string rand_global_path_str;
ASSERT_TRUE(rand_global_path.SerializeToString(&rand_global_path_str));
std::cout << "rand_global_path: " << rand_global_path_str << std::endl;
Polaris::GlobalPath test;
test.ParseFromString(rand_global_path_str);
std::cout << "global path str: " << rand_global_path_str << std::endl;
// This query is comprised entirely of arbitrary values exccept for .data_
//Configure for global path

boost::property_tree::ptree global_path_json;
boost::property_tree::ptree waypoints_arr;

for (const auto & waypoint : rand_global_path.waypoints()) {
std::cout << "test waypoint lat: " << waypoint.latitude() << std::endl;
std::cout << "test waypoint long: " << waypoint.longitude() << std::endl;
boost::property_tree::ptree waypoint_node;
waypoint_node.put("latitude", waypoint.latitude());
waypoint_node.put("longitude", waypoint.longitude());
waypoints_arr.push_back(std::make_pair("", waypoint_node));
std::cout << "waypoint arr size: " << waypoints_arr.size() << std::endl;
}

global_path_json.add_child("waypoints", waypoints_arr);
Expand Down Expand Up @@ -309,7 +301,7 @@ TEST_F(TestRemoteTransceiver, TestPostGlobalPathMult)
{
SCOPED_TRACE("Seed: " + std::to_string(g_rand_seed)); // Print seed on any failure

constexpr int NUM_REQS = 3; // Keep this
constexpr int NUM_REQS = 3; // Keep this number under 60 to simplify timestamp logic
std::array<std::string, NUM_REQS> queries;
std::array<std::thread, NUM_REQS> req_threads;
std::array<http::status, NUM_REQS> res_statuses;
Expand Down Expand Up @@ -340,13 +332,10 @@ TEST_F(TestRemoteTransceiver, TestPostGlobalPathMult)
boost::property_tree::ptree waypoints_arr;

for (const auto & waypoint : rand_globalpaths.waypoints()) {
std::cout << "test waypoint lat: " << waypoint.latitude() << std::endl;
std::cout << "test waypoint long: " << waypoint.longitude() << std::endl;
boost::property_tree::ptree waypoint_node;
waypoint_node.put("latitude", waypoint.latitude());
waypoint_node.put("longitude", waypoint.longitude());
waypoints_arr.push_back(std::make_pair("", waypoint_node));
std::cout << "waypoint arr size: " << waypoints_arr.size() << std::endl;
}

global_path_json.add_child("waypoints", waypoints_arr);
Expand Down

0 comments on commit fa49c78

Please sign in to comment.