forked from tbnobody/OpenDTU
-
-
Notifications
You must be signed in to change notification settings - Fork 102
Feature: add support for Grid Charger via smart-plug using HTTP GET #2347
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
Open
Snoopy-HSS
wants to merge
11
commits into
hoylabs:development
Choose a base branch
from
DTUBAT:development
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b7a5a32
Neues Modul für Shelly etc HTTP Plugs.
Snoopy-HSS 45d3b39
Prettier Probleme behoben
Snoopy-HSS 5b6b1e2
Typo in locales fixed
Snoopy-HSS 5697f82
Updated provider to prevend ON/OFF toggle when soc limit is reached.
Snoopy-HSS 2a709c6
Stop at SoC reached
Snoopy-HSS 942a161
Merge branch 'hoylabs:development' into development
Snoopy-HSS 0840c75
Merge branch 'hoylabs:development' into development
Snoopy-HSS 08ff9a8
Die wünsche des Hasen erfüllt
Snoopy-HSS d347020
So noch mal dem Hasen zu Liebe
Snoopy-HSS 3dcf087
so lieber @coderabbitai hoffe das passt so, ich habe die Änderungen i…
Snoopy-HSS 9998a21
Merge branch 'hoylabs:development' into development
Snoopy-HSS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,33 @@ | ||
| #pragma once | ||
|
|
||
| #include <DataPoints.h> | ||
| #include <string> | ||
|
|
||
| namespace GridChargers::HTTP { | ||
|
|
||
| enum class DataPointLabel : uint8_t { | ||
| AcPowerSetpoint, | ||
| AcPower, | ||
| }; | ||
|
|
||
| template<DataPointLabel> struct DataPointLabelTraits; | ||
|
|
||
| #define LABEL_TRAIT(n, t, u) template<> struct DataPointLabelTraits<DataPointLabel::n> { \ | ||
| using type = t; \ | ||
| static constexpr char const name[] = #n; \ | ||
| static constexpr char const unit[] = u; \ | ||
| }; | ||
|
|
||
| LABEL_TRAIT(AcPowerSetpoint, float, "W"); | ||
| LABEL_TRAIT(AcPower, float, "W"); | ||
| #undef LABEL_TRAIT | ||
|
|
||
| } // namespace GridChargers::HTTP | ||
|
|
||
| template class DataPointContainer<DataPoint<float, std::string>, | ||
| GridChargers::HTTP::DataPointLabel, | ||
| GridChargers::HTTP::DataPointLabelTraits>; | ||
|
|
||
| namespace GridChargers::HTTP { | ||
| using DataPointContainer = ::DataPointContainer<::DataPoint<float, std::string>, DataPointLabel, DataPointLabelTraits>; | ||
| } // namespace GridChargers::HTTP | ||
|
Snoopy-HSS marked this conversation as resolved.
Outdated
|
||
This file contains hidden or 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,103 @@ | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
| #pragma once | ||
|
|
||
| #include <atomic> | ||
| #include <gridcharger/Provider.h> | ||
| #include <gridcharger/HTTP/Stats.h> | ||
| #include <HttpGetter.h> | ||
| #include <Utils.h> | ||
|
|
||
| namespace GridChargers::HTTP { | ||
|
|
||
| class Provider : public ::GridChargers::Provider { | ||
| public: | ||
| bool init() final; | ||
| void deinit() final; | ||
| void loop() final; | ||
| void PowerON(); | ||
| void PowerOFF(); | ||
| bool send_http(String Url); | ||
| float read_http(String uri); | ||
|
Snoopy-HSS marked this conversation as resolved.
Outdated
|
||
| bool powerstate = false; | ||
| float acPowerCurrent=0; | ||
| String uri_on; | ||
| String uri_off; | ||
| String uri_stats; | ||
| String uri_powerparam; | ||
| float maximumAcPower; | ||
|
Snoopy-HSS marked this conversation as resolved.
Outdated
|
||
| std::shared_ptr<::GridChargers::Stats> getStats() const final { return _stats; } | ||
|
|
||
| bool getAutoPowerStatus() const final { | ||
| return _requestedPowerAc > 0 | ||
| || _dataCurrent.get<DataPointLabel::AcPowerSetpoint>().value_or(0) > 0.0f; | ||
| } | ||
|
|
||
| private: | ||
| template<DataPointLabel L> | ||
| void addStringToDataPoints(const JsonDocument& doc, const char* key) | ||
| { | ||
| if (doc[key].is<std::string>()) { | ||
| _dataCurrent.add<L>(doc[key].as<std::string>()); | ||
| return; | ||
| } | ||
|
|
||
| if (doc[key].is<const char*>()) { | ||
| _dataCurrent.add<L>(std::string{doc[key].as<const char*>()}); | ||
| } | ||
| } | ||
|
|
||
| template<DataPointLabel L> | ||
| void addFloatToDataPoints(const JsonDocument& doc, const char* key) | ||
| { | ||
| std::optional<float> value; | ||
|
|
||
| if (doc[key].is<float>()) { | ||
| value = doc[key].as<float>(); | ||
| } | ||
|
|
||
| if (doc[key].is<char const*>()) { | ||
| value = Utils::getFromString<float>(doc[key].as<char const*>()); | ||
| } | ||
|
|
||
| if (!value.has_value()) { return; } | ||
| _dataCurrent.add<L>(*value); | ||
| } | ||
|
|
||
| void powerControlLoop(); | ||
| static void dataPollingLoopHelper(void* context); | ||
| void dataPollingLoop(); | ||
| void pollData(); | ||
|
|
||
| TaskHandle_t _dataPollingTaskHandle = nullptr; | ||
| std::atomic<bool> _dataPollingTaskDone = false; | ||
| bool _stopPollingData = false; | ||
| mutable std::mutex _dataPollingMutex; | ||
| std::condition_variable _dataPollingCv; | ||
| uint32_t _lastDataPoll = 0; | ||
|
|
||
| std::unique_ptr<HttpRequestConfig> _httpRequestConfig; | ||
| std::unique_ptr<HttpGetter> _httpGetter; | ||
|
|
||
| static constexpr int DATA_POLLING_INTERVAL_MS = 5000; // 3 seconds | ||
|
Snoopy-HSS marked this conversation as resolved.
Outdated
|
||
| static constexpr int HTTP_REQUEST_TIMEOUT_MS = 500; // 500ms | ||
|
|
||
| float _requestedPowerAc = 0; | ||
|
|
||
| void parseControlCommandResponse(); | ||
|
|
||
| uint32_t _lastControlCommandRequestMillis = 0; | ||
| uint32_t _lastparseControlCommandRequestMillis = 0; | ||
| static constexpr int CONTROL_COMMAND_INTERVAL_MS = 500; // 500ms | ||
|
|
||
| std::shared_ptr<Stats> _stats = std::make_shared<Stats>(); | ||
|
|
||
| DataPointContainer _dataCurrent; | ||
|
|
||
| uint32_t _lastPowerMeterUpdateReceivedMillis = 0; // Timestamp of last seen power meter value | ||
| uint32_t _autoModeBlockedTillMillis = 0; // Timestamp to block running auto mode for some time | ||
|
|
||
| bool _autoPowerEnabled = false; | ||
| bool _batteryEmergencyCharging = false; | ||
| }; | ||
|
|
||
| } // namespace GridChargers::HTTP | ||
This file contains hidden or 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,50 @@ | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
| #pragma once | ||
|
|
||
| #include <gridcharger/Stats.h> | ||
| #include <gridcharger/HTTP/DataPoints.h> | ||
|
|
||
| namespace GridChargers::HTTP { | ||
|
|
||
| class Stats : public ::GridChargers::Stats { | ||
| friend class Provider; | ||
|
|
||
| public: | ||
| uint32_t getLastUpdate() const final; | ||
|
|
||
| std::optional<float> getInputPower() const final; | ||
|
|
||
| void getLiveViewData(JsonVariant& root) const final; | ||
|
|
||
| protected: | ||
| void mqttPublish() const final {} | ||
|
|
||
| void updateFrom(DataPointContainer const& dataPoints) const; | ||
|
|
||
| private: | ||
| template<DataPointLabel L> | ||
| void addValueInSection(JsonVariant& root, | ||
| std::string const& section, std::string const& name, | ||
| int precision = 2) const | ||
| { | ||
| auto oVal = _dataPoints.get<L>(); | ||
| if (!oVal) { return; } | ||
|
|
||
| ::GridChargers::Stats::addValueInSection(root, section, name, *oVal, DataPointLabelTraits<L>::unit, precision); | ||
| } | ||
|
|
||
| template<DataPointLabel L> | ||
| void addStringInSection(JsonVariant& root, | ||
| std::string const& section, std::string const& name, | ||
| bool translate = true) const | ||
| { | ||
| auto oVal = _dataPoints.get<L>(); | ||
| if (!oVal) { return; } | ||
|
|
||
| ::GridChargers::Stats::addStringInSection(root, section, name, *oVal, translate); | ||
| } | ||
|
|
||
| mutable DataPointContainer _dataPoints; | ||
| }; | ||
|
|
||
| } // namespace GridChargers::HTTP |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.