7
7
#include " ../SinricProNamespace.h"
8
8
namespace SINRICPRO_NAMESPACE {
9
9
10
- FSTR (OPEN_CLOSE, openPercent); // "openPercent"
11
- FSTR (OPEN_CLOSE, openDirection); // "openDirection"
12
- FSTR (OPEN_CLOSE, setOpenClose); // "setOpenClose"
13
- FSTR (OPEN_CLOSE, adjustOpenClose); // "adjustOpenClose"
10
+ FSTR (OPEN_CLOSE, openPercent); // "openPercent"
11
+ FSTR (OPEN_CLOSE, openRelativePercent); // "openRelativePercent"
12
+ FSTR (OPEN_CLOSE, openDirection); // "openDirection"
13
+ FSTR (OPEN_CLOSE, setOpenClose); // "setOpenClose"
14
+ FSTR (OPEN_CLOSE, adjustOpenClose); // "adjustOpenClose"
14
15
15
- using OpenCloseCallback = std::function<bool (const String &, const String &, int &)>;
16
16
17
- using AdjustOpenCloseCallback = std::function<bool (const String &, const String &, int &)>;
17
+ /* *
18
+ * @brief Callback definition for onOpenClose callback
19
+ *
20
+ * Gets called when device receives a `setOpenClose` request
21
+ * @param[in] deviceId String containing the ID of device
22
+ * @param[in,out] openPercent Integer percentage (0-100) of how open the device should be set (0 = closed, 100 = fully open)
23
+ * @return Success status of the request
24
+ * @retval true Request handled successfully
25
+ * @retval false Request handling failed
26
+ *
27
+ * @section OpenCloseCallback Example-Code
28
+ * @snippet callbacks.cpp onSetOpenClose
29
+ **/
30
+ using OpenCloseCallback = std::function<bool (const String &, int &)>;
18
31
32
+ /* *
33
+ * @brief Callback definition for onAdjustOpenClose callback
34
+ *
35
+ * Gets called when device receives an `adjustOpenClose` request
36
+ * @param[in] deviceId String containing the ID of device
37
+ * @param[in,out] openRelativePercent Integer value representing the relative percentage change to apply
38
+ * On output, should contain the absolute percentage value the device has been set to
39
+ * @return Success status of the request
40
+ * @retval true Request handled successfully
41
+ * @retval false Request handling failed
42
+ *
43
+ * @section AdjustOpenCloseCallback Example-Code
44
+ * @snippet callbacks.cpp onAdjustOpenClose
45
+ **/
46
+ using AdjustOpenCloseCallback = std::function<bool (const String &, int &)>;
47
+
48
+ /* *
49
+ * @brief Callback definition for onDirectionOpenClose callback
50
+ *
51
+ * Gets called when device receives a `setOpenClose` request with direction information
52
+ * @param[in] deviceId String containing the ID of device
53
+ * @param[in] openDirection String direction in which to open: `UP`, `DOWN`, `LEFT`, `RIGHT`, `IN`, `OUT`
54
+ * @param[in,out] openPercent Integer percentage (0-100) of how open the device should be set
55
+ * @return Success status of the request
56
+ * @retval true Request handled successfully
57
+ * @retval false Request handling failed
58
+ *
59
+ * @section DirectionOpenCloseCallback Example-Code
60
+ * @snippet callbacks.cpp onDirectionOpenClose
61
+ **/
62
+ using DirectionOpenCloseCallback = std::function<bool (const String &, const String &, int &)>;
63
+
64
+ /* *
65
+ * @brief Callback definition for onAdjustDirectionOpenClose callback
66
+ *
67
+ * Gets called when device receives an `adjustOpenClose` request with direction information
68
+ * @param[in] deviceId String containing the ID of device
69
+ * @param[in] openDirection String direction in which to open: `UP`, `DOWN`, `LEFT`, `RIGHT`, `IN`, `OUT`
70
+ * @param[in,out] openRelativePercent Integer representing relative percentage change to apply
71
+ * On output, should contain the absolute percentage value the device has been set to
72
+ * @return Success status of the request
73
+ * @retval true Request handled successfully
74
+ * @retval false Request handling failed
75
+ *
76
+ * @section AdjustDirectionOpenCloseCallback Example-Code
77
+ * @snippet callbacks.cpp onAdjustDirectionOpenClose
78
+ **/
79
+ using AdjustDirectionOpenCloseCallback = std::function<bool (const String &, const String &, int &)>;
80
+
81
+ /* *
82
+ * @brief Controller class for devices with open/close functionality
83
+ * @ingroup Capabilities
84
+ * This controller handles open/close operations for devices that can be opened or closed by percentage and may supports multiple directions.
85
+ *
86
+ * @tparam T Device type that implements this controller
87
+ */
19
88
template <typename T>
20
89
class OpenCloseController {
21
90
public:
22
91
OpenCloseController ();
23
92
93
+ /* *
94
+ * @brief Set callback function for simple open/close operations
95
+ *
96
+ * @param cb Function pointer to a `OpenCloseCallback` function
97
+ * @return void
98
+ * @see OpenCloseCallback
99
+ **/
24
100
void onOpenClose (OpenCloseCallback cb);
25
101
102
+ /* *
103
+ * @brief Set callback function for directional open/close operations
104
+ *
105
+ * @param cb Function pointer to a `DirectionOpenCloseCallback` function
106
+ * @return void
107
+ * @see DirectionOpenCloseCallback
108
+ **/
109
+ void onDirectionOpenClose (DirectionOpenCloseCallback cb);
110
+
111
+ /* *
112
+ * @brief Set callback function for relative adjustments to open/close status
113
+ *
114
+ * @param cb Function pointer to a `AdjustOpenCloseCallback` function
115
+ * @return void
116
+ * @see AdjustOpenCloseCallback
117
+ **/
26
118
void onAdjustOpenClose (AdjustOpenCloseCallback cb);
27
119
120
+ /* *
121
+ * @brief Set callback function for directional relative adjustments
122
+ *
123
+ * @param cb Function pointer to a `AdjustDirectionOpenCloseCallback` function
124
+ * @return void
125
+ * @see AdjustDirectionOpenCloseCallback
126
+ **/
127
+ void onAdjustDirectionOpenClose (AdjustDirectionOpenCloseCallback cb);
128
+
129
+ /* *
130
+ * @brief Send an event to update the open/close status
131
+ *
132
+ * @param openPercent Current open percentage (0-100)
133
+ * @param cause Cause of the event (default: physical interaction)
134
+ * @return bool Whether the event was sent successfully
135
+ **/
28
136
bool sendOpenCloseEvent (int openPercent, String cause = FSTR_SINRICPRO_PHYSICAL_INTERACTION);
29
137
138
+ /* *
139
+ * @brief Send an event to update the open/close status with direction
140
+ *
141
+ * @param openDirection Direction of opening (UP, DOWN, LEFT, RIGHT, IN, OUT)
142
+ * @param openPercent Current open percentage (0-100)
143
+ * @param cause Cause of the event (default: physical interaction)
144
+ * @return bool Whether the event was sent successfully
145
+ **/
30
146
bool sendOpenCloseEvent (String openDirection, int openPercent, String cause = FSTR_SINRICPRO_PHYSICAL_INTERACTION);
31
147
32
148
protected:
149
+ /* *
150
+ * @brief Handle incoming open/close control requests
151
+ *
152
+ * @param request The incoming request to process
153
+ * @return bool Whether the request was handled successfully
154
+ **/
33
155
bool handleOpenCloseController (SinricProRequest &request);
34
156
35
157
private:
36
158
EventLimiter event_limiter;
37
- OpenCloseCallback openCloseCallbackCallback;
159
+ DirectionOpenCloseCallback directionOpenCloseCallback;
160
+ OpenCloseCallback openCloseCallback;
38
161
AdjustOpenCloseCallback adjustOpenCloseCallback;
162
+ AdjustDirectionOpenCloseCallback adjustDirectionOpenCloseCallback;
39
163
};
40
164
41
165
template <typename T>
@@ -46,11 +170,28 @@ OpenCloseController<T>::OpenCloseController()
46
170
}
47
171
48
172
template <typename T>
49
- void OpenCloseController<T>::onOpenClose(OpenCloseCallback cb) { openCloseCallbackCallback = cb; }
173
+ void OpenCloseController<T>::onOpenClose(OpenCloseCallback cb) { openCloseCallback = cb; }
174
+
175
+ template <typename T>
176
+ void OpenCloseController<T>::onDirectionOpenClose(DirectionOpenCloseCallback cb) { directionOpenCloseCallback = cb; }
50
177
51
178
template <typename T>
52
179
void OpenCloseController<T>::onAdjustOpenClose(AdjustOpenCloseCallback cb) { adjustOpenCloseCallback = cb; }
53
180
181
+ template <typename T>
182
+ void OpenCloseController<T>::onAdjustDirectionOpenClose(AdjustDirectionOpenCloseCallback cb) { adjustDirectionOpenCloseCallback = cb; }
183
+
184
+ /* *
185
+ * @brief Send an event to update open/close status with open direction and precent and information
186
+ *
187
+ * Sends the current open/close status with direction to the Sinric Pro cloud.
188
+ * The event will only be sent if the event rate limiter allows it.
189
+ *
190
+ * @param openDirection Direction in which the device is opening
191
+ * @param openPercent Current open percentage (0-100)
192
+ * @param cause Reason for the state change (default: physical interaction)
193
+ * @return bool Whether the event was sent successfully
194
+ */
54
195
template <typename T>
55
196
bool OpenCloseController<T>::sendOpenCloseEvent(String openDirection, int openPercent, String cause) {
56
197
if (event_limiter) return false ;
@@ -64,6 +205,16 @@ bool OpenCloseController<T>::sendOpenCloseEvent(String openDirection, int openPe
64
205
return device->sendEvent (eventMessage);
65
206
}
66
207
208
+ /* *
209
+ * @brief Send an event to update open/close status
210
+ *
211
+ * Sends the current open/close percentage to the Sinric Pro cloud.
212
+ * The event will only be sent if the event rate limiter allows it.
213
+ *
214
+ * @param openPercent Current open percentage (0-100)
215
+ * @param cause Reason for the state change (default: physical interaction)
216
+ * @return bool Whether the event was sent successfully
217
+ */
67
218
template <typename T>
68
219
bool OpenCloseController<T>::sendOpenCloseEvent(int openPercent, String cause) {
69
220
if (event_limiter) return false ;
@@ -76,30 +227,53 @@ bool OpenCloseController<T>::sendOpenCloseEvent(int openPercent, String cause) {
76
227
return device->sendEvent (eventMessage);
77
228
}
78
229
230
+ /* *
231
+ * @brief Handle incoming open/close requests
232
+ *
233
+ * Processes both setOpenClose and adjustOpenClose requests with or without direction information.
234
+ * Delegates to the appropriate callback function based on the request type and parameters.
235
+ *
236
+ * @param request The incoming request containing action and parameters
237
+ * @return bool Whether the request was handled successfully
238
+ */
79
239
template <typename T>
80
240
bool OpenCloseController<T>::handleOpenCloseController(SinricProRequest &request) {
81
- if (request.action != FSTR_OPEN_CLOSE_setOpenClose) {
241
+ if (request.action != FSTR_OPEN_CLOSE_setOpenClose && request. action != FSTR_OPEN_CLOSE_adjustOpenClose ) {
82
242
return false ;
83
243
}
84
244
85
245
T* device = static_cast <T*>(this );
86
246
87
247
bool success = false ;
88
248
89
- if (openCloseCallbackCallback && request.action == FSTR_OPEN_CLOSE_setOpenClose) {
90
- String openDirection = request.request_value [FSTR_OPEN_CLOSE_openDirection];
249
+ if ((directionOpenCloseCallback || openCloseCallback) && request.action == FSTR_OPEN_CLOSE_setOpenClose) {
250
+ bool hasOpenDirection = ! request.request_value [FSTR_OPEN_CLOSE_openDirection]. isNull () ;
91
251
int openPercent = request.request_value [FSTR_OPEN_CLOSE_openPercent];
92
252
93
- success = openCloseCallbackCallback (device->deviceId , openDirection, openPercent);
94
-
95
- request.response_value [FSTR_OPEN_CLOSE_openDirection] = openDirection;
96
- request.response_value [FSTR_OPEN_CLOSE_openPercent] = openPercent;
97
-
98
- return success;
253
+ if (hasOpenDirection && directionOpenCloseCallback) {
254
+ String openDirection = request.request_value [FSTR_OPEN_CLOSE_openDirection];
255
+ success = directionOpenCloseCallback (device->deviceId , openDirection, openPercent);
256
+ request.response_value [FSTR_OPEN_CLOSE_openDirection] = openDirection;
257
+ request.response_value [FSTR_OPEN_CLOSE_openPercent] = openPercent;
258
+ } else if (!hasOpenDirection && openCloseCallback) {
259
+ success = openCloseCallback (device->deviceId , openPercent);
260
+ request.response_value [FSTR_OPEN_CLOSE_openPercent] = openPercent;
261
+ }
99
262
}
100
263
101
- if (adjustOpenCloseCallback && request.action == FSTR_OPEN_CLOSE_adjustOpenClose) {
102
- // TOOD:
264
+ if ((adjustOpenCloseCallback || adjustDirectionOpenCloseCallback) && request.action == FSTR_OPEN_CLOSE_adjustOpenClose) {
265
+ bool hasOpenDirection = !request.request_value [FSTR_OPEN_CLOSE_openDirection].isNull ();
266
+ int openRelativePercent = request.request_value [FSTR_OPEN_CLOSE_openRelativePercent];
267
+
268
+ if (hasOpenDirection && adjustDirectionOpenCloseCallback) {
269
+ String openDirection = request.request_value [FSTR_OPEN_CLOSE_openDirection];
270
+ success = adjustDirectionOpenCloseCallback (device->deviceId , openDirection, openRelativePercent);
271
+ request.response_value [FSTR_OPEN_CLOSE_openDirection] = openDirection;
272
+ request.response_value [FSTR_OPEN_CLOSE_openPercent] = openRelativePercent;
273
+ } else if (!hasOpenDirection && adjustOpenCloseCallback) {
274
+ success = adjustOpenCloseCallback (device->deviceId , openRelativePercent);
275
+ request.response_value [FSTR_OPEN_CLOSE_openPercent] = openRelativePercent;
276
+ }
103
277
}
104
278
105
279
return success;
0 commit comments