generated from wessel/boilerplate
fix(final_grade_determinator): Various PR fixes
This commit is contained in:
@@ -20,7 +20,9 @@ struct StudentCourse {
|
||||
}
|
||||
|
||||
bool operator<(const StudentCourse& other) const{
|
||||
return student_name < other.student_name || (student_name == other.student_name && course_name < other.course_name);
|
||||
return student_name < other.student_name
|
||||
|| (student_name == other.student_name
|
||||
&& course_name < other.course_name);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -4,11 +4,14 @@ namespace assignments::one::final_grade_determinator {
|
||||
|
||||
FinalGradeDeterminator::FinalGradeDeterminator() : Node("final_grade_determinator") {
|
||||
this->declare_parameter("grade_collection_amount", 5);
|
||||
grade_collection_ammount_ = this->get_parameter("grade_collection_amount").as_int();
|
||||
grade_collection_amount_ = this->get_parameter("grade_collection_amount").as_int();
|
||||
|
||||
db_manager_ = std::make_unique<DatabaseManager>(this->get_logger());
|
||||
|
||||
// Create publisher for exam results
|
||||
student_publisher_ = this->create_publisher<g2_2025_interfaces::msg::Student>("student_course_management", 10);
|
||||
student_publisher_ = this->create_publisher<g2_2025_interfaces::msg::Student>(
|
||||
"student_course_management", 10
|
||||
);
|
||||
|
||||
// Create subscriber for adding/removing student/course combinations
|
||||
exam_subscriber_ = this->create_subscription<g2_2025_interfaces::msg::Exam>(
|
||||
@@ -23,18 +26,21 @@ FinalGradeDeterminator::FinalGradeDeterminator() : Node("final_grade_determinato
|
||||
exam_service_client_= this->create_client<g2_2025_interfaces::srv::Exams>("grade_calculator_service");
|
||||
}
|
||||
|
||||
void FinalGradeDeterminator::exam_results_callback(const g2_2025_interfaces::msg::Exam::SharedPtr msg){
|
||||
studentCourseCombo_.student_name = msg->student_name;
|
||||
studentCourseCombo_.course_name = msg->course_name;
|
||||
|
||||
dataMap_[studentCourseCombo_].push_back(msg->result);
|
||||
if (dataMap_[studentCourseCombo_].size() == static_cast<unsigned long>(grade_collection_ammount_))
|
||||
{
|
||||
void FinalGradeDeterminator::exam_results_callback(
|
||||
const g2_2025_interfaces::msg::Exam::SharedPtr msg
|
||||
) {
|
||||
student_course_combo_.student_name = msg->student_name;
|
||||
student_course_combo_.course_name = msg->course_name;
|
||||
|
||||
data_map_[student_course_combo_].push_back(msg->result);
|
||||
|
||||
auto grade_collection_as_ulong = static_cast<unsigned long>(grade_collection_amount_);
|
||||
if (data_map_[student_course_combo_].size() == grade_collection_as_ulong) {
|
||||
RCLCPP_INFO(this->get_logger(),
|
||||
"%s // %s: results sent to calculator",
|
||||
msg->student_name.c_str(), msg->course_name.c_str()
|
||||
"%s // %s: results sent to calculator",
|
||||
msg->student_name.c_str(), msg->course_name.c_str()
|
||||
);
|
||||
grade_calculator_request(studentCourseCombo_);
|
||||
grade_calculator_request(student_course_combo_);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,20 +53,25 @@ void FinalGradeDeterminator::grade_calculator_request(StudentCourse combo) {
|
||||
auto request = std::make_shared<g2_2025_interfaces::srv::Exams::Request>();
|
||||
request->course_name = combo.course_name;
|
||||
request->student_name = combo.student_name;
|
||||
request->exam_grades = dataMap_[combo];
|
||||
|
||||
auto callback = [this, combo](rclcpp::Client<g2_2025_interfaces::srv::Exams>::SharedFuture future) {
|
||||
request->exam_grades = data_map_[combo];
|
||||
|
||||
// Callback is used due to ros2 not liking passing multiple arguments in async calls
|
||||
auto callback = [this, combo](rclcpp::Client<g2_2025_interfaces::srv::Exams>::SharedFuture future)
|
||||
{
|
||||
this->grade_calculator_response(future, combo);
|
||||
};
|
||||
exam_service_client_->async_send_request(request, callback);
|
||||
}
|
||||
|
||||
void FinalGradeDeterminator::grade_calculator_response(rclcpp::Client<g2_2025_interfaces::srv::Exams>::SharedFuture future, StudentCourse studentCourseCombo){
|
||||
void FinalGradeDeterminator::grade_calculator_response(
|
||||
rclcpp::Client<g2_2025_interfaces::srv::Exams>::SharedFuture future,
|
||||
StudentCourse studentCourseCombo
|
||||
) {
|
||||
if (!db_manager_ || !db_manager_->is_connected()) {
|
||||
RCLCPP_WARN(this->get_logger(), "no database connection");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
auto response = future.get();
|
||||
|
||||
auto student_message = g2_2025_interfaces::msg::Student();
|
||||
@@ -73,7 +84,12 @@ void FinalGradeDeterminator::grade_calculator_response(rclcpp::Client<g2_2025_in
|
||||
"%s // %s is %d",
|
||||
studentCourseCombo.student_name.c_str(), studentCourseCombo.course_name.c_str(), response->result
|
||||
);
|
||||
db_manager_->store_final_course_result(studentCourseCombo, grade_collection_ammount_, response->result);
|
||||
|
||||
db_manager_->store_final_course_result(
|
||||
studentCourseCombo,
|
||||
grade_collection_amount_,
|
||||
response->result
|
||||
);
|
||||
}
|
||||
|
||||
} // namespace assignments::one::final_grade_determinator
|
||||
|
||||
@@ -24,17 +24,21 @@ private:
|
||||
rclcpp::Publisher<g2_2025_interfaces::msg::Student>::SharedPtr student_publisher_;
|
||||
|
||||
rclcpp::Client<g2_2025_interfaces::srv::Exams>::SharedPtr exam_service_client_;
|
||||
|
||||
|
||||
std::unique_ptr<DatabaseManager> db_manager_;
|
||||
|
||||
StudentCourse studentCourseCombo_;
|
||||
StudentCourseResultMap dataMap_;
|
||||
StudentCourse student_course_combo_;
|
||||
StudentCourseResultMap data_map_;
|
||||
|
||||
// Params
|
||||
int grade_collection_amount_;
|
||||
|
||||
//Params:
|
||||
int grade_collection_ammount_;
|
||||
//Functions:
|
||||
void grade_calculator_request(StudentCourse combo);
|
||||
void exam_results_callback(const g2_2025_interfaces::msg::Exam::SharedPtr msg);
|
||||
void grade_calculator_response(rclcpp::Client<g2_2025_interfaces::srv::Exams>::SharedFuture future, StudentCourse studentCourseCombo);
|
||||
void grade_calculator_response(
|
||||
rclcpp::Client<g2_2025_interfaces::srv::Exams>::SharedFuture future,
|
||||
StudentCourse studentCourseCombo
|
||||
);
|
||||
};
|
||||
} // namespace assignments::one::final_grade_determinator
|
||||
|
||||
} // namespace assignments::one::final_grade_determinator
|
||||
|
||||
Reference in New Issue
Block a user