Skip to content

Commit d87c08a

Browse files
committed
feat: start/stop controller
1 parent 6067b2d commit d87c08a

File tree

5 files changed

+190
-5
lines changed

5 files changed

+190
-5
lines changed

Diff for: changelog.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
# Changelog
22

3-
## Version 3.4.1
3+
## Version 3.5.1
4+
Fixed:
5+
- Logging messages.
6+
47
New:
5-
- Fix: invalid signature error message.
8+
- Support Start/Stop Controller for custom device types
9+
10+
## Version 3.4.1
11+
Fixed:
12+
- Fix: invalid signature error message.
613

714
## Version 3.4.0
815
New:

Diff for: library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"maintainer": true
1414
}
1515
],
16-
"version": "3.4.1",
16+
"version": "3.5.1",
1717
"frameworks": "arduino",
1818
"platforms": [
1919
"espressif8266",

Diff for: library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SinricPro
2-
version=3.4.1
2+
version=3.5.1
33
author=Boris Jaeger <[email protected]>
44
maintainer=Boris Jaeger <[email protected]>
55
sentence=Library for https://sinric.pro - simple way to connect your device to alexa

Diff for: src/Capabilities/StartStopController.h

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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>;

Diff for: src/SinricProVersion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
// Version Configuration
77
#define SINRICPRO_VERSION_MAJOR 3
8-
#define SINRICPRO_VERSION_MINOR 4
8+
#define SINRICPRO_VERSION_MINOR 5
99
#define SINRICPRO_VERSION_REVISION 1
1010
#define SINRICPRO_VERSION STR(SINRICPRO_VERSION_MAJOR) "." STR(SINRICPRO_VERSION_MINOR) "." STR(SINRICPRO_VERSION_REVISION)
1111
#define SINRICPRO_VERSION_STR "SinricPro (v" SINRICPRO_VERSION ")"

0 commit comments

Comments
 (0)