feat: add state change events and callbacks (#798)

This commit is contained in:
laride
2025-07-18 01:35:31 +08:00
committed by GitHub
parent c68c959e9b
commit 5c8707075f
7 changed files with 109 additions and 14 deletions

39
main/device_state_event.h Normal file
View File

@@ -0,0 +1,39 @@
#ifndef _DEVICE_STATE_EVENT_H_
#define _DEVICE_STATE_EVENT_H_
#include <esp_event.h>
#include <functional>
#include <vector>
#include <mutex>
#include "device_state.h"
ESP_EVENT_DECLARE_BASE(XIAOZHI_STATE_EVENTS);
enum {
XIAOZHI_STATE_CHANGED_EVENT,
};
struct device_state_event_data_t {
DeviceState previous_state;
DeviceState current_state;
};
class DeviceStateEventManager {
public:
static DeviceStateEventManager& GetInstance();
DeviceStateEventManager(const DeviceStateEventManager&) = delete;
DeviceStateEventManager& operator=(const DeviceStateEventManager&) = delete;
void RegisterStateChangeCallback(std::function<void(DeviceState, DeviceState)> callback);
void PostStateChangeEvent(DeviceState previous_state, DeviceState current_state);
std::vector<std::function<void(DeviceState, DeviceState)>> GetCallbacks();
private:
DeviceStateEventManager();
~DeviceStateEventManager();
std::vector<std::function<void(DeviceState, DeviceState)>> callbacks_;
std::mutex mutex_;
};
#endif // _DEVICE_STATE_EVENT_H_