Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

Commit

Permalink
Merge pull request #16 from NathanaelGandhi/ng/get-type
Browse files Browse the repository at this point in the history
feat(node): get thermal zone type
  • Loading branch information
NathanaelGandhi authored May 16, 2024
2 parents f044035 + 09f3f8b commit 65a35b2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class LinuxThermalZoneBaseNode : public rclcpp::Node
std::vector<linux_thermal_zone_interfaces::msg::LinuxThermalZone> GetZoneMsgVector(void);
linux_thermal_zone_interfaces::msg::LinuxThermalZone GetZoneMsg(
std::string key, uint8_t zone_index);
double GetZoneTemperature(std::string prefix);
double GetZoneTemperature(std::string thermal_zone_dir);
std::string GetZoneString(std::string filepath);
uint8_t CountMatchingDirectories(const std::string & pattern);
};
30 changes: 30 additions & 0 deletions src/linux_thermal_zone_base_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,15 @@ linux_thermal_zone_interfaces::msg::LinuxThermalZone LinuxThermalZoneBaseNode::G
std::string id = key + std::to_string(zone_index);
std::string thermal_zone_dir = prefix + id;

// header
msg.header.set__stamp(now());
msg.header.set__frame_id(id);
// temperature
msg.temperature.header.set__stamp(now());
msg.temperature.header.set__frame_id(id);
msg.temperature.temperature = GetZoneTemperature(thermal_zone_dir);
// type
msg.type = GetZoneString(thermal_zone_dir + "/type");

return msg;
}
Expand Down Expand Up @@ -146,3 +150,29 @@ double LinuxThermalZoneBaseNode::GetZoneTemperature(std::string thermal_zone_dir

return temperature;
}

std::string LinuxThermalZoneBaseNode::GetZoneString(std::string filepath)
{
std::ifstream file(filepath);
std::string return_string;

if (file.is_open()) {
std::string line;
std::getline(file, line); // Read the first line (assuming it contains the value)
file.close();

// Copy string to return and handle errors
try {
return_string = line;
RCLCPP_DEBUG_STREAM(this->get_logger(), filepath << " value: " << return_string);
} catch (const std::invalid_argument & e) {
RCLCPP_WARN_STREAM(this->get_logger(), "Invalid type data in file: " << filepath);
} catch (const std::out_of_range & e) {
RCLCPP_WARN_STREAM(this->get_logger(), "Type value out of range in file: " << filepath);
}
} else {
std::cerr << "Failed to open file " << filepath << std::endl;
}

return return_string;
}

0 comments on commit 65a35b2

Please sign in to comment.