Skip to content

Commit a0abd1c

Browse files
committed
feat: open-close support added
1 parent 4fbf129 commit a0abd1c

File tree

1 file changed

+193
-19
lines changed

1 file changed

+193
-19
lines changed

src/Capabilities/OpenCloseController.h

+193-19
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,159 @@
77
#include "../SinricProNamespace.h"
88
namespace SINRICPRO_NAMESPACE {
99

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"
1415

15-
using OpenCloseCallback = std::function<bool(const String &, const String &, int &)>;
1616

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 &)>;
1831

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+
*/
1988
template <typename T>
2089
class OpenCloseController {
2190
public:
2291
OpenCloseController();
2392

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+
**/
24100
void onOpenClose(OpenCloseCallback cb);
25101

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+
**/
26118
void onAdjustOpenClose(AdjustOpenCloseCallback cb);
27119

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+
**/
28136
bool sendOpenCloseEvent(int openPercent, String cause = FSTR_SINRICPRO_PHYSICAL_INTERACTION);
29137

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+
**/
30146
bool sendOpenCloseEvent(String openDirection, int openPercent, String cause = FSTR_SINRICPRO_PHYSICAL_INTERACTION);
31147

32148
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+
**/
33155
bool handleOpenCloseController(SinricProRequest &request);
34156

35157
private:
36158
EventLimiter event_limiter;
37-
OpenCloseCallback openCloseCallbackCallback;
159+
DirectionOpenCloseCallback directionOpenCloseCallback;
160+
OpenCloseCallback openCloseCallback;
38161
AdjustOpenCloseCallback adjustOpenCloseCallback;
162+
AdjustDirectionOpenCloseCallback adjustDirectionOpenCloseCallback;
39163
};
40164

41165
template <typename T>
@@ -46,11 +170,28 @@ OpenCloseController<T>::OpenCloseController()
46170
}
47171

48172
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; }
50177

51178
template <typename T>
52179
void OpenCloseController<T>::onAdjustOpenClose(AdjustOpenCloseCallback cb) { adjustOpenCloseCallback = cb; }
53180

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+
*/
54195
template <typename T>
55196
bool OpenCloseController<T>::sendOpenCloseEvent(String openDirection, int openPercent, String cause) {
56197
if (event_limiter) return false;
@@ -64,6 +205,16 @@ bool OpenCloseController<T>::sendOpenCloseEvent(String openDirection, int openPe
64205
return device->sendEvent(eventMessage);
65206
}
66207

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+
*/
67218
template <typename T>
68219
bool OpenCloseController<T>::sendOpenCloseEvent(int openPercent, String cause) {
69220
if (event_limiter) return false;
@@ -76,30 +227,53 @@ bool OpenCloseController<T>::sendOpenCloseEvent(int openPercent, String cause) {
76227
return device->sendEvent(eventMessage);
77228
}
78229

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+
*/
79239
template <typename T>
80240
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) {
82242
return false;
83243
}
84244

85245
T* device = static_cast<T*>(this);
86246

87247
bool success = false;
88248

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();
91251
int openPercent = request.request_value[FSTR_OPEN_CLOSE_openPercent];
92252

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+
}
99262
}
100263

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+
}
103277
}
104278

105279
return success;

0 commit comments

Comments
 (0)