SinricPro Library
Loading...
Searching...
No Matches
OpenCloseController.h
1#pragma once
2
3#include "../SinricProRequest.h"
4#include "../EventLimiter.h"
5#include "../SinricProStrings.h"
6
7#include "../SinricProNamespace.h"
8namespace SINRICPRO_NAMESPACE {
9
10FSTR(OPEN_CLOSE, openPercent); // "openPercent"
11FSTR(OPEN_CLOSE, openRelativePercent); // "openRelativePercent"
12FSTR(OPEN_CLOSE, openDirection); // "openDirection"
13FSTR(OPEN_CLOSE, setOpenClose); // "setOpenClose"
14FSTR(OPEN_CLOSE, adjustOpenClose); // "adjustOpenClose"
15
16
30using OpenCloseCallback = std::function<bool(const String &, int &)>;
31
46using AdjustOpenCloseCallback = std::function<bool(const String &, int &)>;
47
62using DirectionOpenCloseCallback = std::function<bool(const String &, const String &, int &)>;
63
79using AdjustDirectionOpenCloseCallback = std::function<bool(const String &, const String &, int &)>;
80
88template <typename T>
90 public:
92
100 void onOpenClose(OpenCloseCallback cb);
101
109 void onDirectionOpenClose(DirectionOpenCloseCallback cb);
110
118 void onAdjustOpenClose(AdjustOpenCloseCallback cb);
119
127 void onAdjustDirectionOpenClose(AdjustDirectionOpenCloseCallback cb);
128
136 bool sendOpenCloseEvent(int openPercent, String cause = FSTR_SINRICPRO_PHYSICAL_INTERACTION);
137
146 bool sendOpenCloseEvent(String openDirection, int openPercent, String cause = FSTR_SINRICPRO_PHYSICAL_INTERACTION);
147
148 protected:
155 bool handleOpenCloseController(SinricProRequest &request);
156
157 private:
158 EventLimiter event_limiter;
159 DirectionOpenCloseCallback directionOpenCloseCallback;
160 OpenCloseCallback openCloseCallback;
161 AdjustOpenCloseCallback adjustOpenCloseCallback;
162 AdjustDirectionOpenCloseCallback adjustDirectionOpenCloseCallback;
163};
164
165template <typename T>
167:event_limiter(EVENT_LIMIT_STATE) {
168 T* device = static_cast<T*>(this);
169 device->registerRequestHandler(std::bind(&OpenCloseController<T>::handleOpenCloseController, this, std::placeholders::_1));
170}
171
172template <typename T>
174
175template <typename T>
177
178template <typename T>
180
181template <typename T>
183
195template <typename T>
207
218template <typename T>
220 if (event_limiter) return false;
221 T* device = static_cast<T*>(this);
222
226
227 return device->sendEvent(eventMessage);
228}
229
239template <typename T>
241 T* device = static_cast<T*>(this);
242
243 bool success = false;
244
246 bool hasOpenDirection = !request.request_value[FSTR_OPEN_CLOSE_openDirection].isNull();
248
249 if (hasOpenDirection && directionOpenCloseCallback) {
251 success = directionOpenCloseCallback(device->deviceId, openDirection, openPercent);
254 } else if (!hasOpenDirection && openCloseCallback) {
255 success = openCloseCallback(device->deviceId, openPercent);
257 }
258
259 return success;
260 }
261
263 bool hasOpenDirection = !request.request_value[FSTR_OPEN_CLOSE_openDirection].isNull();
265
266 if (hasOpenDirection && adjustDirectionOpenCloseCallback) {
268 success = adjustDirectionOpenCloseCallback(device->deviceId, openDirection, openRelativePercent);
271 } else if (!hasOpenDirection && adjustOpenCloseCallback) {
272 success = adjustOpenCloseCallback(device->deviceId, openRelativePercent);
274 }
275
276 return success;
277 }
278
279 return false;
280}
281
282} // SINRICPRO_NAMESPACE
283
284template <typename T>
285using OpenCloseController = SINRICPRO_NAMESPACE::OpenCloseController<T>;
AirQuality.
Definition AirQualitySensor.h:19
Controller class for devices with open/close functionalityThis controller handles open/close operatio...
Definition OpenCloseController.h:89
std::function< bool(const String &, const String &, int &)> DirectionOpenCloseCallback
Callback definition for onDirectionOpenClose callback.
Definition OpenCloseController.h:62
std::function< bool(const String &, int &)> OpenCloseCallback
Callback definition for onOpenClose callback.
Definition OpenCloseController.h:30
std::function< bool(const String &, const String &, int &)> AdjustDirectionOpenCloseCallback
Callback definition for onAdjustDirectionOpenClose callback.
Definition OpenCloseController.h:79
std::function< bool(const String &, int &)> AdjustOpenCloseCallback
Callback definition for onAdjustOpenClose callback.
Definition OpenCloseController.h:46