Skip to content

Commit

Permalink
JSON API: retrieve the average LED color of a given instance
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Jul 23, 2023
1 parent 69da76c commit 03b096f
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 3 deletions.
2 changes: 2 additions & 0 deletions include/api/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ class API : public QObject
///
void stopInstance(quint8 index);

QJsonObject getAverageColor(quint8 index);

//////////////////////////////////
/// AUTH / ADMINISTRATION METHODS
//////////////////////////////////
Expand Down
2 changes: 2 additions & 0 deletions include/api/JsonAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

///
Expand Down
2 changes: 2 additions & 0 deletions include/base/HyperHdrIManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions include/base/HyperHdrInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
Expand Down
12 changes: 12 additions & 0 deletions sources/api/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions sources/api/JSONRPC_schema/schema-current-state.json
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion sources/api/JSONRPC_schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
}
}
}
1 change: 1 addition & 0 deletions sources/api/JSONRPC_schemas.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<file alias="schema-tunnel">JSONRPC_schema/schema-tunnel.json</file>
<file alias="schema-performance-counters">JSONRPC_schema/schema-performance-counters.json</file>
<file alias="schema-smoothing">JSONRPC_schema/schema-smoothing.json</file>
<file alias="schema-current-state">JSONRPC_schema/schema-current-state.json</file>
<!-- The following schemas are derecated but used to ensure backward compatibility with Classic remote control-->
<file alias="schema-transform">JSONRPC_schema/schema-classic.json</file>
<file alias="schema-correction">JSONRPC_schema/schema-classic.json</file>
Expand Down
16 changes: 16 additions & 0 deletions sources/api/JsonAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
13 changes: 13 additions & 0 deletions sources/base/HyperHdrIManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<quint8, HyperHdrInstance*> instCopy = _runningInstances;
Expand Down
26 changes: 26 additions & 0 deletions sources/base/HyperHdrInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(red / count);
ret["green"] = static_cast<int>(green / count);
ret["blue"] = static_cast<int>(blue / count);
}

return ret;
}

unsigned HyperHdrInstance::updateSmoothingConfig(unsigned id, int settlingTime_ms, double ledUpdateFrequency_hz, bool directMode)
{
unsigned retVal = id;
Expand Down
53 changes: 53 additions & 0 deletions www/content/json_api.html
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,59 @@ <h4 class="bs-main-text" data-i18n="conf_helptable_expl">Explanation</h4>
</div>
</div>
</div>

<div class="card card-default col-12 col-xl-11 col-xxl-10 text-start">
<div class="card-header"><i class="fa fa-podcast fa-fw"></i><span data-i18n="json_api_instanceCurrentState_header"></span></div>
<div class="card-body" >
<div class="w-100" >
<div class="row">
<div class="col-12 col-lg-6">
<div class="row">
<div class="col-12 text-start">
<div class="callout callout-info">
<h4 class="bs-main-text" data-i18n="conf_helptable_expl">Explanation</h4>
<span data-i18n="json_api_instanceCurrentState_expl"></span>
</div>
</div>
</div>
<div class="row">
<div class="col-8 text-start">
<div class="input-group">
<span class="input-group-text" data-i18n="json_api_instance_index"></span>
<input class="form-control" type="number" id="instanceCurrentStateIndex" value="">
</div>
</div>
<div class="col-4 text-start">
<div class="form-check">
<input class="form-check-input" type="radio" id="instanceCurrentStateColor" name="instanceCurrentStateCommand" value="average-color">
<label class="form-check-label" for="instanceCurrentStateColor" data-i18n="general_btn_average_color"></label>
</div>
</div>
</div>
</div>
<div class="col-12 col-lg-6">
<div class="form-group">
<textarea class="form-control" id="instanceCurrentState_json" rows="14" placeholder="JSON output" readonly></textarea>
</div>
</div>
</div>
</div>
</div>
<div class="card-footer">
<div class="d-flex flex-row-reverse bd-highlight mb-3">
<div class="ms-2">
<button type="button" name="instanceCurrentStateButtons" onclick='CopyLink($("#instanceCurrentState_json"))' class="disabled btn btn-primary"><span data-i18n="json_api_copy_link">Copy link</span></button>
</div>
<div class="ms-2">
<button type="button" name="instanceCurrentStateButtons" onclick='CopyJson($("#instanceCurrentState_json"))' class="disabled btn btn-secondary"><span data-i18n="json_api_copy_json">Copy json</span></button>
</div>
<div class="ms-2">
<button type="button" name="instanceCurrentStateButtons" onclick='RunIt($("#instanceCurrentState_json"))' class="disabled btn btn-danger"><span data-i18n="json_api_run_it">Run it</span></button>
</div>
</div>
</div>
</div>

</div>
<script>
$.getScript("/js/json_api.js");
Expand Down
2 changes: 1 addition & 1 deletion www/css/adminlte.hyperhdr.min.css

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion www/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1209,5 +1209,8 @@
"main_menu_grabber_lut_path_found_CRC" : "File CRC: $1",
"main_menu_grabber_lut_path_found_NOCRC" : "enable HDR in USB grabber options and refresh the page",
"main_menu_grabber_lut_restart" : "Please restart your system for the changes to take effect fully!<br>USB grabber config with Brightness=$1, Contrast=$2, Saturation=$3 was saved.",
"main_menu_grabber_lut_confirm" : "Are you sure to download and install new LUT file for your grabber?"
"main_menu_grabber_lut_confirm" : "Are you sure to download and install new LUT file for your grabber?",
"json_api_instanceCurrentState_header" : "Get instance current state",
"json_api_instanceCurrentState_expl" : "Get the current, updated state of the instance, such as the average color of the LEDs",
"general_btn_average_color" : "Average color"
}
46 changes: 46 additions & 0 deletions www/js/json_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,50 @@ function BuildSmoothingJson()
}
}

/////////////////////////////////////////////////////////////////////////
var currentStateJson =
`{
"command" : "current-state",
"subcommand" : "{0}",
"instance" : {1}
}`;


function checkCurrentState(){
var num = $("#instanceCurrentStateIndex").val();
if ((!isNaN(num)) && !isNaN(parseFloat(num)) && $("input[name='instanceCurrentStateCommand']:checked").val())
{
BuildCurrentStatetJson();
}
else
{
$('button[name="instanceCurrentStateButtons"]').each(function(i, obj) {
$(this).addClass('disabled');
});
$("#instanceCurrentState_json").html("");
}
};

$('#instanceCurrentStateIndex').on('input',
function(){
checkCurrentState();
});

$('input[name="instanceCurrentStateCommand"]').change(
function(){
checkCurrentState();
});

function BuildCurrentStatetJson()
{
$('button[name="instanceCurrentStateButtons"]').each(function(i, obj) {
$(this).removeClass('disabled');
});

var component = $("#instanceCurrentStateIndex").val();
var state = $('input[name="instanceCurrentStateCommand"]:checked').val();
var finJson = currentStateJson.replace("{1}", component).replace("{0}", state);
$("#instanceCurrentState_json").html(finJson);
}

});

0 comments on commit 03b096f

Please sign in to comment.