Skip to content

Commit 2cdfbfc

Browse files
committed
complete rework of internal structures
1 parent a641e4d commit 2cdfbfc

File tree

17 files changed

+444
-412
lines changed

17 files changed

+444
-412
lines changed

include/Configuration.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ struct GRID_CHARGER_CONFIG_T {
279279
};
280280
using GridChargerConfig = struct GRID_CHARGER_CONFIG_T;
281281

282-
enum SolarChargerProviderType { VEDIRECT = 0, MQTT = 1, SmartBufferBattery = 2 };
282+
enum SolarChargerProviderType { VEDIRECT = 0, MQTT = 1, Integrated = 2 };
283283

284284
struct SOLARCHARGER_MQTT_CONFIG_T {
285285
bool CalculateOutputPower;

include/battery/SmartBufferStats.h

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,108 @@
33

44
#include <battery/Stats.h>
55
#include <solarcharger/Controller.h>
6-
#include <solarcharger/smartbufferbatteries/Provider.h>
7-
#include <solarcharger/smartbufferbatteries/Stats.h>
6+
#include <solarcharger/integrated/Provider.h>
7+
#include <solarcharger/integrated/Stats.h>
88

99
namespace Batteries {
1010

1111
// mandatory interface for all kinds of batteries
1212
class SmartBufferStats : public Stats {
13+
1314
public:
15+
using MPPT = SolarChargers::Integrated::MPPT;
16+
1417
virtual std::optional<String> const& getDeviceName() const = 0;
1518
virtual size_t getNumberMppts() const = 0;
1619

20+
inline std::optional<float> getSolarPowerOverall() const {
21+
return _solar_power_sum;
22+
}
23+
24+
inline std::optional<float> getSolarPower(const MPPT mppt) const {
25+
try {
26+
return _solar_power.at(mppt);
27+
}
28+
catch(const std::out_of_range& ex) {;}
29+
30+
return std::nullopt;
31+
}
32+
33+
inline std::optional<float> getSolarVoltage(const MPPT mppt) const {
34+
try {
35+
return _solar_voltage.at(mppt);
36+
}
37+
catch(const std::out_of_range& ex) {;}
38+
39+
return std::nullopt;
40+
}
41+
1742
protected:
18-
void setMpptPower(const size_t mppt, const float power, const uint32_t timestamp = millis())
19-
{
20-
auto charger = getSolarCharger();
21-
if (charger != nullptr) {
22-
charger->setMpptPower(_solarcharger_id, mppt, power, timestamp);
43+
inline void setSolarPower(const MPPT mppt, const std::optional<float> &power, const uint32_t timestamp = millis()) {
44+
if (!power.has_value()) { return; }
45+
_solar_power[mppt] = power;
46+
47+
// update sum value
48+
std::optional<float> sum = std::nullopt;
49+
for (const auto& [index, power] : _solar_power) {
50+
if (!power.has_value()) { continue; }
51+
sum = sum.value_or(0) + *power;
2352
}
53+
_solar_power_sum = sum;
54+
55+
// publish value to SolcarCharger integration
56+
auto object = getObject(mppt);
57+
if (object == nullptr ) { return; }
58+
object->setPower(*power, timestamp);
2459
}
2560

26-
void setMpptVoltage(const size_t mppt, const float voltage, const uint32_t timestamp = millis())
61+
inline void setSolarVoltage(const MPPT mppt, const std::optional<float> &voltage, const uint32_t timestamp = millis()) {
62+
if (!voltage.has_value()) { return; }
63+
_solar_voltage[mppt] = voltage;
64+
65+
// publish value to SolcarCharger integration
66+
auto object = getObject(mppt);
67+
if (object == nullptr) { return; }
68+
object->setVoltage(*voltage, timestamp);
69+
}
70+
71+
private:
72+
std::shared_ptr<SolarChargers::Integrated::MpptData> getObject(const MPPT mppt)
2773
{
28-
auto charger = getSolarCharger();
29-
if (charger != nullptr) {
30-
charger->setMpptVoltage(_solarcharger_id, mppt, voltage, timestamp);
74+
try {
75+
return getObjects().at(mppt);
3176
}
77+
catch(const std::out_of_range& ex) {;}
78+
79+
return nullptr;
3280
}
3381

34-
std::shared_ptr<SolarChargers::SmartBufferBatteries::Stats> getSolarCharger()
82+
83+
std::map<MPPT, std::shared_ptr<SolarChargers::Integrated::MpptData>> getObjects()
3584
{
36-
auto mppt = SolarCharger.getSmartBufferBatteryStats();
37-
if (mppt != nullptr && !mppt->hasDevice(_solarcharger_id)) {
38-
_solarcharger_id = mppt->addDevice(getManufacturer(), getDeviceName(), getSerial(), getNumberMppts());
39-
}
85+
auto mppt = SolarCharger.getIntegratedStats();
86+
if (mppt == nullptr) { return {}; }
87+
88+
if (mppt->hasDevice(_solarcharger_id)) { return _objects; }
89+
90+
// if device was not found, immediatly clear our cached pointers
91+
_objects.clear();
4092

41-
return mppt;
93+
// amd try to re-add our device
94+
auto result = mppt->addDevice(getManufacturer(), getDeviceName(), getSerial(), getNumberMppts());
95+
if (!result.has_value()) { return {}; }
96+
97+
_solarcharger_id = (*result).first;
98+
_objects = (*result).second->getMppts();
99+
100+
return _objects;
42101
}
43102

44103
std::optional<uint32_t> _solarcharger_id = std::nullopt;
45-
104+
std::optional<float> _solar_power_sum = std::nullopt;
105+
std::map<MPPT, std::shared_ptr<SolarChargers::Integrated::MpptData>> _objects = std::map<MPPT, std::shared_ptr<SolarChargers::Integrated::MpptData>>();
106+
std::map<MPPT, std::optional<float>> _solar_power = std::map<MPPT, std::optional<float>>();
107+
std::map<MPPT, std::optional<float>> _solar_voltage = std::map<MPPT, std::optional<float>>();
46108
};
47109

48110
} // namespace Batteries

include/battery/zendure/Stats.h

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include <battery/SmartBufferStats.h>
66
#include <battery/zendure/Constants.h>
77
#include <solarcharger/Controller.h>
8-
#include <solarcharger/smartbufferbatteries/Provider.h>
9-
#include <solarcharger/smartbufferbatteries/Stats.h>
8+
#include <solarcharger/integrated/Provider.h>
9+
#include <solarcharger/integrated/Stats.h>
1010
#include <map>
1111
#include <optional>
1212
#include <Configuration.h>
@@ -141,6 +141,10 @@ class Stats : public ::Batteries::SmartBufferStats {
141141
virtual std::optional<String> const& getDeviceName() const { return _device; }
142142
virtual size_t getNumberMppts() const { return ZENDURE_NUM_MPPTS; };
143143

144+
inline std::optional<float> getInputPower() const {
145+
return getSolarPowerOverall();
146+
}
147+
144148
protected:
145149
std::shared_ptr<PackStats> getPackData(size_t index) const;
146150
std::shared_ptr<PackStats> addPackData(size_t index, String serial);
@@ -189,36 +193,6 @@ class Stats : public ::Batteries::SmartBufferStats {
189193
_device = std::move(device);
190194
}
191195

192-
std::shared_ptr<SolarChargers::SmartBufferBatteries::Stats> getSolarCharger() {
193-
auto mppt = SolarCharger.getSmartBufferBatteryStats();
194-
195-
if (mppt == nullptr) {
196-
return nullptr;
197-
}
198-
199-
// Doe we need to add our charger, first?
200-
if (!mppt->hasDevice(_solarcharger_id)) {
201-
_solarcharger_id = mppt->addDevice(*getManufacturer(), _device, *getSerial(), ZENDURE_NUM_MPPTS);
202-
}
203-
204-
return mppt;
205-
}
206-
207-
inline void updateSolarInputPower(const size_t num, const float power) {
208-
_input_power = _solar_power_1 + _solar_power_2;
209-
setMpptPower(num, power);
210-
}
211-
212-
inline void setSolarPower1(const uint16_t power) {
213-
_solar_power_1 = power;
214-
updateSolarInputPower(1, power);
215-
}
216-
217-
inline void setSolarPower2(const uint16_t power) {
218-
_solar_power_2 = power;
219-
updateSolarInputPower(2, power);
220-
}
221-
222196
void setChargePower(const uint16_t power) {
223197
_charge_power = power;
224198

@@ -243,16 +217,6 @@ class Stats : public ::Batteries::SmartBufferStats {
243217
_output_power = power;
244218
}
245219

246-
inline void setSolarVoltage1(const float voltage) {
247-
_solar_voltage_1 = voltage;
248-
setMpptVoltage(1, voltage);
249-
}
250-
251-
inline void setSolarVoltage2(const float voltage) {
252-
_solar_voltage_2 = voltage;
253-
setMpptVoltage(2, voltage);
254-
}
255-
256220
inline void setOutputVoltage(const float voltage) {
257221
_output_voltage = voltage;
258222
}
@@ -281,13 +245,6 @@ class Stats : public ::Batteries::SmartBufferStats {
281245
_auto_recover = static_cast<bool>(value);
282246
}
283247

284-
inline void setVoltage(float voltage, uint32_t timestamp) {
285-
if (voltage > 0) {
286-
setDischargeCurrentLimit(static_cast<float>(_inverse_max) / voltage, timestamp);
287-
}
288-
Batteries::Stats::setVoltage(voltage, timestamp);
289-
}
290-
291248
std::optional<String> _device = std::nullopt;
292249

293250
std::map<size_t, std::shared_ptr<PackStats>> _packData = std::map<size_t, std::shared_ptr<PackStats> >();
@@ -314,12 +271,7 @@ class Stats : public ::Batteries::SmartBufferStats {
314271
uint16_t _charge_power = 0;
315272
uint16_t _discharge_power = 0;
316273
uint16_t _output_power = 0;
317-
uint16_t _input_power = 0;
318-
uint16_t _solar_power_1 = 0;
319-
uint16_t _solar_power_2 = 0;
320274

321-
float _solar_voltage_1 = 0.0;
322-
float _solar_voltage_2 = 0.0;
323275
float _output_voltage = 0.0;
324276

325277
uint16_t _charge_power_cycle = 0;

include/solarcharger/Controller.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
#include <TaskSchedulerDeclarations.h>
77
#include <solarcharger/Provider.h>
88
#include <solarcharger/Stats.h>
9-
#include <solarcharger/smartbufferbatteries/Stats.h>
10-
9+
#include <solarcharger/integrated/Stats.h>
1110
namespace SolarChargers {
1211

1312
class Controller {
@@ -16,7 +15,7 @@ class Controller {
1615
void updateSettings();
1716

1817
std::shared_ptr<Stats const> getStats() const;
19-
std::shared_ptr<SmartBufferBatteries::Stats> getSmartBufferBatteryStats();
18+
std::shared_ptr<Integrated::Stats> getIntegratedStats();
2019

2120
private:
2221
void loop();

include/solarcharger/smartbufferbatteries/Provider.h renamed to include/solarcharger/integrated/Provider.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#include <memory>
66
#include <TaskSchedulerDeclarations.h>
77
#include <solarcharger/Provider.h>
8-
#include <solarcharger/smartbufferbatteries/Stats.h>
8+
#include <solarcharger/integrated/Stats.h>
99
#include <espMqttClient.h>
1010

11-
namespace SolarChargers::SmartBufferBatteries {
11+
namespace SolarChargers::Integrated {
1212

1313
class Provider : public ::SolarChargers::Provider {
1414
public:
@@ -19,7 +19,7 @@ class Provider : public ::SolarChargers::Provider {
1919
void deinit() final { return; };
2020
void loop() final { return; };
2121
std::shared_ptr<::SolarChargers::Stats> getStats() const final { return _stats; }
22-
std::shared_ptr<Stats> getStats() { return _stats; }
22+
//std::shared_ptr<Stats> getStats() { return _stats; }
2323

2424
private:
2525
Provider(Provider const& other) = delete;
@@ -30,4 +30,4 @@ class Provider : public ::SolarChargers::Provider {
3030
std::shared_ptr<Stats> _stats = std::make_shared<Stats>();
3131
};
3232

33-
} // namespace SolarChargers::SmartBufferBatteries
33+
} // namespace SolarChargers::Integrated

0 commit comments

Comments
 (0)