forked from hoylabs/OpenDTU-OnBattery
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR hoylabs#1077 from helgeerbe/powermeter-refactoring
this PowerMeter refactoring tackles many issues and prepares to solve many more.
- Loading branch information
Showing
45 changed files
with
2,871 additions
and
1,308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#pragma once | ||
|
||
#include "Configuration.h" | ||
#include <memory> | ||
#include <vector> | ||
#include <utility> | ||
#include <string> | ||
#include <HTTPClient.h> | ||
#include <WiFiClient.h> | ||
|
||
using up_http_client_t = std::unique_ptr<HTTPClient>; | ||
using sp_wifi_client_t = std::shared_ptr<WiFiClient>; | ||
|
||
class HttpRequestResult { | ||
public: | ||
HttpRequestResult(bool success, | ||
up_http_client_t upHttpClient = nullptr, | ||
sp_wifi_client_t spWiFiClient = nullptr) | ||
: _success(success) | ||
, _upHttpClient(std::move(upHttpClient)) | ||
, _spWiFiClient(std::move(spWiFiClient)) { } | ||
|
||
~HttpRequestResult() { | ||
// the wifi client *must* die *after* the http client, as the http | ||
// client uses the wifi client in its destructor. | ||
if (_upHttpClient) { _upHttpClient->end(); } | ||
_upHttpClient = nullptr; | ||
_spWiFiClient = nullptr; | ||
} | ||
|
||
HttpRequestResult(HttpRequestResult const&) = delete; | ||
HttpRequestResult(HttpRequestResult&&) = delete; | ||
HttpRequestResult& operator=(HttpRequestResult const&) = delete; | ||
HttpRequestResult& operator=(HttpRequestResult&&) = delete; | ||
|
||
operator bool() const { return _success; } | ||
|
||
Stream* getStream() { | ||
if(!_upHttpClient) { return nullptr; } | ||
return _upHttpClient->getStreamPtr(); | ||
} | ||
|
||
private: | ||
bool _success; | ||
up_http_client_t _upHttpClient; | ||
sp_wifi_client_t _spWiFiClient; | ||
}; | ||
|
||
class HttpGetter { | ||
public: | ||
explicit HttpGetter(HttpRequestConfig const& cfg) | ||
: _config(cfg) { } | ||
|
||
bool init(); | ||
void addHeader(char const* key, char const* value); | ||
HttpRequestResult performGetRequest(); | ||
|
||
char const* getErrorText() const { return _errBuffer; } | ||
|
||
private: | ||
String getAuthDigest(String const& authReq, unsigned int counter); | ||
HttpRequestConfig const& _config; | ||
|
||
template<typename... Args> | ||
void logError(char const* format, Args... args); | ||
char _errBuffer[256]; | ||
|
||
bool _useHttps; | ||
String _host; | ||
String _uri; | ||
uint16_t _port; | ||
|
||
sp_wifi_client_t _spWiFiClient; // reused for multiple HTTP requests | ||
|
||
std::vector<std::pair<std::string, std::string>> _additionalHeaders; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,27 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#pragma once | ||
|
||
#include "Configuration.h" | ||
#include <espMqttClient.h> | ||
#include <Arduino.h> | ||
#include <map> | ||
#include <list> | ||
#include <mutex> | ||
#include "SDM.h" | ||
#include "sml.h" | ||
#include "PowerMeterProvider.h" | ||
#include <TaskSchedulerDeclarations.h> | ||
#include <SoftwareSerial.h> | ||
|
||
typedef struct { | ||
const unsigned char OBIS[6]; | ||
void (*Fn)(double&); | ||
float* Arg; | ||
} OBISHandler; | ||
#include <memory> | ||
#include <mutex> | ||
|
||
class PowerMeterClass { | ||
public: | ||
enum class Source : unsigned { | ||
MQTT = 0, | ||
SDM1PH = 1, | ||
SDM3PH = 2, | ||
HTTP = 3, | ||
SML = 4, | ||
SMAHM2 = 5 | ||
}; | ||
void init(Scheduler& scheduler); | ||
float getPowerTotal(bool forceUpdate = true); | ||
uint32_t getLastPowerMeterUpdate(); | ||
bool isDataValid(); | ||
|
||
void updateSettings(); | ||
|
||
float getPowerTotal() const; | ||
uint32_t getLastUpdate() const; | ||
bool isDataValid() const; | ||
|
||
private: | ||
void loop(); | ||
void mqtt(); | ||
|
||
void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, | ||
const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total); | ||
|
||
Task _loopTask; | ||
|
||
bool _verboseLogging = true; | ||
uint32_t _lastPowerMeterCheck; | ||
// Used in Power limiter for safety check | ||
uint32_t _lastPowerMeterUpdate; | ||
|
||
float _powerMeter1Power = 0.0; | ||
float _powerMeter2Power = 0.0; | ||
float _powerMeter3Power = 0.0; | ||
float _powerMeter1Voltage = 0.0; | ||
float _powerMeter2Voltage = 0.0; | ||
float _powerMeter3Voltage = 0.0; | ||
float _powerMeterImport = 0.0; | ||
float _powerMeterExport = 0.0; | ||
|
||
std::map<String, float*> _mqttSubscriptions; | ||
|
||
mutable std::mutex _mutex; | ||
|
||
static char constexpr _sdmSerialPortOwner[] = "SDM power meter"; | ||
std::unique_ptr<HardwareSerial> _upSdmSerial = nullptr; | ||
std::unique_ptr<SDM> _upSdm = nullptr; | ||
std::unique_ptr<SoftwareSerial> _upSmlSerial = nullptr; | ||
|
||
void readPowerMeter(); | ||
|
||
bool smlReadLoop(); | ||
const std::list<OBISHandler> smlHandlerList{ | ||
{{0x01, 0x00, 0x10, 0x07, 0x00, 0xff}, &smlOBISW, &_powerMeter1Power}, | ||
{{0x01, 0x00, 0x01, 0x08, 0x00, 0xff}, &smlOBISWh, &_powerMeterImport}, | ||
{{0x01, 0x00, 0x02, 0x08, 0x00, 0xff}, &smlOBISWh, &_powerMeterExport} | ||
}; | ||
std::unique_ptr<PowerMeterProvider> _upProvider = nullptr; | ||
}; | ||
|
||
extern PowerMeterClass PowerMeter; |
Oops, something went wrong.