2022-05-02 17:37:00 +02:00
|
|
|
#include <fsfw/health/HealthTableIF.h>
|
2021-02-01 11:17:20 +01:00
|
|
|
#include <fsfw/ipc/QueueFactory.h>
|
2022-01-17 15:58:27 +01:00
|
|
|
#include <fsfw/objectmanager/ObjectManager.h>
|
2021-08-03 15:58:01 +02:00
|
|
|
#include <fsfw_hal/common/gpio/GpioCookie.h>
|
2023-03-26 16:42:00 +02:00
|
|
|
#include <mission/tcs/HeaterHandler.h>
|
2021-01-28 14:55:21 +01:00
|
|
|
|
2022-05-02 17:37:00 +02:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2022-01-17 15:58:27 +01:00
|
|
|
#include "devices/powerSwitcherList.h"
|
2023-02-17 11:50:14 +01:00
|
|
|
#include "fsfw/subsystem/helper.h"
|
2022-01-17 15:58:27 +01:00
|
|
|
|
2022-05-02 22:45:27 +02:00
|
|
|
HeaterHandler::HeaterHandler(object_id_t setObjectId_, GpioIF* gpioInterface_, HeaterHelper helper,
|
|
|
|
PowerSwitchIF* mainLineSwitcher_, power::Switch_t mainLineSwitch_)
|
2022-01-17 15:58:27 +01:00
|
|
|
: SystemObject(setObjectId_),
|
2022-05-02 22:45:27 +02:00
|
|
|
helper(helper),
|
2023-02-17 11:50:14 +01:00
|
|
|
modeHelper(this),
|
2022-05-02 22:45:27 +02:00
|
|
|
gpioInterface(gpioInterface_),
|
2022-05-02 17:37:00 +02:00
|
|
|
mainLineSwitcher(mainLineSwitcher_),
|
2022-01-17 15:58:27 +01:00
|
|
|
mainLineSwitch(mainLineSwitch_),
|
|
|
|
actionHelper(this, nullptr) {
|
2022-05-02 22:45:27 +02:00
|
|
|
for (const auto& heater : helper.heaters) {
|
|
|
|
if (heater.first == nullptr) {
|
|
|
|
throw std::invalid_argument("HeaterHandler::HeaterHandler: Invalid Heater Object");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gpioInterface_ == nullptr) {
|
|
|
|
throw std::invalid_argument("HeaterHandler::HeaterHandler: Invalid Gpio IF");
|
|
|
|
}
|
2022-05-02 17:37:00 +02:00
|
|
|
if (mainLineSwitcher == nullptr) {
|
|
|
|
throw std::invalid_argument("HeaterHandler::HeaterHandler: Invalid PowerSwitchIF");
|
|
|
|
}
|
2023-03-02 15:32:12 +01:00
|
|
|
handlerLock = MutexFactory::instance()->createMutex();
|
|
|
|
if (handlerLock == nullptr) {
|
2022-05-04 11:11:29 +02:00
|
|
|
throw std::runtime_error("HeaterHandler::HeaterHandler: Creating Mutex failed");
|
|
|
|
}
|
2022-02-22 20:27:13 +01:00
|
|
|
auto mqArgs = MqArgs(setObjectId_, static_cast<void*>(this));
|
2022-01-17 15:58:27 +01:00
|
|
|
commandQueue = QueueFactory::instance()->createMessageQueue(
|
2022-02-22 20:27:13 +01:00
|
|
|
cmdQueueSize, MessageQueueMessage::MAX_MESSAGE_SIZE, &mqArgs);
|
2021-01-23 17:22:40 +01:00
|
|
|
}
|
|
|
|
|
2023-02-22 17:50:03 +01:00
|
|
|
HeaterHandler::~HeaterHandler() = default;
|
2021-01-23 17:22:40 +01:00
|
|
|
|
2021-01-28 14:55:21 +01:00
|
|
|
ReturnValue_t HeaterHandler::performOperation(uint8_t operationCode) {
|
2022-05-02 22:58:06 +02:00
|
|
|
try {
|
|
|
|
readCommandQueue();
|
|
|
|
for (const auto& heater : helper.heaters) {
|
|
|
|
heater.first->performOperation(0);
|
|
|
|
}
|
2022-05-12 20:44:36 +02:00
|
|
|
handleSwitchHandling();
|
2023-02-17 11:50:14 +01:00
|
|
|
if (waitForSwitchOff) {
|
|
|
|
if (mainLineSwitcher->getSwitchState(mainLineSwitch) == SWITCH_OFF) {
|
|
|
|
waitForSwitchOff = false;
|
|
|
|
mode = MODE_OFF;
|
2023-02-17 13:11:16 +01:00
|
|
|
modeHelper.modeChanged(mode, submode);
|
2023-02-17 11:50:14 +01:00
|
|
|
}
|
|
|
|
}
|
2022-05-04 11:11:29 +02:00
|
|
|
} catch (const std::out_of_range& e) {
|
2022-05-02 22:58:06 +02:00
|
|
|
sif::warning << "HeaterHandler::performOperation: "
|
2022-05-04 11:11:29 +02:00
|
|
|
"Out of range error | "
|
|
|
|
<< e.what() << std::endl;
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::OK;
|
2021-01-23 17:22:40 +01:00
|
|
|
}
|
|
|
|
|
2021-01-28 14:55:21 +01:00
|
|
|
ReturnValue_t HeaterHandler::initialize() {
|
2022-01-17 15:58:27 +01:00
|
|
|
ReturnValue_t result = SystemObject::initialize();
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-01-17 15:58:27 +01:00
|
|
|
return ObjectManagerIF::CHILD_INIT_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = initializeHeaterMap();
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-01-17 15:58:27 +01:00
|
|
|
return ObjectManagerIF::CHILD_INIT_FAILED;
|
|
|
|
}
|
|
|
|
|
2022-05-02 22:45:27 +02:00
|
|
|
ipcStore = ObjectManager::instance()->get<StorageManagerIF>(objects::IPC_STORE);
|
|
|
|
if (ipcStore == nullptr) {
|
2022-01-17 15:58:27 +01:00
|
|
|
sif::error << "HeaterHandler::initialize: IPC store not set up in factory." << std::endl;
|
|
|
|
return ObjectManagerIF::CHILD_INIT_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = actionHelper.initialize(commandQueue);
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-01-17 15:58:27 +01:00
|
|
|
return ObjectManagerIF::CHILD_INIT_FAILED;
|
|
|
|
}
|
|
|
|
|
2023-02-17 11:50:14 +01:00
|
|
|
result = modeHelper.initialize();
|
|
|
|
if (result != returnvalue::OK) {
|
|
|
|
return ObjectManagerIF::CHILD_INIT_FAILED;
|
|
|
|
}
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::OK;
|
2021-01-23 17:22:40 +01:00
|
|
|
}
|
|
|
|
|
2022-01-17 15:58:27 +01:00
|
|
|
ReturnValue_t HeaterHandler::initializeHeaterMap() {
|
2022-05-02 22:55:39 +02:00
|
|
|
for (power::Switch_t switchNr = 0; switchNr < heater::NUMBER_OF_SWITCHES; switchNr++) {
|
2022-05-04 11:11:29 +02:00
|
|
|
heaterVec.push_back(HeaterWrapper(helper.heaters[switchNr], SwitchState::OFF));
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::OK;
|
2021-01-28 14:55:21 +01:00
|
|
|
}
|
2021-01-23 17:22:40 +01:00
|
|
|
|
2021-01-28 14:55:21 +01:00
|
|
|
void HeaterHandler::readCommandQueue() {
|
2022-08-24 17:27:47 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
2022-01-17 15:58:27 +01:00
|
|
|
CommandMessage command;
|
2022-05-02 17:51:00 +02:00
|
|
|
do {
|
|
|
|
result = commandQueue->receiveMessage(&command);
|
|
|
|
if (result == MessageQueueIF::EMPTY) {
|
|
|
|
break;
|
2022-08-24 17:27:47 +02:00
|
|
|
} else if (result != returnvalue::OK) {
|
2022-05-02 17:51:00 +02:00
|
|
|
sif::warning << "HeaterHandler::readCommandQueue: Message reception error" << std::endl;
|
2023-02-19 17:06:08 +01:00
|
|
|
break;
|
2022-05-02 17:51:00 +02:00
|
|
|
}
|
|
|
|
result = actionHelper.handleActionMessage(&command);
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result == returnvalue::OK) {
|
2022-05-02 17:51:00 +02:00
|
|
|
continue;
|
|
|
|
}
|
2023-02-17 13:11:16 +01:00
|
|
|
result = modeHelper.handleModeCommand(&command);
|
2023-02-21 01:53:23 +01:00
|
|
|
if (result == returnvalue::OK) {
|
2023-02-17 13:11:16 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-24 17:27:47 +02:00
|
|
|
} while (result == returnvalue::OK);
|
2021-01-23 17:22:40 +01:00
|
|
|
}
|
|
|
|
|
2022-01-17 15:58:27 +01:00
|
|
|
ReturnValue_t HeaterHandler::executeAction(ActionId_t actionId, MessageQueueId_t commandedBy,
|
|
|
|
const uint8_t* data, size_t size) {
|
2022-05-12 20:12:01 +02:00
|
|
|
if (data == nullptr or size < 3) {
|
2022-05-02 22:45:27 +02:00
|
|
|
return HasActionsIF::INVALID_PARAMETERS;
|
|
|
|
}
|
2022-01-17 15:58:27 +01:00
|
|
|
if (actionId != SWITCH_HEATER) {
|
2022-05-02 22:45:27 +02:00
|
|
|
return COMMAND_NOT_SUPPORTED;
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
2022-05-04 10:34:11 +02:00
|
|
|
auto switchNr = data[0];
|
2022-05-02 22:45:27 +02:00
|
|
|
if (switchNr > 7) {
|
|
|
|
return HasActionsIF::INVALID_PARAMETERS;
|
|
|
|
}
|
2022-05-04 10:34:11 +02:00
|
|
|
auto& heater = heaterVec.at(switchNr);
|
2022-05-12 20:44:36 +02:00
|
|
|
|
|
|
|
auto actionRaw = data[1];
|
|
|
|
if (actionRaw != 0 and actionRaw != 1) {
|
2022-05-12 20:12:01 +02:00
|
|
|
return HasActionsIF::INVALID_PARAMETERS;
|
|
|
|
}
|
2022-05-12 20:44:36 +02:00
|
|
|
auto action = static_cast<SwitchAction>(data[1]);
|
|
|
|
// Always accepts OFF commands
|
|
|
|
if (action == SwitchAction::SET_SWITCH_ON) {
|
2023-02-21 01:53:23 +01:00
|
|
|
HasHealthIF::HealthState health;
|
|
|
|
{
|
2023-03-02 15:32:12 +01:00
|
|
|
MutexGuard mg(handlerLock, LOCK_TYPE, LOCK_TIMEOUT, LOCK_CTX);
|
2023-02-21 01:53:23 +01:00
|
|
|
health = heater.healthDevice->getHealth();
|
|
|
|
}
|
2022-05-12 20:44:36 +02:00
|
|
|
if (health == HasHealthIF::FAULTY or health == HasHealthIF::PERMANENT_FAULTY or
|
|
|
|
health == HasHealthIF::NEEDS_RECOVERY) {
|
|
|
|
return HasHealthIF::OBJECT_NOT_HEALTHY;
|
|
|
|
}
|
|
|
|
CmdSourceParam cmdSource = CmdSourceParam::EXTERNAL;
|
|
|
|
uint8_t cmdSourceRaw = data[2];
|
|
|
|
if (cmdSourceRaw > 1) {
|
|
|
|
return HasActionsIF::INVALID_PARAMETERS;
|
|
|
|
}
|
|
|
|
cmdSource = static_cast<CmdSourceParam>(data[2]);
|
|
|
|
if (health == HasHealthIF::EXTERNAL_CONTROL and cmdSource == CmdSourceParam::INTERNAL) {
|
|
|
|
return HasHealthIF::IS_EXTERNALLY_CONTROLLED;
|
|
|
|
}
|
2022-05-12 20:12:01 +02:00
|
|
|
}
|
2022-05-12 20:44:36 +02:00
|
|
|
|
|
|
|
if (heater.cmdActive) {
|
2022-05-02 22:45:27 +02:00
|
|
|
return COMMAND_ALREADY_WAITING;
|
|
|
|
}
|
2022-05-12 20:44:36 +02:00
|
|
|
heater.action = action;
|
|
|
|
heater.cmdActive = true;
|
2022-05-02 22:45:27 +02:00
|
|
|
heater.replyQueue = commandedBy;
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::OK;
|
2021-01-23 17:22:40 +01:00
|
|
|
}
|
|
|
|
|
2022-03-30 12:22:49 +02:00
|
|
|
ReturnValue_t HeaterHandler::sendSwitchCommand(uint8_t switchNr, ReturnValue_t onOff) {
|
2022-01-17 15:58:27 +01:00
|
|
|
ReturnValue_t result;
|
|
|
|
store_address_t storeAddress;
|
2022-05-12 20:12:01 +02:00
|
|
|
uint8_t commandData[3] = {};
|
2022-01-17 15:58:27 +01:00
|
|
|
|
|
|
|
switch (onOff) {
|
|
|
|
case PowerSwitchIF::SWITCH_ON:
|
|
|
|
commandData[0] = switchNr;
|
|
|
|
commandData[1] = SET_SWITCH_ON;
|
2022-05-12 20:12:01 +02:00
|
|
|
commandData[2] = CmdSourceParam::INTERNAL;
|
2022-01-17 15:58:27 +01:00
|
|
|
break;
|
|
|
|
case PowerSwitchIF::SWITCH_OFF:
|
|
|
|
commandData[0] = switchNr;
|
|
|
|
commandData[1] = SET_SWITCH_OFF;
|
2022-05-12 20:12:01 +02:00
|
|
|
commandData[2] = CmdSourceParam::INTERNAL;
|
2022-01-17 15:58:27 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sif::error << "HeaterHandler::sendSwitchCommand: Invalid switch request" << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-05-02 22:45:27 +02:00
|
|
|
result = ipcStore->addData(&storeAddress, commandData, sizeof(commandData));
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result == returnvalue::OK) {
|
2022-01-17 15:58:27 +01:00
|
|
|
CommandMessage message;
|
|
|
|
ActionMessage::setCommand(&message, SWITCH_HEATER, storeAddress);
|
|
|
|
/* Send heater command to own command queue */
|
|
|
|
result = commandQueue->sendMessage(commandQueue->getId(), &message, 0);
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-01-17 15:58:27 +01:00
|
|
|
sif::debug << "HeaterHandler::sendSwitchCommand: Failed to send switch"
|
|
|
|
<< "message" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2022-03-30 12:22:49 +02:00
|
|
|
return result;
|
2021-01-23 17:22:40 +01:00
|
|
|
}
|
|
|
|
|
2022-05-12 20:44:36 +02:00
|
|
|
void HeaterHandler::handleSwitchHandling() {
|
2022-05-02 22:55:39 +02:00
|
|
|
for (uint8_t idx = 0; idx < heater::NUMBER_OF_SWITCHES; idx++) {
|
2022-05-12 20:44:36 +02:00
|
|
|
auto health = heaterVec[idx].healthDevice->getHealth();
|
|
|
|
if (heaterVec[idx].switchState == SwitchState::ON) {
|
|
|
|
// If a heater is still on but the device was marked faulty by the operator, the SW
|
|
|
|
// will shut down the heater
|
|
|
|
if (health == HasHealthIF::FAULTY or health == HasHealthIF::PERMANENT_FAULTY) {
|
|
|
|
heaterVec[idx].cmdActive = true;
|
|
|
|
heaterVec[idx].action = SET_SWITCH_OFF;
|
|
|
|
triggerEvent(FAULTY_HEATER_WAS_ON, idx, 0);
|
2023-04-06 12:13:24 +02:00
|
|
|
handleSwitchOffCommand(static_cast<heater::Switch>(idx));
|
2022-05-12 20:44:36 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (heaterVec[idx].cmdActive) {
|
2022-05-02 22:45:27 +02:00
|
|
|
switch (heaterVec[idx].action) {
|
2022-01-17 15:58:27 +01:00
|
|
|
case SET_SWITCH_ON:
|
2023-04-06 12:13:24 +02:00
|
|
|
handleSwitchOnCommand(static_cast<heater::Switch>(idx));
|
2022-01-17 15:58:27 +01:00
|
|
|
break;
|
|
|
|
case SET_SWITCH_OFF:
|
2023-04-06 12:13:24 +02:00
|
|
|
handleSwitchOffCommand(static_cast<heater::Switch>(idx));
|
2022-01-17 15:58:27 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sif::error << "HeaterHandler::handleActiveCommands: Invalid action commanded"
|
|
|
|
<< std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-23 17:22:40 +01:00
|
|
|
}
|
|
|
|
|
2023-04-06 12:13:24 +02:00
|
|
|
void HeaterHandler::handleSwitchOnCommand(heater::Switch heaterIdx) {
|
2022-08-24 17:27:47 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
2022-05-02 22:45:27 +02:00
|
|
|
auto& heater = heaterVec.at(heaterIdx);
|
2023-02-17 11:50:14 +01:00
|
|
|
if (waitForSwitchOff) {
|
|
|
|
waitForSwitchOff = false;
|
|
|
|
}
|
2022-01-17 15:58:27 +01:00
|
|
|
/* Check if command waits for main switch being set on and whether the timeout has expired */
|
2022-05-02 22:45:27 +02:00
|
|
|
if (heater.waitMainSwitchOn && heater.mainSwitchCountdown.hasTimedOut()) {
|
2022-01-17 15:58:27 +01:00
|
|
|
// TODO - This requires the initiation of an FDIR procedure
|
|
|
|
triggerEvent(MAIN_SWITCH_TIMEOUT);
|
|
|
|
sif::error << "HeaterHandler::handleSwitchOnCommand: Main switch setting on timeout"
|
|
|
|
<< std::endl;
|
2022-05-12 20:44:36 +02:00
|
|
|
heater.cmdActive = false;
|
2022-05-02 22:45:27 +02:00
|
|
|
heater.waitMainSwitchOn = false;
|
|
|
|
if (heater.replyQueue != commandQueue->getId()) {
|
|
|
|
actionHelper.finish(false, heater.replyQueue, heater.action, MAIN_SWITCH_SET_TIMEOUT);
|
2021-02-11 08:19:48 +01:00
|
|
|
}
|
2022-01-17 15:58:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-02 22:45:27 +02:00
|
|
|
// Check state of main line switch
|
2022-01-17 15:58:27 +01:00
|
|
|
ReturnValue_t mainSwitchState = mainLineSwitcher->getSwitchState(mainLineSwitch);
|
|
|
|
if (mainSwitchState == PowerSwitchIF::SWITCH_ON) {
|
2023-04-03 14:09:54 +02:00
|
|
|
if (getSwitchState(heaterIdx) == SwitchState::OFF) {
|
2022-05-02 22:45:27 +02:00
|
|
|
gpioId_t gpioId = heater.gpioId;
|
2022-01-17 15:58:27 +01:00
|
|
|
result = gpioInterface->pullHigh(gpioId);
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-01-17 15:58:27 +01:00
|
|
|
sif::error << "HeaterHandler::handleSwitchOnCommand: Failed to pull gpio with id " << gpioId
|
|
|
|
<< " high" << std::endl;
|
|
|
|
triggerEvent(GPIO_PULL_HIGH_FAILED, result);
|
|
|
|
} else {
|
2022-05-04 11:11:29 +02:00
|
|
|
triggerEvent(HEATER_WENT_ON, heaterIdx, 0);
|
2023-02-21 01:53:23 +01:00
|
|
|
{
|
2023-03-02 15:32:12 +01:00
|
|
|
MutexGuard mg(handlerLock, LOCK_TYPE, LOCK_TIMEOUT, LOCK_CTX);
|
2023-02-21 01:53:23 +01:00
|
|
|
heater.switchState = ON;
|
|
|
|
}
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-05-02 22:45:27 +02:00
|
|
|
triggerEvent(SWITCH_ALREADY_ON, heaterIdx);
|
2021-02-01 11:17:20 +01:00
|
|
|
}
|
2023-02-17 13:11:16 +01:00
|
|
|
mode = HasModesIF::MODE_ON;
|
|
|
|
modeHelper.modeChanged(mode, submode);
|
2022-05-02 22:45:27 +02:00
|
|
|
// There is no need to send action finish replies if the sender was the
|
|
|
|
// HeaterHandler itself
|
|
|
|
if (heater.replyQueue != commandQueue->getId()) {
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result == returnvalue::OK) {
|
2022-05-02 22:45:27 +02:00
|
|
|
actionHelper.finish(true, heater.replyQueue, heater.action, result);
|
2022-01-17 15:58:27 +01:00
|
|
|
} else {
|
2022-05-02 22:45:27 +02:00
|
|
|
actionHelper.finish(false, heater.replyQueue, heater.action, result);
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
2021-02-01 11:17:20 +01:00
|
|
|
}
|
2022-05-12 20:44:36 +02:00
|
|
|
heater.cmdActive = false;
|
2022-05-02 22:45:27 +02:00
|
|
|
heater.waitMainSwitchOn = false;
|
|
|
|
} else if (mainSwitchState == PowerSwitchIF::SWITCH_OFF && heater.waitMainSwitchOn) {
|
|
|
|
// Just waiting for the main switch being set on
|
2022-01-17 15:58:27 +01:00
|
|
|
return;
|
2023-04-03 14:38:00 +02:00
|
|
|
} else if (mainSwitchState == PowerSwitchIF::SWITCH_OFF or
|
2023-04-03 15:16:53 +02:00
|
|
|
mainSwitchState == PowerSwitchIF::SWITCH_UNKNOWN) {
|
2022-01-17 15:58:27 +01:00
|
|
|
mainLineSwitcher->sendSwitchCommand(mainLineSwitch, PowerSwitchIF::SWITCH_ON);
|
2022-05-02 22:45:27 +02:00
|
|
|
heater.mainSwitchCountdown.setTimeout(mainLineSwitcher->getSwitchDelayMs());
|
|
|
|
heater.waitMainSwitchOn = true;
|
2022-01-17 15:58:27 +01:00
|
|
|
} else {
|
2023-02-22 19:14:35 +01:00
|
|
|
sif::debug << "HeaterHandler::handleSwitchOnCommand: Failed to get state of"
|
2022-01-17 15:58:27 +01:00
|
|
|
<< " main line switch" << std::endl;
|
2022-05-02 22:45:27 +02:00
|
|
|
if (heater.replyQueue != commandQueue->getId()) {
|
|
|
|
actionHelper.finish(false, heater.replyQueue, heater.action, mainSwitchState);
|
2021-02-01 11:17:20 +01:00
|
|
|
}
|
2022-05-12 20:44:36 +02:00
|
|
|
heater.cmdActive = false;
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
2021-02-01 11:17:20 +01:00
|
|
|
}
|
|
|
|
|
2023-04-06 12:13:24 +02:00
|
|
|
void HeaterHandler::handleSwitchOffCommand(heater::Switch heaterIdx) {
|
2022-08-24 17:27:47 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
2022-05-02 22:45:27 +02:00
|
|
|
auto& heater = heaterVec.at(heaterIdx);
|
|
|
|
// Check whether switch is already off
|
2023-04-03 14:09:54 +02:00
|
|
|
if (getSwitchState(heaterIdx)) {
|
2022-05-02 22:45:27 +02:00
|
|
|
gpioId_t gpioId = heater.gpioId;
|
2022-01-17 15:58:27 +01:00
|
|
|
result = gpioInterface->pullLow(gpioId);
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-01-17 15:58:27 +01:00
|
|
|
sif::error << "HeaterHandler::handleSwitchOffCommand: Failed to pull gpio with id" << gpioId
|
|
|
|
<< " low" << std::endl;
|
|
|
|
triggerEvent(GPIO_PULL_LOW_FAILED, result);
|
|
|
|
} else {
|
2023-02-21 01:53:23 +01:00
|
|
|
{
|
2023-03-02 15:32:12 +01:00
|
|
|
MutexGuard mg(handlerLock, LOCK_TYPE, LOCK_TIMEOUT, LOCK_CTX);
|
2023-02-21 01:53:23 +01:00
|
|
|
heater.switchState = OFF;
|
2022-05-04 11:11:29 +02:00
|
|
|
}
|
|
|
|
triggerEvent(HEATER_WENT_OFF, heaterIdx, 0);
|
2022-05-02 22:45:27 +02:00
|
|
|
// When all switches are off, also main line switch will be turned off
|
2022-01-17 15:58:27 +01:00
|
|
|
if (allSwitchesOff()) {
|
|
|
|
mainLineSwitcher->sendSwitchCommand(mainLineSwitch, PowerSwitchIF::SWITCH_OFF);
|
2023-02-17 11:50:14 +01:00
|
|
|
waitForSwitchOff = true;
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
2021-02-01 11:17:20 +01:00
|
|
|
}
|
2022-01-17 15:58:27 +01:00
|
|
|
} else {
|
|
|
|
sif::info << "HeaterHandler::handleSwitchOffCommand: Switch already off" << std::endl;
|
2022-05-02 22:45:27 +02:00
|
|
|
triggerEvent(SWITCH_ALREADY_OFF, heaterIdx);
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
2022-05-02 22:45:27 +02:00
|
|
|
if (heater.replyQueue != NO_COMMANDER) {
|
|
|
|
// Report back switch command reply if necessary
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result == returnvalue::OK) {
|
2022-05-02 22:45:27 +02:00
|
|
|
actionHelper.finish(true, heater.replyQueue, heater.action, result);
|
2022-01-17 15:58:27 +01:00
|
|
|
} else {
|
2022-05-02 22:45:27 +02:00
|
|
|
actionHelper.finish(false, heater.replyQueue, heater.action, result);
|
2021-02-01 11:17:20 +01:00
|
|
|
}
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
2022-05-12 20:44:36 +02:00
|
|
|
heater.cmdActive = false;
|
2021-02-01 11:17:20 +01:00
|
|
|
}
|
|
|
|
|
2023-04-06 12:13:24 +02:00
|
|
|
HeaterHandler::SwitchState HeaterHandler::getSwitchState(heater::Switch switchNr) const {
|
2023-03-02 15:32:12 +01:00
|
|
|
MutexGuard mg(handlerLock, LOCK_TYPE, LOCK_TIMEOUT, LOCK_CTX);
|
2022-05-02 22:45:27 +02:00
|
|
|
return heaterVec.at(switchNr).switchState;
|
|
|
|
}
|
2021-01-28 14:55:21 +01:00
|
|
|
|
2023-04-06 12:13:24 +02:00
|
|
|
ReturnValue_t HeaterHandler::switchHeater(heater::Switch heater, SwitchState switchState) {
|
2022-11-28 08:56:36 +01:00
|
|
|
if (switchState == SwitchState::ON) {
|
|
|
|
return sendSwitchCommand(heater, PowerSwitchIF::SWITCH_ON);
|
|
|
|
} else if (switchState == SwitchState::OFF) {
|
|
|
|
return sendSwitchCommand(heater, PowerSwitchIF::SWITCH_OFF);
|
|
|
|
}
|
|
|
|
return returnvalue::FAILED;
|
2022-11-16 16:36:59 +01:00
|
|
|
}
|
|
|
|
|
2023-02-17 11:50:14 +01:00
|
|
|
void HeaterHandler::announceMode(bool recursive) {
|
|
|
|
triggerEvent(MODE_INFO, mode, submode);
|
2023-02-21 01:53:23 +01:00
|
|
|
|
2023-02-21 02:28:57 +01:00
|
|
|
std::array<SwitchState, 8> states;
|
|
|
|
getAllSwitchStates(states);
|
2023-02-17 11:50:14 +01:00
|
|
|
for (unsigned idx = 0; idx < helper.heaters.size(); idx++) {
|
2023-02-21 01:53:23 +01:00
|
|
|
if (states[idx] == ON) {
|
2023-02-17 11:50:14 +01:00
|
|
|
EventManagerIF::triggerEvent(helper.heaters[idx].first->getObjectId(), MODE_INFO, MODE_ON, 0);
|
|
|
|
} else {
|
|
|
|
EventManagerIF::triggerEvent(helper.heaters[idx].first->getObjectId(), MODE_INFO, MODE_OFF,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HeaterHandler::getMode(Mode_t* mode, Submode_t* submode) {
|
|
|
|
if (!mode || !submode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*mode = this->mode;
|
|
|
|
*submode = this->submode;
|
|
|
|
}
|
|
|
|
|
|
|
|
const HasHealthIF* HeaterHandler::getOptHealthIF() const { return nullptr; }
|
|
|
|
|
|
|
|
const HasModesIF& HeaterHandler::getModeIF() const { return *this; }
|
|
|
|
|
|
|
|
ReturnValue_t HeaterHandler::connectModeTreeParent(HasModeTreeChildrenIF& parent) {
|
|
|
|
return modetree::connectModeTreeParent(parent, *this, nullptr, modeHelper);
|
|
|
|
}
|
|
|
|
|
|
|
|
ModeTreeChildIF& HeaterHandler::getModeTreeChildIF() { return *this; }
|
|
|
|
|
|
|
|
object_id_t HeaterHandler::getObjectId() const { return SystemObject::getObjectId(); }
|
|
|
|
|
2023-02-21 02:28:57 +01:00
|
|
|
ReturnValue_t HeaterHandler::getAllSwitchStates(std::array<SwitchState, 8>& statesBuf) {
|
|
|
|
{
|
2023-03-02 15:32:12 +01:00
|
|
|
MutexGuard mg(handlerLock, LOCK_TYPE, LOCK_TIMEOUT, LOCK_CTX);
|
2023-02-21 02:28:57 +01:00
|
|
|
if (mg.getLockResult() != returnvalue::OK) {
|
|
|
|
return returnvalue::FAILED;
|
|
|
|
}
|
|
|
|
for (unsigned idx = 0; idx < helper.heaters.size(); idx++) {
|
|
|
|
statesBuf[idx] = heaterVec[idx].switchState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
|
|
|
|
2021-02-11 08:19:48 +01:00
|
|
|
bool HeaterHandler::allSwitchesOff() {
|
2022-01-17 15:58:27 +01:00
|
|
|
bool allSwitchesOrd = false;
|
2023-03-02 15:32:12 +01:00
|
|
|
MutexGuard mg(handlerLock, LOCK_TYPE, LOCK_TIMEOUT, LOCK_CTX);
|
2022-01-17 15:58:27 +01:00
|
|
|
/* Or all switches. As soon one switch is on, allSwitchesOrd will be true */
|
2022-05-02 22:55:39 +02:00
|
|
|
for (power::Switch_t switchNr = 0; switchNr < heater::NUMBER_OF_SWITCHES; switchNr++) {
|
2022-05-02 22:45:27 +02:00
|
|
|
allSwitchesOrd = allSwitchesOrd || heaterVec.at(switchNr).switchState;
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
|
|
|
return !allSwitchesOrd;
|
2021-02-11 08:19:48 +01:00
|
|
|
}
|
|
|
|
|
2022-01-17 15:58:27 +01:00
|
|
|
MessageQueueId_t HeaterHandler::getCommandQueue() const { return commandQueue->getId(); }
|
2021-02-01 11:17:20 +01:00
|
|
|
|
2022-08-24 17:27:47 +02:00
|
|
|
ReturnValue_t HeaterHandler::sendFuseOnCommand(uint8_t fuseNr) { return returnvalue::OK; }
|
2021-02-01 11:17:20 +01:00
|
|
|
|
2022-05-04 11:11:29 +02:00
|
|
|
ReturnValue_t HeaterHandler::getSwitchState(uint8_t switchNr) const {
|
|
|
|
ReturnValue_t mainSwitchState = mainLineSwitcher->getSwitchState(mainLineSwitch);
|
|
|
|
if (mainSwitchState == PowerSwitchIF::SWITCH_OFF) {
|
|
|
|
return PowerSwitchIF::SWITCH_OFF;
|
|
|
|
}
|
|
|
|
if (switchNr > 7) {
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-05-04 11:11:29 +02:00
|
|
|
}
|
2023-04-06 12:13:24 +02:00
|
|
|
if (getSwitchState(static_cast<heater::Switch>(switchNr)) == SwitchState::ON) {
|
2022-05-04 11:11:29 +02:00
|
|
|
return PowerSwitchIF::SWITCH_ON;
|
|
|
|
}
|
|
|
|
return PowerSwitchIF::SWITCH_OFF;
|
|
|
|
}
|
2021-02-01 11:17:20 +01:00
|
|
|
|
2022-01-17 15:58:27 +01:00
|
|
|
ReturnValue_t HeaterHandler::getFuseState(uint8_t fuseNr) const { return 0; }
|
2021-02-01 11:17:20 +01:00
|
|
|
|
2022-05-02 17:37:00 +02:00
|
|
|
uint32_t HeaterHandler::getSwitchDelayMs(void) const { return 2000; }
|
2022-12-13 10:19:28 +01:00
|
|
|
|
2023-04-06 12:13:24 +02:00
|
|
|
HasHealthIF::HealthState HeaterHandler::getHealth(heater::Switch heater) {
|
2022-12-13 10:19:28 +01:00
|
|
|
auto* healthDev = heaterVec.at(heater).healthDevice;
|
|
|
|
if (healthDev != nullptr) {
|
2023-03-02 15:32:12 +01:00
|
|
|
MutexGuard mg(handlerLock, LOCK_TYPE, LOCK_TIMEOUT, LOCK_CTX);
|
2022-12-13 10:19:28 +01:00
|
|
|
return healthDev->getHealth();
|
|
|
|
}
|
|
|
|
return HasHealthIF::HealthState::FAULTY;
|
|
|
|
}
|