ros2_canopen master
C++ ROS CANopen Library
Loading...
Searching...
No Matches
diagnostic_collector.hpp
Go to the documentation of this file.
1// Copyright 2023 ROS-Industrial
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#ifndef DIAGNOSTICS_COLLECTOR_HPP_
16#define DIAGNOSTICS_COLLECTOR_HPP_
17
18#include <atomic>
19#include <mutex>
20#include <string>
21#include <unordered_map>
22#include "diagnostic_msgs/msg/diagnostic_status.hpp"
23#include "diagnostic_updater/diagnostic_updater.hpp"
24#include "diagnostic_updater/publisher.hpp"
25
26namespace ros2_canopen
27{
28
34{
35public:
36 DiagnosticsCollector() : level_(diagnostic_msgs::msg::DiagnosticStatus::OK) {}
37
43 unsigned char getLevel() const
44 {
45 return static_cast<unsigned char>(level_.load(std::memory_order_relaxed));
46 }
47
53 std::string getMessage() const { return message_; }
54
62 std::string getValue(const std::string & key) const
63 {
64 std::lock_guard<std::mutex> lock(mutex_);
65 auto it = values_.find(key);
66 if (it != values_.end())
67 return it->second;
68 else
69 return ""; // Return an empty string if key not found
70 }
71
78 void summary(unsigned char lvl, const std::string & message)
79 {
80 std::lock_guard<std::mutex> lock(mutex_);
81 this->setLevel(lvl);
82 this->setMessage(message);
83 }
84
92 void summaryf(unsigned char lvl, const char * format, ...)
93 {
94 va_list args;
95 va_start(args, format);
96 char buffer[1024];
97 vsnprintf(buffer, sizeof(buffer), format, args);
98 va_end(args);
99 summary(lvl, std::string(buffer));
100 }
101
108 void add(const std::string & key, const std::string & value)
109 {
110 std::lock_guard<std::mutex> lock(mutex_);
111 this->setValue(key, value);
112 }
113
121 void addf(const std::string & key, const char * format, ...)
122 {
123 va_list args;
124 va_start(args, format);
125 char buffer[1024];
126 vsnprintf(buffer, sizeof(buffer), format, args);
127 va_end(args);
128 add(key, std::string(buffer));
129 }
130
140 unsigned char lvl, const std::string & message, const std::string & key,
141 const std::string & value)
142 {
143 std::lock_guard<std::mutex> lock(mutex_);
144 this->setLevel(lvl);
145 this->setMessage(message);
146 this->setValue(key, value);
147 }
148
149private:
150 std::atomic<unsigned char> level_;
151 std::string message_;
152 std::unordered_map<std::string, std::string> values_;
153 mutable std::mutex mutex_;
154
155 void setLevel(unsigned char lvl)
156 {
157 level_.store(static_cast<unsigned char>(lvl), std::memory_order_relaxed);
158 }
159
160 void setMessage(const std::string & message) { message_ = message; }
161
162 void setValue(const std::string & key, const std::string & value) { values_[key] = value; }
163
164 // std::unordered_map<std::string, std::string> getValues() const
165 // {
166 // std::lock_guard<std::mutex> lock(mutex_);
167 // return values_;
168 // }
169};
170} // namespace ros2_canopen
171
172#endif // DIAGNOSTICS_COLLECTOR_HPP_
A class to collect diagnostic information.
Definition diagnostic_collector.hpp:34
void updateAll(unsigned char lvl, const std::string &message, const std::string &key, const std::string &value)
Update all diagnostic information.
Definition diagnostic_collector.hpp:139
void addf(const std::string &key, const char *format,...)
Add a device state value.
Definition diagnostic_collector.hpp:121
void summaryf(unsigned char lvl, const char *format,...)
Store current device summary.
Definition diagnostic_collector.hpp:92
void add(const std::string &key, const std::string &value)
Add a device state value.
Definition diagnostic_collector.hpp:108
std::string getValue(const std::string &key) const
Get the Value object Returns the current different device state values of the diagnostic.
Definition diagnostic_collector.hpp:62
std::string getMessage() const
Get the Message object Returns the current message of the diagnostic.
Definition diagnostic_collector.hpp:53
unsigned char getLevel() const
Get the Level Returns the current level (OK, WARN, ERROR or STALE) of the diagnostic.
Definition diagnostic_collector.hpp:43
DiagnosticsCollector()
Definition diagnostic_collector.hpp:36
void summary(unsigned char lvl, const std::string &message)
Store current device summary.
Definition diagnostic_collector.hpp:78
Definition configuration_manager.hpp:28