Skip to content

Commit

Permalink
Fix: web LED live simulator doesn't update (awawa-dev#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev authored and chbartsch committed Nov 29, 2022
1 parent f36e430 commit da294c0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
8 changes: 6 additions & 2 deletions include/api/JsonAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ private slots:
///
void handleInstanceStateChange(InstanceState state, quint8 instance, const QString& name = QString());

void handleLedColorsIncoming(const std::vector<ColorRgb>& ledValues);

void handleLedColorsTimer();

signals:
///
/// Signal emits with the reply message provided with handleMessage()
Expand Down Expand Up @@ -122,8 +126,8 @@ private slots:
/// timer for led color refresh
QTimer* _ledStreamTimer;

/// led stream connection handle
QMetaObject::Connection _ledStreamConnection;
/// led stream refresh interval
qint64 _colorsStreamingInterval;

/// the current streaming led values
std::vector<ColorRgb> _currentLedValues;
Expand Down
41 changes: 22 additions & 19 deletions sources/api/JsonAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ JsonAPI::JsonAPI(QString peerAddress, Logger* log, bool localConnection, QObject
_streaming_logging_activated = false;
_ledStreamTimer = new QTimer(this);
_lastSendImage = InternalClock::now();
_colorsStreamingInterval = 50;

connect(_ledStreamTimer, &QTimer::timeout, this, &JsonAPI::handleLedColorsTimer, Qt::UniqueConnection);

Q_INIT_RESOURCE(JSONRPC_schemas);
}
Expand Down Expand Up @@ -1165,44 +1168,45 @@ void JsonAPI::handleComponentStateCommand(const QJsonObject& message, const QStr
sendSuccessReply(command, tan);
}

void JsonAPI::handleLedColorsIncoming(const std::vector<ColorRgb>& ledValues)
{
_currentLedValues = ledValues;

if (_ledStreamTimer->interval() != _colorsStreamingInterval)
_ledStreamTimer->start(_colorsStreamingInterval);
}

void JsonAPI::handleLedColorsTimer()
{
emit streamLedcolorsUpdate(_currentLedValues);
}

void JsonAPI::handleLedColorsCommand(const QJsonObject& message, const QString& command, int tan)
{
// create result
QString subcommand = message["subcommand"].toString("");

// max 20 Hz (50ms) interval for streaming (default: 10 Hz (100ms))
qint64 streaming_interval = qMax(message["interval"].toInt(100), 50);
_colorsStreamingInterval = qMax(message["interval"].toInt(100), 50);

if (subcommand == "ledstream-start")
{
_streaming_leds_reply["success"] = true;
_streaming_leds_reply["command"] = command + "-ledstream-update";
_streaming_leds_reply["tan"] = tan;

connect(_hyperhdr, &HyperHdrInstance::rawLedColors, this, [=](const std::vector<ColorRgb>& ledValues) {
_currentLedValues = ledValues;

// necessary because Qt::UniqueConnection for lambdas does not work until 5.9
// see: https://bugreports.qt.io/browse/QTBUG-52438
if (!_ledStreamConnection)
_ledStreamConnection = connect(_ledStreamTimer, &QTimer::timeout, this, [=]() {
emit streamLedcolorsUpdate(_currentLedValues);
},
Qt::UniqueConnection);

// start the timer
if (!_ledStreamTimer->isActive() || _ledStreamTimer->interval() != streaming_interval)
_ledStreamTimer->start(streaming_interval);
},
Qt::UniqueConnection);
connect(_hyperhdr, &HyperHdrInstance::rawLedColors, this, &JsonAPI::handleLedColorsIncoming, Qt::UniqueConnection);

if (!_ledStreamTimer->isActive() || _ledStreamTimer->interval() != _colorsStreamingInterval)
_ledStreamTimer->start(_colorsStreamingInterval);

// push once
QMetaObject::invokeMethod(_hyperhdr, "update");
}
else if (subcommand == "ledstream-stop")
{
disconnect(_hyperhdr, &HyperHdrInstance::rawLedColors, this, 0);
_ledStreamTimer->stop();
disconnect(_ledStreamConnection);
}
else if (subcommand == "imagestream-start")
{
Expand Down Expand Up @@ -1877,7 +1881,6 @@ void JsonAPI::stopDataConnections()
// led stream colors
disconnect(_hyperhdr, &HyperHdrInstance::rawLedColors, this, 0);
_ledStreamTimer->stop();
disconnect(_ledStreamConnection);
}

void JsonAPI::handleTunnel(const QJsonObject& message, const QString& command, int tan)
Expand Down

0 comments on commit da294c0

Please sign in to comment.