1
+ #pragma once
2
+
3
+ #include " ../SinricProRequest.h"
4
+ #include " ../EventLimiter.h"
5
+ #include " ../SinricProStrings.h"
6
+
7
+ #include " ../SinricProNamespace.h"
8
+ namespace SINRICPRO_NAMESPACE {
9
+
10
+ FSTR (START_STOP, start); // "start"
11
+ FSTR (START_STOP, pause); // "pause"
12
+ FSTR (START_STOP, setStartStop); // "setStartStop"
13
+ FSTR (START_STOP, setPauseUnpause); // "setPauseUnpause"
14
+
15
+ /* *
16
+ * @brief Definition for onStartStop callback
17
+ *
18
+ * Gets called when device receive a `setStartStop` reuqest \n
19
+ * @param[in] deviceId String which contains the ID of device
20
+ * @param[in] start true for start, false for stop
21
+ * @return the success of the request
22
+ * @retval true request handled properly
23
+ * @retval false request was not handled properly because of some error
24
+ *
25
+ * @section StartStopCallback Example-Code
26
+ * @snippet callbacks.cpp onStartStop
27
+ **/
28
+ using StartStopCallback = std::function<bool (const String &, bool &)>;
29
+
30
+ /* *
31
+ * @brief Definition for onPauseUnpause callback
32
+ *
33
+ * Gets called when device receive a `setPauseUnpause` reuqest \n
34
+ * @param[in] deviceId String which contains the ID of device
35
+ * @param[in] pause true for pause, false for unpause
36
+ * @return the success of the request
37
+ * @retval true request handled properly
38
+ * @retval false request was not handled properly because of some error
39
+ *
40
+ * @section StartStopCallback Example-Code
41
+ * @snippet callbacks.cpp onPauseUnpause
42
+ **/
43
+
44
+ using PauseUnpauseCallback = std::function<bool (const String &, bool &)>;
45
+
46
+ /* *
47
+ * @brief StartStopController class to handle start/stop and pause/unpause functionality
48
+ *
49
+ * This template class provides functionality to control devices that can be started,
50
+ * stopped, paused, and unpaused (like a vacuum cleaner, washing machine, etc.)
51
+ *
52
+ * @tparam T The device type that this controller is attached to
53
+ */
54
+ template <typename T>
55
+ class StartStopController {
56
+ public:
57
+ StartStopController ();
58
+
59
+ /* *
60
+ * @brief Set the callback function for start/stop events
61
+ *
62
+ * @param cb Callback function that will be called when a start/stop request is received
63
+ */
64
+ void onStartStop (StartStopCallback cb);
65
+
66
+ /* *
67
+ * @brief Set the callback function for pause/unpause events
68
+ *
69
+ * @param cb Callback function that will be called when a pause/unpause request is received
70
+ */
71
+ void onPauseUnpause (StartStopCallback cb);
72
+
73
+ /* *
74
+ * @brief Send a start/stop event to the SinricPro server
75
+ *
76
+ * @param start true for start, false for stop
77
+ * @param cause The cause of the event (default: "PHYSICAL_INTERACTION")
78
+ * @return true if event was sent successfully, false otherwise
79
+ */
80
+ bool sendStartStopEvent (bool start, String cause = FSTR_SINRICPRO_PHYSICAL_INTERACTION);
81
+
82
+ /* *
83
+ * @brief Send a pause/unpause event to the SinricPro server
84
+ *
85
+ * @param pause true for pause, false for unpause
86
+ * @param cause The cause of the event (default: "PHYSICAL_INTERACTION")
87
+ * @return true if event was sent successfully, false otherwise
88
+ */
89
+ bool sendPauseUnpauseEvent (bool pause, String cause = FSTR_SINRICPRO_PHYSICAL_INTERACTION);
90
+
91
+ protected:
92
+ bool handleStartStopController (SinricProRequest &request);
93
+
94
+ private:
95
+ EventLimiter event_limiter;
96
+ StartStopCallback startStopCallbackCallback;
97
+ PauseUnpauseCallback pauseUnpauseCallback;
98
+ };
99
+
100
+ template <typename T>
101
+ StartStopController<T>::StartStopController()
102
+ :event_limiter(EVENT_LIMIT_STATE) {
103
+ T* device = static_cast <T*>(this );
104
+ device->registerRequestHandler (std::bind (&StartStopController<T>::handleStartStopController, this , std::placeholders::_1));
105
+ }
106
+
107
+ template <typename T>
108
+ void StartStopController<T>::onStartStop(StartStopCallback cb) { startStopCallbackCallback = cb; }
109
+
110
+ template <typename T>
111
+ void StartStopController<T>::onPauseUnpause(PauseUnpauseCallback cb) { pauseUnpauseCallback = cb; }
112
+
113
+ /* *
114
+ * @brief Send a start/stop event to the SinricPro server
115
+ *
116
+ * Creates a JSON message with the start status and sends it to the server
117
+ * @param start `bool` true for start, false for stop
118
+ * @param cause (optional) Reason why event is sent (default = `"PHYSICAL_INTERACTION"`)
119
+ */
120
+ template <typename T>
121
+ bool StartStopController<T>::sendStartStopEvent(bool start, String cause) {
122
+ if (event_limiter) return false ;
123
+ T* device = static_cast <T*>(this );
124
+
125
+ JsonDocument eventMessage = device->prepareEvent (FSTR_START_STOP_setStartStop, cause.c_str ());
126
+ JsonObject event_value = eventMessage[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_value];
127
+ event_value[FSTR_START_STOP_start] = start;
128
+ return device->sendEvent (eventMessage);
129
+ }
130
+
131
+ /* *
132
+ * @brief Send a pause/unpause event to the SinricPro server
133
+ *
134
+ * Creates a JSON message with the pause status and sends it to the server
135
+ * @param pause `bool` true for pause, false for unpause
136
+ * @param cause (optional) Reason why event is sent (default = `"PHYSICAL_INTERACTION"`)
137
+ */
138
+ template <typename T>
139
+ bool StartStopController<T>::sendPauseUnpauseEvent(bool pause, String cause) {
140
+ if (event_limiter) return false ;
141
+ T* device = static_cast <T*>(this );
142
+
143
+ JsonDocument eventMessage = device->prepareEvent (FSTR_START_STOP_setPauseUnpause, cause.c_str ());
144
+ JsonObject event_value = eventMessage[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_value];
145
+ event_value[FSTR_START_STOP_pause] = pause ;
146
+ return device->sendEvent (eventMessage);
147
+ }
148
+
149
+ template <typename T>
150
+ bool StartStopController<T>::handleStartStopController(SinricProRequest &request) {
151
+ if (request.action != FSTR_START_STOP_setStartStop || request.action != FSTR_START_STOP_setPauseUnpause) {
152
+ return false ;
153
+ }
154
+
155
+ T* device = static_cast <T*>(this );
156
+
157
+ bool success = false ;
158
+
159
+ if (startStopCallbackCallback && request.action == FSTR_START_STOP_setStartStop) {
160
+ bool start = request.request_value [FSTR_START_STOP_start];
161
+ success = startStopCallbackCallback (device->deviceId , start);
162
+ request.response_value [FSTR_START_STOP_start] = start;
163
+ return success;
164
+ }
165
+ else if (startStopCallbackCallback && request.action == FSTR_START_STOP_setPauseUnpause) {
166
+ bool pause = request.request_value [FSTR_START_STOP_pause];
167
+ success = startStopCallbackCallback (device->deviceId , pause );
168
+ request.response_value [FSTR_START_STOP_pause] = pause ;
169
+ return success;
170
+ }
171
+
172
+ return success;
173
+ }
174
+
175
+ } // SINRICPRO_NAMESPACE
176
+
177
+ template <typename T>
178
+ using StartStopController = SINRICPRO_NAMESPACE::StartStopController<T>;
0 commit comments