fix(final_grade_determinator): implementation finished, added parameter

Added cross communication with database and grade calculator node.
Furthermore added grade_collection_amount parameter for customization
This commit is contained in:
2025-10-01 15:19:17 +02:00
parent 42aadfb0ce
commit 1b0f04b8be
3 changed files with 58 additions and 11 deletions

View File

@@ -36,11 +36,21 @@ target_link_libraries(exam_result_generator pqxx pq tomlplusplus::tomlplusplus)
add_executable(final_grade_determinator
src/final_grade_determinator/main.cpp
src/database/DatabaseManager.cpp
src/config/ConfigManager.cpp
src/final_grade_determinator/nodes/FinalGradeDeterminator.cpp
)
target_include_directories(final_grade_determinator PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/src/final_grade_determinator
)
ament_target_dependencies(final_grade_determinator rclcpp g2_2025_interfaces)
target_link_libraries(final_grade_determinator pqxx pq tomlplusplus::tomlplusplus)
add_executable(grade_calculator
src/grade_calculator/main.cpp
src/grade_calculator/nodes/GradeCalculator.cpp
)
ament_target_dependencies(grade_calculator rclcpp g2_2025_interfaces)

View File

@@ -3,7 +3,9 @@
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();
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);
@@ -26,20 +28,52 @@ void FinalGradeDeterminator::exam_results_callback(const g2_2025_interfaces::msg
studentCourseCombo_.course_name = msg->course_name;
dataMap_[studentCourseCombo_].push_back(msg->result);
if (dataMap_[studentCourseCombo_].size() == PARAM)
if (dataMap_[studentCourseCombo_].size() == static_cast<unsigned long>(grade_collection_ammount_))
{
RCLCPP_INFO(this->get_logger(),
"%s // %s is full",
"%s // %s: results sent to calculator",
msg->student_name.c_str(), msg->course_name.c_str()
);
);
grade_calculator_request(studentCourseCombo_);
}
}
void FinalGradeDeterminator::grade_calculator_response(rclcpp::Client<g2_2025_interfaces::srv::Exams>::SharedFuture future){
void FinalGradeDeterminator::grade_calculator_request(StudentCourse combo) {
if (!exam_service_client_->wait_for_service(std::chrono::seconds(1))) {
RCLCPP_WARN(this->get_logger(), "Service not available");
return;
}
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) {
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){
if (!db_manager_ || !db_manager_->is_connected()) {
RCLCPP_WARN(this->get_logger(), "no database connection");
return;
}
auto response = future.get();
// StudentCourse passthrough;
// passthrough.course_name
// db_manager_->store_final_course_result();
auto student_message = g2_2025_interfaces::msg::Student();
student_message.student_name = studentCourseCombo.student_name;
student_message.course_name = studentCourseCombo.course_name;
student_message.timestamp = this->now();
student_publisher_->publish(student_message);
RCLCPP_INFO(this->get_logger(),
"%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);
}
} // namespace assignments::one::final_grade_determinator

View File

@@ -4,6 +4,7 @@
#include <string>
#include <random>
#include <vector>
#include <chrono>
#include "rclcpp/rclcpp.hpp"
#include "g2_2025_interfaces/msg/exam.hpp"
@@ -31,9 +32,11 @@ private:
StudentCourse studentCourseCombo_;
StudentCourseResultMap dataMap_;
//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);
void grade_calculator_response(rclcpp::Client<g2_2025_interfaces::srv::Exams>::SharedFuture future, StudentCourse studentCourseCombo);
};
} // namespace assignments::one::final_grade_determinator