Skip to content

Commit 4fbf129

Browse files
committed
feat: open/close support for GH
1 parent d87c08a commit 4fbf129

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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(OPEN_CLOSE, openPercent); // "openPercent"
11+
FSTR(OPEN_CLOSE, openDirection); // "openDirection"
12+
FSTR(OPEN_CLOSE, setOpenClose); // "setOpenClose"
13+
FSTR(OPEN_CLOSE, adjustOpenClose); // "adjustOpenClose"
14+
15+
using OpenCloseCallback = std::function<bool(const String &, const String &, int &)>;
16+
17+
using AdjustOpenCloseCallback = std::function<bool(const String &, const String &, int &)>;
18+
19+
template <typename T>
20+
class OpenCloseController {
21+
public:
22+
OpenCloseController();
23+
24+
void onOpenClose(OpenCloseCallback cb);
25+
26+
void onAdjustOpenClose(AdjustOpenCloseCallback cb);
27+
28+
bool sendOpenCloseEvent(int openPercent, String cause = FSTR_SINRICPRO_PHYSICAL_INTERACTION);
29+
30+
bool sendOpenCloseEvent(String openDirection, int openPercent, String cause = FSTR_SINRICPRO_PHYSICAL_INTERACTION);
31+
32+
protected:
33+
bool handleOpenCloseController(SinricProRequest &request);
34+
35+
private:
36+
EventLimiter event_limiter;
37+
OpenCloseCallback openCloseCallbackCallback;
38+
AdjustOpenCloseCallback adjustOpenCloseCallback;
39+
};
40+
41+
template <typename T>
42+
OpenCloseController<T>::OpenCloseController()
43+
:event_limiter(EVENT_LIMIT_STATE) {
44+
T* device = static_cast<T*>(this);
45+
device->registerRequestHandler(std::bind(&OpenCloseController<T>::handleOpenCloseController, this, std::placeholders::_1));
46+
}
47+
48+
template <typename T>
49+
void OpenCloseController<T>::onOpenClose(OpenCloseCallback cb) { openCloseCallbackCallback = cb; }
50+
51+
template <typename T>
52+
void OpenCloseController<T>::onAdjustOpenClose(AdjustOpenCloseCallback cb) { adjustOpenCloseCallback = cb; }
53+
54+
template <typename T>
55+
bool OpenCloseController<T>::sendOpenCloseEvent(String openDirection, int openPercent, String cause) {
56+
if (event_limiter) return false;
57+
T* device = static_cast<T*>(this);
58+
59+
JsonDocument eventMessage = device->prepareEvent(FSTR_OPEN_CLOSE_setOpenClose, cause.c_str());
60+
JsonObject event_value = eventMessage[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_value];
61+
event_value[FSTR_OPEN_CLOSE_openDirection] = openDirection;
62+
event_value[FSTR_OPEN_CLOSE_openPercent] = openPercent;
63+
64+
return device->sendEvent(eventMessage);
65+
}
66+
67+
template <typename T>
68+
bool OpenCloseController<T>::sendOpenCloseEvent(int openPercent, String cause) {
69+
if (event_limiter) return false;
70+
T* device = static_cast<T*>(this);
71+
72+
JsonDocument eventMessage = device->prepareEvent(FSTR_OPEN_CLOSE_setOpenClose, cause.c_str());
73+
JsonObject event_value = eventMessage[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_value];
74+
event_value[FSTR_OPEN_CLOSE_openPercent] = openPercent;
75+
76+
return device->sendEvent(eventMessage);
77+
}
78+
79+
template <typename T>
80+
bool OpenCloseController<T>::handleOpenCloseController(SinricProRequest &request) {
81+
if (request.action != FSTR_OPEN_CLOSE_setOpenClose) {
82+
return false;
83+
}
84+
85+
T* device = static_cast<T*>(this);
86+
87+
bool success = false;
88+
89+
if (openCloseCallbackCallback && request.action == FSTR_OPEN_CLOSE_setOpenClose) {
90+
String openDirection = request.request_value[FSTR_OPEN_CLOSE_openDirection];
91+
int openPercent = request.request_value[FSTR_OPEN_CLOSE_openPercent];
92+
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;
99+
}
100+
101+
if (adjustOpenCloseCallback && request.action == FSTR_OPEN_CLOSE_adjustOpenClose) {
102+
// TOOD:
103+
}
104+
105+
return success;
106+
}
107+
108+
} // SINRICPRO_NAMESPACE
109+
110+
template <typename T>
111+
using OpenCloseController = SINRICPRO_NAMESPACE::OpenCloseController<T>;

0 commit comments

Comments
 (0)