JaiaBot  1.19.0
JaiaBot micro-AUV software
health.h
Go to the documentation of this file.
1 // Copyright 2022:
2 // JaiaRobotics LLC
3 // File authors:
4 // Toby Schneider <toby@gobysoft.org>
5 //
6 //
7 // This file is part of the JaiaBot Project Libraries
8 // ("The Jaia Libraries").
9 //
10 // The Jaia Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Jaia Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with the Jaia Libraries. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef JAIABOT_SRC_LIB_HEALTH_HEALTH_H
24 #define JAIABOT_SRC_LIB_HEALTH_HEALTH_H
25 
27 
28 namespace jaiabot
29 {
30 namespace health
31 {
32 template <typename HuborBotStatus>
33 void populate_status_from_health(HuborBotStatus& status,
34  const goby::middleware::protobuf::VehicleHealth& vehicle_health,
35  bool truncate_to_fit_dccl = true)
36 {
37  status.set_health_state(vehicle_health.state());
38  status.clear_error();
39  status.clear_warning();
40 
41  if (vehicle_health.state() != goby::middleware::protobuf::HEALTH__OK)
42  {
43  auto add_errors_and_warnings =
44  [&status](const goby::middleware::protobuf::ThreadHealth& health) {
45  const auto& jaiabot_health = health.GetExtension(jaiabot::protobuf::jaiabot_thread);
46  for (auto error : jaiabot_health.error())
47  status.add_error(static_cast<jaiabot::protobuf::Error>(error));
48  for (auto warning : jaiabot_health.warning())
49  status.add_warning(static_cast<jaiabot::protobuf::Warning>(warning));
50  };
51 
52  for (const auto& proc : vehicle_health.process())
53  {
54  add_errors_and_warnings(proc.main());
55  for (const auto& thread : proc.main().child()) add_errors_and_warnings(thread);
56  }
57 
58  const int max_errors = HuborBotStatus::descriptor()
59  ->FindFieldByName("error")
60  ->options()
61  .GetExtension(dccl::field)
62  .max_repeat();
63 
64  const int max_warnings = HuborBotStatus::descriptor()
65  ->FindFieldByName("warning")
66  ->options()
67  .GetExtension(dccl::field)
68  .max_repeat();
69 
70  if (truncate_to_fit_dccl && status.error_size() > max_errors)
71  {
72  status.mutable_error()->Truncate(max_errors - 1);
74  }
75  if (truncate_to_fit_dccl && status.warning_size() > max_warnings)
76  {
77  status.mutable_warning()->Truncate(max_warnings - 1);
79  }
80  }
81 }
82 
83 } // namespace health
84 } // namespace jaiabot
85 
86 #endif
jaiabot::protobuf::jaiabot_thread
extern ::google::protobuf::internal::ExtensionIdentifier< ::goby::middleware::protobuf::ThreadHealth, ::google::protobuf::internal::MessageTypeTraits< ::jaiabot::protobuf::ThreadHealth >, 11, false > jaiabot_thread
Definition: health.pb.h:2961
jaiabot::protobuf::WARNING__TOO_MANY_WARNINGS_TO_REPORT_ALL
@ WARNING__TOO_MANY_WARNINGS_TO_REPORT_ALL
Definition: health.pb.h:388
jaiabot::protobuf::ERROR__TOO_MANY_ERRORS_TO_REPORT_ALL
@ ERROR__TOO_MANY_ERRORS_TO_REPORT_ALL
Definition: health.pb.h:269
jaiabot::protobuf::Error
Error
Definition: health.pb.h:268
health.pb.h
jaiabot::protobuf::Warning
Warning
Definition: health.pb.h:387
jaia::field
extern ::google::protobuf::internal::ExtensionIdentifier< ::google::protobuf::FieldOptions, ::google::protobuf::internal::MessageTypeTraits< ::jaia::FieldOptions >, 11, false > field
Definition: option_extensions.pb.h:792
jaiabot
Definition: config.pb.h:56
jaiabot::health::populate_status_from_health
void populate_status_from_health(HuborBotStatus &status, const goby::middleware::protobuf::VehicleHealth &vehicle_health, bool truncate_to_fit_dccl=true)
Definition: health.h:33