5
5
#include " ../SinricProStrings.h"
6
6
#include " ../SinricProNamespace.h"
7
7
8
+ /* *
9
+ * @brief Enum defining the different types of button press events
10
+ */
11
+ enum class SmartButtonPressType {
12
+ SINGLE_PRESS,
13
+ DOUBLE_PRESS,
14
+ LONG_PRESS
15
+ };
16
+
8
17
namespace SINRICPRO_NAMESPACE {
9
18
10
- // String constants for button states and actions
11
19
FSTR (BUTTONSTATE, state); // Button state key
12
20
FSTR (BUTTONSTATE, singlePress); // Single press state value
13
21
FSTR (BUTTONSTATE, doublePress); // Double press state value
14
22
FSTR (BUTTONSTATE, longPress); // Long press state value
15
23
FSTR (BUTTONSTATE, setSmartButtonState); // Set state action name
16
24
17
- // Callback type definitions for different button press events
18
- using SmartButtonPressCallback = std::function<bool (const String & )>;
25
+ // Callback type definition for button press events
26
+ using SmartButtonPressCallback = std::function<bool (const String&, SmartButtonPressType )>;
19
27
20
28
/* *
21
29
* @brief Controller class for managing smart button state and interactions
@@ -32,22 +40,10 @@ class SmartButtonStateController {
32
40
SmartButtonStateController ();
33
41
34
42
/* *
35
- * @brief Register callback for single press event from app
36
- * @param cb Callback function to handle single press
43
+ * @brief Register callback for button press events
44
+ * @param cb Callback function to handle button press events
37
45
*/
38
- void onSinglePress (SmartButtonPressCallback cb);
39
-
40
- /* *
41
- * @brief Register callback for double press event from app
42
- * @param cb Callback function to handle double press
43
- */
44
- void onDoublePress (SmartButtonPressCallback cb);
45
-
46
- /* *
47
- * @brief Register callback for long press event from app
48
- * @param cb Callback function to handle long press
49
- */
50
- void onLongPress (SmartButtonPressCallback cb);
46
+ void onButtonPress (SmartButtonPressCallback cb);
51
47
52
48
protected:
53
49
/* *
@@ -58,9 +54,14 @@ class SmartButtonStateController {
58
54
bool handleSmartButtonStateController (SinricProRequest &request);
59
55
60
56
private:
61
- SmartButtonPressCallback smartButtonSinglePressCallback;
62
- SmartButtonPressCallback smartButtonDoublePressCallback;
63
- SmartButtonPressCallback smartButtonLongPressCallback;
57
+ SmartButtonPressCallback buttonPressCallback;
58
+
59
+ /* *
60
+ * @brief Convert string state to SmartButtonPressType enum
61
+ * @param stateStr The state string from the request
62
+ * @return corresponding SmartButtonPressType enum value
63
+ */
64
+ SmartButtonPressType getSmartButtonPressType (const String& stateStr);
64
65
65
66
/* *
66
67
* returns true if states match, false otherwise
@@ -70,8 +71,6 @@ class SmartButtonStateController {
70
71
}
71
72
};
72
73
73
- // Implementation
74
-
75
74
template <typename T>
76
75
SmartButtonStateController<T>::SmartButtonStateController() {
77
76
T* device = static_cast <T*>(this );
@@ -83,44 +82,40 @@ SmartButtonStateController<T>::SmartButtonStateController() {
83
82
}
84
83
85
84
template <typename T>
86
- void SmartButtonStateController<T>::onSinglePress(SmartButtonPressCallback cb) {
87
- smartButtonSinglePressCallback = cb;
88
- }
89
-
90
- template <typename T>
91
- void SmartButtonStateController<T>::onDoublePress(SmartButtonPressCallback cb) {
92
- smartButtonDoublePressCallback = cb;
85
+ void SmartButtonStateController<T>::onButtonPress(SmartButtonPressCallback cb) {
86
+ buttonPressCallback = cb;
93
87
}
94
88
95
89
template <typename T>
96
- void SmartButtonStateController<T>::onLongPress(SmartButtonPressCallback cb) {
97
- smartButtonLongPressCallback = cb;
90
+ SmartButtonPressType SmartButtonStateController<T>::getSmartButtonPressType(const String& stateStr) {
91
+ if (stateStr == FSTR_BUTTONSTATE_singlePress) {
92
+ return SmartButtonPressType::SINGLE_PRESS;
93
+ } else if (stateStr == FSTR_BUTTONSTATE_doublePress) {
94
+ return SmartButtonPressType::DOUBLE_PRESS;
95
+ } else {
96
+ return SmartButtonPressType::LONG_PRESS;
97
+ }
98
98
}
99
99
100
100
template <typename T>
101
101
bool SmartButtonStateController<T>::handleSmartButtonStateController(SinricProRequest &request) {
102
102
// Only process setSmartButtonState actions
103
- if (request.action != FSTR_BUTTONSTATE_setSmartButtonState) {
103
+ if (request.action != FSTR_BUTTONSTATE_setSmartButtonState || !buttonPressCallback ) {
104
104
return false ;
105
105
}
106
106
107
107
T* device = static_cast <T*>(this );
108
- bool success = false ;
109
-
110
- // Handle single press
111
- if (smartButtonSinglePressCallback && isStateMatch (request, FSTR_BUTTONSTATE_singlePress)) {
112
- success = smartButtonSinglePressCallback (device->deviceId );
113
- }
114
- // Handle double press
115
- else if (smartButtonDoublePressCallback && isStateMatch (request, FSTR_BUTTONSTATE_doublePress)) {
116
- success = smartButtonDoublePressCallback (device->deviceId );
117
- }
118
- // Handle long press
119
- else if (smartButtonLongPressCallback && isStateMatch (request, FSTR_BUTTONSTATE_longPress)) {
120
- success = smartButtonLongPressCallback (device->deviceId );
108
+ String stateStr = request.request_value [FSTR_BUTTONSTATE_state];
109
+
110
+ // Only process valid button states
111
+ if (stateStr != FSTR_BUTTONSTATE_singlePress &&
112
+ stateStr != FSTR_BUTTONSTATE_doublePress &&
113
+ stateStr != FSTR_BUTTONSTATE_longPress) {
114
+ return false ;
121
115
}
122
116
123
- return success;
117
+ SmartButtonPressType pressType = getSmartButtonPressType (stateStr);
118
+ return buttonPressCallback (device->deviceId , pressType);
124
119
}
125
120
126
121
} // namespace SINRICPRO_NAMESPACE
0 commit comments