diff --git a/changelog.md b/changelog.md index 7cfe0628..de046570 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ # Changelog +## Version 3.3.1 + - Support SmartButton. + ## Version 3.2.1 - Fixed Arduino Lint errors - LICENSE.txt added diff --git a/library.json b/library.json index d6440f57..8e5b45c9 100644 --- a/library.json +++ b/library.json @@ -13,7 +13,7 @@ "maintainer": true } ], - "version": "3.2.1", + "version": "3.3.1", "frameworks": "arduino", "platforms": [ "espressif8266", diff --git a/library.properties b/library.properties index 6d52c194..5bf00481 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SinricPro -version=3.2.1 +version=3.3.1 author=Boris Jaeger maintainer=Boris Jaeger sentence=Library for https://sinric.pro - simple way to connect your device to alexa diff --git a/src/Capabilities/SmartButtonStateController.h b/src/Capabilities/SmartButtonStateController.h new file mode 100644 index 00000000..147abe6e --- /dev/null +++ b/src/Capabilities/SmartButtonStateController.h @@ -0,0 +1,117 @@ +#pragma once + +#include "../SinricProRequest.h" +#include "../EventLimiter.h" +#include "../SinricProStrings.h" +#include "../SinricProNamespace.h" + +/** + * @brief Enum defining the different types of button press events + */ +enum class SmartButtonPressType { + SINGLE_PRESS, + DOUBLE_PRESS, + LONG_PRESS +}; + +namespace SINRICPRO_NAMESPACE { + +FSTR(BUTTONSTATE, state); // Button state key +FSTR(BUTTONSTATE, singlePress); // Single press state value +FSTR(BUTTONSTATE, doublePress); // Double press state value +FSTR(BUTTONSTATE, longPress); // Long press state value +FSTR(BUTTONSTATE, setSmartButtonState); // Set state action name + +// Callback type definition for button press events +using SmartButtonPressCallback = std::function; + +/** + * @brief Controller class for managing smart button state and interactions + * + * @tparam T The device type that this controller is attached to + */ +template +class SmartButtonStateController { +public: + /** + * @brief Construct a new Smart Button State Controller + * Automatically registers the request handler with the device + */ + SmartButtonStateController(); + + /** + * @brief Register callback for button press events from the app + * @param cb Callback function to handle button press events + */ + void onButtonPress(SmartButtonPressCallback cb); + +protected: + /** + * @brief Handle incoming button state change requests + * @param request The incoming request to process + * @return true if request was handled successfully, false otherwise + */ + bool handleSmartButtonStateController(SinricProRequest &request); + +private: + SmartButtonPressCallback buttonPressCallback; + + /** + * @brief Convert string state to SmartButtonPressType enum + * @param stateStr The state string from the request + * @return corresponding SmartButtonPressType enum value + */ + SmartButtonPressType getSmartButtonPressType(const String& stateStr); +}; + +template +SmartButtonStateController::SmartButtonStateController() { + T* device = static_cast(this); + device->registerRequestHandler( + std::bind(&SmartButtonStateController::handleSmartButtonStateController, + this, + std::placeholders::_1) + ); +} + +template +void SmartButtonStateController::onButtonPress(SmartButtonPressCallback cb) { + buttonPressCallback = cb; +} + +template +SmartButtonPressType SmartButtonStateController::getSmartButtonPressType(const String& stateStr) { + if (stateStr == FSTR_BUTTONSTATE_singlePress) { + return SmartButtonPressType::SINGLE_PRESS; + } else if (stateStr == FSTR_BUTTONSTATE_doublePress) { + return SmartButtonPressType::DOUBLE_PRESS; + } else { + return SmartButtonPressType::LONG_PRESS; + } +} + +template +bool SmartButtonStateController::handleSmartButtonStateController(SinricProRequest &request) { + // Only process setSmartButtonState actions + if (request.action != FSTR_BUTTONSTATE_setSmartButtonState || !buttonPressCallback) { + return false; + } + + T* device = static_cast(this); + String stateStr = request.request_value[FSTR_BUTTONSTATE_state]; + + // Only process valid button states + if (stateStr != FSTR_BUTTONSTATE_singlePress && + stateStr != FSTR_BUTTONSTATE_doublePress && + stateStr != FSTR_BUTTONSTATE_longPress) { + return false; + } + + SmartButtonPressType pressType = getSmartButtonPressType(stateStr); + return buttonPressCallback(device->deviceId, pressType); +} + +} // namespace SINRICPRO_NAMESPACE + +template +using SmartButtonStateController = SINRICPRO_NAMESPACE::SmartButtonStateController; \ No newline at end of file diff --git a/src/SinricProVersion.h b/src/SinricProVersion.h index 12758f4c..59024ce8 100644 --- a/src/SinricProVersion.h +++ b/src/SinricProVersion.h @@ -5,7 +5,7 @@ // Version Configuration #define SINRICPRO_VERSION_MAJOR 3 -#define SINRICPRO_VERSION_MINOR 2 +#define SINRICPRO_VERSION_MINOR 3 #define SINRICPRO_VERSION_REVISION 1 #define SINRICPRO_VERSION STR(SINRICPRO_VERSION_MAJOR) "." STR(SINRICPRO_VERSION_MINOR) "." STR(SINRICPRO_VERSION_REVISION) #define SINRICPRO_VERSION_STR "SinricPro (v" SINRICPRO_VERSION ")"