* fix compiler warning in SerialPortManager.cpp: function must not return void * clean up and simplify implementation of usesHwPort2() * make const * overrides are final * default implementation returns false * implement in header, as the implementation is very simple * rename PortManager to SerialPortManager. as "PortManager" is too generic, the static instance of the serial port manager is renamed to "SerialPortManager". the class is therefore renamed to SerialPortManagerClass, which is in line with other (static) classes withing OpenDTU(-OnBattery). * implement separate data ages for MPPT charge controllers * make sure MPPT data and live data time out * do not use invalid data of MPPT controlers for calculations * add :key binding to v-for iterating over MPPT instances
91 lines
2.3 KiB
C++
91 lines
2.3 KiB
C++
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
#include "Battery.h"
|
|
#include "MessageOutput.h"
|
|
#include "PylontechCanReceiver.h"
|
|
#include "JkBmsController.h"
|
|
#include "VictronSmartShunt.h"
|
|
#include "MqttBattery.h"
|
|
#include "SerialPortManager.h"
|
|
|
|
BatteryClass Battery;
|
|
|
|
std::shared_ptr<BatteryStats const> BatteryClass::getStats() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
if (!_upProvider) {
|
|
static auto sspDummyStats = std::make_shared<BatteryStats>();
|
|
return sspDummyStats;
|
|
}
|
|
|
|
return _upProvider->getStats();
|
|
}
|
|
|
|
void BatteryClass::init(Scheduler& scheduler)
|
|
{
|
|
scheduler.addTask(_loopTask);
|
|
_loopTask.setCallback(std::bind(&BatteryClass::loop, this));
|
|
_loopTask.setIterations(TASK_FOREVER);
|
|
_loopTask.enable();
|
|
|
|
this->updateSettings();
|
|
}
|
|
|
|
void BatteryClass::updateSettings()
|
|
{
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
if (_upProvider) {
|
|
_upProvider->deinit();
|
|
_upProvider = nullptr;
|
|
}
|
|
SerialPortManager.invalidateBatteryPort();
|
|
|
|
CONFIG_T& config = Configuration.get();
|
|
if (!config.Battery.Enabled) { return; }
|
|
|
|
bool verboseLogging = config.Battery.VerboseLogging;
|
|
|
|
switch (config.Battery.Provider) {
|
|
case 0:
|
|
_upProvider = std::make_unique<PylontechCanReceiver>();
|
|
break;
|
|
case 1:
|
|
_upProvider = std::make_unique<JkBms::Controller>();
|
|
break;
|
|
case 2:
|
|
_upProvider = std::make_unique<MqttBattery>();
|
|
break;
|
|
case 3:
|
|
_upProvider = std::make_unique<VictronSmartShunt>();
|
|
break;
|
|
default:
|
|
MessageOutput.printf("Unknown battery provider: %d\r\n", config.Battery.Provider);
|
|
return;
|
|
}
|
|
|
|
if(_upProvider->usesHwPort2()) {
|
|
if (!SerialPortManager.allocateBatteryPort(2)) {
|
|
MessageOutput.printf("[Battery] Serial port %d already in use. Initialization aborted!\r\n", 2);
|
|
_upProvider = nullptr;
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (!_upProvider->init(verboseLogging)) {
|
|
SerialPortManager.invalidateBatteryPort();
|
|
_upProvider = nullptr;
|
|
}
|
|
}
|
|
|
|
void BatteryClass::loop()
|
|
{
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
if (!_upProvider) { return; }
|
|
|
|
_upProvider->loop();
|
|
|
|
_upProvider->getStats()->mqttLoop();
|
|
}
|