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 ONEWAY_COMMAND, ///< 1W named command sent as a controller identity.
36 ONEWAY_POSITION, ///< 1W numeric position sent as a controller identity.
37 ONEWAY_ENROLL, ///< 1W add-controller (0x30) registering an identity.
38 ONEWAY_UNENROLL, ///< 1W remove-controller (0x39) un-registering an identity.
39 REQUEST_STATUS, ///< request_device_status call (poll for current position).
40 REQUEST_NAME, ///< request_device_name call (poll for stored device name).
41 DISCOVER_AND_PAIR, ///< discover_and_pair call (starts 3-phase pairing flow).
42};
43
44/// @brief A single queued operation to be dispatched from loop().
46 PendingOperationType type; ///< Operation type (determines which handler to invoke).
47 /// Target device ID (hex string, e.g., "123ABC") — **except** for the ONEWAY_* types, where it
48 /// carries the controller-identity handle instead. 1W addresses a device class, not a node, so
49 /// there is no device ID to put here; the identity is what the operation is bound to. The field
50 /// is reused rather than duplicated because every queue entry would otherwise grow a second
51 /// string for the benefit of two operation types.
52 std::string device_id;
53 uint8_t position{0}; ///< Position/tilt value (0–100) or binary state (ON=0, OFF=100).
54 uint8_t tilt{0}; ///< Tilt value for SET_POSITION_AND_TILT (0–100).
55 CoverCommand command{CoverCommand::STOP}; ///< Named command for DEVICE_COMMAND operations.
56};
57
58/// @brief Serialized pending-operation queue with coalescing, deduplication, and two-band ordering.
59///
60/// **Ordering:** DISCOVER_AND_PAIR > control operations > background polls.
61/// Control operations (SET_*, DEVICE_COMMAND) are inserted before any queued REQUEST_STATUS /
62/// REQUEST_NAME entries so user-visible commands execute with minimum queue latency.
63/// DISCOVER_AND_PAIR always front-inserts ahead of all other entries.
64///
65/// **Stale-poll drop:** enqueueing a control operation for device X drops any queued
66/// REQUEST_STATUS for X, since the command reply supersedes it and re-arms tracking.
67///
68/// Coalescing rules (both directions):
69/// - SET_POSITION arriving while SET_TILT is pending for the same device → SET_POSITION_AND_TILT.
70/// - SET_TILT arriving while SET_POSITION is pending for the same device → SET_POSITION_AND_TILT.
71///
72/// Deduplication rules:
73/// - At most one REQUEST_STATUS per device.
74/// - At most one REQUEST_NAME per device.
75/// - At most one DISCOVER_AND_PAIR (any pending REQUEST_STATUS / REQUEST_NAME entries are flushed
76/// and the discover entry is pushed to the front to minimize pairing-window latency).
77///
78/// All coalescing and deduplication log messages are suppressed here — callers are responsible
79/// for emitting them because OperationQueue must remain free of ESPHome logging dependencies.
81 public:
82 // --- Position / tilt (with coalescing) ---
83
84 /// Enqueue SET_POSITION, or coalesce with a pending SET_TILT into SET_POSITION_AND_TILT.
85 /// @return true if coalesced (caller should log the merge), false if a new entry was pushed.
86 bool enqueue_set_position(const std::string &device_id, uint8_t position);
87
88 /// Enqueue SET_TILT, or coalesce with a pending SET_POSITION into SET_POSITION_AND_TILT.
89 /// @return true if coalesced (caller should log the merge), false if a new entry was pushed.
90 bool enqueue_set_tilt(const std::string &device_id, uint8_t tilt_percent);
91
92 /// Enqueue SET_POSITION_AND_TILT directly (no coalescing needed).
93 void enqueue_set_position_and_tilt(const std::string &device_id, uint8_t position, uint8_t tilt_percent);
94
95 // --- Named cover command ---
96 void enqueue_device_command(const std::string &device_id, CoverCommand cmd);
97
98 // --- Binary entity commands (no coalescing) ---
99 /// Enqueue SET_LIGHT_STATE with an arbitrary IO position (0-100), for dimmable lights.
100 /// enqueue_set_light_state() is a thin binary-position wrapper around this.
101 void enqueue_set_light_position(const std::string &device_id, uint8_t position);
102 void enqueue_set_light_state(const std::string &device_id, bool on);
103 void enqueue_set_lock_state(const std::string &device_id, bool locked);
104 void enqueue_set_switch_state(const std::string &device_id, bool on);
105
106 // --- 1W commands (no coalescing, no deduplication) ---
107
108 /// Enqueue a 1W named command for a controller identity.
109 ///
110 /// Deliberately neither coalesced nor deduplicated, unlike their 2W counterparts. A 2W command
111 /// can be superseded because the device reports back what it did; a 1W command cannot be
112 /// confirmed at all, so dropping one is dropping a press the user made with nothing to notice
113 /// it. Each press also consumes its own sequence, and merging two would leave a gap that looks
114 /// like a lost frame to a device tracking the counter.
115 /// @param controller_id Controller-identity handle (see PendingOperation::device_id).
116 /// @param cmd Named command to send.
117 void enqueue_oneway_command(const std::string &controller_id, CoverCommand cmd);
118
119 /// Enqueue a 1W numeric position for a controller identity.
120 /// @param controller_id Controller-identity handle (see PendingOperation::device_id).
121 /// @param position Target position 0–100.
122 void enqueue_oneway_position(const std::string &controller_id, uint8_t position);
123
124 /// Enqueue a 1W enrollment (add-controller) for a controller identity. Same no-coalesce,
125 /// no-dedup contract as the other 1W ops, for the same reason.
126 /// @param controller_id Controller-identity handle (see PendingOperation::device_id).
127 void enqueue_oneway_enroll(const std::string &controller_id);
128
129 /// Enqueue a 1W un-enrollment (remove-controller) for a controller identity.
130 /// @param controller_id Controller-identity handle (see PendingOperation::device_id).
131 void enqueue_oneway_unenroll(const std::string &controller_id);
132
133 // --- Background polls (with deduplication) ---
134
135 /// Enqueue REQUEST_STATUS, suppressing duplicates.
136 /// @return true if pushed, false if a duplicate was already pending.
137 bool enqueue_request_status(const std::string &device_id);
138
139 /// Enqueue REQUEST_NAME, suppressing duplicates.
140 /// @return true if pushed, false if a duplicate was already pending.
141 bool enqueue_request_name(const std::string &device_id);
142
143 // --- Pairing (dedup + priority front-push) ---
144
145 /// Enqueue DISCOVER_AND_PAIR with elevated priority.
146 /// Flushes pending REQUEST_STATUS / REQUEST_NAME entries and pushes to front.
147 /// @return true if pushed, false if a DISCOVER_AND_PAIR was already pending.
149
150 // --- Queue access ---
151 [[nodiscard]] std::optional<PendingOperation> pop();
152 [[nodiscard]] bool empty() const;
153 [[nodiscard]] std::size_t size() const;
154
155 // --- Read-only inspection ---
156 [[nodiscard]] const PendingOperation &front() const;
157 [[nodiscard]] const PendingOperation &back() const;
158 [[nodiscard]] const PendingOperation &operator[](std::size_t index) const;
159 [[nodiscard]] std::deque<PendingOperation>::const_iterator begin() const;
160 [[nodiscard]] std::deque<PendingOperation>::const_iterator end() const;
161
162 /// True for background poll types (REQUEST_STATUS, REQUEST_NAME) that yield to control operations.
163 /// Public because the dispatch gate in loop() defers *only* background work when a 1W remote is
164 /// transmitting; a user command must never be held back for it.
165 [[nodiscard]] static bool is_background_op(PendingOperationType t);
166
167 private:
168 std::deque<PendingOperation> queue_;
169 /// Insert a control operation before the first background entry; also drops any queued
170 /// REQUEST_STATUS for the same device since the incoming command supersedes it.
171 void push_control_(PendingOperation op);
172};
173
174} // namespace home_io_control
175} // 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_oneway_unenroll(const std::string &controller_id)
Enqueue a 1W un-enrollment (remove-controller) for a controller identity.
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).
void enqueue_oneway_command(const std::string &controller_id, CoverCommand cmd)
Enqueue a 1W named command for a controller identity.
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)
void enqueue_oneway_position(const std::string &controller_id, uint8_t position)
Enqueue a 1W numeric position for a controller identity.
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_oneway_enroll(const std::string &controller_id)
Enqueue a 1W enrollment (add-controller) for a controller identity.
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).
@ ONEWAY_COMMAND
1W named command sent as a controller identity.
@ DISCOVER_AND_PAIR
discover_and_pair call (starts 3-phase pairing flow).
@ REQUEST_STATUS
request_device_status call (poll for current position).
@ ONEWAY_UNENROLL
1W remove-controller (0x39) un-registering an identity.
@ DEVICE_COMMAND
Named device command (STOP, FAVORITE, VENT).
@ SET_POSITION
set_device_position call (position 0–100 or special values).
@ ONEWAY_ENROLL
1W add-controller (0x30) registering an identity.
@ ONEWAY_POSITION
1W numeric position sent as a controller identity.
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") — except for the ONEWAY_* types, where it carries the c...
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).