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

re-factoring of HttpPowerMeter #594

Merged
merged 8 commits into from
Jan 16, 2024
Merged

re-factoring of HttpPowerMeter #594

merged 8 commits into from
Jan 16, 2024

Conversation

Fribur
Copy link

@Fribur Fribur commented Jan 4, 2024

Added ability to deal with local host names (mDNS), remove use of FirebasedJson to save ~20kB build size, some changes to PowerLimiter to avoid setting new inverter power limits when not needed (=current limit as reported by inverter is within hysteresis)

Fribur added 2 commits January 4, 2024 16:20
Added ability to deal with local host names (mDNS), remove use of FirebasedJson to save ~20kB build size, some changes to PowerLimiter to avoid setting new inverter power limits when not needed (=current limit as reported by inverter is within hysteresis)
@helgeerbe
Copy link
Collaborator

Can we remove in platformio.ini mobizt/FirebaseJson @ ^3.0.6 or is it used elsewhere?

@Fribur
Copy link
Author

Fribur commented Jan 4, 2024

Indeed, I cannot see FirebaseJson used anywhere else, so can be removed from platformio.ini. Done in last commit.

Copy link
Member

@schlimmchen schlimmchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate that you switched the JSON parsing to a library that is already available, saving so much code. Good job! This PR is hard to do a review on because you changed a lot at once. The refactoring you did is desirable and a refactoring is always harder to review.

src/HttpPowerMeter.cpp Outdated Show resolved Hide resolved
src/HttpPowerMeter.cpp Outdated Show resolved Hide resolved
src/HttpPowerMeter.cpp Outdated Show resolved Hide resolved
src/HttpPowerMeter.cpp Show resolved Hide resolved
src/PowerLimiter.cpp Outdated Show resolved Hide resolved
src/WebApi_powermeter.cpp Outdated Show resolved Hide resolved
…tpPowerMeter

For non IP address URLs, HttpPowerMeter now first tries DNS for resolution as done in WifiClient::connect, and only if that fails tries mDNS. For the latter to work mDNS needs to be enabled in settings. Log in console if mDNS is disabled. String building for Digest authorization still tries to avoid "+" for reasons outlined in https://cpp4arduino.com/2020/02/07/how-to-format-strings-without-the-string-class.html This should also be saver than just concatenating user input strings in preventing format string attacks. https://owasp.org/www-community/attacks/Format_string_attack
@Fribur
Copy link
Author

Fribur commented Jan 5, 2024

OK I made a new commit, hopefully addressing all your requests. Do you see the commits? Thanks a bunch for the review!

Fribur added 3 commits January 5, 2024 14:36
OpenDTU console gets spammed with "WifiGeneric::hostByName() error when first trying to resolve the hostname via DNS. So reverse order: first try mDNS, if that fails try DNS. Also ensure that https bool is passed correctly to HTTPClient::begin(). Lastly, concatenate strings for building Digest authorization using "+" and not via snprintf.
@Fribur Fribur requested a review from schlimmchen January 6, 2024 23:32
@helgeerbe
Copy link
Collaborator

@Fribur I'm sorry. With the latest merge of the upstream code, we have a merge conflict. Can you resolve it.
For the rest, is it ready to be merged into development?

@Fribur
Copy link
Author

Fribur commented Jan 8, 2024

Thanks for notifying me about the conflict...did not realize the ball is on my end to resolve. I just did. From my perspective this pull request is final. It is working reliably with those changes now since 2 days on my end. Please note however I cannot test the refactored digest authorization as I got no Digest compatible Power Meter (Shelly EM can only do basic authorization). So if you could verify Digest authorization after the merge that would be great!

@helgeerbe
Copy link
Collaborator

Thanks for resolving the conflicts. I know, I'm a bit lazy, but you know your code, better than me. I own, only a normal Sehelly EM, but I have a Shelly PM 1 plus (which support digest auth). I will give it a try.

@helgeerbe
Copy link
Collaborator

Just installed this PR locally.
I get successful requests but see sometime also:

  • http error connection lost
  • Bad HTTP code: 401
    I use basic authentication.

But in the past I saw also connection problems to my shelly EM

@Fribur
Copy link
Author

Fribur commented Jan 8, 2024

Exactly those errors (besides the mDNS issue) prompted me to begin looking into re-factoring HttpPowerMeter: see issue I opened prior to the re-ractoring. #579 Regretably this is not improved via this Pull Request.

I came to realize this is only related to the frequency of querying the PowerMeter (I believe it is already limited to once per second in PowerLimiter class) and the configured timeout. When I set it to 2000ms via the WebInterface it works in 99% of the cases. Maybe this can be reduced by setting different timeouts for "Connect" and "Response"? What are reasonable values? Currently it is implemented like this:

httpClient.setConnectTimeout(timeout);
httpClient.setTimeout(timeout);

It might also have something to with the new threading/scheduling? Not quite sure which thread is responsible for the WIFI, the webinterface etc.

Lastly: I also tried to query both phases of the Shelly EM with the exact same HTTP client with ONE connection (to reduce the connection refused lost issues): does not work, as Shelly EM forces "connection close" after each and every response. So each query needs to be done with a new http connection :-(

@helgeerbe helgeerbe merged commit e136e09 into hoylabs:development Jan 16, 2024
2 of 6 checks passed
Copy link

github-actions bot commented Apr 4, 2024

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new discussion or issue for related concerns.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants