Skip to content

Commit

Permalink
restructure solarcharger classes
Browse files Browse the repository at this point in the history
did not touch 'MqttHandleVedirect'. will be done in a stats realted branch/PR
  • Loading branch information
AndreasBoehm committed Dec 13, 2024
1 parent f43b7a5 commit 20ead7c
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 134 deletions.
15 changes: 10 additions & 5 deletions include/SolarCharger.h → include/solarcharger/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
#include <memory>
#include <mutex>
#include <TaskSchedulerDeclarations.h>
#include "SolarChargerProvider.h"
#include "VeDirectMpptController.h"
#include <solarcharger/Provider.h>
#include <VeDirectMpptController.h>

class SolarChargerClass {
namespace SolarChargers {

class Controller {
public:
void init(Scheduler&);
void updateSettings();
Expand Down Expand Up @@ -42,7 +44,10 @@ class SolarChargerClass {

Task _loopTask;
mutable std::mutex _mutex;
std::unique_ptr<SolarChargerProvider> _upProvider = nullptr;
std::unique_ptr<Provider> _upProvider = nullptr;
bool _publishSensors = false;
};

extern SolarChargerClass SolarCharger;
} // namespace SolarChargers

extern SolarChargers::Controller SolarCharger;
17 changes: 17 additions & 0 deletions include/solarcharger/HassIntegration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include <Arduino.h>
#include <ArduinoJson.h>

namespace SolarChargers {

class HassIntegration {
public:
virtual void publishSensors() const;

protected:
void publish(const String& subtopic, const String& payload) const;
};

} // namespace SolarChargers
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include "VeDirectMpptController.h"
#include <VeDirectMpptController.h>

class SolarChargerProvider {
namespace SolarChargers {

class HassIntegration;

class Provider {
public:
// returns true if the provider is ready for use, false otherwise
virtual bool init(bool verboseLogging) = 0;
virtual void deinit() = 0;
virtual void loop() = 0;
virtual HassIntegration const& getHassIntegration() const = 0;

// TODO(andreasboehm): below methods are taken from VictronMppt to start abstracting
// solar chargers without breaking everything.
Expand All @@ -34,3 +39,5 @@ class SolarChargerProvider {

virtual bool isDataValid() const = 0;
};

} // namespace SolarChargers
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,26 @@
#pragma once

#include <ArduinoJson.h>
#include <solarcharger/HassIntegration.h>
#include "VeDirectMpptController.h"
#include <TaskSchedulerDeclarations.h>

class MqttHandleVedirectHassClass {
namespace SolarChargers::Victron {

class HassIntegration : public ::SolarChargers::HassIntegration {
public:
void init(Scheduler& scheduler);
void forceUpdate();
void publishSensors() const final;

private:
void loop();
void publishConfig();
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::data_t &mpptData);
const VeDirectMpptController::data_t &mpptData) const;
void publishSensor(const char *caption, const char *icon, const char *subTopic,
const char *deviceClass, const char *stateClass,
const char *unitOfMeasurement,
const VeDirectMpptController::data_t &mpptData);
const VeDirectMpptController::data_t &mpptData) const;
void createDeviceInfo(JsonObject &object,
const VeDirectMpptController::data_t &mpptData);

Task _loopTask;
const VeDirectMpptController::data_t &mpptData) const;

bool _wasConnected = false;
bool _updateForced = false;
};

extern MqttHandleVedirectHassClass MqttHandleVedirectHass;
} // namespace SolarChargers
29 changes: 18 additions & 11 deletions include/VictronMppt.h → include/solarcharger/victron/Provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@
#include <memory>
#include <TaskSchedulerDeclarations.h>

#include "SolarChargerProvider.h"
#include "VeDirectMpptController.h"
#include "Configuration.h"
#include <Configuration.h>
#include <solarcharger/Provider.h>
#include <solarcharger/victron/HassIntegration.h>
#include <VeDirectMpptController.h>

class VictronMppt : public SolarChargerProvider {
namespace SolarChargers::Victron {

class Provider : public ::SolarChargers::Provider {
public:
VictronMppt() = default;
~VictronMppt() = default;
Provider() = default;
~Provider() = default;

bool init(bool verboseLogging) final;
void deinit() final;
void loop() final;
::SolarChargers::HassIntegration const& getHassIntegration() const final { return _hassIntegration; }

bool isDataValid() const final;

Expand Down Expand Up @@ -55,16 +59,19 @@ class VictronMppt : public SolarChargerProvider {
std::optional<float> getVoltage(MPPTVoltage kindOf) const;

private:
VictronMppt(VictronMppt const& other) = delete;
VictronMppt(VictronMppt&& other) = delete;
VictronMppt& operator=(VictronMppt const& other) = delete;
VictronMppt& operator=(VictronMppt&& other) = delete;
Provider(Provider const& other) = delete;
Provider(Provider&& other) = delete;
Provider& operator=(Provider const& other) = delete;
Provider& operator=(Provider&& other) = delete;

mutable std::mutex _mutex;
using controller_t = std::unique_ptr<VeDirectMpptController>;
std::vector<controller_t> _controllers;

std::vector<String> _serialPortOwners;
HassIntegration _hassIntegration;

bool initController(int8_t rx, int8_t tx, bool logging,
uint8_t instance);
};

} // namespace SolarChargers::Victron
2 changes: 1 addition & 1 deletion src/MqttHandleVedirect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "MqttHandleVedirect.h"
#include "MqttSettings.h"
#include "MessageOutput.h"
#include "SolarCharger.h"
#include <solarCharger/Controller.h>

MqttHandleVedirectClass MqttHandleVedirect;

Expand Down
2 changes: 1 addition & 1 deletion src/PowerLimiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <limits>
#include <frozen/map.h>
#include "SunPosition.h"
#include "SolarCharger.h"
#include <solarCharger/Controller.h>

static auto sBatteryPoweredFilter = [](PowerLimiterInverter const& inv) {
return !inv.isSolarPowered();
Expand Down
5 changes: 3 additions & 2 deletions src/WebApi_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "MqttHandleInverter.h"
#include "MqttHandleHuawei.h"
#include "MqttHandlePowerLimiter.h"
#include "MqttHandleVedirectHass.h"
#include "MqttHandleVedirect.h"
#include "MqttSettings.h"
#include "WebApi.h"
Expand All @@ -19,6 +18,7 @@
#include "PowerLimiter.h"
#include "PowerMeter.h"
#include <AsyncJson.h>
#include <solarcharger/Controller.h>

void WebApiMqttClass::init(AsyncWebServer& server, Scheduler& scheduler)
{
Expand Down Expand Up @@ -334,10 +334,11 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
MqttHandleBatteryHass.forceUpdate();
MqttHandleHass.forceUpdate();
MqttHandlePowerLimiterHass.forceUpdate();
MqttHandleVedirectHass.forceUpdate();

MqttHandleHuawei.forceUpdate();
MqttHandlePowerLimiter.forceUpdate();

SolarCharger.updateSettings();
MqttHandleVedirect.forceUpdate();
}

Expand Down
1 change: 0 additions & 1 deletion src/WebApi_powermeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "ArduinoJson.h"
#include "AsyncJson.h"
#include "Configuration.h"
#include "MqttHandleVedirectHass.h"
#include "MqttHandleHass.h"
#include "MqttSettings.h"
#include "PowerLimiter.h"
Expand Down
2 changes: 1 addition & 1 deletion src/WebApi_solar_charger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "WebApi_errors.h"
#include "helper.h"
#include "MqttHandlePowerLimiterHass.h"
#include "SolarCharger.h"
#include <solarCharger/Controller.h>

void WebApiSolarChargerlass::init(AsyncWebServer& server, Scheduler& scheduler)
{
Expand Down
2 changes: 1 addition & 1 deletion src/WebApi_ws_live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "Huawei_can.h"
#include "PowerMeter.h"
#include "defaults.h"
#include "SolarCharger.h"
#include <solarCharger/Controller.h>
#include <AsyncJson.h>

WebApiWsLiveClass::WebApiWsLiveClass()
Expand Down
2 changes: 1 addition & 1 deletion src/WebApi_ws_solarcharger_live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "WebApi.h"
#include "defaults.h"
#include "PowerLimiter.h"
#include "SolarCharger.h"
#include <solarCharger/Controller.h>

WebApiWsSolarChargerLiveClass::WebApiWsSolarChargerLiveClass()
: _ws("/solarchargerlivedata")
Expand Down
4 changes: 1 addition & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "Huawei_can.h"
#include "MqttHandleDtu.h"
#include "MqttHandleHass.h"
#include "MqttHandleVedirectHass.h"
#include "MqttHandleBatteryHass.h"
#include "MqttHandleInverter.h"
#include "MqttHandleInverterTotal.h"
Expand All @@ -34,7 +33,7 @@
#include "PowerMeter.h"
#include "PowerLimiter.h"
#include "defaults.h"
#include "SolarCharger.h"
#include <solarCharger/Controller.h>
#include <Arduino.h>
#include <LittleFS.h>
#include <SpiManager.h>
Expand Down Expand Up @@ -138,7 +137,6 @@ void setup()
MqttHandleInverterTotal.init(scheduler);
MqttHandleVedirect.init(scheduler);
MqttHandleHass.init(scheduler);
MqttHandleVedirectHass.init(scheduler);
MqttHandleBatteryHass.init(scheduler);
MqttHandleHuawei.init(scheduler);
MqttHandlePowerLimiter.init(scheduler);
Expand Down
Loading

0 comments on commit 20ead7c

Please sign in to comment.