fix(final_grade_determinator): Various PR fixes

This commit is contained in:
2025-10-02 10:00:04 +02:00
parent a7a51337be
commit e517022fa3
3 changed files with 49 additions and 27 deletions

View File

@@ -20,7 +20,9 @@ struct StudentCourse {
} }
bool operator<(const StudentCourse& other) const{ 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);
} }
}; };

View File

@@ -4,11 +4,14 @@ namespace assignments::one::final_grade_determinator {
FinalGradeDeterminator::FinalGradeDeterminator() : Node("final_grade_determinator") { FinalGradeDeterminator::FinalGradeDeterminator() : Node("final_grade_determinator") {
this->declare_parameter("grade_collection_amount", 5); 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()); db_manager_ = std::make_unique<DatabaseManager>(this->get_logger());
// Create publisher for exam results // 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 // Create subscriber for adding/removing student/course combinations
exam_subscriber_ = this->create_subscription<g2_2025_interfaces::msg::Exam>( 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"); 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){ void FinalGradeDeterminator::exam_results_callback(
studentCourseCombo_.student_name = msg->student_name; const g2_2025_interfaces::msg::Exam::SharedPtr msg
studentCourseCombo_.course_name = msg->course_name; ) {
student_course_combo_.student_name = msg->student_name;
student_course_combo_.course_name = msg->course_name;
dataMap_[studentCourseCombo_].push_back(msg->result); data_map_[student_course_combo_].push_back(msg->result);
if (dataMap_[studentCourseCombo_].size() == static_cast<unsigned long>(grade_collection_ammount_))
{ 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(), RCLCPP_INFO(this->get_logger(),
"%s // %s: results sent to calculator", "%s // %s: results sent to calculator",
msg->student_name.c_str(), msg->course_name.c_str() msg->student_name.c_str(), msg->course_name.c_str()
); );
grade_calculator_request(studentCourseCombo_); grade_calculator_request(student_course_combo_);
} }
} }
@@ -47,15 +53,20 @@ void FinalGradeDeterminator::grade_calculator_request(StudentCourse combo) {
auto request = std::make_shared<g2_2025_interfaces::srv::Exams::Request>(); auto request = std::make_shared<g2_2025_interfaces::srv::Exams::Request>();
request->course_name = combo.course_name; request->course_name = combo.course_name;
request->student_name = combo.student_name; request->student_name = combo.student_name;
request->exam_grades = dataMap_[combo]; request->exam_grades = data_map_[combo];
auto callback = [this, combo](rclcpp::Client<g2_2025_interfaces::srv::Exams>::SharedFuture future) { // 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); this->grade_calculator_response(future, combo);
}; };
exam_service_client_->async_send_request(request, callback); 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()) { if (!db_manager_ || !db_manager_->is_connected()) {
RCLCPP_WARN(this->get_logger(), "no database connection"); RCLCPP_WARN(this->get_logger(), "no database connection");
return; return;
@@ -73,7 +84,12 @@ void FinalGradeDeterminator::grade_calculator_response(rclcpp::Client<g2_2025_in
"%s // %s is %d", "%s // %s is %d",
studentCourseCombo.student_name.c_str(), studentCourseCombo.course_name.c_str(), response->result 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 } // namespace assignments::one::final_grade_determinator

View File

@@ -27,14 +27,18 @@ private:
std::unique_ptr<DatabaseManager> db_manager_; std::unique_ptr<DatabaseManager> db_manager_;
StudentCourse studentCourseCombo_; StudentCourse student_course_combo_;
StudentCourseResultMap dataMap_; StudentCourseResultMap data_map_;
// Params
int grade_collection_amount_;
//Params:
int grade_collection_ammount_;
//Functions:
void grade_calculator_request(StudentCourse combo); void grade_calculator_request(StudentCourse combo);
void exam_results_callback(const g2_2025_interfaces::msg::Exam::SharedPtr msg); 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