|
| 1 | +// Copyright 2024 TIER IV, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include "nebula_ros/hesai/hw_monitor/diagnostic_provider.hpp" |
| 18 | + |
| 19 | +#include <diagnostic_updater/diagnostic_status_wrapper.hpp> |
| 20 | + |
| 21 | +#include <diagnostic_msgs/msg/detail/diagnostic_status__struct.hpp> |
| 22 | + |
| 23 | +#include <boost/lexical_cast/bad_lexical_cast.hpp> |
| 24 | +#include <boost/property_tree/ptree.hpp> |
| 25 | + |
| 26 | +#include <map> |
| 27 | +#include <memory> |
| 28 | +#include <string> |
| 29 | +#include <vector> |
| 30 | + |
| 31 | +namespace nebula::ros::hw_monitor |
| 32 | +{ |
| 33 | +class HttpLidarMonitorProvider : public DiagnosticProvider |
| 34 | +{ |
| 35 | +public: |
| 36 | + std::map<std::string, hook_t> getHooks() override |
| 37 | + { |
| 38 | + return {{"hesai_voltage", [this](auto & diagnostics) { updateVoltage(diagnostics); }}}; |
| 39 | + } |
| 40 | + |
| 41 | + void fetch(drivers::HesaiHwInterface & hw_interface, rclcpp::Clock & clock) override |
| 42 | + { |
| 43 | + hw_interface.GetLidarMonitorAsyncHttp([&](const std::string & str) { |
| 44 | + std::scoped_lock lock(mtx_lidar_monitor_); |
| 45 | + current_lidar_monitor_time_ = std::make_unique<rclcpp::Time>(clock.now()); |
| 46 | + current_data_ = std::make_unique<boost::property_tree::ptree>(hw_interface.ParseJson(str)); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + [[nodiscard]] std::optional<rclcpp::Time> getLastUpdate() const override |
| 51 | + { |
| 52 | + if (!current_lidar_monitor_time_) return {}; |
| 53 | + return *current_lidar_monitor_time_; |
| 54 | + } |
| 55 | + |
| 56 | + [[nodiscard]] std::string getName() const override { return "LidarMonitor"; } |
| 57 | + |
| 58 | +private: |
| 59 | + void updateVoltage(diagnostic_updater::DiagnosticStatusWrapper & diagnostics) |
| 60 | + { |
| 61 | + std::scoped_lock lock(mtx_lidar_monitor_); |
| 62 | + if (!current_data_) { |
| 63 | + diagnostics.summary(diagnostic_msgs::msg::DiagnosticStatus::WARN, "No data available"); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + uint8_t level = diagnostic_msgs::msg::DiagnosticStatus::OK; |
| 68 | + std::vector<std::string> msgs; |
| 69 | + |
| 70 | + std::vector<std::string> keys = {"lidarInCur", "lidarInVol"}; |
| 71 | + |
| 72 | + for (const auto & key : keys) { |
| 73 | + std::optional<std::string> value_opt{}; |
| 74 | + std::string full_key = "Body." + key; |
| 75 | + |
| 76 | + try { |
| 77 | + auto result = current_data_->get_optional<std::string>(full_key); |
| 78 | + if (result.has_value()) { |
| 79 | + value_opt.emplace(result.value()); |
| 80 | + } else { |
| 81 | + level = diagnostic_msgs::msg::DiagnosticStatus::ERROR; |
| 82 | + msgs.emplace_back("field '" + full_key + "' not present"); |
| 83 | + } |
| 84 | + } catch (boost::bad_lexical_cast & ex) { |
| 85 | + level = diagnostic_msgs::msg::DiagnosticStatus::ERROR; |
| 86 | + msgs.emplace_back("field '" + full_key + "' not parsed: " + std::string(ex.what())); |
| 87 | + } |
| 88 | + |
| 89 | + if (value_opt.has_value()) { |
| 90 | + diagnostics.add(key, value_opt.value()); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + diagnostics.summary(level, boost::algorithm::join(msgs, ", ")); |
| 95 | + } |
| 96 | + |
| 97 | + std::unique_ptr<boost::property_tree::ptree> current_data_; |
| 98 | + std::unique_ptr<rclcpp::Time> current_lidar_monitor_time_; |
| 99 | + std::mutex mtx_lidar_monitor_; |
| 100 | +}; |
| 101 | +} // namespace nebula::ros::hw_monitor |
0 commit comments