fsfw/src/fsfw/health/HealthHelper.cpp

110 lines
3.5 KiB
C++
Raw Normal View History

2021-07-13 20:22:54 +02:00
#include "fsfw/health/HealthHelper.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
2020-09-04 15:19:33 +02:00
2022-02-02 10:29:30 +01:00
HealthHelper::HealthHelper(HasHealthIF* owner, object_id_t objectId)
: objectId(objectId), owner(owner) {}
2022-11-15 11:27:31 +01:00
HealthHelper::~HealthHelper() {
if (healthTable != nullptr) {
healthTable->removeObject(objectId);
}
}
ReturnValue_t HealthHelper::handleHealthCommand(CommandMessage* message) {
2022-02-02 10:29:30 +01:00
switch (message->getCommand()) {
case HealthMessage::HEALTH_SET:
handleSetHealthCommand(message);
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2022-02-02 10:29:30 +01:00
case HealthMessage::HEALTH_ANNOUNCE: {
eventSender->forwardEvent(HasHealthIF::HEALTH_INFO, getHealth(), getHealth());
}
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2022-02-02 10:29:30 +01:00
default:
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
}
2022-02-02 10:29:30 +01:00
HasHealthIF::HealthState HealthHelper::getHealth() { return healthTable->getHealth(objectId); }
ReturnValue_t HealthHelper::initialize(MessageQueueId_t parentQueue) {
2022-02-02 10:29:30 +01:00
setParentQueue(parentQueue);
return initialize();
}
2022-02-02 10:29:30 +01:00
void HealthHelper::setParentQueue(MessageQueueId_t parentQueue) { this->parentQueue = parentQueue; }
ReturnValue_t HealthHelper::initialize() {
2022-02-02 10:29:30 +01:00
healthTable = ObjectManager::instance()->get<HealthTableIF>(objects::HEALTH_TABLE);
eventSender = ObjectManager::instance()->get<EventReportingProxyIF>(objectId);
2020-09-04 15:19:33 +02:00
2022-02-02 10:29:30 +01:00
if (healthTable == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "HealthHelper::initialize: Health table object needs"
"to be created in factory."
<< std::endl;
#endif
2022-02-02 10:29:30 +01:00
return ObjectManagerIF::CHILD_INIT_FAILED;
}
2020-09-04 15:19:33 +02:00
2022-02-02 10:29:30 +01:00
if (eventSender == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "HealthHelper::initialize: Owner has to implement "
"ReportingProxyIF."
<< std::endl;
#endif
2022-02-02 10:29:30 +01:00
return ObjectManagerIF::CHILD_INIT_FAILED;
}
2020-09-04 15:19:33 +02:00
2022-02-02 10:29:30 +01:00
ReturnValue_t result = healthTable->registerObject(objectId, HasHealthIF::HEALTHY);
2022-08-15 20:28:16 +02:00
if (result != returnvalue::OK) {
2022-02-02 10:29:30 +01:00
return result;
}
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
}
void HealthHelper::setHealth(HasHealthIF::HealthState health) {
2022-02-02 10:29:30 +01:00
HasHealthIF::HealthState oldHealth = getHealth();
eventSender->forwardEvent(HasHealthIF::HEALTH_INFO, health, oldHealth);
if (health != oldHealth) {
healthTable->setHealth(objectId, health);
informParent(health, oldHealth);
}
}
void HealthHelper::informParent(HasHealthIF::HealthState health,
2022-02-02 10:29:30 +01:00
HasHealthIF::HealthState oldHealth) {
if (parentQueue == MessageQueueIF::NO_QUEUE) {
return;
}
CommandMessage information;
HealthMessage::setHealthMessage(&information, HealthMessage::HEALTH_INFO, health, oldHealth);
if (MessageQueueSenderIF::sendMessage(parentQueue, &information, owner->getCommandQueue()) !=
2022-08-15 20:28:16 +02:00
returnvalue::OK) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::debug << "HealthHelper::informParent: sending health reply failed." << std::endl;
#endif
2022-02-02 10:29:30 +01:00
}
}
2020-09-04 15:19:33 +02:00
void HealthHelper::handleSetHealthCommand(CommandMessage* command) {
2022-02-02 10:29:30 +01:00
ReturnValue_t result = owner->setHealth(HealthMessage::getHealth(command));
if (command->getSender() == MessageQueueIF::NO_QUEUE) {
return;
}
CommandMessage reply;
2022-08-15 20:28:16 +02:00
if (result == returnvalue::OK) {
2022-02-02 10:29:30 +01:00
HealthMessage::setHealthMessage(&reply, HealthMessage::REPLY_HEALTH_SET);
} else {
reply.setReplyRejected(result, command->getCommand());
}
if (MessageQueueSenderIF::sendMessage(command->getSender(), &reply, owner->getCommandQueue()) !=
2022-08-15 20:28:16 +02:00
returnvalue::OK) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::debug << "HealthHelper::handleHealthCommand: sending health "
"reply failed."
<< std::endl;
#endif
2022-02-02 10:29:30 +01:00
}
}