mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Fix WLED & Smoothing (#1567)
* WLED - Fix empty segment element in DB * WLED - Fix to not overwrite on state when not isStayOnAfterStreaming * Refactor ProviderRestApi, increase default timeout * Fix Smoothing - Fix empty updates, consider smoothing configs for effects * UI - Fix not removed priority * Add missing header and code updates * setRedirectPolicy was only introduced in Qt 5.9 * Adalight - Align to HyperSerial v9.0.0 * HyperSerial Hyperion with awa protocol v8.0.0 * Correct line-endings
This commit is contained in:
@@ -121,11 +121,17 @@ void EffectEngine::handleUpdatedEffectList()
|
||||
// add smoothing configurations to Hyperion
|
||||
if (def.args["smoothing-custom-settings"].toBool())
|
||||
{
|
||||
int settlingTime_ms = def.args["smoothing-time_ms"].toInt();
|
||||
double ledUpdateFrequency_hz = def.args["smoothing-updateFrequency"].toDouble();
|
||||
unsigned updateDelay {0};
|
||||
|
||||
Debug(_log, "Effect \"%s\": Add custom smoothing settings [%d]. Type: Linear, Settling time: %dms, Interval: %.fHz ", QSTRING_CSTR(def.name), specificId, settlingTime_ms, ledUpdateFrequency_hz);
|
||||
|
||||
def.smoothCfg = _hyperion->updateSmoothingConfig(
|
||||
++specificId,
|
||||
def.args["smoothing-time_ms"].toInt(),
|
||||
def.args["smoothing-updateFrequency"].toDouble(),
|
||||
0 );
|
||||
++specificId,
|
||||
settlingTime_ms,
|
||||
ledUpdateFrequency_hz,
|
||||
updateDelay );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -155,11 +161,18 @@ int EffectEngine::runEffect(const QString &effectName, const QJsonObject &args,
|
||||
//In case smoothing information is provided dynamically use temp smoothing config item (2)
|
||||
if (smoothCfg == SmoothingConfigID::SYSTEM && args["smoothing-custom-settings"].toBool())
|
||||
{
|
||||
int settlingTime_ms = args["smoothing-time_ms"].toInt();
|
||||
double ledUpdateFrequency_hz = args["smoothing-updateFrequency"].toDouble();
|
||||
unsigned updateDelay {0};
|
||||
|
||||
Debug(_log, "Effect \"%s\": Apply dynamic smoothing settings, if smoothing. Type: Linear, Settling time: %dms, Interval: %.fHz ", QSTRING_CSTR(effectName), settlingTime_ms, ledUpdateFrequency_hz);
|
||||
|
||||
smoothCfg = _hyperion->updateSmoothingConfig(
|
||||
SmoothingConfigID::EFFECT_DYNAMIC,
|
||||
args["smoothing-time_ms"].toInt(),
|
||||
args["smoothing-updateFrequency"].toDouble(),
|
||||
0 );
|
||||
SmoothingConfigID::EFFECT_DYNAMIC,
|
||||
settlingTime_ms,
|
||||
ledUpdateFrequency_hz,
|
||||
updateDelay
|
||||
);
|
||||
}
|
||||
|
||||
if (pythonScript.isEmpty())
|
||||
|
@@ -70,6 +70,7 @@ LinearColorSmoothing::LinearColorSmoothing(const QJsonDocument &config, Hyperion
|
||||
, _pause(false)
|
||||
, _currentConfigId(SmoothingConfigID::SYSTEM)
|
||||
, _enabled(false)
|
||||
, _enabledSystemCfg(false)
|
||||
, _smoothingType(SmoothingType::Linear)
|
||||
, tempValues(std::vector<uint64_t>(0, 0L))
|
||||
{
|
||||
@@ -114,11 +115,12 @@ void LinearColorSmoothing::handleSettingsUpdate(settings::type type, const QJson
|
||||
QJsonObject obj = config.object();
|
||||
|
||||
setEnable(obj["enable"].toBool(_enabled));
|
||||
_enabledSystemCfg = _enabled;
|
||||
|
||||
SmoothingCfg cfg(false,
|
||||
static_cast<int64_t>(obj[SETTINGS_KEY_SETTLING_TIME].toInt(DEFAULT_SETTLINGTIME)),
|
||||
static_cast<int64_t>(MS_PER_MICRO / obj[SETTINGS_KEY_UPDATE_FREQUENCY].toDouble(DEFAULT_UPDATEFREQUENCY))
|
||||
);
|
||||
int64_t settlingTime_ms = static_cast<int64_t>(obj[SETTINGS_KEY_SETTLING_TIME].toInt(DEFAULT_SETTLINGTIME));
|
||||
int _updateInterval_ms =static_cast<int>(MS_PER_MICRO / obj[SETTINGS_KEY_UPDATE_FREQUENCY].toDouble(DEFAULT_UPDATEFREQUENCY));
|
||||
|
||||
SmoothingCfg cfg(false, settlingTime_ms, _updateInterval_ms);
|
||||
|
||||
const QString typeString = obj[SETTINGS_KEY_SMOOTHING_TYPE].toString();
|
||||
|
||||
@@ -162,7 +164,10 @@ int LinearColorSmoothing::write(const std::vector<ColorRgb> &ledValues)
|
||||
_previousValues = ledValues;
|
||||
_previousInterpolationTime = micros();
|
||||
|
||||
_timer->start(_updateInterval);
|
||||
if (!_pause)
|
||||
{
|
||||
_timer->start(_updateInterval);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -510,6 +515,8 @@ void LinearColorSmoothing::clearRememberedFrames()
|
||||
|
||||
void LinearColorSmoothing::queueColors(const std::vector<ColorRgb> &ledColors)
|
||||
{
|
||||
assert (ledColors.size() > 0);
|
||||
|
||||
if (_outputDelay == 0)
|
||||
{
|
||||
// No output delay => immediate write
|
||||
@@ -558,13 +565,16 @@ void LinearColorSmoothing::componentStateChange(hyperion::Components component,
|
||||
|
||||
void LinearColorSmoothing::setEnable(bool enable)
|
||||
{
|
||||
_enabled = enable;
|
||||
if (!_enabled)
|
||||
if ( _enabled != enable)
|
||||
{
|
||||
clearQueuedColors();
|
||||
_enabled = enable;
|
||||
if (!_enabled)
|
||||
{
|
||||
clearQueuedColors();
|
||||
}
|
||||
// update comp register
|
||||
_hyperion->setNewComponentState(hyperion::COMP_SMOOTHING, enable);
|
||||
}
|
||||
// update comp register
|
||||
_hyperion->setNewComponentState(hyperion::COMP_SMOOTHING, enable);
|
||||
}
|
||||
|
||||
void LinearColorSmoothing::setPause(bool pause)
|
||||
@@ -603,7 +613,7 @@ unsigned LinearColorSmoothing::updateConfig(int cfgID, int settlingTime_ms, doub
|
||||
updateDelay
|
||||
};
|
||||
_cfgList[updatedCfgID] = cfg;
|
||||
DebugIf(verbose && _enabled, _log,"%s", QSTRING_CSTR(getConfig(updatedCfgID)));
|
||||
Debug(_log,"%s", QSTRING_CSTR(getConfig(updatedCfgID)));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -660,6 +670,19 @@ bool LinearColorSmoothing::selectConfig(int cfgID, bool force)
|
||||
_interpolationCounter = 0;
|
||||
_interpolationStatCounter = 0;
|
||||
|
||||
//Enable smoothing for effects with smoothing
|
||||
if (cfgID >= SmoothingConfigID::EFFECT_DYNAMIC)
|
||||
{
|
||||
Debug(_log,"Run Effect with Smoothing enabled");
|
||||
_enabledSystemCfg = _enabled;
|
||||
setEnable(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Restore enabled state after running an effect with smoothing
|
||||
setEnable(_enabledSystemCfg);
|
||||
}
|
||||
|
||||
if (_cfgList[cfgID]._updateInterval != _updateInterval)
|
||||
{
|
||||
|
||||
@@ -667,7 +690,10 @@ bool LinearColorSmoothing::selectConfig(int cfgID, bool force)
|
||||
_updateInterval = _cfgList[cfgID]._updateInterval;
|
||||
if (this->enabled())
|
||||
{
|
||||
_timer->start(_updateInterval);
|
||||
if (!_pause && !_targetValues.empty())
|
||||
{
|
||||
_timer->start(_updateInterval);
|
||||
}
|
||||
}
|
||||
}
|
||||
_currentConfigId = cfgID;
|
||||
@@ -689,30 +715,36 @@ QString LinearColorSmoothing::getConfig(int cfgID)
|
||||
{
|
||||
SmoothingCfg cfg = _cfgList[cfgID];
|
||||
|
||||
configText = QString ("[%1] - type: %2, pause: %3, settlingTime: %4ms, interval: %5ms (%6Hz), delay: %7 frames")
|
||||
.arg(cfgID)
|
||||
.arg(SmoothingCfg::EnumToString(cfg._type),(cfg._pause) ? "true" : "false")
|
||||
.arg(cfg._settlingTime)
|
||||
.arg(cfg._updateInterval)
|
||||
.arg(int(MS_PER_MICRO/cfg._updateInterval))
|
||||
.arg(cfg._outputDelay);
|
||||
configText = QString ("[%1] - Type: %2, Pause: %3")
|
||||
.arg(cfgID)
|
||||
.arg(SmoothingCfg::EnumToString(cfg._type),(cfg._pause) ? "true" : "false") ;
|
||||
|
||||
switch (cfg._type) {
|
||||
case SmoothingType::Linear:
|
||||
break;
|
||||
|
||||
case SmoothingType::Decay:
|
||||
{
|
||||
const double thalf = (1.0-std::pow(1.0/2, 1.0/_decay))*_settlingTime;
|
||||
configText += QString (", interpolationRate: %1Hz, dithering: %2, decay: %3 -> halftime: %4ms")
|
||||
.arg(cfg._interpolationRate,0,'f',2)
|
||||
.arg((cfg._dithering) ? "true" : "false")
|
||||
.arg(cfg._decay,0,'f',2)
|
||||
.arg(thalf,0,'f',2);
|
||||
configText += QString (", Interpolation rate: %1Hz, Dithering: %2, decay: %3 -> Halftime: %4ms")
|
||||
.arg(cfg._interpolationRate,0,'f',2)
|
||||
.arg((cfg._dithering) ? "true" : "false")
|
||||
.arg(cfg._decay,0,'f',2)
|
||||
.arg(thalf,0,'f',2);
|
||||
[[fallthrough]];
|
||||
}
|
||||
|
||||
case SmoothingType::Linear:
|
||||
{
|
||||
configText += QString (", Settling time: %1ms, Interval: %2ms (%3Hz)")
|
||||
.arg(cfg._settlingTime)
|
||||
.arg(cfg._updateInterval)
|
||||
.arg(int(MS_PER_MICRO/cfg._updateInterval));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
configText += QString (", delay: %1 frames")
|
||||
.arg(cfg._outputDelay);
|
||||
}
|
||||
|
||||
return configText;
|
||||
}
|
||||
|
||||
@@ -736,7 +768,6 @@ LinearColorSmoothing::SmoothingCfg::SmoothingCfg(bool pause, int64_t settlingTim
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
QString LinearColorSmoothing::SmoothingCfg::EnumToString(SmoothingType type)
|
||||
{
|
||||
if (type == SmoothingType::Linear) {
|
||||
|
@@ -523,7 +523,10 @@ bool LedDeviceWled::restoreState()
|
||||
|
||||
_originalStateProperties[STATE_LIVE] = false;
|
||||
_originalStateProperties[STATE_TRANSITIONTIME_CURRENTCALL] = 0;
|
||||
_originalStateProperties[STATE_ON] = _isStayOnAfterStreaming;
|
||||
if (_isStayOnAfterStreaming)
|
||||
{
|
||||
_originalStateProperties[STATE_ON] = true;
|
||||
}
|
||||
|
||||
httpResponse response = _restApi->put(_originalStateProperties);
|
||||
if ( response.error() )
|
||||
|
@@ -20,25 +20,34 @@ enum HttpStatusCode {
|
||||
NoContent = 204,
|
||||
BadRequest = 400,
|
||||
UnAuthorized = 401,
|
||||
Forbidden = 403,
|
||||
NotFound = 404
|
||||
};
|
||||
|
||||
constexpr std::chrono::milliseconds DEFAULT_REST_TIMEOUT{ 400 };
|
||||
|
||||
} //End of constants
|
||||
|
||||
ProviderRestApi::ProviderRestApi(const QString& host, int port, const QString& basePath)
|
||||
:_log(Logger::getInstance("LEDDEVICE"))
|
||||
, _networkManager(nullptr)
|
||||
ProviderRestApi::ProviderRestApi(const QString& scheme, const QString& host, int port, const QString& basePath)
|
||||
: _log(Logger::getInstance("LEDDEVICE"))
|
||||
, _networkManager(nullptr)
|
||||
, _requestTimeout(DEFAULT_REST_TIMEOUT)
|
||||
{
|
||||
_networkManager = new QNetworkAccessManager();
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0))
|
||||
_networkManager->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
#endif
|
||||
|
||||
_apiUrl.setScheme("http");
|
||||
_apiUrl.setScheme(scheme);
|
||||
_apiUrl.setHost(host);
|
||||
_apiUrl.setPort(port);
|
||||
_basePath = basePath;
|
||||
}
|
||||
|
||||
ProviderRestApi::ProviderRestApi(const QString& scheme, const QString& host, int port)
|
||||
: ProviderRestApi(scheme, host, port, "") {}
|
||||
|
||||
ProviderRestApi::ProviderRestApi(const QString& host, int port, const QString& basePath)
|
||||
: ProviderRestApi("http", host, port, basePath) {}
|
||||
|
||||
ProviderRestApi::ProviderRestApi(const QString& host, int port)
|
||||
: ProviderRestApi(host, port, "") {}
|
||||
|
||||
@@ -62,6 +71,12 @@ void ProviderRestApi::setBasePath(const QString& basePath)
|
||||
appendPath(_basePath, basePath);
|
||||
}
|
||||
|
||||
void ProviderRestApi::setPath(const QStringList& pathElements)
|
||||
{
|
||||
_path.clear();
|
||||
appendPath(_path, pathElements.join(ONE_SLASH));
|
||||
}
|
||||
|
||||
void ProviderRestApi::setPath(const QString& path)
|
||||
{
|
||||
_path.clear();
|
||||
@@ -73,6 +88,11 @@ void ProviderRestApi::appendPath(const QString& path)
|
||||
appendPath(_path, path);
|
||||
}
|
||||
|
||||
void ProviderRestApi::appendPath(const QStringList& pathElements)
|
||||
{
|
||||
appendPath(_path, pathElements.join(ONE_SLASH));
|
||||
}
|
||||
|
||||
void ProviderRestApi::appendPath ( QString& path, const QString &appendPath)
|
||||
{
|
||||
if (!appendPath.isEmpty() && appendPath != ONE_SLASH)
|
||||
@@ -132,40 +152,7 @@ httpResponse ProviderRestApi::get()
|
||||
|
||||
httpResponse ProviderRestApi::get(const QUrl& url)
|
||||
{
|
||||
// Perform request
|
||||
QNetworkRequest request(_networkRequestHeaders);
|
||||
request.setUrl(url);
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
|
||||
_networkManager->setTransferTimeout(DEFAULT_REST_TIMEOUT.count());
|
||||
#endif
|
||||
|
||||
QNetworkReply* reply = _networkManager->get(request);
|
||||
|
||||
// Connect requestFinished signal to quit slot of the loop.
|
||||
QEventLoop loop;
|
||||
QEventLoop::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
|
||||
ReplyTimeout::set(reply, DEFAULT_REST_TIMEOUT.count());
|
||||
#endif
|
||||
|
||||
// Go into the loop until the request is finished.
|
||||
loop.exec();
|
||||
|
||||
httpResponse response;
|
||||
if (reply->operation() == QNetworkAccessManager::GetOperation)
|
||||
{
|
||||
if(reply->error() != QNetworkReply::NoError)
|
||||
{
|
||||
Debug(_log, "GET: [%s]", QSTRING_CSTR( url.toString() ));
|
||||
}
|
||||
response = getResponse(reply );
|
||||
}
|
||||
// Free space.
|
||||
reply->deleteLater();
|
||||
// Return response
|
||||
return response;
|
||||
return executeOperation(QNetworkAccessManager::GetOperation, url);
|
||||
}
|
||||
|
||||
httpResponse ProviderRestApi::put(const QJsonObject &body)
|
||||
@@ -180,40 +167,7 @@ httpResponse ProviderRestApi::put(const QString &body)
|
||||
|
||||
httpResponse ProviderRestApi::put(const QUrl &url, const QByteArray &body)
|
||||
{
|
||||
// Perform request
|
||||
QNetworkRequest request(_networkRequestHeaders);
|
||||
request.setUrl(url);
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
|
||||
_networkManager->setTransferTimeout(DEFAULT_REST_TIMEOUT.count());
|
||||
#endif
|
||||
|
||||
QNetworkReply* reply = _networkManager->put(request, body);
|
||||
// Connect requestFinished signal to quit slot of the loop.
|
||||
QEventLoop loop;
|
||||
QEventLoop::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
|
||||
ReplyTimeout::set(reply, DEFAULT_REST_TIMEOUT.count());
|
||||
#endif
|
||||
|
||||
// Go into the loop until the request is finished.
|
||||
loop.exec();
|
||||
|
||||
httpResponse response;
|
||||
if (reply->operation() == QNetworkAccessManager::PutOperation)
|
||||
{
|
||||
if(reply->error() != QNetworkReply::NoError)
|
||||
{
|
||||
Debug(_log, "PUT: [%s] [%s]", QSTRING_CSTR( url.toString() ),body.constData() );
|
||||
}
|
||||
response = getResponse(reply);
|
||||
}
|
||||
// Free space.
|
||||
reply->deleteLater();
|
||||
|
||||
// Return response
|
||||
return response;
|
||||
return executeOperation(QNetworkAccessManager::PutOperation, url, body);
|
||||
}
|
||||
|
||||
httpResponse ProviderRestApi::post(const QJsonObject& body)
|
||||
@@ -228,76 +182,69 @@ httpResponse ProviderRestApi::post(const QString& body)
|
||||
|
||||
httpResponse ProviderRestApi::post(const QUrl& url, const QByteArray& body)
|
||||
{
|
||||
// Perform request
|
||||
QNetworkRequest request(_networkRequestHeaders);
|
||||
request.setUrl(url);
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
|
||||
_networkManager->setTransferTimeout(DEFAULT_REST_TIMEOUT.count());
|
||||
#endif
|
||||
|
||||
QNetworkReply* reply = _networkManager->post(request, body);
|
||||
// Connect requestFinished signal to quit slot of the loop.
|
||||
QEventLoop loop;
|
||||
QEventLoop::connect(reply,&QNetworkReply::finished,&loop,&QEventLoop::quit);
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
|
||||
ReplyTimeout::set(reply, DEFAULT_REST_TIMEOUT.count());
|
||||
#endif
|
||||
|
||||
// Go into the loop until the request is finished.
|
||||
loop.exec();
|
||||
|
||||
httpResponse response;
|
||||
if (reply->operation() == QNetworkAccessManager::PostOperation)
|
||||
{
|
||||
if(reply->error() != QNetworkReply::NoError)
|
||||
{
|
||||
Debug(_log, "POST: [%s] [%s]", QSTRING_CSTR( url.toString() ),body.constData() );
|
||||
}
|
||||
response = getResponse(reply);
|
||||
}
|
||||
// Free space.
|
||||
reply->deleteLater();
|
||||
|
||||
// Return response
|
||||
return response;
|
||||
return executeOperation(QNetworkAccessManager::PostOperation, url, body);
|
||||
}
|
||||
|
||||
httpResponse ProviderRestApi::deleteResource(const QUrl& url)
|
||||
{
|
||||
return executeOperation(QNetworkAccessManager::DeleteOperation, url);
|
||||
}
|
||||
|
||||
httpResponse ProviderRestApi::executeOperation(QNetworkAccessManager::Operation operation, const QUrl& url, const QByteArray& body)
|
||||
{
|
||||
// Perform request
|
||||
QNetworkRequest request(_networkRequestHeaders);
|
||||
request.setUrl(url);
|
||||
request.setOriginatingObject(this);
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
|
||||
_networkManager->setTransferTimeout(DEFAULT_REST_TIMEOUT.count());
|
||||
_networkManager->setTransferTimeout(_requestTimeout.count());
|
||||
#endif
|
||||
|
||||
QNetworkReply* reply = _networkManager->deleteResource(request);
|
||||
QDateTime start = QDateTime::currentDateTime();
|
||||
QString opCode;
|
||||
QNetworkReply* reply;
|
||||
switch (operation) {
|
||||
case QNetworkAccessManager::GetOperation:
|
||||
opCode = "GET";
|
||||
reply = _networkManager->get(request);
|
||||
break;
|
||||
case QNetworkAccessManager::PutOperation:
|
||||
opCode = "PUT";
|
||||
reply = _networkManager->put(request, body);
|
||||
break;
|
||||
case QNetworkAccessManager::PostOperation:
|
||||
opCode = "POST";
|
||||
reply = _networkManager->post(request, body);
|
||||
break;
|
||||
case QNetworkAccessManager::DeleteOperation:
|
||||
opCode = "DELETE";
|
||||
reply = _networkManager->deleteResource(request);
|
||||
break;
|
||||
default:
|
||||
Error(_log, "Unsupported operation");
|
||||
return httpResponse();
|
||||
}
|
||||
|
||||
// Connect requestFinished signal to quit slot of the loop.
|
||||
QEventLoop loop;
|
||||
QEventLoop::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
||||
// Go into the loop until the request is finished.
|
||||
loop.exec();
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
|
||||
ReplyTimeout::set(reply, DEFAULT_REST_TIMEOUT.count());
|
||||
ReplyTimeout* timeout = ReplyTimeout::set(reply, _requestTimeout.count());
|
||||
#endif
|
||||
|
||||
httpResponse response;
|
||||
if (reply->operation() == QNetworkAccessManager::DeleteOperation)
|
||||
{
|
||||
if(reply->error() != QNetworkReply::NoError)
|
||||
{
|
||||
Debug(_log, "DELETE: [%s]", QSTRING_CSTR(url.toString()));
|
||||
}
|
||||
response = getResponse(reply);
|
||||
}
|
||||
// Go into the loop until the request is finished.
|
||||
loop.exec();
|
||||
QDateTime end = QDateTime::currentDateTime();
|
||||
|
||||
httpResponse response = (reply->operation() == operation) ? getResponse(reply) : httpResponse();
|
||||
|
||||
Debug(_log, "%s took %lldms, HTTP %d: [%s] [%s]", QSTRING_CSTR(opCode), start.msecsTo(end), response.getHttpStatusCode(), QSTRING_CSTR(url.toString()), body.constData());
|
||||
|
||||
// Free space.
|
||||
reply->deleteLater();
|
||||
|
||||
// Return response
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -311,34 +258,31 @@ httpResponse ProviderRestApi::getResponse(QNetworkReply* const& reply)
|
||||
|
||||
if (reply->error() == QNetworkReply::NoError)
|
||||
{
|
||||
if ( httpStatusCode != HttpStatusCode::NoContent ){
|
||||
QByteArray replyData = reply->readAll();
|
||||
QByteArray replyData = reply->readAll();
|
||||
|
||||
if (!replyData.isEmpty())
|
||||
if (!replyData.isEmpty())
|
||||
{
|
||||
QJsonParseError error;
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(replyData, &error);
|
||||
|
||||
if (error.error != QJsonParseError::NoError)
|
||||
{
|
||||
QJsonParseError error;
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(replyData, &error);
|
||||
|
||||
if (error.error != QJsonParseError::NoError)
|
||||
{
|
||||
//Received not valid JSON response
|
||||
response.setError(true);
|
||||
response.setErrorReason(error.errorString());
|
||||
}
|
||||
else
|
||||
{
|
||||
response.setBody(jsonDoc);
|
||||
}
|
||||
//Received not valid JSON response
|
||||
response.setError(true);
|
||||
response.setErrorReason(error.errorString());
|
||||
}
|
||||
else
|
||||
{ // Create valid body which is empty
|
||||
response.setBody(QJsonDocument());
|
||||
{
|
||||
response.setBody(jsonDoc);
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // Create valid body which is empty
|
||||
response.setBody(QJsonDocument());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug(_log, "Reply.httpStatusCode [%d]", httpStatusCode );
|
||||
QString errorReason;
|
||||
if (httpStatusCode > 0) {
|
||||
QString httpReason = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
|
||||
@@ -350,25 +294,32 @@ httpResponse ProviderRestApi::getResponse(QNetworkReply* const& reply)
|
||||
case HttpStatusCode::UnAuthorized:
|
||||
advise = "Check Authentication Token (API Key)";
|
||||
break;
|
||||
case HttpStatusCode::Forbidden:
|
||||
advise = "No permission to access the given resource";
|
||||
break;
|
||||
case HttpStatusCode::NotFound:
|
||||
advise = "Check Resource given";
|
||||
break;
|
||||
default:
|
||||
advise = httpReason;
|
||||
break;
|
||||
}
|
||||
errorReason = QString ("[%3 %4] - %5").arg(httpStatusCode).arg(httpReason, advise);
|
||||
}
|
||||
else
|
||||
{
|
||||
errorReason = reply->errorString();
|
||||
if (reply->error() == QNetworkReply::OperationCanceledError)
|
||||
{
|
||||
response.setError(true);
|
||||
response.setErrorReason(errorReason);
|
||||
errorReason = "Network request timeout error";
|
||||
}
|
||||
else
|
||||
{
|
||||
errorReason = reply->errorString();
|
||||
}
|
||||
}
|
||||
|
||||
// Create valid body which is empty
|
||||
response.setBody(QJsonDocument());
|
||||
}
|
||||
response.setError(true);
|
||||
response.setErrorReason(errorReason);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
@@ -388,3 +339,8 @@ void ProviderRestApi::setHeader(QNetworkRequest::KnownHeaders header, const QVar
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ProviderRestApi::setHeader(const QByteArray &headerName, const QByteArray &headerValue)
|
||||
{
|
||||
_networkRequestHeaders.setRawHeader(headerName, headerValue);
|
||||
}
|
||||
|
@@ -13,15 +13,22 @@
|
||||
#include <QBasicTimer>
|
||||
#include <QTimerEvent>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
constexpr std::chrono::milliseconds DEFAULT_REST_TIMEOUT{ 1000 };
|
||||
|
||||
//Set QNetworkReply timeout without external timer
|
||||
//https://stackoverflow.com/questions/37444539/how-to-set-qnetworkreply-timeout-without-external-timer
|
||||
|
||||
class ReplyTimeout : public QObject {
|
||||
class ReplyTimeout : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum HandleMethod { Abort, Close };
|
||||
|
||||
ReplyTimeout(QNetworkReply* reply, const int timeout, HandleMethod method = Abort) :
|
||||
QObject(reply), m_method(method)
|
||||
QObject(reply), m_method(method), m_timedout(false)
|
||||
{
|
||||
Q_ASSERT(reply);
|
||||
if (reply && reply->isRunning()) {
|
||||
@@ -29,20 +36,30 @@ public:
|
||||
connect(reply, &QNetworkReply::finished, this, &QObject::deleteLater);
|
||||
}
|
||||
}
|
||||
static void set(QNetworkReply* reply, const int timeout, HandleMethod method = Abort)
|
||||
|
||||
bool isTimedout() const
|
||||
{
|
||||
new ReplyTimeout(reply, timeout, method);
|
||||
return m_timedout;
|
||||
}
|
||||
|
||||
static ReplyTimeout * set(QNetworkReply* reply, const int timeout, HandleMethod method = Abort)
|
||||
{
|
||||
return new ReplyTimeout(reply, timeout, method);
|
||||
}
|
||||
|
||||
signals:
|
||||
void timedout();
|
||||
|
||||
protected:
|
||||
QBasicTimer m_timer;
|
||||
HandleMethod m_method;
|
||||
|
||||
void timerEvent(QTimerEvent * ev) override {
|
||||
if (!m_timer.isActive() || ev->timerId() != m_timer.timerId())
|
||||
return;
|
||||
auto reply = static_cast<QNetworkReply*>(parent());
|
||||
if (reply->isRunning())
|
||||
{
|
||||
m_timedout = true;
|
||||
emit timedout();
|
||||
if (m_method == Close)
|
||||
reply->close();
|
||||
else if (m_method == Abort)
|
||||
@@ -50,6 +67,10 @@ protected:
|
||||
m_timer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
QBasicTimer m_timer;
|
||||
HandleMethod m_method;
|
||||
bool m_timedout;
|
||||
};
|
||||
|
||||
///
|
||||
@@ -104,11 +125,12 @@ private:
|
||||
///
|
||||
///@endcode
|
||||
///
|
||||
class ProviderRestApi
|
||||
class ProviderRestApi : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
///
|
||||
/// @brief Constructor of the REST-API wrapper
|
||||
///
|
||||
ProviderRestApi();
|
||||
@@ -121,6 +143,15 @@ public:
|
||||
///
|
||||
explicit ProviderRestApi(const QString& host, int port);
|
||||
|
||||
///
|
||||
/// @brief Constructor of the REST-API wrapper
|
||||
///
|
||||
/// @param[in] scheme
|
||||
/// @param[in] host
|
||||
/// @param[in] port
|
||||
///
|
||||
explicit ProviderRestApi(const QString& scheme, const QString& host, int port);
|
||||
|
||||
///
|
||||
/// @brief Constructor of the REST-API wrapper
|
||||
///
|
||||
@@ -130,10 +161,20 @@ public:
|
||||
///
|
||||
explicit ProviderRestApi(const QString& host, int port, const QString& basePath);
|
||||
|
||||
///
|
||||
/// @brief Constructor of the REST-API wrapper
|
||||
///
|
||||
/// @param[in] scheme
|
||||
/// @param[in] host
|
||||
/// @param[in] port
|
||||
/// @param[in] API base-path
|
||||
///
|
||||
explicit ProviderRestApi(const QString& scheme, const QString& host, int port, const QString& basePath);
|
||||
|
||||
///
|
||||
/// @brief Destructor of the REST-API wrapper
|
||||
///
|
||||
virtual ~ProviderRestApi();
|
||||
virtual ~ProviderRestApi() override;
|
||||
|
||||
///
|
||||
/// @brief Set an API's host
|
||||
@@ -177,6 +218,12 @@ public:
|
||||
///
|
||||
void setPath(const QString& path);
|
||||
|
||||
/// @brief Set an API's path to address resources
|
||||
///
|
||||
/// @param[in] pathElements to form a path, e.g. (lights,1,state) results in "/lights/1/state/"
|
||||
///
|
||||
void setPath(const QStringList& pathElements);
|
||||
|
||||
///
|
||||
/// @brief Append an API's path element to path set before
|
||||
///
|
||||
@@ -184,6 +231,13 @@ public:
|
||||
///
|
||||
void appendPath(const QString& appendPath);
|
||||
|
||||
///
|
||||
/// @brief Append API's path elements to path set before
|
||||
///
|
||||
/// @param[in] pathElements
|
||||
///
|
||||
void appendPath(const QStringList& pathElements);
|
||||
|
||||
///
|
||||
/// @brief Set an API's fragment
|
||||
///
|
||||
@@ -283,14 +337,28 @@ public:
|
||||
/// @param[in] The type of the header field.
|
||||
/// @param[in] The value of the header field.
|
||||
/// If the header field exists, the value will be combined as comma separated string.
|
||||
|
||||
void setHeader(QNetworkRequest::KnownHeaders header, const QVariant& value);
|
||||
|
||||
///
|
||||
/// Set a header field.
|
||||
///
|
||||
/// @param[in] The type of the header field.
|
||||
/// @param[in] The value of the header field.
|
||||
/// If the header field exists, the value will override the previous setting.
|
||||
void setHeader(const QByteArray &headerName, const QByteArray &headerValue);
|
||||
|
||||
///
|
||||
/// Remove all header fields.
|
||||
///
|
||||
void removeAllHeaders() { _networkRequestHeaders = QNetworkRequest(); }
|
||||
|
||||
///
|
||||
/// Sets the timeout time frame after a request is aborted
|
||||
/// Zero means no timer is set.
|
||||
///
|
||||
/// @param[in] timeout in milliseconds.
|
||||
void setTransferTimeout(std::chrono::milliseconds timeout = DEFAULT_REST_TIMEOUT) { _requestTimeout = timeout; }
|
||||
|
||||
///
|
||||
/// @brief Set the common logger for LED-devices.
|
||||
///
|
||||
@@ -308,10 +376,14 @@ private:
|
||||
///
|
||||
static void appendPath (QString &path, const QString &appendPath) ;
|
||||
|
||||
|
||||
httpResponse executeOperation(QNetworkAccessManager::Operation op, const QUrl& url, const QByteArray& body = {});
|
||||
|
||||
Logger* _log;
|
||||
|
||||
// QNetworkAccessManager object for sending REST-requests.
|
||||
QNetworkAccessManager* _networkManager;
|
||||
std::chrono::milliseconds _requestTimeout;
|
||||
|
||||
QUrl _apiUrl;
|
||||
|
||||
|
@@ -94,7 +94,7 @@ void LedDeviceAdalight::prepareHeader()
|
||||
break;
|
||||
|
||||
case Adalight::AWA:
|
||||
_bufferLength += 7;
|
||||
_bufferLength += 8;
|
||||
[[fallthrough]];
|
||||
case Adalight::ADA:
|
||||
[[fallthrough]];
|
||||
@@ -162,14 +162,20 @@ int LedDeviceAdalight::write(const std::vector<ColorRgb> & ledValues)
|
||||
{
|
||||
whiteChannelExtension(writer);
|
||||
|
||||
uint16_t fletcher1 = 0, fletcher2 = 0;
|
||||
uint16_t fletcher1 = 0;
|
||||
uint16_t fletcher2 = 0;
|
||||
uint16_t fletcherExt = 0;
|
||||
uint8_t position = 0;
|
||||
|
||||
while (hasher < writer)
|
||||
{
|
||||
fletcherExt = (fletcherExt + (*(hasher) ^ (position++))) % 255;
|
||||
fletcher1 = (fletcher1 + *(hasher++)) % 255;
|
||||
fletcher2 = (fletcher2 + fletcher1) % 255;
|
||||
}
|
||||
*(writer++) = static_cast<uint8_t>(fletcher1);
|
||||
*(writer++) = static_cast<uint8_t>(fletcher2);
|
||||
*(writer++) = static_cast<uint8_t>((fletcherExt != 0x41) ? fletcherExt : 0xaa);
|
||||
}
|
||||
_bufferLength = writer - _ledBuffer.data();
|
||||
}
|
||||
|
Reference in New Issue
Block a user