Skip to content

Commit

Permalink
plugins/os2l: fix receiving multiple messages at once (fix #1633)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcallegari committed Oct 30, 2024
1 parent 1f7f95a commit ccd3322
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
1 change: 1 addition & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ qlcplus (4.13.2) stable; urgency=low
* engine: add stopOnExit, waitFunctionStart and waitFunctionStop commands to Script - see documentation (thanks to ldebs)
* UI/Fixture Manager: limit the number of RGB panel columns for RGBW to avoid crash
* UI/Show Manager: show step notes on the timeline (thanks to anarchid)
* Plugins/OS2L: fix receiving multiple messages at once
* Web Access: fix grand master stopping running functions
* Web Access: fix simple desk not resetting the current universe
* RGB scripts: added 'Sine Wave' script
Expand Down
76 changes: 46 additions & 30 deletions plugins/os2l/os2lplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,39 +207,55 @@ void OS2LPlugin::slotProcessTCPPackets()
if (socket == NULL)
return;

QHostAddress senderAddress = socket->peerAddress();
QByteArray message = socket->readAll();
QJsonDocument json = QJsonDocument::fromJson(message);
QHostAddress senderAddress = QHostAddress(socket->peerAddress().toIPv4Address());

qDebug() << "[TCP] Received" << message.length() << "bytes from" << senderAddress.toString();
QJsonObject jsonObj = json.object();
QJsonValue jEvent = jsonObj.value("evt");
if (jEvent.isUndefined())
return;
while (1)
{
m_packetLeftOver.append(socket->readAll());

QString event = jEvent.toString();
int endIndex = m_packetLeftOver.indexOf("}");
if (endIndex == -1)
{
if (socket->bytesAvailable())
continue;
else
break;
}

if (event == "btn")
{
QJsonValue jName = jsonObj.value("name");
QJsonValue jState = jsonObj.value("state");
qDebug() << "Got button event with name" << jName.toString() << "and state" << jState.toString();
uchar value = jState.toString() == "off" ? 0 : 255;
emit valueChanged(m_inputUniverse, 0, getHash(jName.toString()), value, jName.toString());
}
else if (event == "cmd")
{
QJsonValue jId = jsonObj.value("id");
QJsonValue jParam = jsonObj.value("param");
qDebug() << "Got CMD message" << jId.toInt() << "with param" << jParam.toDouble();
quint32 channel = quint32(jId.toInt());
QString cmd = QString("cmd%1").arg(channel);
emit valueChanged(m_inputUniverse, 0, quint32(jId.toInt()), uchar(jParam.toDouble()), cmd);
}
else if (event == "beat")
{
qDebug() << "Got beat message" << message;
emit valueChanged(m_inputUniverse, 0, 8341, 255, "beat");
QByteArray message = m_packetLeftOver.left(endIndex + 1);
m_packetLeftOver.remove(0, endIndex + 1);
QJsonDocument json = QJsonDocument::fromJson(message);

qDebug() << "[TCP] Received" << message.length() << "bytes from" << senderAddress.toString();
QJsonObject jsonObj = json.object();
QJsonValue jEvent = jsonObj.value("evt");
if (jEvent.isUndefined())
return;

QString event = jEvent.toString();

if (event == "btn")
{
QJsonValue jName = jsonObj.value("name");
QJsonValue jState = jsonObj.value("state");
qDebug() << "Got button event with name" << jName.toString() << "and state" << jState.toString();
uchar value = jState.toString() == "off" ? 0 : 255;
emit valueChanged(m_inputUniverse, 0, getHash(jName.toString()), value, jName.toString());
}
else if (event == "cmd")
{
QJsonValue jId = jsonObj.value("id");
QJsonValue jParam = jsonObj.value("param");
qDebug() << "Got CMD message" << jId.toInt() << "with param" << jParam.toDouble();
quint32 channel = quint32(jId.toInt());
QString cmd = QString("cmd%1").arg(channel);
emit valueChanged(m_inputUniverse, 0, quint32(jId.toInt()), uchar(jParam.toDouble()), cmd);
}
else if (event == "beat")
{
qDebug() << "Got beat message" << message;
emit valueChanged(m_inputUniverse, 0, 8341, 255, "beat");
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions plugins/os2l/os2lplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ protected slots:
*/
QHash<QString, quint16> m_hashMap;

QByteArray m_packetLeftOver;

/*********************************************************************
* Configuration
*********************************************************************/
Expand Down

0 comments on commit ccd3322

Please sign in to comment.