2016-06-15 23:48:41 +02:00
|
|
|
#include <framework/health/HealthHelper.h>
|
|
|
|
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
2020-06-07 01:17:24 +02:00
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
HealthHelper::HealthHelper(HasHealthIF* owner, object_id_t objectId) :
|
2020-06-07 01:17:24 +02:00
|
|
|
objectId(objectId), owner(owner) {
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
setParentQeueue(parentQueue);
|
|
|
|
return initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HealthHelper::setParentQeueue(MessageQueueId_t parentQueue) {
|
|
|
|
this->parentQueue = parentQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t HealthHelper::initialize() {
|
|
|
|
healthTable = objectManager->get<HealthTableIF>(objects::HEALTH_TABLE);
|
|
|
|
eventSender = objectManager->get<EventReportingProxyIF>(objectId);
|
2020-07-16 12:38:21 +02:00
|
|
|
|
|
|
|
if (healthTable == nullptr) {
|
|
|
|
sif::error << "HealthHelper::initialize: Health table object needs"
|
|
|
|
"to be created in factory." << std::endl;
|
|
|
|
return ObjectManagerIF::CHILD_INIT_FAILED;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2020-07-16 12:38:21 +02:00
|
|
|
|
|
|
|
if(eventSender == nullptr) {
|
|
|
|
sif::error << "HealthHelper::initialize: Owner has to implement "
|
|
|
|
"ReportingProxyIF." << std::endl;
|
|
|
|
return ObjectManagerIF::CHILD_INIT_FAILED;
|
|
|
|
}
|
|
|
|
|
2016-06-15 23:48:41 +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-06-11 16:03:09 +02:00
|
|
|
if (parentQueue == MessageQueueMessageIF::NO_QUEUE) {
|
2016-06-15 23:48:41 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-06-24 00:24:15 +02:00
|
|
|
CommandMessage information;
|
2020-06-09 02:18:39 +02:00
|
|
|
HealthMessage::setHealthMessage(&information, HealthMessage::HEALTH_INFO,
|
2016-06-15 23:48:41 +02:00
|
|
|
health, oldHealth);
|
2020-06-09 02:18:39 +02:00
|
|
|
if (MessageQueueSenderIF::sendMessage(parentQueue, &information,
|
2020-06-22 20:18:13 +02:00
|
|
|
owner->getCommandQueue()) != HasReturnvaluesIF::RETURN_OK) {
|
2020-04-23 19:13:18 +02:00
|
|
|
sif::debug << "HealthHelper::informParent: sending health reply failed."
|
2016-06-15 23:48:41 +02:00
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 02:18:39 +02:00
|
|
|
void HealthHelper::handleSetHealthCommand(CommandMessage* command) {
|
|
|
|
ReturnValue_t result = owner->setHealth(HealthMessage::getHealth(command));
|
|
|
|
if (command->getSender() == MessageQueueMessageIF::NO_QUEUE) {
|
2016-06-15 23:48:41 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-06-24 00:24:15 +02:00
|
|
|
CommandMessage reply;
|
2016-06-15 23:48:41 +02:00
|
|
|
if (result == HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
HealthMessage::setHealthMessage(&reply,
|
|
|
|
HealthMessage::REPLY_HEALTH_SET);
|
|
|
|
} else {
|
2020-06-09 02:18:39 +02:00
|
|
|
reply.setReplyRejected(result, command->getCommand());
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2020-06-09 02:18:39 +02:00
|
|
|
if (MessageQueueSenderIF::sendMessage(command->getSender(), &reply,
|
2020-06-22 20:18:13 +02:00
|
|
|
owner->getCommandQueue()) != HasReturnvaluesIF::RETURN_OK) {
|
2020-04-23 19:21:12 +02:00
|
|
|
sif::debug << "HealthHelper::handleHealthCommand: sending health "
|
2020-04-08 18:08:14 +02:00
|
|
|
"reply failed." << std::endl;
|
2020-04-23 19:21:12 +02:00
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|