JaiaBot 3.0.0
JaiaBot micro-AUV software
Loading...
Searching...
No Matches
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
26#include <algorithm>
27#include <map>
28#include <string>
29
31
32namespace jaiabot
33{
34namespace health
35{
36// Apps for sensors a bot can operate without, with the warning shown when one is not responding
37inline const std::map<std::string, protobuf::Warning>& optional_apps()
38{
39 static const std::map<std::string, protobuf::Warning> apps{
40 {"jaiabot_driver_salinity",
42 {"jaiabot_driver_tsys01",
46 return apps;
47}
48
49// The failure reporter's error for each optional app's service, with the warning shown instead
62
63// goby_coroner's state, except that an optional app can only degrade the vehicle
64inline goby::middleware::protobuf::HealthState
65vehicle_health_state(const goby::middleware::protobuf::VehicleHealth& vehicle_health)
66{
67 auto state = goby::middleware::protobuf::HEALTH__OK;
68 for (const auto& proc : vehicle_health.process())
69 {
70 auto proc_state = proc.main().state();
71 if (optional_apps().count(proc.name()))
72 proc_state = std::min(proc_state, goby::middleware::protobuf::HEALTH__DEGRADED);
73 state = std::max(state, proc_state);
74 }
75 return state;
76}
77
78template <typename HuborBotStatus>
79void populate_status_from_health(HuborBotStatus& status,
80 const goby::middleware::protobuf::VehicleHealth& vehicle_health,
81 bool truncate_to_fit_dccl = true)
82{
83 status.set_health_state(vehicle_health_state(vehicle_health));
84 status.clear_error();
85 status.clear_warning();
86
87 if (status.health_state() != goby::middleware::protobuf::HEALTH__OK)
88 {
89 auto add_errors_and_warnings =
90 [&status](const goby::middleware::protobuf::ThreadHealth& health) {
91 const auto& jaiabot_health = health.GetExtension(jaiabot::protobuf::jaiabot_thread);
92 for (auto error : jaiabot_health.error())
93 status.add_error(static_cast<jaiabot::protobuf::Error>(error));
94 for (auto warning : jaiabot_health.warning())
95 status.add_warning(static_cast<jaiabot::protobuf::Warning>(warning));
96 };
97
98 for (const auto& proc : vehicle_health.process())
99 {
100 add_errors_and_warnings(proc.main());
101 for (const auto& thread : proc.main().child()) add_errors_and_warnings(thread);
102
103 auto optional = optional_apps().find(proc.name());
104 if (optional != optional_apps().end() && proc.main().has_error() &&
105 proc.main().error() == goby::middleware::protobuf::ERROR__PROCESS_DIED)
106 status.add_warning(optional->second);
107 }
108
109 const int max_errors = HuborBotStatus::descriptor()
110 ->FindFieldByName("error")
111 ->options()
112 .GetExtension(dccl::field)
113 .max_repeat();
114
115 const int max_warnings = HuborBotStatus::descriptor()
116 ->FindFieldByName("warning")
117 ->options()
118 .GetExtension(dccl::field)
119 .max_repeat();
120
121 if (truncate_to_fit_dccl && status.error_size() > max_errors)
122 {
123 status.mutable_error()->Truncate(max_errors - 1);
125 }
126 if (truncate_to_fit_dccl && status.warning_size() > max_warnings)
127 {
128 status.mutable_warning()->Truncate(max_warnings - 1);
130 }
131 }
132}
133
134} // namespace health
135} // namespace jaiabot
136
137#endif
const std::map< protobuf::Error, protobuf::Warning > & optional_app_failures()
Definition health.h:50
void populate_status_from_health(HuborBotStatus &status, const goby::middleware::protobuf::VehicleHealth &vehicle_health, bool truncate_to_fit_dccl=true)
Definition health.h:79
goby::middleware::protobuf::HealthState vehicle_health_state(const goby::middleware::protobuf::VehicleHealth &vehicle_health)
Definition health.h:65
const std::map< std::string, protobuf::Warning > & optional_apps()
Definition health.h:37
@ ERROR__FAILED__PYTHON_JAIABOT_TSYS01_TEMPERATURE_SENSOR_DRIVER
Definition health.pb.h:276
@ ERROR__TOO_MANY_ERRORS_TO_REPORT_ALL
Definition health.pb.h:240
@ ERROR__FAILED__PYTHON_JAIABOT_PAM
Definition health.pb.h:274
@ ERROR__FAILED__PYTHON_JAIABOT_AS_EZO_EC
Definition health.pb.h:266
extern ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< ::goby::middleware::protobuf::ThreadHealth, ::PROTOBUF_NAMESPACE_ID::internal::MessageTypeTraits< ::jaiabot::protobuf::ThreadHealth >, 11, false > jaiabot_thread
Definition health.pb.h:4108
@ WARNING__TOO_MANY_WARNINGS_TO_REPORT_ALL
Definition health.pb.h:380
@ WARNING__NOT_RESPONDING__JAIABOT_ATLAS_SCIENTIFIC_EZO_EC_DRIVER
Definition health.pb.h:382
@ WARNING__NOT_RESPONDING__UNKNOWN_APP
Definition health.pb.h:381
@ WARNING__NOT_RESPONDING__JAIABOT_TSYS01_TEMPERATURE_SENSOR_DRIVER
Definition health.pb.h:387
@ WARNING__NOT_RESPONDING__JAIABOT_PAM_DRIVER
Definition health.pb.h:386