fsfw/health/HealthHelper.cpp

114 lines
3.4 KiB
C++
Raw Normal View History

2020-08-13 20:53:35 +02:00
#include "HealthHelper.h"
#include "../serviceinterface/ServiceInterfaceStream.h"
2020-09-04 15:19:33 +02:00
HealthHelper::HealthHelper(HasHealthIF* owner, object_id_t objectId) :
2020-09-04 15:19:33 +02:00
objectId(objectId), owner(owner) {
}
HealthHelper::~HealthHelper() {
}
ReturnValue_t HealthHelper::handleHealthCommand(CommandMessage* message) {
switch (message->getCommand()) {
case HealthMessage::HEALTH_SET:
handleSetHealthCommand(message);
return HasReturnvaluesIF::RETURN_OK;
case HealthMessage::HEALTH_ANNOUNCE: {
eventSender->forwardEvent(HasHealthIF::HEALTH_INFO, getHealth(),
getHealth());
}
return HasReturnvaluesIF::RETURN_OK;
default:
return HasReturnvaluesIF::RETURN_FAILED;
}
}
HasHealthIF::HealthState HealthHelper::getHealth() {
return healthTable->getHealth(objectId);
}
ReturnValue_t HealthHelper::initialize(MessageQueueId_t parentQueue) {
2020-06-25 18:09:32 +02:00
setParentQueue(parentQueue);
return initialize();
}
2020-06-25 18:09:32 +02:00
void HealthHelper::setParentQueue(MessageQueueId_t parentQueue) {
this->parentQueue = parentQueue;
}
ReturnValue_t HealthHelper::initialize() {
healthTable = objectManager->get<HealthTableIF>(objects::HEALTH_TABLE);
eventSender = objectManager->get<EventReportingProxyIF>(objectId);
2020-09-04 15:19:33 +02:00
if (healthTable == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-09-04 15:19:33 +02:00
sif::error << "HealthHelper::initialize: Health table object needs"
"to be created in factory." << std::endl;
#endif
2020-09-04 15:19:33 +02:00
return ObjectManagerIF::CHILD_INIT_FAILED;
}
if(eventSender == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-09-04 15:19:33 +02:00
sif::error << "HealthHelper::initialize: Owner has to implement "
"ReportingProxyIF." << std::endl;
#endif
2020-09-04 15:19:33 +02:00
return ObjectManagerIF::CHILD_INIT_FAILED;
}
2020-09-04 15:19:33 +02:00
ReturnValue_t result = healthTable->registerObject(objectId,
HasHealthIF::HEALTHY);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
return HasReturnvaluesIF::RETURN_OK;
}
void HealthHelper::setHealth(HasHealthIF::HealthState health) {
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,
HasHealthIF::HealthState oldHealth) {
2020-09-10 16:42:11 +02:00
if (parentQueue == MessageQueueIF::NO_QUEUE) {
return;
}
2020-09-04 15:19:33 +02:00
CommandMessage information;
HealthMessage::setHealthMessage(&information, HealthMessage::HEALTH_INFO,
health, oldHealth);
2020-09-04 15:19:33 +02:00
if (MessageQueueSenderIF::sendMessage(parentQueue, &information,
owner->getCommandQueue()) != HasReturnvaluesIF::RETURN_OK) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::debug << "HealthHelper::informParent: sending health reply failed."
<< std::endl;
#endif
}
}
2020-09-04 15:19:33 +02:00
void HealthHelper::handleSetHealthCommand(CommandMessage* command) {
ReturnValue_t result = owner->setHealth(HealthMessage::getHealth(command));
2020-09-10 16:42:11 +02:00
if (command->getSender() == MessageQueueIF::NO_QUEUE) {
return;
}
CommandMessage reply;
if (result == HasReturnvaluesIF::RETURN_OK) {
HealthMessage::setHealthMessage(&reply,
HealthMessage::REPLY_HEALTH_SET);
} else {
2020-09-04 15:19:33 +02:00
reply.setReplyRejected(result, command->getCommand());
}
2020-09-04 15:19:33 +02:00
if (MessageQueueSenderIF::sendMessage(command->getSender(), &reply,
owner->getCommandQueue()) != HasReturnvaluesIF::RETURN_OK) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-09-04 15:19:33 +02:00
sif::debug << "HealthHelper::handleHealthCommand: sending health "
"reply failed." << std::endl;
#endif
2020-09-04 15:19:33 +02:00
}
}