From 442fab9c59a4c6e8a335834cce105e7565d814f2 Mon Sep 17 00:00:00 2001 From: LordGrey <48840279+Lord-Grey@users.noreply.github.com> Date: Tue, 14 Dec 2021 07:54:53 +0100 Subject: [PATCH] Fix/Workaround set-music error when music is already off (#1379) * Fix/Workaround set-music error when music is already off * Do not ignore error when setting music mode --- CHANGELOG.md | 1 + .../leddevice/dev_net/LedDeviceYeelight.cpp | 48 ++++++++++++++----- libsrc/leddevice/dev_net/LedDeviceYeelight.h | 6 ++- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cd5d665..49750fd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Yeelight - Workaround: Ignore error when setting music mode = off, but the music-mode is already off (#1372) - Standalone grabbers: Improved fps help/error text, fixed default address and port ## Removed diff --git a/libsrc/leddevice/dev_net/LedDeviceYeelight.cpp b/libsrc/leddevice/dev_net/LedDeviceYeelight.cpp index d96f7288..7c41f99d 100644 --- a/libsrc/leddevice/dev_net/LedDeviceYeelight.cpp +++ b/libsrc/leddevice/dev_net/LedDeviceYeelight.cpp @@ -1,4 +1,4 @@ -#include "LedDeviceYeelight.h" +#include "LedDeviceYeelight.h" #include #include @@ -202,13 +202,13 @@ bool YeelightLight::close() return rc; } -int YeelightLight::writeCommand( const QJsonDocument &command ) +int YeelightLight::writeCommand( const QJsonDocument &command, bool ignoreErrors ) { QJsonArray result; - return writeCommand(command, result ); + return writeCommand(command, result, ignoreErrors ); } -int YeelightLight::writeCommand( const QJsonDocument &command, QJsonArray &result ) +int YeelightLight::writeCommand( const QJsonDocument &command, QJsonArray &result, bool ignoreErrors ) { log( 3, "writeCommand()", @@ -278,8 +278,16 @@ int YeelightLight::writeCommand( const QJsonDocument &command, QJsonArray &resul QString errorReason = QString ("(%1) %2").arg(yeeResponse.getErrorCode()).arg( yeeResponse.getErrorReason() ); if ( yeeResponse.getErrorCode() != -1) { - this->setInError ( errorReason ); - rc =-1; + if (!ignoreErrors) + { + this->setInError ( errorReason ); + rc =-1; + } + else + { + log ( 1, "writeCommand():", "Ignore Error: %s", QSTRING_CSTR(errorReason) ); + rc = 0; + } } else { @@ -693,6 +701,13 @@ bool YeelightLight::setPower(bool on, YeelightLight::API_EFFECT effect, int dura _tcpStreamSocket->close(); } } + else + { + if ( !_isInMusicMode && isInMusicMode(true) ) + { + setMusicMode(false); + } + } QString powerParam = on ? API_METHOD_POWER_ON : API_METHOD_POWER_OFF; QString effectParam = effect == YeelightLight::API_EFFECT_SMOOTH ? API_PARAM_EFFECT_SMOOTH : API_PARAM_EFFECT_SUDDEN; @@ -711,7 +726,7 @@ bool YeelightLight::setPower(bool on, YeelightLight::API_EFFECT effect, int dura } log( 2, "setPower() rc", - "%d, isON[%d], isInMusicMode[)%d]", + "%d, isON[%d], isInMusicMode[%d]", static_cast(rc), static_cast( _isOn ), static_cast( _isInMusicMode ) ); return rc; @@ -930,13 +945,22 @@ bool YeelightLight::setMusicMode(bool on, const QHostAddress &hostAddress, int p if ( on ) { paramlist << hostAddress.toString() << port; - } - // Music Mode is only on, if write did not fail nor quota was exceeded - if ( writeCommand( getCommand( API_METHOD_MUSIC_MODE, paramlist ) ) > -1 ) + // Music Mode is only on, if write did not fail nor quota was exceeded + if ( writeCommand( getCommand( API_METHOD_MUSIC_MODE, paramlist ) ) > -1 ) + { + _isInMusicMode = on; + rc = true; + } + } + else { - _isInMusicMode = on; - rc = true; + QJsonArray offParams = { API_METHOD_MUSIC_MODE_OFF }; + if ( writeCommand( getCommand( API_METHOD_MUSIC_MODE, offParams ) ) > -1 ) + { + _isInMusicMode = false; + rc = true; + } } log( 2, diff --git a/libsrc/leddevice/dev_net/LedDeviceYeelight.h b/libsrc/leddevice/dev_net/LedDeviceYeelight.h index 481ac91d..c0957aca 100644 --- a/libsrc/leddevice/dev_net/LedDeviceYeelight.h +++ b/libsrc/leddevice/dev_net/LedDeviceYeelight.h @@ -151,18 +151,20 @@ public: /// @brief Execute a Yeelight-API command /// /// @param[in] command The API command request in JSON + /// @param[in] ignoreErrors Return success, even if errors occured /// @return 0: success, -1: error, -2: command quota exceeded /// - int writeCommand( const QJsonDocument &command ); + int writeCommand( const QJsonDocument &command, bool ignoreErrors = false ); /// /// @brief Execute a Yeelight-API command /// /// @param[in] command The API command request in JSON /// @param[out] result The response to the command in JSON + /// @param[in] ignoreErrors Return success, even if errors occured /// @return 0: success, -1: error, -2: command quota exceeded /// - int writeCommand( const QJsonDocument &command, QJsonArray &result ); + int writeCommand( const QJsonDocument &command, QJsonArray &result, bool ignoreErrors = false ); /// /// @brief Stream a Yeelight-API command