Skip to content

Commit

Permalink
Fix: Ensure that only completly assembled packets are put into the co…
Browse files Browse the repository at this point in the history
…mmand queue
  • Loading branch information
tbnobody committed Aug 2, 2023
1 parent 686112e commit be09c40
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
11 changes: 8 additions & 3 deletions lib/Hoymiles/src/HoymilesRadio.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ class HoymilesRadio {
bool isInitialized();

template <typename T>
T* enqueCommand()
void enqueCommand(std::shared_ptr<T> cmd)
{
_commandQueue.push(std::make_shared<T>());
return static_cast<T*>(_commandQueue.back().get());
_commandQueue.push(cmd);
}

template <typename T>
std::shared_ptr<T> prepareCommand()
{
return std::make_shared<T>();
}

protected:
Expand Down
3 changes: 2 additions & 1 deletion lib/Hoymiles/src/inverters/HMS_Abstract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ bool HMS_Abstract::sendChangeChannelRequest()
return false;
}

ChannelChangeCommand* cmdChannel = _radio->enqueCommand<ChannelChangeCommand>();
auto cmdChannel = _radio->prepareCommand<ChannelChangeCommand>();
cmdChannel->setChannel(HoymilesRadio_CMT::getChannelFromFrequency(Hoymiles.getRadioCmt()->getInverterTargetFrequency()));
cmdChannel->setTargetAddress(serial());
_radio->enqueCommand(cmdChannel);

return true;
};
3 changes: 2 additions & 1 deletion lib/Hoymiles/src/inverters/HMT_Abstract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ bool HMT_Abstract::sendChangeChannelRequest()
return false;
}

ChannelChangeCommand* cmdChannel = _radio->enqueCommand<ChannelChangeCommand>();
auto cmdChannel = _radio->prepareCommand<ChannelChangeCommand>();
cmdChannel->setChannel(HoymilesRadio_CMT::getChannelFromFrequency(Hoymiles.getRadioCmt()->getInverterTargetFrequency()));
cmdChannel->setTargetAddress(serial());
_radio->enqueCommand(cmdChannel);

return true;
};
24 changes: 16 additions & 8 deletions lib/Hoymiles/src/inverters/HM_Abstract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ bool HM_Abstract::sendStatsRequest()
time_t now;
time(&now);

RealTimeRunDataCommand* cmd = _radio->enqueCommand<RealTimeRunDataCommand>();
auto cmd = _radio->prepareCommand<RealTimeRunDataCommand>();
cmd->setTime(now);
cmd->setTargetAddress(serial());
_radio->enqueCommand(cmd);

return true;
}
Expand Down Expand Up @@ -60,9 +61,10 @@ bool HM_Abstract::sendAlarmLogRequest(bool force)
time_t now;
time(&now);

AlarmDataCommand* cmd = _radio->enqueCommand<AlarmDataCommand>();
auto cmd = _radio->prepareCommand<AlarmDataCommand>();
cmd->setTime(now);
cmd->setTargetAddress(serial());
_radio->enqueCommand(cmd);
EventLog()->setLastAlarmRequestSuccess(CMD_PENDING);

return true;
Expand All @@ -82,13 +84,15 @@ bool HM_Abstract::sendDevInfoRequest()
time_t now;
time(&now);

DevInfoAllCommand* cmdAll = _radio->enqueCommand<DevInfoAllCommand>();
auto cmdAll = _radio->prepareCommand<DevInfoAllCommand>();
cmdAll->setTime(now);
cmdAll->setTargetAddress(serial());
_radio->enqueCommand(cmdAll);

DevInfoSimpleCommand* cmdSimple = _radio->enqueCommand<DevInfoSimpleCommand>();
auto cmdSimple = _radio->prepareCommand<DevInfoSimpleCommand>();
cmdSimple->setTime(now);
cmdSimple->setTargetAddress(serial());
_radio->enqueCommand(cmdSimple);

return true;
}
Expand All @@ -107,9 +111,10 @@ bool HM_Abstract::sendSystemConfigParaRequest()
time_t now;
time(&now);

SystemConfigParaCommand* cmd = _radio->enqueCommand<SystemConfigParaCommand>();
auto cmd = _radio->prepareCommand<SystemConfigParaCommand>();
cmd->setTime(now);
cmd->setTargetAddress(serial());
_radio->enqueCommand(cmd);
SystemConfigPara()->setLastLimitRequestSuccess(CMD_PENDING);

return true;
Expand All @@ -128,9 +133,10 @@ bool HM_Abstract::sendActivePowerControlRequest(float limit, PowerLimitControlTy
_activePowerControlLimit = limit;
_activePowerControlType = type;

ActivePowerControlCommand* cmd = _radio->enqueCommand<ActivePowerControlCommand>();
auto cmd = _radio->prepareCommand<ActivePowerControlCommand>();
cmd->setActivePowerLimit(limit, type);
cmd->setTargetAddress(serial());
_radio->enqueCommand(cmd);
SystemConfigPara()->setLastLimitCommandSuccess(CMD_PENDING);

return true;
Expand All @@ -153,9 +159,10 @@ bool HM_Abstract::sendPowerControlRequest(bool turnOn)
_powerState = 0;
}

PowerControlCommand* cmd = _radio->enqueCommand<PowerControlCommand>();
auto cmd = _radio->prepareCommand<PowerControlCommand>();
cmd->setPowerOn(turnOn);
cmd->setTargetAddress(serial());
_radio->enqueCommand(cmd);
PowerCommand()->setLastPowerCommandSuccess(CMD_PENDING);

return true;
Expand All @@ -169,9 +176,10 @@ bool HM_Abstract::sendRestartControlRequest()

_powerState = 2;

PowerControlCommand* cmd = _radio->enqueCommand<PowerControlCommand>();
auto cmd = _radio->prepareCommand<PowerControlCommand>();
cmd->setRestart();
cmd->setTargetAddress(serial());
_radio->enqueCommand(cmd);
PowerCommand()->setLastPowerCommandSuccess(CMD_PENDING);

return true;
Expand Down

0 comments on commit be09c40

Please sign in to comment.