From 03b096f96b5855bd0985045e292ac867daa99862 Mon Sep 17 00:00:00 2001 From: Awawa Date: Mon, 24 Jul 2023 00:18:27 +0200 Subject: [PATCH 1/3] JSON API: retrieve the average LED color of a given instance --- include/api/API.h | 2 + include/api/JsonAPI.h | 2 + include/base/HyperHdrIManager.h | 2 + include/base/HyperHdrInstance.h | 3 ++ sources/api/API.cpp | 12 +++++ .../JSONRPC_schema/schema-current-state.json | 26 +++++++++ sources/api/JSONRPC_schema/schema.json | 2 +- sources/api/JSONRPC_schemas.qrc | 1 + sources/api/JsonAPI.cpp | 16 ++++++ sources/base/HyperHdrIManager.cpp | 13 +++++ sources/base/HyperHdrInstance.cpp | 26 +++++++++ www/content/json_api.html | 53 +++++++++++++++++++ www/css/adminlte.hyperhdr.min.css | 2 +- www/i18n/en.json | 5 +- www/js/json_api.js | 46 ++++++++++++++++ 15 files changed, 208 insertions(+), 3 deletions(-) create mode 100644 sources/api/JSONRPC_schema/schema-current-state.json diff --git a/include/api/API.h b/include/api/API.h index a4b9ff021..d1731f12e 100644 --- a/include/api/API.h +++ b/include/api/API.h @@ -212,6 +212,8 @@ class API : public QObject /// void stopInstance(quint8 index); + QJsonObject getAverageColor(quint8 index); + ////////////////////////////////// /// AUTH / ADMINISTRATION METHODS ////////////////////////////////// diff --git a/include/api/JsonAPI.h b/include/api/JsonAPI.h index 4433bee96..cab0d9a00 100644 --- a/include/api/JsonAPI.h +++ b/include/api/JsonAPI.h @@ -308,6 +308,8 @@ private slots: void handleSmoothingCommand(const QJsonObject& message, const QString& command, int tan); + void handleCurrentStateCommand(const QJsonObject& message, const QString& command, int tan); + void handleTunnel(const QJsonObject& message, const QString& command, int tan); /// diff --git a/include/base/HyperHdrIManager.h b/include/base/HyperHdrIManager.h index 7bc771cbb..da4ed10de 100644 --- a/include/base/HyperHdrIManager.h +++ b/include/base/HyperHdrIManager.h @@ -84,6 +84,8 @@ public slots: /// bool stopInstance(quint8 inst); + QJsonObject getAverageColor(quint8 index); + /// /// @brief Toggle the state of all HyperHDR instances /// @param pause If true all instances toggle to pause, else to resume diff --git a/include/base/HyperHdrInstance.h b/include/base/HyperHdrInstance.h index f4dfbf3ae..c6a31cf34 100644 --- a/include/base/HyperHdrInstance.h +++ b/include/base/HyperHdrInstance.h @@ -109,7 +109,10 @@ class HyperHdrInstance : public QObject public slots: + QJsonObject getAverageColor(); + void setSmoothing(int time); + void identifyLed(const QJsonObject& params); bool getReadOnlyMode() { return _readOnlyMode; }; diff --git a/sources/api/API.cpp b/sources/api/API.cpp index 78394f540..1b4fa36ee 100644 --- a/sources/api/API.cpp +++ b/sources/api/API.cpp @@ -349,6 +349,18 @@ void API::stopInstance(quint8 index) QMetaObject::invokeMethod(_instanceManager, "stopInstance", Qt::QueuedConnection, Q_ARG(quint8, index)); } +QJsonObject API::getAverageColor(quint8 index) +{ + QJsonObject res; + + if (_instanceManager->thread() != this->thread()) + QMetaObject::invokeMethod(_instanceManager, "getAverageColor", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QJsonObject, res), Q_ARG(quint8, index)); + else + res = _instanceManager->getAverageColor(index); + + return res; +} + void API::requestActiveRegister(QObject* callerInstance) { // TODO FIXME diff --git a/sources/api/JSONRPC_schema/schema-current-state.json b/sources/api/JSONRPC_schema/schema-current-state.json new file mode 100644 index 000000000..acae70648 --- /dev/null +++ b/sources/api/JSONRPC_schema/schema-current-state.json @@ -0,0 +1,26 @@ +{ + "type":"object", + "required":true, + "properties":{ + "command": { + "type" : "string", + "required" : true, + "enum" : ["current-state"] + }, + "subcommand" : { + "type" : "string", + "required" : true, + "enum" : ["average-color"] + }, + "tan" : { + "type" : "integer" + }, + "instance": { + "type": "integer", + "minimum" : 0, + "maximum" : 255, + "required": true + } + }, + "additionalProperties": false +} diff --git a/sources/api/JSONRPC_schema/schema.json b/sources/api/JSONRPC_schema/schema.json index 4078c063d..a6b969690 100644 --- a/sources/api/JSONRPC_schema/schema.json +++ b/sources/api/JSONRPC_schema/schema.json @@ -5,7 +5,7 @@ "command": { "type" : "string", "required" : true, - "enum": [ "color", "tunnel", "smoothing", "benchmark", "lut-install", "image", "effect", "create-effect", "delete-effect", "serverinfo", "clear", "clearall", "adjustment", "sourceselect", "config", "componentstate", "ledcolors", "load-db", "save-db", "logging", "performance-counters", "lut-calibration", "signal-calibration", "processing", "sysinfo", "videomodehdr", "video-crop", "videomode", "authorize", "instance", "leddevice", "transform", "correction", "temperature", "help", "video-controls" ] + "enum": [ "color", "tunnel", "smoothing", "benchmark", "lut-install", "image", "effect", "create-effect", "delete-effect", "serverinfo", "clear", "clearall", "adjustment", "sourceselect", "config", "componentstate", "current-state", "ledcolors", "load-db", "save-db", "logging", "performance-counters", "lut-calibration", "signal-calibration", "processing", "sysinfo", "videomodehdr", "video-crop", "videomode", "authorize", "instance", "leddevice", "transform", "correction", "temperature", "help", "video-controls" ] } } } diff --git a/sources/api/JSONRPC_schemas.qrc b/sources/api/JSONRPC_schemas.qrc index d4e7c8050..7fe0b6e9b 100644 --- a/sources/api/JSONRPC_schemas.qrc +++ b/sources/api/JSONRPC_schemas.qrc @@ -34,6 +34,7 @@ JSONRPC_schema/schema-tunnel.json JSONRPC_schema/schema-performance-counters.json JSONRPC_schema/schema-smoothing.json + JSONRPC_schema/schema-current-state.json JSONRPC_schema/schema-classic.json JSONRPC_schema/schema-classic.json diff --git a/sources/api/JsonAPI.cpp b/sources/api/JsonAPI.cpp index ab4b7eaf8..749f94d63 100644 --- a/sources/api/JsonAPI.cpp +++ b/sources/api/JsonAPI.cpp @@ -225,6 +225,8 @@ void JsonAPI::handleMessage(const QString& messageString, const QString& httpAut handleLutInstallCommand(message, command, tan); else if (command == "smoothing") handleSmoothingCommand(message, command, tan); + else if (command == "current-state") + handleCurrentStateCommand(message, command, tan); else if (command == "transform" || command == "correction" || command == "temperature") sendErrorReply("The command " + command + "is deprecated, please use the HyperHDR Web Interface to configure", command, tan); // END @@ -904,6 +906,20 @@ void JsonAPI::handleLutInstallCommand(const QJsonObject& message, const QString& sendErrorReply("No Authorization", command, tan); } +void JsonAPI::handleCurrentStateCommand(const QJsonObject& message, const QString& command, int tan) +{ + const QString& subc = message["subcommand"].toString().trimmed(); + int instance = message["instance"].toInt(0); + + if (subc == "average-color") + { + QJsonObject avColor = API::getAverageColor(instance); + sendSuccessDataReply(QJsonDocument(avColor), command + "-" + subc, tan); + } + else + handleNotImplemented(command, tan); +} + void JsonAPI::handleSmoothingCommand(const QJsonObject& message, const QString& command, int tan) { const QString& subc = message["subcommand"].toString().trimmed().toLower(); diff --git a/sources/base/HyperHdrIManager.cpp b/sources/base/HyperHdrIManager.cpp index 160b24308..857452754 100644 --- a/sources/base/HyperHdrIManager.cpp +++ b/sources/base/HyperHdrIManager.cpp @@ -101,6 +101,19 @@ void HyperHdrIManager::setSmoothing(int time) QTimer::singleShot(0, instance, [=]() { instance->setSmoothing(time); }); } +QJsonObject HyperHdrIManager::getAverageColor(quint8 index) +{ + HyperHdrInstance* instance = HyperHdrIManager::getHyperHdrInstance(index); + QJsonObject res; + + if (instance->thread() != this->thread()) + QMetaObject::invokeMethod(instance, "getAverageColor", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QJsonObject, res)); + else + res = instance->getAverageColor(); + + return res; +} + bool HyperHdrIManager::isCEC() { QMap instCopy = _runningInstances; diff --git a/sources/base/HyperHdrInstance.cpp b/sources/base/HyperHdrInstance.cpp index 940adc6f8..698ab71f2 100644 --- a/sources/base/HyperHdrInstance.cpp +++ b/sources/base/HyperHdrInstance.cpp @@ -315,6 +315,32 @@ void HyperHdrInstance::setSmoothing(int time) _smoothing->updateCurrentConfig(time); } +QJsonObject HyperHdrInstance::getAverageColor() +{ + QJsonObject ret; + + auto copy = _globalLedBuffer; + long red = 0, green = 0, blue = 0, count = 0; + + for (const ColorRgb& c : copy) + { + red += c.red; + green += c.green; + blue += c.blue; + + count++; + } + + if (count > 0) + { + ret["red"] = static_cast(red / count); + ret["green"] = static_cast(green / count); + ret["blue"] = static_cast(blue / count); + } + + return ret; +} + unsigned HyperHdrInstance::updateSmoothingConfig(unsigned id, int settlingTime_ms, double ledUpdateFrequency_hz, bool directMode) { unsigned retVal = id; diff --git a/www/content/json_api.html b/www/content/json_api.html index 20bb7e4d4..e84a420c7 100644 --- a/www/content/json_api.html +++ b/www/content/json_api.html @@ -660,6 +660,59 @@

Explanation

+ +
+
+
+
+
+
+
+
+
+

Explanation

+ +
+
+
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+
+
+ +
+
+
+
+
+ +
+ - - - @@ -237,7 +234,20 @@

HyperHDR Web Configuration requires Javascript. Please enable Javascript in
- +
+
+ +
+