added command queue
This commit is contained in:
parent
f2a401aedf
commit
015349d2e5
@ -148,6 +148,9 @@ void IntegrationPluginSma::executeAction(ThingActionInfo *info)
|
||||
return;
|
||||
if (action.actionTypeId() == sunnyWebBoxSearchDevicesActionTypeId) {
|
||||
QString requestId = sunnyWebBox->getDevices();
|
||||
if (requestId.isEmpty()) {
|
||||
return info->finish(Thing::ThingErrorHardwareNotAvailable);
|
||||
}
|
||||
m_asyncActions.insert(requestId, info);
|
||||
connect(info, &ThingActionInfo::aborted, info, [requestId, this] {m_asyncActions.remove(requestId);});
|
||||
} else {
|
||||
|
||||
@ -60,10 +60,11 @@ SunnyWebBoxCommunication::SunnyWebBoxCommunication(QObject *parent) : QObject(pa
|
||||
|
||||
QString SunnyWebBoxCommunication::sendMessage(const QHostAddress &address, const QString &procedure)
|
||||
{
|
||||
QString requestId = QUuid::createUuid().toString().remove('{').left(14);
|
||||
QString requestId = QUuid::createUuid().toString().remove('{').remove('-').left(14);
|
||||
|
||||
QJsonDocument doc;
|
||||
QJsonObject obj;
|
||||
obj.insert("params", QJsonObject());
|
||||
obj["format"] = "JSON";
|
||||
obj["id"] = requestId;
|
||||
obj["proc"] = procedure;
|
||||
@ -71,20 +72,27 @@ QString SunnyWebBoxCommunication::sendMessage(const QHostAddress &address, const
|
||||
doc.setObject(obj);
|
||||
QByteArray data = doc.toJson(QJsonDocument::JsonFormat::Compact);
|
||||
qCDebug(dcSma()) << "Send message" << data << address << m_port;
|
||||
m_udpSocket->writeDatagram(data, address, m_port);
|
||||
if(m_messageQueue.value(address).isEmpty()) {
|
||||
m_udpSocket->writeDatagram(data, address, m_port);
|
||||
} else {
|
||||
if (m_messageQueue[address].length() < 40) {
|
||||
m_messageQueue[address].append(data);
|
||||
} else {
|
||||
qCDebug(dcSma()) << "Message queue overflow";
|
||||
return "";
|
||||
}
|
||||
}
|
||||
return requestId;
|
||||
}
|
||||
|
||||
QString SunnyWebBoxCommunication::sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject ¶ms)
|
||||
{
|
||||
QString requestId = QUuid::createUuid().toString().remove('{').left(14);
|
||||
QString requestId = QUuid::createUuid().toString().remove('{').remove('-').left(14);
|
||||
|
||||
QJsonDocument doc;
|
||||
QJsonObject obj;
|
||||
|
||||
if (!params.isEmpty()) {
|
||||
obj.insert("params", params);
|
||||
}
|
||||
obj.insert("params", params);
|
||||
obj["format"] = "JSON";
|
||||
obj["id"] = requestId;
|
||||
obj["proc"] = procedure;
|
||||
@ -92,12 +100,27 @@ QString SunnyWebBoxCommunication::sendMessage(const QHostAddress &address, const
|
||||
doc.setObject(obj);
|
||||
QByteArray data = doc.toJson(QJsonDocument::JsonFormat::Compact);
|
||||
qCDebug(dcSma()) << "Send message" << data << address << m_port;
|
||||
m_udpSocket->writeDatagram(data, address, m_port);
|
||||
if(m_messageQueue.value(address).isEmpty()) {
|
||||
m_udpSocket->writeDatagram(data, address, m_port);
|
||||
} else {
|
||||
if (m_messageQueue[address].length() < 40) {
|
||||
m_messageQueue[address].append(data);
|
||||
} else {
|
||||
qCDebug(dcSma()) << "Message queue overflow";
|
||||
return "";
|
||||
}
|
||||
}
|
||||
return requestId;
|
||||
}
|
||||
|
||||
void SunnyWebBoxCommunication::datagramReceived(const QHostAddress &address, const QByteArray &data)
|
||||
{
|
||||
if(!m_messageQueue.value(address).isEmpty()) {
|
||||
QByteArray data = m_messageQueue[address].takeFirst();
|
||||
qCDebug(dcSma()) << "Send message from queue" << data << address << m_port;
|
||||
m_udpSocket->writeDatagram(data, address, m_port);
|
||||
}
|
||||
|
||||
QList<QByteArray> arrayList = data.split('\x00');
|
||||
QByteArray cleanData;
|
||||
Q_FOREACH(QByteArray i, arrayList) {
|
||||
|
||||
@ -46,6 +46,7 @@ public:
|
||||
private:
|
||||
int m_port = 34268;
|
||||
QUdpSocket *m_udpSocket;
|
||||
QHash<QHostAddress, QList<QByteArray>> m_messageQueue;
|
||||
|
||||
void datagramReceived(const QHostAddress &address, const QByteArray &data);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user