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

PowerMeter fixes #342

Merged
merged 5 commits into from
Jul 31, 2023
Merged

Conversation

schlimmchen
Copy link
Member

When subscribing to a topic whose payload cannot be parsed as a float value, an exception would be triggered in the PowerMeter implementation. The code in this PR catches the exception and handles it gracefully:

Received MQTT message on topic: solar-staging/dtu/status
PowerMeterClass: cannot parse payload of topic 'solar-staging/dtu/status' as float: online

Make sure that the timestamp _lastPowerMeterUpdate is only updated by the MQTT callback if a topic was processed that actually matches one of the subscribed topics.

Upon changing the MQTT settings and submitting the new settings through the web application, PowerMeter::init() is called. That probably makes sense so it can be ensured that the topics are subscribed to using a possible new broker. However, the old subscriptions were not cancelled, resulting in the callback being called n+1 times after n changes to the MQTT settings.

Received MQTT message on topic: grid/power/phase1/watts
PowerMeterClass: Updated from 'grid/power/phase1/watts', TotalPower: -10.00
PowerMeterClass: Updated from 'grid/power/phase1/watts', TotalPower: -10.00
PowerMeterClass: Updated from 'grid/power/phase1/watts', TotalPower: -10.00
Received MQTT message on topic: grid/power/phase2/watts
PowerMeterClass: Updated from 'grid/power/phase2/watts', TotalPower: -10.00
PowerMeterClass: Updated from 'grid/power/phase2/watts', TotalPower: -10.00
PowerMeterClass: Updated from 'grid/power/phase2/watts', TotalPower: -10.00

This problem is resolved by unsubscribing from all subscribed topics, if any, before resubscribing.

The subscriptions are organized in a std::map, which allows iterating them, which in turn allows for slightly more elegant code and less code overall.

Removed an unused member variable and moved private function to the private section of the class declaration.

Use a switch-case-statement over an if-else construct in init().

update the timestampt only if the topic actually matched any
subscription and if the value could be parsed as a float.
this allows for a slightly more elegant code and reduced amount of code
overall.
* move private methods to private section of class declaration.
* remove unused member variable.
@schlimmchen
Copy link
Member Author

This might fix #340.

@helgeerbe helgeerbe merged commit 6b425d9 into hoylabs:development Jul 31, 2023
@schlimmchen schlimmchen deleted the powermeter-fixes branch July 31, 2023 12:20
Copy link

github-actions bot commented Apr 5, 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 5, 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.

2 participants