mirror of
https://github.com/esphome/esphome.git
synced 2025-10-15 08:13:51 +01:00
remove std::map, only 1 or 2 callbacks in flight ever
This commit is contained in:
committed by
Jesse Hills
parent
1f557b46b3
commit
cd4c4eab35
@@ -406,27 +406,31 @@ void APIServer::send_homeassistant_action(const HomeassistantActionRequest &call
|
|||||||
}
|
}
|
||||||
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
|
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
|
||||||
void APIServer::register_action_response_callback(uint32_t call_id, ActionResponseCallback callback) {
|
void APIServer::register_action_response_callback(uint32_t call_id, ActionResponseCallback callback) {
|
||||||
this->action_response_callbacks_[call_id] = std::move(callback);
|
this->action_response_callbacks_.push_back({call_id, std::move(callback)});
|
||||||
}
|
}
|
||||||
|
|
||||||
void APIServer::handle_action_response(uint32_t call_id, bool success, const std::string &error_message) {
|
void APIServer::handle_action_response(uint32_t call_id, bool success, const std::string &error_message) {
|
||||||
auto it = this->action_response_callbacks_.find(call_id);
|
for (auto it = this->action_response_callbacks_.begin(); it != this->action_response_callbacks_.end(); ++it) {
|
||||||
if (it != this->action_response_callbacks_.end()) {
|
if (it->call_id == call_id) {
|
||||||
auto callback = std::move(it->second);
|
auto callback = std::move(it->callback);
|
||||||
this->action_response_callbacks_.erase(it);
|
this->action_response_callbacks_.erase(it);
|
||||||
auto response = std::make_shared<ActionResponse>(success, error_message);
|
ActionResponse response(success, error_message);
|
||||||
callback(response);
|
callback(response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
|
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
|
||||||
void APIServer::handle_action_response(uint32_t call_id, bool success, const std::string &error_message,
|
void APIServer::handle_action_response(uint32_t call_id, bool success, const std::string &error_message,
|
||||||
const uint8_t *response_data, size_t response_data_len) {
|
const uint8_t *response_data, size_t response_data_len) {
|
||||||
auto it = this->action_response_callbacks_.find(call_id);
|
for (auto it = this->action_response_callbacks_.begin(); it != this->action_response_callbacks_.end(); ++it) {
|
||||||
if (it != this->action_response_callbacks_.end()) {
|
if (it->call_id == call_id) {
|
||||||
auto callback = std::move(it->second);
|
auto callback = std::move(it->callback);
|
||||||
this->action_response_callbacks_.erase(it);
|
this->action_response_callbacks_.erase(it);
|
||||||
auto response = std::make_shared<ActionResponse>(success, error_message, response_data, response_data_len);
|
ActionResponse response(success, error_message, response_data, response_data_len);
|
||||||
callback(response);
|
callback(response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
|
#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
|
||||||
|
@@ -199,7 +199,11 @@ class APIServer : public Component, public Controller {
|
|||||||
std::vector<UserServiceDescriptor *> user_services_;
|
std::vector<UserServiceDescriptor *> user_services_;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
|
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
|
||||||
std::map<uint32_t, ActionResponseCallback> action_response_callbacks_;
|
struct PendingActionResponse {
|
||||||
|
uint32_t call_id;
|
||||||
|
ActionResponseCallback callback;
|
||||||
|
};
|
||||||
|
std::vector<PendingActionResponse> action_response_callbacks_;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Group smaller types together
|
// Group smaller types together
|
||||||
|
Reference in New Issue
Block a user