Skip to content

Commit

Permalink
Code Refactoring: Add inverter reference to each command
Browse files Browse the repository at this point in the history
Instead of just adding the target_address to a command this patch adds a reference to the whole inverter instance
  • Loading branch information
tbnobody committed May 16, 2024
1 parent 6358b1e commit 6a7bed0
Show file tree
Hide file tree
Showing 34 changed files with 105 additions and 111 deletions.
4 changes: 2 additions & 2 deletions lib/Hoymiles/src/HoymilesRadio.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class HoymilesRadio {
}

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

protected:
Expand Down
8 changes: 4 additions & 4 deletions lib/Hoymiles/src/commands/ActivePowerControlCommand.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022-2023 Thomas Basler and others
* Copyright (C) 2022-2024 Thomas Basler and others
*/

/*
Expand All @@ -25,8 +25,8 @@ ID Target Addr Source Addr Cmd SCmd ? Limit Type CRC16 CRC8

#define CRC_SIZE 6

ActivePowerControlCommand::ActivePowerControlCommand(const uint64_t target_address, const uint64_t router_address)
: DevControlCommand(target_address, router_address)
ActivePowerControlCommand::ActivePowerControlCommand(InverterAbstract* inv, const uint64_t router_address)
: DevControlCommand(inv, router_address)
{
_payload[10] = 0x0b;
_payload[11] = 0x00;
Expand Down Expand Up @@ -97,4 +97,4 @@ PowerLimitControlType ActivePowerControlCommand::getType()
void ActivePowerControlCommand::gotTimeout(InverterAbstract& inverter)
{
inverter.SystemConfigPara()->setLastLimitCommandSuccess(CMD_NOK);
}
}
4 changes: 2 additions & 2 deletions lib/Hoymiles/src/commands/ActivePowerControlCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ typedef enum { // ToDo: to be verified by field tests

class ActivePowerControlCommand : public DevControlCommand {
public:
explicit ActivePowerControlCommand(const uint64_t target_address = 0, const uint64_t router_address = 0);
explicit ActivePowerControlCommand(InverterAbstract* inv, const uint64_t router_address = 0);

virtual String getCommandName() const;

Expand All @@ -22,4 +22,4 @@ class ActivePowerControlCommand : public DevControlCommand {
void setActivePowerLimit(const float limit, const PowerLimitControlType type = RelativNonPersistent);
float getLimit() const;
PowerLimitControlType getType();
};
};
8 changes: 4 additions & 4 deletions lib/Hoymiles/src/commands/AlarmDataCommand.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022-2023 Thomas Basler and others
* Copyright (C) 2022-2024 Thomas Basler and others
*/

/*
Expand All @@ -23,8 +23,8 @@ ID Target Addr Source Addr Idx DT ? Time Gap AlarmId Pa
#include "AlarmDataCommand.h"
#include "inverters/InverterAbstract.h"

AlarmDataCommand::AlarmDataCommand(const uint64_t target_address, const uint64_t router_address, const time_t time)
: MultiDataCommand(target_address, router_address)
AlarmDataCommand::AlarmDataCommand(InverterAbstract* inv, const uint64_t router_address, const time_t time)
: MultiDataCommand(inv, router_address)
{
setTime(time);
setDataType(0x11);
Expand Down Expand Up @@ -60,4 +60,4 @@ bool AlarmDataCommand::handleResponse(InverterAbstract& inverter, const fragment
void AlarmDataCommand::gotTimeout(InverterAbstract& inverter)
{
inverter.EventLog()->setLastAlarmRequestSuccess(CMD_NOK);
}
}
4 changes: 2 additions & 2 deletions lib/Hoymiles/src/commands/AlarmDataCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

class AlarmDataCommand : public MultiDataCommand {
public:
explicit AlarmDataCommand(const uint64_t target_address = 0, const uint64_t router_address = 0, const time_t time = 0);
explicit AlarmDataCommand(InverterAbstract* inv, const uint64_t router_address = 0, const time_t time = 0);

virtual String getCommandName() const;

virtual bool handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id);
virtual void gotTimeout(InverterAbstract& inverter);
};
};
4 changes: 2 additions & 2 deletions lib/Hoymiles/src/commands/ChannelChangeCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ ID Target Addr Source Addr ? ? ? CH ? CRC8
*/
#include "ChannelChangeCommand.h"

ChannelChangeCommand::ChannelChangeCommand(const uint64_t target_address, const uint64_t router_address, const uint8_t channel)
: CommandAbstract(target_address, router_address)
ChannelChangeCommand::ChannelChangeCommand(InverterAbstract* inv, const uint64_t router_address, const uint8_t channel)
: CommandAbstract(inv, router_address)
{
_payload[0] = 0x56;
_payload[13] = 0x14;
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 @@ -6,7 +6,7 @@

class ChannelChangeCommand : public CommandAbstract {
public:
explicit ChannelChangeCommand(const uint64_t target_address = 0, const uint64_t router_address = 0, const uint8_t channel = 0);
explicit ChannelChangeCommand(InverterAbstract* inv, const uint64_t router_address = 0, const uint8_t channel = 0);

virtual String getCommandName() const;

Expand Down
9 changes: 6 additions & 3 deletions lib/Hoymiles/src/commands/CommandAbstract.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022-2023 Thomas Basler and others
* Copyright (C) 2022-2024 Thomas Basler and others
*/

/*
Expand Down Expand Up @@ -29,13 +29,16 @@ Source Address: 80 12 23 04
#include "CommandAbstract.h"
#include "crc.h"
#include <string.h>
#include "../inverters/InverterAbstract.h"

CommandAbstract::CommandAbstract(const uint64_t target_address, const uint64_t router_address)
CommandAbstract::CommandAbstract(InverterAbstract* inv, const uint64_t router_address)
{
memset(_payload, 0, RF_LEN);
_payload_size = 0;

setTargetAddress(target_address);
_inv = inv;

setTargetAddress(_inv->serial());
setRouterAddress(router_address);
setSendCount(0);
setTimeout(0);
Expand Down
8 changes: 5 additions & 3 deletions lib/Hoymiles/src/commands/CommandAbstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ class InverterAbstract;

class CommandAbstract {
public:
explicit CommandAbstract(const uint64_t target_address = 0, const uint64_t router_address = 0);
explicit CommandAbstract(InverterAbstract* inv, const uint64_t router_address = 0);
virtual ~CommandAbstract() {};

const uint8_t* getDataPayload();
void dumpDataPayload(Print* stream);

uint8_t getDataSize() const;

void setTargetAddress(const uint64_t address);
uint64_t getTargetAddress() const;

void setRouterAddress(const uint64_t address);
Expand Down Expand Up @@ -56,6 +55,9 @@ class CommandAbstract {
uint64_t _targetAddress;
uint64_t _routerAddress;

InverterAbstract* _inv;

private:
void setTargetAddress(const uint64_t address);
static void convertSerialToPacketId(uint8_t buffer[], const uint64_t serial);
};
};
8 changes: 4 additions & 4 deletions lib/Hoymiles/src/commands/DevControlCommand.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022-2023 Thomas Basler and others
* Copyright (C) 2022-2024 Thomas Basler and others
*/

/*
Expand All @@ -23,8 +23,8 @@ ID Target Addr Source Addr Cmd Payload CRC16 CRC8
#include "DevControlCommand.h"
#include "crc.h"

DevControlCommand::DevControlCommand(const uint64_t target_address, const uint64_t router_address)
: CommandAbstract(target_address, router_address)
DevControlCommand::DevControlCommand(InverterAbstract* inv, const uint64_t router_address)
: CommandAbstract(inv, router_address)
{
_payload[0] = 0x51;
_payload[9] = 0x81;
Expand All @@ -48,4 +48,4 @@ bool DevControlCommand::handleResponse(InverterAbstract& inverter, const fragmen
}

return true;
}
}
4 changes: 2 additions & 2 deletions lib/Hoymiles/src/commands/DevControlCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

class DevControlCommand : public CommandAbstract {
public:
explicit DevControlCommand(const uint64_t target_address = 0, const uint64_t router_address = 0);
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);

protected:
void udpateCRC(const uint8_t len);
};
};
8 changes: 4 additions & 4 deletions lib/Hoymiles/src/commands/DevInfoAllCommand.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022-2023 Thomas Basler and others
* Copyright (C) 2022-2024 Thomas Basler and others
*/

/*
Expand All @@ -21,8 +21,8 @@ ID Target Addr Source Addr Idx DT ? Time Gap Pa
#include "DevInfoAllCommand.h"
#include "inverters/InverterAbstract.h"

DevInfoAllCommand::DevInfoAllCommand(const uint64_t target_address, const uint64_t router_address, const time_t time)
: MultiDataCommand(target_address, router_address)
DevInfoAllCommand::DevInfoAllCommand(InverterAbstract* inv, const uint64_t router_address, const time_t time)
: MultiDataCommand(inv, router_address)
{
setTime(time);
setDataType(0x01);
Expand Down Expand Up @@ -52,4 +52,4 @@ bool DevInfoAllCommand::handleResponse(InverterAbstract& inverter, const fragmen
inverter.DevInfo()->endAppendFragment();
inverter.DevInfo()->setLastUpdateAll(millis());
return true;
}
}
4 changes: 2 additions & 2 deletions lib/Hoymiles/src/commands/DevInfoAllCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

class DevInfoAllCommand : public MultiDataCommand {
public:
explicit DevInfoAllCommand(const uint64_t target_address = 0, const uint64_t router_address = 0, const time_t time = 0);
explicit DevInfoAllCommand(InverterAbstract* inv, const uint64_t router_address = 0, const time_t time = 0);

virtual String getCommandName() const;

virtual bool handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id);
};
};
8 changes: 4 additions & 4 deletions lib/Hoymiles/src/commands/DevInfoSimpleCommand.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022-2023 Thomas Basler and others
* Copyright (C) 2022-2024 Thomas Basler and others
*/

/*
Expand All @@ -21,8 +21,8 @@ ID Target Addr Source Addr Idx DT ? Time Gap Pa
#include "DevInfoSimpleCommand.h"
#include "inverters/InverterAbstract.h"

DevInfoSimpleCommand::DevInfoSimpleCommand(const uint64_t target_address, const uint64_t router_address, const time_t time)
: MultiDataCommand(target_address, router_address)
DevInfoSimpleCommand::DevInfoSimpleCommand(InverterAbstract* inv, const uint64_t router_address, const time_t time)
: MultiDataCommand(inv, router_address)
{
setTime(time);
setDataType(0x00);
Expand Down Expand Up @@ -52,4 +52,4 @@ bool DevInfoSimpleCommand::handleResponse(InverterAbstract& inverter, const frag
inverter.DevInfo()->endAppendFragment();
inverter.DevInfo()->setLastUpdateSimple(millis());
return true;
}
}
4 changes: 2 additions & 2 deletions lib/Hoymiles/src/commands/DevInfoSimpleCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

class DevInfoSimpleCommand : public MultiDataCommand {
public:
explicit DevInfoSimpleCommand(const uint64_t target_address = 0, const uint64_t router_address = 0, const time_t time = 0);
explicit DevInfoSimpleCommand(InverterAbstract* inv, const uint64_t router_address = 0, const time_t time = 0);

virtual String getCommandName() const;

virtual bool handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id);
};
};
8 changes: 4 additions & 4 deletions lib/Hoymiles/src/commands/GridOnProFilePara.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022-2023 Thomas Basler and others
* Copyright (C) 2022-2024 Thomas Basler and others
*/

/*
Expand All @@ -22,8 +22,8 @@ ID Target Addr Source Addr Idx DT ? Time Gap Pa
#include "Hoymiles.h"
#include "inverters/InverterAbstract.h"

GridOnProFilePara::GridOnProFilePara(const uint64_t target_address, const uint64_t router_address, const time_t time)
: MultiDataCommand(target_address, router_address)
GridOnProFilePara::GridOnProFilePara(InverterAbstract* inv, const uint64_t router_address, const time_t time)
: MultiDataCommand(inv, router_address)
{
setTime(time);
setDataType(0x02);
Expand Down Expand Up @@ -53,4 +53,4 @@ bool GridOnProFilePara::handleResponse(InverterAbstract& inverter, const fragmen
inverter.GridProfile()->endAppendFragment();
inverter.GridProfile()->setLastUpdate(millis());
return true;
}
}
4 changes: 2 additions & 2 deletions lib/Hoymiles/src/commands/GridOnProFilePara.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

class GridOnProFilePara : public MultiDataCommand {
public:
explicit GridOnProFilePara(const uint64_t target_address = 0, const uint64_t router_address = 0, const time_t time = 0);
explicit GridOnProFilePara(InverterAbstract* inv, const uint64_t router_address = 0, const time_t time = 0);

virtual String getCommandName() const;

virtual bool handleResponse(InverterAbstract& inverter, const fragment_t fragment[], const uint8_t max_fragment_id);
};
};
8 changes: 4 additions & 4 deletions lib/Hoymiles/src/commands/MultiDataCommand.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022-2023 Thomas Basler and others
* Copyright (C) 2022-2024 Thomas Basler and others
*/

/*
Expand Down Expand Up @@ -28,8 +28,9 @@ ID Target Addr Source Addr Idx DT ? Time Gap Pa
#include "MultiDataCommand.h"
#include "crc.h"

MultiDataCommand::MultiDataCommand(const uint64_t target_address, const uint64_t router_address, const uint8_t data_type, const time_t time)
: CommandAbstract(target_address, router_address)
MultiDataCommand::MultiDataCommand(InverterAbstract* inv, const uint64_t router_address, const uint8_t data_type, const time_t time)
: CommandAbstract(inv, router_address)
, _cmdRequestFrame(inv)
{
_payload[0] = 0x15;
_payload[9] = 0x80;
Expand Down Expand Up @@ -79,7 +80,6 @@ time_t MultiDataCommand::getTime() const

CommandAbstract* MultiDataCommand::getRequestFrameCommand(const uint8_t frame_no)
{
_cmdRequestFrame.setTargetAddress(getTargetAddress());
_cmdRequestFrame.setFrameNo(frame_no);

return &_cmdRequestFrame;
Expand Down
4 changes: 2 additions & 2 deletions lib/Hoymiles/src/commands/MultiDataCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class MultiDataCommand : public CommandAbstract {
public:
explicit MultiDataCommand(const uint64_t target_address = 0, const uint64_t router_address = 0, const uint8_t data_type = 0, const time_t time = 0);
explicit MultiDataCommand(InverterAbstract* inv, const uint64_t router_address = 0, const uint8_t data_type = 0, const time_t time = 0);

void setTime(const time_t time);
time_t getTime() const;
Expand All @@ -23,4 +23,4 @@ class MultiDataCommand : public CommandAbstract {
static uint8_t getTotalFragmentSize(const fragment_t fragment[], const uint8_t max_fragment_id);

RequestFrameCommand _cmdRequestFrame;
};
};
8 changes: 4 additions & 4 deletions lib/Hoymiles/src/commands/ParaSetCommand.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022 Thomas Basler and others
* Copyright (C) 2022-2024 Thomas Basler and others
*/
#include "ParaSetCommand.h"

ParaSetCommand::ParaSetCommand(const uint64_t target_address, const uint64_t router_address)
: CommandAbstract(target_address, router_address)
ParaSetCommand::ParaSetCommand(InverterAbstract* inv, const uint64_t router_address)
: CommandAbstract(inv, router_address)
{
_payload[0] = 0x52;
}
}
4 changes: 2 additions & 2 deletions lib/Hoymiles/src/commands/ParaSetCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

class ParaSetCommand : public CommandAbstract {
public:
explicit ParaSetCommand(const uint64_t target_address = 0, const uint64_t router_address = 0);
};
explicit ParaSetCommand(InverterAbstract* inv, const uint64_t router_address = 0);
};
Loading

0 comments on commit 6a7bed0

Please sign in to comment.