remove batch

This commit is contained in:
J. Nick Koston
2025-06-06 02:10:50 +01:00
parent f5611bccb6
commit f77544c9b5
2 changed files with 514 additions and 470 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -328,90 +328,112 @@ class APIConnection : public APIServerConnection {
response.disabled_by_default = entity->is_disabled_by_default();
response.entity_category = static_cast<enums::EntityCategory>(entity->get_entity_category());
}
// Helper function to encode a message to buffer
template<typename MessageT>
static MessageInfo encode_message_to_buffer(MessageT &msg, ProtoWriteBuffer &buffer, uint32_t max_size) {
// Calculate size
uint32_t size = 0;
msg.calculate_size(size);
// Check if it fits
if (size > max_size) {
return {msg.get_message_type(), static_cast<uint16_t>(size), false};
}
// Encode directly into buffer
msg.encode(buffer);
return {msg.get_message_type(), static_cast<uint16_t>(size), true};
}
#ifdef USE_BINARY_SENSOR
static std::unique_ptr<ProtoMessage> try_send_binary_sensor_state_(EntityBase *binary_sensor);
static std::unique_ptr<ProtoMessage> try_send_binary_sensor_info_(EntityBase *binary_sensor);
static MessageInfo try_send_binary_sensor_state_(EntityBase *binary_sensor, ProtoWriteBuffer &buffer,
uint32_t max_size);
static MessageInfo try_send_binary_sensor_info_(EntityBase *binary_sensor, ProtoWriteBuffer &buffer,
uint32_t max_size);
#endif
#ifdef USE_COVER
static std::unique_ptr<ProtoMessage> try_send_cover_state_(EntityBase *cover);
static std::unique_ptr<ProtoMessage> try_send_cover_info_(EntityBase *cover);
static MessageInfo try_send_cover_state_(EntityBase *cover, ProtoWriteBuffer &buffer, uint32_t max_size);
static MessageInfo try_send_cover_info_(EntityBase *cover, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_FAN
static std::unique_ptr<ProtoMessage> try_send_fan_state_(EntityBase *fan);
static std::unique_ptr<ProtoMessage> try_send_fan_info_(EntityBase *fan);
static MessageInfo try_send_fan_state_(EntityBase *fan, ProtoWriteBuffer &buffer, uint32_t max_size);
static MessageInfo try_send_fan_info_(EntityBase *fan, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_LIGHT
static std::unique_ptr<ProtoMessage> try_send_light_state_(EntityBase *light);
static std::unique_ptr<ProtoMessage> try_send_light_info_(EntityBase *light);
static MessageInfo try_send_light_state_(EntityBase *light, ProtoWriteBuffer &buffer, uint32_t max_size);
static MessageInfo try_send_light_info_(EntityBase *light, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_SENSOR
static std::unique_ptr<ProtoMessage> try_send_sensor_state_(EntityBase *sensor);
static std::unique_ptr<ProtoMessage> try_send_sensor_info_(EntityBase *sensor);
static MessageInfo try_send_sensor_state_(EntityBase *sensor, ProtoWriteBuffer &buffer, uint32_t max_size);
static MessageInfo try_send_sensor_info_(EntityBase *sensor, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_SWITCH
static std::unique_ptr<ProtoMessage> try_send_switch_state_(EntityBase *a_switch);
static std::unique_ptr<ProtoMessage> try_send_switch_info_(EntityBase *a_switch);
static MessageInfo try_send_switch_state_(EntityBase *a_switch, ProtoWriteBuffer &buffer, uint32_t max_size);
static MessageInfo try_send_switch_info_(EntityBase *a_switch, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_TEXT_SENSOR
static std::unique_ptr<ProtoMessage> try_send_text_sensor_state_(EntityBase *text_sensor);
static std::unique_ptr<ProtoMessage> try_send_text_sensor_info_(EntityBase *text_sensor);
static MessageInfo try_send_text_sensor_state_(EntityBase *text_sensor, ProtoWriteBuffer &buffer, uint32_t max_size);
static MessageInfo try_send_text_sensor_info_(EntityBase *text_sensor, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_CLIMATE
static std::unique_ptr<ProtoMessage> try_send_climate_state_(EntityBase *climate);
static std::unique_ptr<ProtoMessage> try_send_climate_info_(EntityBase *climate);
static MessageInfo try_send_climate_state_(EntityBase *climate, ProtoWriteBuffer &buffer, uint32_t max_size);
static MessageInfo try_send_climate_info_(EntityBase *climate, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_NUMBER
static std::unique_ptr<ProtoMessage> try_send_number_state_(EntityBase *number);
static std::unique_ptr<ProtoMessage> try_send_number_info_(EntityBase *number);
static MessageInfo try_send_number_state_(EntityBase *number, ProtoWriteBuffer &buffer, uint32_t max_size);
static MessageInfo try_send_number_info_(EntityBase *number, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_DATETIME_DATE
static std::unique_ptr<ProtoMessage> try_send_date_state_(EntityBase *date);
static std::unique_ptr<ProtoMessage> try_send_date_info_(EntityBase *date);
static MessageInfo try_send_date_state_(EntityBase *date, ProtoWriteBuffer &buffer, uint32_t max_size);
static MessageInfo try_send_date_info_(EntityBase *date, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_DATETIME_TIME
static std::unique_ptr<ProtoMessage> try_send_time_state_(EntityBase *time);
static std::unique_ptr<ProtoMessage> try_send_time_info_(EntityBase *time);
static MessageInfo try_send_time_state_(EntityBase *time, ProtoWriteBuffer &buffer, uint32_t max_size);
static MessageInfo try_send_time_info_(EntityBase *time, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_DATETIME_DATETIME
static std::unique_ptr<ProtoMessage> try_send_datetime_state_(EntityBase *datetime);
static std::unique_ptr<ProtoMessage> try_send_datetime_info_(EntityBase *datetime);
static MessageInfo try_send_datetime_state_(EntityBase *datetime, ProtoWriteBuffer &buffer, uint32_t max_size);
static MessageInfo try_send_datetime_info_(EntityBase *datetime, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_TEXT
static std::unique_ptr<ProtoMessage> try_send_text_state_(EntityBase *text);
static std::unique_ptr<ProtoMessage> try_send_text_info_(EntityBase *text);
static MessageInfo try_send_text_state_(EntityBase *text, ProtoWriteBuffer &buffer, uint32_t max_size);
static MessageInfo try_send_text_info_(EntityBase *text, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_SELECT
static std::unique_ptr<ProtoMessage> try_send_select_state_(EntityBase *select);
static std::unique_ptr<ProtoMessage> try_send_select_info_(EntityBase *select);
static MessageInfo try_send_select_state_(EntityBase *select, ProtoWriteBuffer &buffer, uint32_t max_size);
static MessageInfo try_send_select_info_(EntityBase *select, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_BUTTON
static std::unique_ptr<ProtoMessage> try_send_button_info_(EntityBase *button);
static MessageInfo try_send_button_info_(EntityBase *button, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_LOCK
static std::unique_ptr<ProtoMessage> try_send_lock_state_(EntityBase *a_lock);
static std::unique_ptr<ProtoMessage> try_send_lock_info_(EntityBase *a_lock);
static MessageInfo try_send_lock_state_(EntityBase *a_lock, ProtoWriteBuffer &buffer, uint32_t max_size);
static MessageInfo try_send_lock_info_(EntityBase *a_lock, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_VALVE
static std::unique_ptr<ProtoMessage> try_send_valve_state_(EntityBase *valve);
static std::unique_ptr<ProtoMessage> try_send_valve_info_(EntityBase *valve);
static MessageInfo try_send_valve_state_(EntityBase *valve, ProtoWriteBuffer &buffer, uint32_t max_size);
static MessageInfo try_send_valve_info_(EntityBase *valve, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_MEDIA_PLAYER
static std::unique_ptr<ProtoMessage> try_send_media_player_state_(EntityBase *media_player);
static std::unique_ptr<ProtoMessage> try_send_media_player_info_(EntityBase *media_player);
static MessageInfo try_send_media_player_state_(EntityBase *media_player, ProtoWriteBuffer &buffer,
uint32_t max_size);
static MessageInfo try_send_media_player_info_(EntityBase *media_player, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_ALARM_CONTROL_PANEL
static std::unique_ptr<ProtoMessage> try_send_alarm_control_panel_state_(EntityBase *a_alarm_control_panel);
static std::unique_ptr<ProtoMessage> try_send_alarm_control_panel_info_(EntityBase *a_alarm_control_panel);
static MessageInfo try_send_alarm_control_panel_state_(EntityBase *a_alarm_control_panel, ProtoWriteBuffer &buffer,
uint32_t max_size);
static MessageInfo try_send_alarm_control_panel_info_(EntityBase *a_alarm_control_panel, ProtoWriteBuffer &buffer,
uint32_t max_size);
#endif
#ifdef USE_EVENT
static std::unique_ptr<ProtoMessage> try_send_event_info_(EntityBase *event);
static MessageInfo try_send_event_info_(EntityBase *event, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_UPDATE
static std::unique_ptr<ProtoMessage> try_send_update_state_(EntityBase *update);
static std::unique_ptr<ProtoMessage> try_send_update_info_(EntityBase *update);
static MessageInfo try_send_update_state_(EntityBase *update, ProtoWriteBuffer &buffer, uint32_t max_size);
static MessageInfo try_send_update_info_(EntityBase *update, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
#ifdef USE_ESP32_CAMERA
static std::unique_ptr<ProtoMessage> try_send_camera_info_(EntityBase *camera);
static MessageInfo try_send_camera_info_(EntityBase *camera, ProtoWriteBuffer &buffer, uint32_t max_size);
#endif
enum class ConnectionState {
@@ -449,8 +471,15 @@ class APIConnection : public APIServerConnection {
ListEntitiesIterator list_entities_iterator_;
int state_subs_at_ = -1;
// Function type that creates a ProtoMessage from an entity
using MessageCreator = std::function<std::unique_ptr<ProtoMessage>(EntityBase *)>;
// Info about an encoded message
struct MessageInfo {
uint16_t type;
uint16_t size;
bool encoded;
};
// Function type that encodes a message directly to buffer
using MessageCreator = std::function<MessageInfo(EntityBase *, ProtoWriteBuffer &, uint32_t max_size)>;
// Generic batching mechanism for both state updates and entity info
struct DeferredBatch {