Skip to content

Commit

Permalink
Code Refactoring: Use internal inverter instance in handleResponse me…
Browse files Browse the repository at this point in the history
…thod
  • Loading branch information
tbnobody committed May 16, 2024
1 parent 6d6d62b commit 90711dd
Show file tree
Hide file tree
Showing 26 changed files with 76 additions and 76 deletions.
14 changes: 7 additions & 7 deletions lib/Hoymiles/src/commands/ActivePowerControlCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,24 @@ void ActivePowerControlCommand::setActivePowerLimit(const float limit, const Pow
udpateCRC(CRC_SIZE);
}

bool ActivePowerControlCommand::handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id)
bool ActivePowerControlCommand::handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id)
{
if (!DevControlCommand::handleResponse(inverter, fragment, max_fragment_id)) {
if (!DevControlCommand::handleResponse(fragment, max_fragment_id)) {
return false;
}

if ((getType() == PowerLimitControlType::RelativNonPersistent) || (getType() == PowerLimitControlType::RelativPersistent)) {
inverter.SystemConfigPara()->setLimitPercent(getLimit());
_inv->SystemConfigPara()->setLimitPercent(getLimit());
} else {
const uint16_t max_power = inverter.DevInfo()->getMaxPower();
const uint16_t max_power = _inv->DevInfo()->getMaxPower();
if (max_power > 0) {
inverter.SystemConfigPara()->setLimitPercent(static_cast<float>(getLimit()) / max_power * 100);
_inv->SystemConfigPara()->setLimitPercent(static_cast<float>(getLimit()) / max_power * 100);
} else {
// TODO(tbnobody): Not implemented yet because we only can publish the percentage value
}
}
inverter.SystemConfigPara()->setLastUpdateCommand(millis());
inverter.SystemConfigPara()->setLastLimitCommandSuccess(CMD_OK);
_inv->SystemConfigPara()->setLastUpdateCommand(millis());
_inv->SystemConfigPara()->setLastLimitCommandSuccess(CMD_OK);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/commands/ActivePowerControlCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ActivePowerControlCommand : public DevControlCommand {

virtual String getCommandName() const;

virtual bool handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id);
virtual bool handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id);
virtual void gotTimeout();

void setActivePowerLimit(const float limit, const PowerLimitControlType type = RelativNonPersistent);
Expand Down
16 changes: 8 additions & 8 deletions lib/Hoymiles/src/commands/AlarmDataCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,24 @@ String AlarmDataCommand::getCommandName() const
return "AlarmData";
}

bool AlarmDataCommand::handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id)
bool AlarmDataCommand::handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id)
{
// Check CRC of whole payload
if (!MultiDataCommand::handleResponse(inverter, fragment, max_fragment_id)) {
if (!MultiDataCommand::handleResponse(fragment, max_fragment_id)) {
return false;
}

// Move all fragments into target buffer
uint8_t offs = 0;
inverter.EventLog()->beginAppendFragment();
inverter.EventLog()->clearBuffer();
_inv->EventLog()->beginAppendFragment();
_inv->EventLog()->clearBuffer();
for (uint8_t i = 0; i < max_fragment_id; i++) {
inverter.EventLog()->appendFragment(offs, fragment[i].fragment, fragment[i].len);
_inv->EventLog()->appendFragment(offs, fragment[i].fragment, fragment[i].len);
offs += (fragment[i].len);
}
inverter.EventLog()->endAppendFragment();
inverter.EventLog()->setLastAlarmRequestSuccess(CMD_OK);
inverter.EventLog()->setLastUpdate(millis());
_inv->EventLog()->endAppendFragment();
_inv->EventLog()->setLastAlarmRequestSuccess(CMD_OK);
_inv->EventLog()->setLastUpdate(millis());
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/commands/AlarmDataCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class AlarmDataCommand : public MultiDataCommand {

virtual String getCommandName() const;

virtual bool handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id);
virtual bool handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id);
virtual void gotTimeout();
};
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/commands/ChannelChangeCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void ChannelChangeCommand::setCountryMode(const CountryModeId_t mode)
}
}

bool ChannelChangeCommand::handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id)
bool ChannelChangeCommand::handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id)
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/commands/ChannelChangeCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ChannelChangeCommand : public CommandAbstract {

void setCountryMode(const CountryModeId_t mode);

virtual bool handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id);
virtual bool handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id);

virtual uint8_t getMaxResendCount();
};
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/commands/CommandAbstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CommandAbstract {

virtual CommandAbstract* getRequestFrameCommand(const uint8_t frame_no);

virtual bool handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id) = 0;
virtual bool handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id) = 0;
virtual void gotTimeout();

// Sets the amount how often the specific command is resent if all fragments where missing
Expand Down
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/commands/DevControlCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void DevControlCommand::udpateCRC(const uint8_t len)
_payload[10 + len + 1] = (uint8_t)(crc);
}

bool DevControlCommand::handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id)
bool DevControlCommand::handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id)
{
for (uint8_t i = 0; i < max_fragment_id; i++) {
if (fragment[i].mainCmd != (_payload[0] | 0x80)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/commands/DevControlCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class DevControlCommand : public CommandAbstract {
public:
explicit DevControlCommand(InverterAbstract* inv, const uint64_t router_address = 0);

virtual bool handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id);
virtual bool handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id);

protected:
void udpateCRC(const uint8_t len);
Expand Down
14 changes: 7 additions & 7 deletions lib/Hoymiles/src/commands/DevInfoAllCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ String DevInfoAllCommand::getCommandName() const
return "DevInfoAll";
}

bool DevInfoAllCommand::handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id)
bool DevInfoAllCommand::handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id)
{
// Check CRC of whole payload
if (!MultiDataCommand::handleResponse(inverter, fragment, max_fragment_id)) {
if (!MultiDataCommand::handleResponse(fragment, max_fragment_id)) {
return false;
}

// Move all fragments into target buffer
uint8_t offs = 0;
inverter.DevInfo()->beginAppendFragment();
inverter.DevInfo()->clearBufferAll();
_inv->DevInfo()->beginAppendFragment();
_inv->DevInfo()->clearBufferAll();
for (uint8_t i = 0; i < max_fragment_id; i++) {
inverter.DevInfo()->appendFragmentAll(offs, fragment[i].fragment, fragment[i].len);
_inv->DevInfo()->appendFragmentAll(offs, fragment[i].fragment, fragment[i].len);
offs += (fragment[i].len);
}
inverter.DevInfo()->endAppendFragment();
inverter.DevInfo()->setLastUpdateAll(millis());
_inv->DevInfo()->endAppendFragment();
_inv->DevInfo()->setLastUpdateAll(millis());
return true;
}
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/commands/DevInfoAllCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class DevInfoAllCommand : public MultiDataCommand {

virtual String getCommandName() const;

virtual bool handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id);
virtual bool handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id);
};
14 changes: 7 additions & 7 deletions lib/Hoymiles/src/commands/DevInfoSimpleCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ String DevInfoSimpleCommand::getCommandName() const
return "DevInfoSimple";
}

bool DevInfoSimpleCommand::handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id)
bool DevInfoSimpleCommand::handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id)
{
// Check CRC of whole payload
if (!MultiDataCommand::handleResponse(inverter, fragment, max_fragment_id)) {
if (!MultiDataCommand::handleResponse(fragment, max_fragment_id)) {
return false;
}

// Move all fragments into target buffer
uint8_t offs = 0;
inverter.DevInfo()->beginAppendFragment();
inverter.DevInfo()->clearBufferSimple();
_inv->DevInfo()->beginAppendFragment();
_inv->DevInfo()->clearBufferSimple();
for (uint8_t i = 0; i < max_fragment_id; i++) {
inverter.DevInfo()->appendFragmentSimple(offs, fragment[i].fragment, fragment[i].len);
_inv->DevInfo()->appendFragmentSimple(offs, fragment[i].fragment, fragment[i].len);
offs += (fragment[i].len);
}
inverter.DevInfo()->endAppendFragment();
inverter.DevInfo()->setLastUpdateSimple(millis());
_inv->DevInfo()->endAppendFragment();
_inv->DevInfo()->setLastUpdateSimple(millis());
return true;
}
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/commands/DevInfoSimpleCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class DevInfoSimpleCommand : public MultiDataCommand {

virtual String getCommandName() const;

virtual bool handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id);
virtual bool handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id);
};
14 changes: 7 additions & 7 deletions lib/Hoymiles/src/commands/GridOnProFilePara.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ String GridOnProFilePara::getCommandName() const
return "GridOnProFilePara";
}

bool GridOnProFilePara::handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id)
bool GridOnProFilePara::handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id)
{
// Check CRC of whole payload
if (!MultiDataCommand::handleResponse(inverter, fragment, max_fragment_id)) {
if (!MultiDataCommand::handleResponse(fragment, max_fragment_id)) {
return false;
}

// Move all fragments into target buffer
uint8_t offs = 0;
inverter.GridProfile()->beginAppendFragment();
inverter.GridProfile()->clearBuffer();
_inv->GridProfile()->beginAppendFragment();
_inv->GridProfile()->clearBuffer();
for (uint8_t i = 0; i < max_fragment_id; i++) {
inverter.GridProfile()->appendFragment(offs, fragment[i].fragment, fragment[i].len);
_inv->GridProfile()->appendFragment(offs, fragment[i].fragment, fragment[i].len);
offs += (fragment[i].len);
}
inverter.GridProfile()->endAppendFragment();
inverter.GridProfile()->setLastUpdate(millis());
_inv->GridProfile()->endAppendFragment();
_inv->GridProfile()->setLastUpdate(millis());
return true;
}
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/commands/GridOnProFilePara.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class GridOnProFilePara : public MultiDataCommand {

virtual String getCommandName() const;

virtual bool handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id);
virtual bool handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id);
};
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/commands/MultiDataCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ CommandAbstract* MultiDataCommand::getRequestFrameCommand(const uint8_t frame_no
return &_cmdRequestFrame;
}

bool MultiDataCommand::handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id)
bool MultiDataCommand::handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id)
{
// All fragments are available --> Check CRC
uint16_t crc = 0xffff, crcRcv = 0;
Expand Down
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/commands/MultiDataCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MultiDataCommand : public CommandAbstract {

CommandAbstract* getRequestFrameCommand(const uint8_t frame_no);

virtual bool handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id);
virtual bool handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id);

protected:
void setDataType(const uint8_t data_type);
Expand Down
8 changes: 4 additions & 4 deletions lib/Hoymiles/src/commands/PowerControlCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ String PowerControlCommand::getCommandName() const
return "PowerControl";
}

bool PowerControlCommand::handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id)
bool PowerControlCommand::handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id)
{
if (!DevControlCommand::handleResponse(inverter, fragment, max_fragment_id)) {
if (!DevControlCommand::handleResponse(fragment, max_fragment_id)) {
return false;
}

inverter.PowerCommand()->setLastUpdateCommand(millis());
inverter.PowerCommand()->setLastPowerCommandSuccess(CMD_OK);
_inv->PowerCommand()->setLastUpdateCommand(millis());
_inv->PowerCommand()->setLastPowerCommandSuccess(CMD_OK);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/commands/PowerControlCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PowerControlCommand : public DevControlCommand {

virtual String getCommandName() const;

virtual bool handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id);
virtual bool handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id);
virtual void gotTimeout();

void setPowerOn(const bool state);
Expand Down
18 changes: 9 additions & 9 deletions lib/Hoymiles/src/commands/RealTimeRunDataCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ String RealTimeRunDataCommand::getCommandName() const
return "RealTimeRunData";
}

bool RealTimeRunDataCommand::handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id)
bool RealTimeRunDataCommand::handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id)
{
// Check CRC of whole payload
if (!MultiDataCommand::handleResponse(inverter, fragment, max_fragment_id)) {
if (!MultiDataCommand::handleResponse(fragment, max_fragment_id)) {
return false;
}

// Check if at least all required bytes are received
// In case of low power in the inverter it occours that some incomplete fragments
// with a valid CRC are received.
const uint8_t fragmentsSize = getTotalFragmentSize(fragment, max_fragment_id);
const uint8_t expectedSize = inverter.Statistics()->getExpectedByteCount();
const uint8_t expectedSize = _inv->Statistics()->getExpectedByteCount();
if (fragmentsSize < expectedSize) {
Hoymiles.getMessageOutput()->printf("ERROR in %s: Received fragment size: %d, min expected size: %d\r\n",
getCommandName().c_str(), fragmentsSize, expectedSize);
Expand All @@ -56,15 +56,15 @@ bool RealTimeRunDataCommand::handleResponse(InverterAbstract& inverter, const fr

// Move all fragments into target buffer
uint8_t offs = 0;
inverter.Statistics()->beginAppendFragment();
inverter.Statistics()->clearBuffer();
_inv->Statistics()->beginAppendFragment();
_inv->Statistics()->clearBuffer();
for (uint8_t i = 0; i < max_fragment_id; i++) {
inverter.Statistics()->appendFragment(offs, fragment[i].fragment, fragment[i].len);
_inv->Statistics()->appendFragment(offs, fragment[i].fragment, fragment[i].len);
offs += (fragment[i].len);
}
inverter.Statistics()->endAppendFragment();
inverter.Statistics()->resetRxFailureCount();
inverter.Statistics()->setLastUpdate(millis());
_inv->Statistics()->endAppendFragment();
_inv->Statistics()->resetRxFailureCount();
_inv->Statistics()->setLastUpdate(millis());
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/commands/RealTimeRunDataCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class RealTimeRunDataCommand : public MultiDataCommand {

virtual String getCommandName() const;

virtual bool handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id);
virtual bool handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id);
virtual void gotTimeout();
};
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/commands/RequestFrameCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ uint8_t RequestFrameCommand::getFrameNo() const
return _payload[9] & (~0x80);
}

bool RequestFrameCommand::handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id)
bool RequestFrameCommand::handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id)
{
return true;
}
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/commands/RequestFrameCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ class RequestFrameCommand : public SingleDataCommand {
void setFrameNo(const uint8_t frame_no);
uint8_t getFrameNo() const;

virtual bool handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id);
virtual bool handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id);
};
18 changes: 9 additions & 9 deletions lib/Hoymiles/src/commands/SystemConfigParaCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ String SystemConfigParaCommand::getCommandName() const
return "SystemConfigPara";
}

bool SystemConfigParaCommand::handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id)
bool SystemConfigParaCommand::handleResponse(const fragment_t fragment[], const uint8_t max_fragment_id)
{
// Check CRC of whole payload
if (!MultiDataCommand::handleResponse(inverter, fragment, max_fragment_id)) {
if (!MultiDataCommand::handleResponse(fragment, max_fragment_id)) {
return false;
}

// Check if at least all required bytes are received
// In case of low power in the inverter it occours that some incomplete fragments
// with a valid CRC are received.
const uint8_t fragmentsSize = getTotalFragmentSize(fragment, max_fragment_id);
const uint8_t expectedSize = inverter.SystemConfigPara()->getExpectedByteCount();
const uint8_t expectedSize = _inv->SystemConfigPara()->getExpectedByteCount();
if (fragmentsSize < expectedSize) {
Hoymiles.getMessageOutput()->printf("ERROR in %s: Received fragment size: %d, min expected size: %d\r\n",
getCommandName().c_str(), fragmentsSize, expectedSize);
Expand All @@ -56,15 +56,15 @@ bool SystemConfigParaCommand::handleResponse(InverterAbstract& inverter, const f

// Move all fragments into target buffer
uint8_t offs = 0;
inverter.SystemConfigPara()->beginAppendFragment();
inverter.SystemConfigPara()->clearBuffer();
_inv->SystemConfigPara()->beginAppendFragment();
_inv->SystemConfigPara()->clearBuffer();
for (uint8_t i = 0; i < max_fragment_id; i++) {
inverter.SystemConfigPara()->appendFragment(offs, fragment[i].fragment, fragment[i].len);
_inv->SystemConfigPara()->appendFragment(offs, fragment[i].fragment, fragment[i].len);
offs += (fragment[i].len);
}
inverter.SystemConfigPara()->endAppendFragment();
inverter.SystemConfigPara()->setLastUpdateRequest(millis());
inverter.SystemConfigPara()->setLastLimitRequestSuccess(CMD_OK);
_inv->SystemConfigPara()->endAppendFragment();
_inv->SystemConfigPara()->setLastUpdateRequest(millis());
_inv->SystemConfigPara()->setLastLimitRequestSuccess(CMD_OK);
return true;
}

Expand Down
Loading

0 comments on commit 90711dd

Please sign in to comment.