mirror of
https://github.com/esphome/esphome.git
synced 2025-08-07 10:57:46 +00:00
fix
This commit is contained in:
parent
490ca8ad5a
commit
c96ffefa42
@ -69,29 +69,29 @@ class OTAComponent : public Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/** Thread-safe callback manager that automatically defers to main loop.
|
/** Thread-safe callback wrapper that automatically defers to main loop.
|
||||||
*
|
*
|
||||||
* This ensures all OTA callbacks are executed in the main loop task,
|
* This ensures all OTA callbacks are executed in the main loop task,
|
||||||
* making them safe to call from any context (including web_server's OTA task).
|
* making them safe to call from any context (including web_server's OTA task).
|
||||||
* Existing code doesn't need changes - callbacks are automatically deferred.
|
* Existing code doesn't need changes - callbacks are automatically deferred.
|
||||||
*/
|
*/
|
||||||
class DeferredCallbackManager : public CallbackManager<void(OTAState, float, uint8_t)> {
|
class StateCallbackManager {
|
||||||
public:
|
public:
|
||||||
DeferredCallbackManager(OTAComponent *component) : component_(component) {}
|
StateCallbackManager(OTAComponent *component) : component_(component) {}
|
||||||
|
|
||||||
|
void add(std::function<void(OTAState, float, uint8_t)> &&callback) { callbacks_.add(std::move(callback)); }
|
||||||
|
|
||||||
/// Override call to automatically defer to main loop
|
|
||||||
void call(OTAState state, float progress, uint8_t error) {
|
void call(OTAState state, float progress, uint8_t error) {
|
||||||
// Always defer to main loop for thread safety
|
// Always defer to main loop for thread safety
|
||||||
component_->defer([this, state, progress, error]() {
|
component_->defer([this, state, progress, error]() { this->callbacks_.call(state, progress, error); });
|
||||||
CallbackManager<void(OTAState, float, uint8_t)>::call(state, progress, error);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
OTAComponent *component_;
|
OTAComponent *component_;
|
||||||
|
CallbackManager<void(OTAState, float, uint8_t)> callbacks_;
|
||||||
};
|
};
|
||||||
|
|
||||||
DeferredCallbackManager state_callback_{this};
|
StateCallbackManager state_callback_{this};
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -114,10 +114,10 @@ class OTAGlobalCallback {
|
|||||||
OTAGlobalCallback *get_global_ota_callback();
|
OTAGlobalCallback *get_global_ota_callback();
|
||||||
void register_ota_platform(OTAComponent *ota_caller);
|
void register_ota_platform(OTAComponent *ota_caller);
|
||||||
|
|
||||||
// Thread-safe callback execution is automatically provided by DeferredCallbackManager
|
// Thread-safe callback execution is automatically provided by StateCallbackManager
|
||||||
// which overrides call() to use Component::defer(). This ensures all OTA callbacks
|
// which uses Component::defer() to ensure all OTA callbacks run in the main loop task.
|
||||||
// run in the main loop task, making them safe to call from any context including
|
// This makes OTA callbacks safe to call from any context including web_server's
|
||||||
// web_server's separate OTA task. No code changes needed.
|
// separate OTA task. No code changes needed.
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
} // namespace ota_base
|
} // namespace ota_base
|
||||||
|
Loading…
x
Reference in New Issue
Block a user