Home IO Control
ESPHome add-on for IO-Homecontrol devices
Loading...
Searching...
No Matches
operation_queue.h
Go to the documentation of this file.
1#pragma once
2
3/// @file operation_queue.h
4/// @brief Pending-operation queue with per-type coalescing and deduplication.
5/// @ingroup hioc_hub
6///
7/// Owns the PendingOperationType / PendingOperation types and the coalescing
8/// rules applied when operations are enqueued. Pure data and logic — no ESPHome
9/// dependencies, fully host-testable.
10
11#include "proto_device_model.h"
12
13#include <cstdint>
14#include <deque>
15#include <optional>
16#include <string>
17
18namespace esphome {
19namespace home_io_control {
20
21/// Position value written for binary ON commands (light on, switch on, lock unlock).
22static constexpr uint8_t BINARY_ENTITY_ON_POSITION = 0;
23/// Position value written for binary OFF commands (light off, switch off, lock lock).
24static constexpr uint8_t BINARY_ENTITY_OFF_POSITION = 100;
25
26/// @brief Discriminator for entries in the pending-operation deque.
27enum class PendingOperationType : uint8_t {
28 SET_POSITION, ///< set_device_position call (position 0–100 or special values).
29 SET_TILT, ///< set_device_tilt call (tilt percentage 0–100).
30 SET_POSITION_AND_TILT, ///< Combined set_device_position_and_tilt call.
31 DEVICE_COMMAND, ///< Named device command (STOP, FAVORITE, VENT).
32 SET_LIGHT_STATE, ///< set_light_state call (binary on/off).
33 SET_LOCK_STATE, ///< set_lock_state call (locked/unlocked).
34 SET_SWITCH_STATE, ///< set_switch_state call (binary on/off).
35 REQUEST_STATUS, ///< request_device_status call (poll for current position).
36 REQUEST_NAME, ///< request_device_name call (poll for stored device name).
37 DISCOVER_AND_PAIR, ///< discover_and_pair call (starts 3-phase pairing flow).
38};
39
40/// @brief A single queued operation to be dispatched from loop().
42 PendingOperationType type; ///< Operation type (determines which handler to invoke).
43 std::string device_id; ///< Target device ID (hex string, e.g., "123ABC").
44 uint8_t position{0}; ///< Position/tilt value (0–100) or binary state (ON=0, OFF=100).
45 uint8_t tilt{0}; ///< Tilt value for SET_POSITION_AND_TILT (0–100).
46 CoverCommand command{CoverCommand::STOP}; ///< Named command for DEVICE_COMMAND operations.
47};
48
49/// @brief Serialized pending-operation queue with coalescing, deduplication, and two-band ordering.
50///
51/// **Ordering:** DISCOVER_AND_PAIR > control operations > background polls.
52/// Control operations (SET_*, DEVICE_COMMAND) are inserted before any queued REQUEST_STATUS /
53/// REQUEST_NAME entries so user-visible commands execute with minimum queue latency.
54/// DISCOVER_AND_PAIR always front-inserts ahead of all other entries.
55///
56/// **Stale-poll drop:** enqueueing a control operation for device X drops any queued
57/// REQUEST_STATUS for X, since the command reply supersedes it and re-arms tracking.
58///
59/// Coalescing rules (both directions):
60/// - SET_POSITION arriving while SET_TILT is pending for the same device → SET_POSITION_AND_TILT.
61/// - SET_TILT arriving while SET_POSITION is pending for the same device → SET_POSITION_AND_TILT.
62///
63/// Deduplication rules:
64/// - At most one REQUEST_STATUS per device.
65/// - At most one REQUEST_NAME per device.
66/// - At most one DISCOVER_AND_PAIR (any pending REQUEST_STATUS / REQUEST_NAME entries are flushed
67/// and the discover entry is pushed to the front to minimize pairing-window latency).
68///
69/// All coalescing and deduplication log messages are suppressed here — callers are responsible
70/// for emitting them because OperationQueue must remain free of ESPHome logging dependencies.
72 public:
73 // --- Position / tilt (with coalescing) ---
74
75 /// Enqueue SET_POSITION, or coalesce with a pending SET_TILT into SET_POSITION_AND_TILT.
76 /// @return true if coalesced (caller should log the merge), false if a new entry was pushed.
77 bool enqueue_set_position(const std::string &device_id, uint8_t position);
78
79 /// Enqueue SET_TILT, or coalesce with a pending SET_POSITION into SET_POSITION_AND_TILT.
80 /// @return true if coalesced (caller should log the merge), false if a new entry was pushed.
81 bool enqueue_set_tilt(const std::string &device_id, uint8_t tilt_percent);
82
83 /// Enqueue SET_POSITION_AND_TILT directly (no coalescing needed).
84 void enqueue_set_position_and_tilt(const std::string &device_id, uint8_t position, uint8_t tilt_percent);
85
86 // --- Named cover command ---
87 void enqueue_device_command(const std::string &device_id, CoverCommand cmd);
88
89 // --- Binary entity commands (no coalescing) ---
90 /// Enqueue SET_LIGHT_STATE with an arbitrary IO position (0-100), for dimmable lights.
91 /// enqueue_set_light_state() is a thin binary-position wrapper around this.
92 void enqueue_set_light_position(const std::string &device_id, uint8_t position);
93 void enqueue_set_light_state(const std::string &device_id, bool on);
94 void enqueue_set_lock_state(const std::string &device_id, bool locked);
95 void enqueue_set_switch_state(const std::string &device_id, bool on);
96
97 // --- Background polls (with deduplication) ---
98
99 /// Enqueue REQUEST_STATUS, suppressing duplicates.
100 /// @return true if pushed, false if a duplicate was already pending.
101 bool enqueue_request_status(const std::string &device_id);
102
103 /// Enqueue REQUEST_NAME, suppressing duplicates.
104 /// @return true if pushed, false if a duplicate was already pending.
105 bool enqueue_request_name(const std::string &device_id);
106
107 // --- Pairing (dedup + priority front-push) ---
108
109 /// Enqueue DISCOVER_AND_PAIR with elevated priority.
110 /// Flushes pending REQUEST_STATUS / REQUEST_NAME entries and pushes to front.
111 /// @return true if pushed, false if a DISCOVER_AND_PAIR was already pending.
113
114 // --- Queue access ---
115 [[nodiscard]] std::optional<PendingOperation> pop();
116 [[nodiscard]] bool empty() const;
117 [[nodiscard]] std::size_t size() const;
118
119 // --- Read-only inspection ---
120 [[nodiscard]] const PendingOperation &front() const;
121 [[nodiscard]] const PendingOperation &back() const;
122 [[nodiscard]] const PendingOperation &operator[](std::size_t index) const;
123 [[nodiscard]] std::deque<PendingOperation>::const_iterator begin() const;
124 [[nodiscard]] std::deque<PendingOperation>::const_iterator end() const;
125
126 /// True for background poll types (REQUEST_STATUS, REQUEST_NAME) that yield to control operations.
127 /// Public because the dispatch gate in loop() defers *only* background work when a 1W remote is
128 /// transmitting; a user command must never be held back for it.
129 [[nodiscard]] static bool is_background_op(PendingOperationType t);
130
131 private:
132 std::deque<PendingOperation> queue_;
133 /// Insert a control operation before the first background entry; also drops any queued
134 /// REQUEST_STATUS for the same device since the incoming command supersedes it.
135 void push_control_(PendingOperation op);
136};
137
138} // namespace home_io_control
139} // namespace esphome
Serialized pending-operation queue with coalescing, deduplication, and two-band ordering.
std::deque< PendingOperation >::const_iterator begin() const
const PendingOperation & front() const
void enqueue_set_lock_state(const std::string &device_id, bool locked)
void enqueue_set_position_and_tilt(const std::string &device_id, uint8_t position, uint8_t tilt_percent)
Enqueue SET_POSITION_AND_TILT directly (no coalescing needed).
std::optional< PendingOperation > pop()
void enqueue_device_command(const std::string &device_id, CoverCommand cmd)
bool enqueue_request_status(const std::string &device_id)
Enqueue REQUEST_STATUS, suppressing duplicates.
bool enqueue_request_name(const std::string &device_id)
Enqueue REQUEST_NAME, suppressing duplicates.
void enqueue_set_switch_state(const std::string &device_id, bool on)
bool enqueue_discover_and_pair()
Enqueue DISCOVER_AND_PAIR with elevated priority.
static bool is_background_op(PendingOperationType t)
True for background poll types (REQUEST_STATUS, REQUEST_NAME) that yield to control operations.
std::deque< PendingOperation >::const_iterator end() const
bool enqueue_set_position(const std::string &device_id, uint8_t position)
Enqueue SET_POSITION, or coalesce with a pending SET_TILT into SET_POSITION_AND_TILT.
const PendingOperation & back() const
void enqueue_set_light_state(const std::string &device_id, bool on)
void enqueue_set_light_position(const std::string &device_id, uint8_t position)
Enqueue SET_LIGHT_STATE with an arbitrary IO position (0-100), for dimmable lights.
const PendingOperation & operator[](std::size_t index) const
bool enqueue_set_tilt(const std::string &device_id, uint8_t tilt_percent)
Enqueue SET_TILT, or coalesce with a pending SET_POSITION into SET_POSITION_AND_TILT.
PendingOperationType
Discriminator for entries in the pending-operation deque.
@ SET_LOCK_STATE
set_lock_state call (locked/unlocked).
@ SET_TILT
set_device_tilt call (tilt percentage 0–100).
@ SET_POSITION_AND_TILT
Combined set_device_position_and_tilt call.
@ SET_LIGHT_STATE
set_light_state call (binary on/off).
@ SET_SWITCH_STATE
set_switch_state call (binary on/off).
@ REQUEST_NAME
request_device_name call (poll for stored device name).
@ DISCOVER_AND_PAIR
discover_and_pair call (starts 3-phase pairing flow).
@ REQUEST_STATUS
request_device_status call (poll for current position).
@ DEVICE_COMMAND
Named device command (STOP, FAVORITE, VENT).
@ SET_POSITION
set_device_position call (position 0–100 or special values).
CoverCommand
Named device commands for cover-type actuators.
static constexpr uint8_t BINARY_ENTITY_ON_POSITION
Position value written for binary ON commands (light on, switch on, lock unlock).
static constexpr uint8_t BINARY_ENTITY_OFF_POSITION
Position value written for binary OFF commands (light off, switch off, lock lock).
IO-Homecontrol device-type model, capabilities and runtime device state.
A single queued operation to be dispatched from loop().
CoverCommand command
Named command for DEVICE_COMMAND operations.
std::string device_id
Target device ID (hex string, e.g., "123ABC").
uint8_t tilt
Tilt value for SET_POSITION_AND_TILT (0–100).
uint8_t position
Position/tilt value (0–100) or binary state (ON=0, OFF=100).
PendingOperationType type
Operation type (determines which handler to invoke).