2022-12-21 14:11:31 +01:00
|
|
|
#include "Stack5VHandler.h"
|
|
|
|
|
2022-12-21 14:34:07 +01:00
|
|
|
Stack5VHandler::Stack5VHandler(PowerSwitchIF& switcher) : switcher(switcher) {
|
|
|
|
stackLock = MutexFactory::instance()->createMutex();
|
|
|
|
}
|
2022-12-21 14:11:31 +01:00
|
|
|
|
2023-01-10 11:13:34 +01:00
|
|
|
ReturnValue_t Stack5VHandler::deviceToOn(StackCommander commander, bool updateStates) {
|
2023-03-02 15:32:12 +01:00
|
|
|
MutexGuard mg(stackLock, LOCK_TYPE, LOCK_TIMEOUT, LOCK_CTX);
|
2023-01-10 11:13:34 +01:00
|
|
|
if (updateStates) {
|
|
|
|
updateInternalStates();
|
|
|
|
}
|
2023-01-10 15:04:49 +01:00
|
|
|
if (handlerState == HandlerState::SWITCH_PENDING) {
|
|
|
|
return BUSY;
|
|
|
|
}
|
2022-12-21 17:30:21 +01:00
|
|
|
if (switchIsOn) {
|
|
|
|
if (commander == StackCommander::PL_PCDU) {
|
|
|
|
plPcduIsOn = true;
|
|
|
|
} else {
|
|
|
|
radSensorIsOn = true;
|
|
|
|
}
|
2022-12-21 14:34:07 +01:00
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
2023-01-10 15:04:49 +01:00
|
|
|
|
2022-12-22 16:30:38 +01:00
|
|
|
handlerState = HandlerState::SWITCH_PENDING;
|
2023-01-10 15:04:49 +01:00
|
|
|
targetState = true;
|
2022-12-21 14:34:07 +01:00
|
|
|
return switcher.sendSwitchCommand(stackSwitch, PowerSwitchIF::SWITCH_ON);
|
|
|
|
}
|
2022-12-21 14:11:31 +01:00
|
|
|
|
2023-01-10 11:13:34 +01:00
|
|
|
ReturnValue_t Stack5VHandler::deviceToOff(StackCommander commander, bool updateStates) {
|
2023-03-02 15:32:12 +01:00
|
|
|
MutexGuard mg(stackLock, LOCK_TYPE, LOCK_TIMEOUT, LOCK_CTX);
|
2023-01-10 11:13:34 +01:00
|
|
|
if (updateStates) {
|
|
|
|
updateInternalStates();
|
|
|
|
}
|
2023-01-10 15:04:49 +01:00
|
|
|
// wait for our turn
|
|
|
|
if (handlerState == HandlerState::SWITCH_PENDING) {
|
|
|
|
return BUSY;
|
|
|
|
}
|
|
|
|
// If the switch is already off, we are done
|
2022-12-21 14:43:12 +01:00
|
|
|
if (not switchIsOn) {
|
2022-12-21 17:30:21 +01:00
|
|
|
if (commander == StackCommander::PL_PCDU) {
|
|
|
|
plPcduIsOn = false;
|
|
|
|
} else {
|
|
|
|
radSensorIsOn = false;
|
|
|
|
}
|
2022-12-21 14:34:07 +01:00
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
2023-01-10 15:04:49 +01:00
|
|
|
// If one device is still on, do not turn off the switch
|
2022-12-21 17:30:21 +01:00
|
|
|
if ((commander == StackCommander::PL_PCDU and radSensorIsOn) or
|
|
|
|
(commander == StackCommander::RAD_SENSOR and plPcduIsOn)) {
|
|
|
|
return returnvalue::OK;
|
2022-12-21 14:34:07 +01:00
|
|
|
}
|
2022-12-22 16:30:38 +01:00
|
|
|
handlerState = HandlerState::SWITCH_PENDING;
|
2023-01-10 15:04:49 +01:00
|
|
|
targetState = false;
|
2022-12-21 14:34:07 +01:00
|
|
|
return switcher.sendSwitchCommand(stackSwitch, PowerSwitchIF::SWITCH_OFF);
|
|
|
|
}
|
2022-12-21 14:11:31 +01:00
|
|
|
|
2022-12-21 14:34:07 +01:00
|
|
|
bool Stack5VHandler::isSwitchOn() {
|
2023-03-02 15:32:12 +01:00
|
|
|
MutexGuard mg(stackLock, LOCK_TYPE, LOCK_TIMEOUT, LOCK_CTX);
|
2023-01-10 11:13:34 +01:00
|
|
|
return updateInternalStates();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stack5VHandler::update() {
|
2023-03-02 15:32:12 +01:00
|
|
|
MutexGuard mg(stackLock, LOCK_TYPE, LOCK_TIMEOUT, LOCK_CTX);
|
2023-01-10 11:13:34 +01:00
|
|
|
updateInternalStates();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Stack5VHandler::updateInternalStates() {
|
2022-12-21 14:34:07 +01:00
|
|
|
if (switcher.getSwitchState(stackSwitch) == PowerSwitchIF::SWITCH_ON) {
|
2023-01-10 15:04:49 +01:00
|
|
|
if (handlerState == HandlerState::SWITCH_PENDING and targetState) {
|
|
|
|
handlerState = HandlerState::IDLE;
|
2022-12-21 14:43:12 +01:00
|
|
|
switchIsOn = true;
|
|
|
|
}
|
2022-12-21 14:34:07 +01:00
|
|
|
return true;
|
2023-01-10 15:04:49 +01:00
|
|
|
} else if (handlerState == HandlerState::SWITCH_PENDING and not targetState) {
|
|
|
|
handlerState = HandlerState::IDLE;
|
2023-01-10 11:13:34 +01:00
|
|
|
switchIsOn = false;
|
2023-01-10 15:04:49 +01:00
|
|
|
radSensorIsOn = false;
|
|
|
|
plPcduIsOn = false;
|
2022-12-21 14:34:07 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|