Skip to content

Commit

Permalink
VE.Direct: simplify access to data
Browse files Browse the repository at this point in the history
hand out const& to the data structs. this is possible now that this
struct is "stable", i.e., not reset regularly.
  • Loading branch information
schlimmchen committed Apr 2, 2024
1 parent ad125ea commit b299b9d
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 185 deletions.
2 changes: 1 addition & 1 deletion include/MqttHandleVedirect.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MqttHandleVedirectClass {

bool _PublishFull;

void publish_mppt_data(const VeDirectMpptController::spData_t &spMpptData,
void publish_mppt_data(const VeDirectMpptController::data_t &mpptData,
const VeDirectMpptController::data_t &frame) const;
};

Expand Down
6 changes: 3 additions & 3 deletions include/MqttHandleVedirectHass.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class MqttHandleVedirectHassClass {
void publish(const String& subtopic, const String& payload);
void publishBinarySensor(const char *caption, const char *icon, const char *subTopic,
const char *payload_on, const char *payload_off,
const VeDirectMpptController::spData_t &spMpptData);
const VeDirectMpptController::data_t &mpptData);
void publishSensor(const char *caption, const char *icon, const char *subTopic,
const char *deviceClass, const char *stateClass,
const char *unitOfMeasurement,
const VeDirectMpptController::spData_t &spMpptData);
const VeDirectMpptController::data_t &mpptData);
void createDeviceInfo(JsonObject &object,
const VeDirectMpptController::spData_t &spMpptData);
const VeDirectMpptController::data_t &mpptData);

Task _loopTask;

Expand Down
2 changes: 1 addition & 1 deletion include/VictronMppt.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class VictronMpptClass {
uint32_t getDataAgeMillis(size_t idx) const;

size_t controllerAmount() const { return _controllers.size(); }
std::optional<VeDirectMpptController::spData_t> getData(size_t idx = 0) const;
std::optional<VeDirectMpptController::data_t> getData(size_t idx = 0) const;

// total output of all MPPT charge controllers in Watts
int32_t getPowerOutputWatts() const;
Expand Down
2 changes: 1 addition & 1 deletion include/WebApi_ws_vedirect_live.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class WebApiWsVedirectLiveClass {

private:
void generateJsonResponse(JsonVariant& root, bool fullUpdate);
static void populateJson(const JsonObject &root, const VeDirectMpptController::spData_t &spMpptData);
static void populateJson(const JsonObject &root, const VeDirectMpptController::data_t &mpptData);
void onLivedataStatus(AsyncWebServerRequest* request);
void onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len);
bool hasUpdate(size_t idx);
Expand Down
3 changes: 2 additions & 1 deletion lib/VeDirectFrameHandler/VeDirectFrameHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class VeDirectFrameHandler {
void loop(); // main loop to read ve.direct data
uint32_t getLastUpdate() const; // timestamp of last successful frame read
bool isDataValid() const; // return true if data valid and not outdated
T const& getData() const { return _tmpFrame; }

protected:
VeDirectFrameHandler();
Expand All @@ -41,7 +42,7 @@ class VeDirectFrameHandler {
void rxData(uint8_t inbyte); // byte of serial data
void processTextData(std::string const& name, std::string const& value);
virtual bool processTextDataDerived(std::string const& name, std::string const& value) = 0;
virtual void frameValidEvent() = 0;
virtual void frameValidEvent() { }
int hexRxEvent(uint8_t);

std::unique_ptr<HardwareSerial> _vedirectSerial;
Expand Down
3 changes: 0 additions & 3 deletions lib/VeDirectFrameHandler/VeDirectMpptController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
void VeDirectMpptController::init(int8_t rx, int8_t tx, Print* msgOut, bool verboseLogging, uint16_t hwSerialPort)
{
VeDirectFrameHandler::init("MPPT", rx, tx, msgOut, verboseLogging, hwSerialPort);
_spData = std::make_shared<veMpptStruct>();
}

bool VeDirectMpptController::processTextDataDerived(std::string const& name, std::string const& value)
Expand Down Expand Up @@ -80,6 +79,4 @@ void VeDirectMpptController::frameValidEvent() {
_efficiency.addNumber(static_cast<double>(_tmpFrame.P * 100) / _tmpFrame.PPV);
_tmpFrame.E = _efficiency.getAverage();
}

_spData = std::make_shared<veMpptStruct>(_tmpFrame);
}
3 changes: 0 additions & 3 deletions lib/VeDirectFrameHandler/VeDirectMpptController.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,9 @@ class VeDirectMpptController : public VeDirectFrameHandler<veMpptStruct> {
void init(int8_t rx, int8_t tx, Print* msgOut, bool verboseLogging, uint16_t hwSerialPort);

using data_t = veMpptStruct;
using spData_t = std::shared_ptr<data_t const>;
spData_t getData() const { return _spData; }

private:
bool processTextDataDerived(std::string const& name, std::string const& value) final;
void frameValidEvent() final;
spData_t _spData = nullptr;
MovingAverage<double, 5> _efficiency;
};
8 changes: 0 additions & 8 deletions lib/VeDirectFrameHandler/VeDirectShuntController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,3 @@ bool VeDirectShuntController::processTextDataDerived(std::string const& name, st

return false;
}

/*
* frameValidEvent
* This function is called at the end of the received frame.
*/
void VeDirectShuntController::frameValidEvent() {
veFrame = _tmpFrame;
}
2 changes: 0 additions & 2 deletions lib/VeDirectFrameHandler/VeDirectShuntController.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ class VeDirectShuntController : public VeDirectFrameHandler<veShuntStruct> {
void init(int8_t rx, int8_t tx, Print* msgOut, bool verboseLogging);

using data_t = veShuntStruct;
data_t veFrame{};

private:
bool processTextDataDerived(std::string const& name, std::string const& value) final;
void frameValidEvent() final;
};

extern VeDirectShuntController VeDirectShunt;
73 changes: 32 additions & 41 deletions src/MqttHandlVedirectHass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,42 +58,33 @@ void MqttHandleVedirectHassClass::publishConfig()

// device info
for (int idx = 0; idx < VictronMppt.controllerAmount(); ++idx) {
// ensure data is received from victron
if (!VictronMppt.isDataValid(idx)) {
continue;
}

std::optional<VeDirectMpptController::spData_t> spOptMpptData = VictronMppt.getData(idx);
if (!spOptMpptData.has_value()) {
continue;
}

VeDirectMpptController::spData_t &spMpptData = spOptMpptData.value();

publishBinarySensor("MPPT load output state", "mdi:export", "LOAD", "ON", "OFF", spMpptData);
publishSensor("MPPT serial number", "mdi:counter", "SER", nullptr, nullptr, nullptr, spMpptData);
publishSensor("MPPT firmware number", "mdi:counter", "FW", nullptr, nullptr, nullptr, spMpptData);
publishSensor("MPPT state of operation", "mdi:wrench", "CS", nullptr, nullptr, nullptr, spMpptData);
publishSensor("MPPT error code", "mdi:bell", "ERR", nullptr, nullptr, nullptr, spMpptData);
publishSensor("MPPT off reason", "mdi:wrench", "OR", nullptr, nullptr, nullptr, spMpptData);
publishSensor("MPPT tracker operation mode", "mdi:wrench", "MPPT", nullptr, nullptr, nullptr, spMpptData);
publishSensor("MPPT Day sequence number (0...364)", "mdi:calendar-month-outline", "HSDS", NULL, "total", "d", spMpptData);
auto optMpptData = VictronMppt.getData(idx);
if (!optMpptData.has_value()) { continue; }

publishBinarySensor("MPPT load output state", "mdi:export", "LOAD", "ON", "OFF", *optMpptData);
publishSensor("MPPT serial number", "mdi:counter", "SER", nullptr, nullptr, nullptr, *optMpptData);
publishSensor("MPPT firmware number", "mdi:counter", "FW", nullptr, nullptr, nullptr, *optMpptData);
publishSensor("MPPT state of operation", "mdi:wrench", "CS", nullptr, nullptr, nullptr, *optMpptData);
publishSensor("MPPT error code", "mdi:bell", "ERR", nullptr, nullptr, nullptr, *optMpptData);
publishSensor("MPPT off reason", "mdi:wrench", "OR", nullptr, nullptr, nullptr, *optMpptData);
publishSensor("MPPT tracker operation mode", "mdi:wrench", "MPPT", nullptr, nullptr, nullptr, *optMpptData);
publishSensor("MPPT Day sequence number (0...364)", "mdi:calendar-month-outline", "HSDS", NULL, "total", "d", *optMpptData);

// battery info
publishSensor("Battery voltage", NULL, "V", "voltage", "measurement", "V", spMpptData);
publishSensor("Battery current", NULL, "I", "current", "measurement", "A", spMpptData);
publishSensor("Battery power (calculated)", NULL, "P", "power", "measurement", "W", spMpptData);
publishSensor("Battery efficiency (calculated)", NULL, "E", NULL, "measurement", "%", spMpptData);
publishSensor("Battery voltage", NULL, "V", "voltage", "measurement", "V", *optMpptData);
publishSensor("Battery current", NULL, "I", "current", "measurement", "A", *optMpptData);
publishSensor("Battery power (calculated)", NULL, "P", "power", "measurement", "W", *optMpptData);
publishSensor("Battery efficiency (calculated)", NULL, "E", NULL, "measurement", "%", *optMpptData);

// panel info
publishSensor("Panel voltage", NULL, "VPV", "voltage", "measurement", "V", spMpptData);
publishSensor("Panel current (calculated)", NULL, "IPV", "current", "measurement", "A", spMpptData);
publishSensor("Panel power", NULL, "PPV", "power", "measurement", "W", spMpptData);
publishSensor("Panel yield total", NULL, "H19", "energy", "total_increasing", "kWh", spMpptData);
publishSensor("Panel yield today", NULL, "H20", "energy", "total", "kWh", spMpptData);
publishSensor("Panel maximum power today", NULL, "H21", "power", "measurement", "W", spMpptData);
publishSensor("Panel yield yesterday", NULL, "H22", "energy", "total", "kWh", spMpptData);
publishSensor("Panel maximum power yesterday", NULL, "H23", "power", "measurement", "W", spMpptData);
publishSensor("Panel voltage", NULL, "VPV", "voltage", "measurement", "V", *optMpptData);
publishSensor("Panel current (calculated)", NULL, "IPV", "current", "measurement", "A", *optMpptData);
publishSensor("Panel power", NULL, "PPV", "power", "measurement", "W", *optMpptData);
publishSensor("Panel yield total", NULL, "H19", "energy", "total_increasing", "kWh", *optMpptData);
publishSensor("Panel yield today", NULL, "H20", "energy", "total", "kWh", *optMpptData);
publishSensor("Panel maximum power today", NULL, "H21", "power", "measurement", "W", *optMpptData);
publishSensor("Panel yield yesterday", NULL, "H22", "energy", "total", "kWh", *optMpptData);
publishSensor("Panel maximum power yesterday", NULL, "H23", "power", "measurement", "W", *optMpptData);
}

yield();
Expand All @@ -102,9 +93,9 @@ void MqttHandleVedirectHassClass::publishConfig()
void MqttHandleVedirectHassClass::publishSensor(const char *caption, const char *icon, const char *subTopic,
const char *deviceClass, const char *stateClass,
const char *unitOfMeasurement,
const VeDirectMpptController::spData_t &spMpptData)
const VeDirectMpptController::data_t &mpptData)
{
String serial = spMpptData->SER;
String serial = mpptData.SER;

String sensorId = caption;
sensorId.replace(" ", "_");
Expand Down Expand Up @@ -139,7 +130,7 @@ void MqttHandleVedirectHassClass::publishSensor(const char *caption, const char
}

JsonObject deviceObj = root.createNestedObject("dev");
createDeviceInfo(deviceObj, spMpptData);
createDeviceInfo(deviceObj, mpptData);

if (Configuration.get().Mqtt.Hass.Expire) {
root["exp_aft"] = Configuration.get().Mqtt.PublishInterval * 3;
Expand All @@ -160,9 +151,9 @@ void MqttHandleVedirectHassClass::publishSensor(const char *caption, const char
}
void MqttHandleVedirectHassClass::publishBinarySensor(const char *caption, const char *icon, const char *subTopic,
const char *payload_on, const char *payload_off,
const VeDirectMpptController::spData_t &spMpptData)
const VeDirectMpptController::data_t &mpptData)
{
String serial = spMpptData->SER;
String serial = mpptData.SER;

String sensorId = caption;
sensorId.replace(" ", "_");
Expand Down Expand Up @@ -195,7 +186,7 @@ void MqttHandleVedirectHassClass::publishBinarySensor(const char *caption, const
}

JsonObject deviceObj = root.createNestedObject("dev");
createDeviceInfo(deviceObj, spMpptData);
createDeviceInfo(deviceObj, mpptData);

if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }

Expand All @@ -205,14 +196,14 @@ void MqttHandleVedirectHassClass::publishBinarySensor(const char *caption, const
}

void MqttHandleVedirectHassClass::createDeviceInfo(JsonObject &object,
const VeDirectMpptController::spData_t &spMpptData)
const VeDirectMpptController::data_t &mpptData)
{
String serial = spMpptData->SER;
String serial = mpptData.SER;
object["name"] = "Victron(" + serial + ")";
object["ids"] = serial;
object["cu"] = String("http://") + NetworkSettings.localIP().toString();
object["mf"] = "OpenDTU";
object["mdl"] = spMpptData->getPidAsString();
object["mdl"] = mpptData.getPidAsString();
object["sw"] = AUTO_GIT_HASH;
}

Expand Down
Loading

0 comments on commit b299b9d

Please sign in to comment.