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

Fix: switch context when handling AC charger MQTT messages #571

Conversation

schlimmchen
Copy link
Member

MQTT message callbacks are executed in the MQTT thread context. when processing topics that control the huawei AC charger, we must avoid executing methods that are not thread-safe. this change bound the methods to be called to the respective parameters and executes them in the TaskScheduler context, such that they no longer need to be thread-safe.

I tested this change by publishing values to the respective topics and generating a log message in the respective HuaweiClass method. Hence I am highly confident that this change does only what it is intended to do: make sure that the functions are executed in the TaskScheduler context, i.e., within the MqttHandleHuaweiClass::loop() method.

@schlimmchen schlimmchen marked this pull request as draft December 30, 2023 17:07
MQTT message callbacks are executed in the MQTT thread context. when
processing topics that control the huawei AC charger, we must avoid
executing methods that are not thread-safe. this change bound the
methods to be called to the respective parameters and executes them
in the TaskScheduler context, such that they no longer need to be
thread-safe.
* bind the callback to a topic (enum value) such that there is no need
  to tokenize the full topic (string) to find out what value is being
  processed. tokenizing is expensive.
* get rid of using the config in the callback, which improves
  thread-safety since the MQTT callback is running in the MQTT thread.
* prefer C++ method stof to convert MQTT value to a float, which saves
  us from using new and delete for a buffer in particular.
* prefer switch statements over if-else-trees.
* split long lines.
* get rid of topic #defines.
* fix indention.
@schlimmchen schlimmchen force-pushed the switch-context-on-huawei-mqtt-message branch from 828128a to 4632260 Compare December 30, 2023 17:45
@schlimmchen
Copy link
Member Author

I had the urge to clean my own changes for the context switch up a little and also to clean the whole class up quite a bit.

The changes were re-tested, so my statement in the description is still true: I am highly confident that this change is working well.

@schlimmchen schlimmchen marked this pull request as ready for review December 30, 2023 17:48
@schlimmchen
Copy link
Member Author

@helgeerbe Would you consider not squashing the commits when merging, such that the first one appears as a fix in the changelog and the second one appears in the section "under the hood"?

@helgeerbe
Copy link
Collaborator

@schlimmchen sure, I do a simple merge

@helgeerbe helgeerbe merged commit ef1aec3 into hoylabs:development Dec 31, 2023
8 checks passed
@schlimmchen schlimmchen deleted the switch-context-on-huawei-mqtt-message branch December 31, 2023 14:29
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.

2 participants