fsfw/src/fsfw/power/PowerSwitcherComponent.cpp

126 lines
3.8 KiB
C++
Raw Normal View History

2022-04-01 17:27:53 +02:00
#include "PowerSwitcherComponent.h"
#include <fsfw/ipc/QueueFactory.h>
#include <fsfw/power/PowerSwitchIF.h>
#include <fsfw/subsystem/helper.h>
2022-04-01 17:27:53 +02:00
2022-11-10 15:38:34 +01:00
PowerSwitcherComponent::PowerSwitcherComponent(object_id_t objectId, PowerSwitchIF* pwrSwitcher,
2022-05-16 14:55:15 +02:00
power::Switch_t pwrSwitch)
: SystemObject(objectId),
switcher(pwrSwitcher, pwrSwitch),
modeHelper(this),
healthHelper(this, objectId) {
2022-04-01 17:27:53 +02:00
queue = QueueFactory::instance()->createMessageQueue();
}
ReturnValue_t PowerSwitcherComponent::performOperation(uint8_t opCode) {
ReturnValue_t result;
CommandMessage command;
2022-08-16 01:08:26 +02:00
for (result = queue->receiveMessage(&command); result == returnvalue::OK;
2022-04-01 17:27:53 +02:00
result = queue->receiveMessage(&command)) {
result = healthHelper.handleHealthCommand(&command);
2022-08-16 01:08:26 +02:00
if (result == returnvalue::OK) {
2022-04-01 17:27:53 +02:00
continue;
}
result = modeHelper.handleModeCommand(&command);
2022-08-16 01:08:26 +02:00
if (result == returnvalue::OK) {
2022-04-01 17:27:53 +02:00
continue;
}
}
if (getHealth() == FAULTY) {
performFaultyOperation();
}
2022-05-16 14:55:15 +02:00
if (switcher.active()) {
2022-04-01 17:27:53 +02:00
switcher.doStateMachine();
auto currState = switcher.getState();
if (currState == PowerSwitcher::SWITCH_IS_OFF) {
setMode(MODE_OFF, 0);
2022-05-16 14:55:15 +02:00
} else if (currState == PowerSwitcher::SWITCH_IS_ON) {
2022-04-01 17:27:53 +02:00
setMode(MODE_ON, 0);
}
}
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2022-04-01 17:27:53 +02:00
}
ReturnValue_t PowerSwitcherComponent::initialize() {
ReturnValue_t result = modeHelper.initialize();
2022-08-16 01:08:26 +02:00
if (result != returnvalue::OK) {
2022-04-01 17:27:53 +02:00
return result;
}
result = healthHelper.initialize();
2022-08-16 01:08:26 +02:00
if (result != returnvalue::OK) {
2022-04-01 17:27:53 +02:00
return result;
}
return SystemObject::initialize();
}
2022-05-16 14:55:15 +02:00
MessageQueueId_t PowerSwitcherComponent::getCommandQueue() const { return queue->getId(); }
2022-04-01 17:27:53 +02:00
2022-11-10 15:38:34 +01:00
void PowerSwitcherComponent::getMode(Mode_t* mode, Submode_t* submode) {
2022-04-01 17:27:53 +02:00
*mode = this->mode;
*submode = this->submode;
}
ReturnValue_t PowerSwitcherComponent::setHealth(HealthState health) {
healthHelper.setHealth(health);
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2022-04-01 17:27:53 +02:00
}
ReturnValue_t PowerSwitcherComponent::checkModeCommand(Mode_t mode, Submode_t submode,
2022-11-10 15:38:34 +01:00
uint32_t* msToReachTheMode) {
2022-04-01 17:27:53 +02:00
*msToReachTheMode = 5000;
2022-05-16 14:55:15 +02:00
if (mode != MODE_ON and mode != MODE_OFF) {
2022-04-01 17:27:53 +02:00
return TRANS_NOT_ALLOWED;
}
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2022-04-01 17:27:53 +02:00
}
void PowerSwitcherComponent::startTransition(Mode_t mode, Submode_t submode) {
2022-05-16 14:55:15 +02:00
if (mode == MODE_OFF) {
2022-04-01 17:27:53 +02:00
switcher.turnOff(true);
switcher.doStateMachine();
2022-05-16 14:55:15 +02:00
if (switcher.getState() == PowerSwitcher::SWITCH_IS_OFF) {
2022-04-01 17:27:53 +02:00
setMode(MODE_OFF, 0);
}
} else if (mode == MODE_ON) {
switcher.turnOn(true);
switcher.doStateMachine();
2022-05-16 14:55:15 +02:00
if (switcher.getState() == PowerSwitcher::SWITCH_IS_ON) {
2022-04-01 17:27:53 +02:00
setMode(MODE_ON, 0);
}
}
}
void PowerSwitcherComponent::setToExternalControl() {
healthHelper.setHealth(HasHealthIF::EXTERNAL_CONTROL);
}
void PowerSwitcherComponent::announceMode(bool recursive) {
triggerEvent(MODE_INFO, mode, submode);
}
void PowerSwitcherComponent::setMode(Mode_t newMode, Submode_t newSubmode) {
this->mode = newMode;
this->submode = newSubmode;
modeHelper.modeChanged(mode, submode);
announceMode(false);
}
2022-05-16 14:55:15 +02:00
HasHealthIF::HealthState PowerSwitcherComponent::getHealth() { return healthHelper.getHealth(); }
2022-11-10 15:38:34 +01:00
const HasHealthIF* PowerSwitcherComponent::getOptHealthIF() const { return this; }
const HasModesIF& PowerSwitcherComponent::getModeIF() const { return *this; }
ReturnValue_t PowerSwitcherComponent::connectModeTreeParent(HasModeTreeChildrenIF& parent) {
return modetree::connectModeTreeParent(parent, *this, &healthHelper, modeHelper);
2022-11-10 15:38:34 +01:00
}
object_id_t PowerSwitcherComponent::getObjectId() const { return SystemObject::getObjectId(); }
ModeTreeChildIF& PowerSwitcherComponent::getModeTreeChildIF() { return *this; }
void PowerSwitcherComponent::performFaultyOperation() {}