Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure solarcharger classes #1457

Draft
wants to merge 4 commits into
base: rename-vedirect-to-solarcharger
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions include/MqttHandleVedirect.h

This file was deleted.

48 changes: 0 additions & 48 deletions include/SolarCharger.h

This file was deleted.

36 changes: 0 additions & 36 deletions include/SolarChargerProvider.h

This file was deleted.

70 changes: 0 additions & 70 deletions include/VictronMppt.h

This file was deleted.

3 changes: 0 additions & 3 deletions include/WebApi_ws_solarcharger_live.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@ class WebApiWsSolarChargerLiveClass {

private:
void generateCommonJsonResponse(JsonVariant& root, bool fullUpdate);
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);

AsyncWebServer* _server;
AsyncWebSocket _ws;
AuthenticationMiddleware _simpleDigestAuth;

uint32_t _lastFullPublish = 0;
uint32_t _lastPublish = 0;
uint16_t responseSize() const;

std::mutex _mutex;

Expand Down
31 changes: 31 additions & 0 deletions include/solarcharger/Controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include <memory>
#include <mutex>
#include <TaskSchedulerDeclarations.h>
#include <solarcharger/Provider.h>
#include <solarcharger/Stats.h>
#include <VeDirectMpptController.h>

namespace SolarChargers {

class Controller {
public:
void init(Scheduler&);
void updateSettings();

std::shared_ptr<Stats const> getStats() const;

private:
void loop();

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

} // namespace SolarChargers

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

#include <solarcharger/Stats.h>

namespace SolarChargers {

class DummyStats : public Stats {
public:
uint32_t getAgeMillis() const override { return 0; }
std::optional<int32_t> getOutputPowerWatts() const override { return std::nullopt; }
std::optional<float> getOutputVoltage() const override { return std::nullopt; }
int32_t getPanelPowerWatts() const override { return 0; }
float getYieldTotal() const override { return 0; }
float getYieldDay() const override { return 0; }
void getLiveViewData(JsonVariant& root, boolean fullUpdate, uint32_t lastPublish) const override {}
void mqttPublish() const override {}
};

} // namespace SolarChargers
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
21 changes: 21 additions & 0 deletions include/solarcharger/Provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include <VeDirectMpptController.h>

namespace SolarChargers {

class Stats;
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 std::shared_ptr<Stats> getStats() const = 0;
virtual HassIntegration const& getHassIntegration() const = 0;
};

} // namespace SolarChargers
45 changes: 45 additions & 0 deletions include/solarcharger/Stats.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include <Arduino.h>
#include <AsyncJson.h>

namespace SolarChargers {

class Stats {
public:
// the last time *any* data was updated
virtual uint32_t getAgeMillis() const;

// total output of all MPPT charge controllers in Watts
virtual std::optional<int32_t> getOutputPowerWatts() const;

// minimum of all MPPT charge controllers' output voltages in V
virtual std::optional<float> getOutputVoltage() const;

// total panel input power of all MPPT charge controllers in Watts
virtual int32_t getPanelPowerWatts() const;

// sum of total yield of all MPPT charge controllers in kWh
virtual float getYieldTotal() const;

// sum of today's yield of all MPPT charge controllers in Wh
virtual float getYieldDay() const;

// convert stats to JSON for web application live view
virtual void getLiveViewData(JsonVariant& root, boolean fullUpdate, uint32_t lastPublish) const;

void mqttLoop();

// the interval at which all data will be re-published, even
// if they did not change. used to calculate Home Assistent expiration.
uint32_t getMqttFullPublishIntervalMs() const;

protected:
virtual void mqttPublish() const;

private:
uint32_t _lastMqttPublish = 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::Victron
Loading