Implement timing for messages

This commit is contained in:
Simon Stürz 2021-09-28 16:48:06 +02:00
parent 408edc3739
commit a21cf98aa4
3 changed files with 165 additions and 130 deletions

View File

@ -428,7 +428,7 @@ void IntegrationPluginKeba::onReportTwoReceived(const KeContact::ReportTwo &repo
thing->setStateValue(wallboxOutputX2StateTypeId, reportTwo.output); thing->setStateValue(wallboxOutputX2StateTypeId, reportTwo.output);
thing->setStateValue(wallboxInputStateTypeId, reportTwo.input); thing->setStateValue(wallboxInputStateTypeId, reportTwo.input);
thing->setStateValue(wallboxUptimeStateTypeId, reportTwo.seconds/60); thing->setStateValue(wallboxUptimeStateTypeId, reportTwo.seconds / 60);
} else { } else {
qCWarning(dcKebaKeContact()) << "Received report but the serial number didn't match"; qCWarning(dcKebaKeContact()) << "Received report but the serial number didn't match";
} }

View File

@ -41,13 +41,25 @@ KeContact::KeContact(const QHostAddress &address, KeContactDataLayer *dataLayer,
qCDebug(dcKebaKeContact()) << "Creating KeContact connection for address" << m_address; qCDebug(dcKebaKeContact()) << "Creating KeContact connection for address" << m_address;
m_requestTimeoutTimer = new QTimer(this); m_requestTimeoutTimer = new QTimer(this);
m_requestTimeoutTimer->setSingleShot(true); m_requestTimeoutTimer->setSingleShot(true);
connect(m_requestTimeoutTimer, &QTimer::timeout, this, [this] { connect(m_requestTimeoutTimer, &QTimer::timeout, this, [this]() {
// This timer will be started when a request is sent and stopped or resetted when a response has been received // This timer will be started when a request is sent and stopped or resetted when a response has been received
setReachable(false); setReachable(false);
//Try to send the next command if (m_currentRequest.isValid()) {
handleNextCommandInQueue(); // Schedule pause timer to send next request
m_deviceBlocked = false; qCWarning(dcKebaKeContact()) << "Command timeouted" << m_currentRequest.command();
emit commandExecuted(m_currentRequest.requestId(), false);
}
// Timeout...send the next request right the way since at least 5 seconds passed since tha last command
m_currentRequest = KeContactRequest();
sendNextCommand();
});
m_pauseTimer = new QTimer(this);
m_pauseTimer->setSingleShot(true);
connect(m_pauseTimer, &QTimer::timeout, this, [this](){
sendNextCommand();
}); });
connect(m_dataLayer, &KeContactDataLayer::datagramReceived, this, &KeContact::onReceivedDatagram); connect(m_dataLayer, &KeContactDataLayer::datagramReceived, this, &KeContact::onReceivedDatagram);
@ -71,12 +83,12 @@ QUuid KeContact::start(const QByteArray &rfidToken, const QByteArray &rfidClassi
return QUuid(); return QUuid();
} }
QUuid requestId = QUuid::createUuid();
m_pendingRequests.append(requestId);
QByteArray datagram = "start " + rfidToken + " " + rfidClassifier; QByteArray datagram = "start " + rfidToken + " " + rfidClassifier;
qCDebug(dcKebaKeContact()) << "Datagram : " << datagram; KeContactRequest request(QUuid::createUuid(), datagram);
sendCommand(datagram, requestId);; qCDebug(dcKebaKeContact()) << "Start: Datagram:" << datagram;
return requestId; m_requestQueue.enqueue(request);
sendNextCommand();
return request.requestId();
} }
QUuid KeContact::stop(const QByteArray &rfidToken) QUuid KeContact::stop(const QByteArray &rfidToken)
@ -87,12 +99,12 @@ QUuid KeContact::stop(const QByteArray &rfidToken)
return QUuid(); return QUuid();
} }
QUuid requestId = QUuid::createUuid();
m_pendingRequests.append(requestId);
QByteArray datagram = "stop " + rfidToken; QByteArray datagram = "stop " + rfidToken;
qCDebug(dcKebaKeContact()) << "Datagram : " << datagram; KeContactRequest request(QUuid::createUuid(), datagram);
sendCommand(datagram, requestId); qCDebug(dcKebaKeContact()) << "Stop: Datagram:" << datagram;
return requestId; m_requestQueue.enqueue(request);
sendNextCommand();
return request.requestId();
} }
void KeContact::setAddress(const QHostAddress &address) void KeContact::setAddress(const QHostAddress &address)
@ -109,18 +121,6 @@ bool KeContact::reachable() const
return m_reachable; return m_reachable;
} }
void KeContact::sendCommand(const QByteArray &command, const QUuid &requestId)
{
QTimer::singleShot(5000, this, [requestId, this] {
if (m_pendingRequests.contains(requestId)) {
m_pendingRequests.removeOne(requestId);
emit commandExecuted(requestId, false);
}
});
sendCommand(command);
}
void KeContact::sendCommand(const QByteArray &command) void KeContact::sendCommand(const QByteArray &command)
{ {
if (!m_dataLayer) { if (!m_dataLayer) {
@ -129,32 +129,23 @@ void KeContact::sendCommand(const QByteArray &command)
return; return;
} }
if (m_deviceBlocked) { qCDebug(dcKebaKeContact()) << "--> Writing datagram to" << m_address.toString() << command;
// Add command to queue m_dataLayer->write(m_address, command);
m_commandList.append(command); m_requestTimeoutTimer->start(5000);
} else {
qCDebug(dcKebaKeContact()) << "Writing datagram to" << m_address.toString() << command;
m_dataLayer->write(m_address, command);
m_requestTimeoutTimer->start(5000);
m_deviceBlocked = true;
}
} }
void KeContact::handleNextCommandInQueue() void KeContact::sendNextCommand()
{ {
if (!m_dataLayer) { // No message left, we are done
qCWarning(dcKebaKeContact()) << "Data layer not initialized"; if (m_requestQueue.isEmpty())
setReachable(false);
return; return;
}
qCDebug(dcKebaKeContact()) << "Handle Command Queue - Pending commands" << m_commandList.length() << "Pending requestIds" << m_pendingRequests.length(); // Still a request pending
if (!m_commandList.isEmpty()) { if (m_currentRequest.isValid())
QByteArray command = m_commandList.takeFirst(); return;
qCDebug(dcKebaKeContact()) << "Writing datagram to" << m_address.toString() << command;
m_dataLayer->write( m_address, command); m_currentRequest = m_requestQueue.dequeue();
m_requestTimeoutTimer->start(5000); sendCommand(m_currentRequest.command());
}
} }
void KeContact::setReachable(bool reachable) void KeContact::setReachable(bool reachable)
@ -180,8 +171,6 @@ QUuid KeContact::enableOutput(bool state)
return QUuid(); return QUuid();
} }
QUuid requestId = QUuid::createUuid();
m_pendingRequests.append(requestId);
// Print information that we are executing now the update action; // Print information that we are executing now the update action;
QByteArray datagram; QByteArray datagram;
if (state){ if (state){
@ -190,9 +179,12 @@ QUuid KeContact::enableOutput(bool state)
datagram.append("ena 0"); datagram.append("ena 0");
} }
qCDebug(dcKebaKeContact()) << "Enable output, command:" << datagram; KeContactRequest request(QUuid::createUuid(), datagram);
sendCommand(datagram, requestId); request.setDelayUntilNextCommand(2000);
return requestId; qCDebug(dcKebaKeContact()) << "Enable output: Datagram:" << datagram;
m_requestQueue.enqueue(request);
sendNextCommand();
return request.requestId();
} }
QUuid KeContact::setMaxAmpere(int milliAmpere) QUuid KeContact::setMaxAmpere(int milliAmpere)
@ -208,15 +200,15 @@ QUuid KeContact::setMaxAmpere(int milliAmpere)
return QUuid(); return QUuid();
} }
QUuid requestId = QUuid::createUuid();
m_pendingRequests.append(requestId);
// Print information that we are executing now the update action // Print information that we are executing now the update action
qCDebug(dcKebaKeContact()) << "Update max current to : " << milliAmpere; qCDebug(dcKebaKeContact()) << "Update max current to : " << milliAmpere;
QString commandLine = QString("currtime %1 %2").arg(milliAmpere, 1); QString commandLine = QString("currtime %1 0").arg(milliAmpere);
QByteArray data = commandLine.toUtf8(); QByteArray datagram = commandLine.toUtf8();
qCDebug(dcKebaKeContact()) << "Set max. ampere, command: " << data; KeContactRequest request(QUuid::createUuid(), datagram);
sendCommand(data, requestId); qCDebug(dcKebaKeContact()) << "Set max charging amps: Datagram:" << datagram;
return requestId; m_requestQueue.enqueue(request);
sendNextCommand();
return request.requestId();
} }
QUuid KeContact::displayMessage(const QByteArray &message) QUuid KeContact::displayMessage(const QByteArray &message)
@ -232,19 +224,20 @@ QUuid KeContact::displayMessage(const QByteArray &message)
$ == blank $ == blank
, == comma , == comma
*/ */
QUuid requestId = QUuid::createUuid();
m_pendingRequests.append(requestId);
qCDebug(dcKebaKeContact()) << "Set display message: " << message; qCDebug(dcKebaKeContact()) << "Set display message: " << message;
QByteArray data; QByteArray datagram;
QByteArray modifiedMessage = message; QByteArray modifiedMessage = message;
modifiedMessage.replace(" ", "$"); modifiedMessage.replace(" ", "$");
if (modifiedMessage.size() > 23) { if (modifiedMessage.size() > 23) {
modifiedMessage.resize(23); modifiedMessage.resize(23);
} }
data.append("display 0 0 0 0 " + modifiedMessage); datagram.append("display 0 0 0 0 " + modifiedMessage);
qCDebug(dcKebaKeContact()) << "Display message, command: " << data; KeContactRequest request(QUuid::createUuid(), datagram);
sendCommand(data, requestId); qCDebug(dcKebaKeContact()) << "Display message: Datagram:" << datagram;
return requestId; m_requestQueue.enqueue(request);
sendNextCommand();
return request.requestId();
} }
QUuid KeContact::chargeWithEnergyLimit(double energy) QUuid KeContact::chargeWithEnergyLimit(double energy)
@ -255,14 +248,13 @@ QUuid KeContact::chargeWithEnergyLimit(double energy)
return QUuid(); return QUuid();
} }
QUuid requestId = QUuid::createUuid(); QByteArray datagram;
m_pendingRequests.append(requestId); datagram.append("setenergy " + QVariant(static_cast<int>(energy*10000)).toByteArray());
KeContactRequest request(QUuid::createUuid(), datagram);
QByteArray data; qCDebug(dcKebaKeContact()) << "Charge with energy limit: Datagram: " << datagram;
data.append("setenergy " + QVariant(static_cast<int>(energy*10000)).toByteArray()); m_requestQueue.enqueue(request);
qCDebug(dcKebaKeContact()) << "Charge with energy limit, command: " << data; sendNextCommand();
sendCommand(data, requestId); return request.requestId();
return requestId;
} }
QUuid KeContact::setFailsafe(int timeout, int current, bool save) QUuid KeContact::setFailsafe(int timeout, int current, bool save)
@ -273,25 +265,26 @@ QUuid KeContact::setFailsafe(int timeout, int current, bool save)
return QUuid(); return QUuid();
} }
QUuid requestId = QUuid::createUuid();
m_pendingRequests.append(requestId);
QByteArray data; QByteArray data;
data.append("failsave"); data.append("failsave");
data.append(" "+QVariant(timeout).toByteArray()); data.append(" "+QVariant(timeout).toByteArray());
data.append(" "+QVariant(current).toByteArray()); data.append(" "+QVariant(current).toByteArray());
data.append((save ? " 1":" 0")); data.append((save ? " 1":" 0"));
qCDebug(dcKebaKeContact()) << "Set failsafe mode, command: " << data; KeContactRequest request(QUuid::createUuid(), data);
sendCommand(data, requestId); qCDebug(dcKebaKeContact()) << "Set failsafe mode: Datagram: " << data;
return requestId; m_requestQueue.enqueue(request);
sendNextCommand();
return request.requestId();
} }
void KeContact::getDeviceInformation() void KeContact::getDeviceInformation()
{ {
QByteArray data; QByteArray data;
data.append("i"); data.append("i");
qCDebug(dcKebaKeContact()) << "Get device information, command: " << data; KeContactRequest request(QUuid::createUuid(), data);
sendCommand(data); qCDebug(dcKebaKeContact()) << "Get device information: Datagram: " << data;
m_requestQueue.enqueue(request);
sendNextCommand();
} }
void KeContact::getReport1() void KeContact::getReport1()
@ -322,21 +315,32 @@ QUuid KeContact::setOutputX2(bool state)
return QUuid(); return QUuid();
} }
QUuid requestId = QUuid::createUuid();
m_pendingRequests.append(requestId); QByteArray datagram;
QByteArray data; datagram.append("output " + QVariant((state ? 1 : 0)).toByteArray());
data.append("output "+QVariant((state ? 1 : 0)).toByteArray());
qCDebug(dcKebaKeContact()) << "Set Output X2, state:" << state << "Command:" << data; KeContactRequest request(QUuid::createUuid(), datagram);
sendCommand(data, requestId); qCDebug(dcKebaKeContact()) << "Set Output X2, state:" << state << "Datagram:" << datagram;
return requestId; m_requestQueue.enqueue(request);
sendNextCommand();
return request.requestId();
} }
void KeContact::getReport(int reportNumber) void KeContact::getReport(int reportNumber)
{ {
QByteArray data; if (!m_dataLayer) {
data.append("report "+QVariant(reportNumber).toByteArray()); qCWarning(dcKebaKeContact()) << "UDP socket not initialized";
qCDebug(dcKebaKeContact()) << "Get report" << reportNumber << "Command:" << data; setReachable(false);
sendCommand(data); return;
}
QByteArray datagram;
datagram.append("report " + QVariant(reportNumber).toByteArray());
KeContactRequest request(QUuid::createUuid(), datagram);
qCDebug(dcKebaKeContact()) << "Get report" << reportNumber << "Datagram:" << datagram;
m_requestQueue.enqueue(request);
sendNextCommand();
} }
QUuid KeContact::unlockCharger() QUuid KeContact::unlockCharger()
@ -347,13 +351,14 @@ QUuid KeContact::unlockCharger()
return QUuid(); return QUuid();
} }
QUuid requestId = QUuid::createUuid(); QByteArray datagram;
m_pendingRequests.append(requestId); datagram.append("unlock");
QByteArray data;
data.append("unlock"); KeContactRequest request(QUuid::createUuid(), datagram);
qCDebug(dcKebaKeContact()) << "Unlock charger, command: " << data; qCDebug(dcKebaKeContact()) << "Unlock charger: Datagram:" << datagram;
sendCommand(data); m_requestQueue.enqueue(request);
return requestId; sendNextCommand();
return request.requestId();
} }
void KeContact::onReceivedDatagram(const QHostAddress &address, const QByteArray &datagram) void KeContact::onReceivedDatagram(const QHostAddress &address, const QByteArray &datagram)
@ -368,29 +373,35 @@ void KeContact::onReceivedDatagram(const QHostAddress &address, const QByteArray
setReachable(true); setReachable(true);
//Command response has been received, now send the next command //Command response has been received, now send the next command
m_deviceBlocked = false;
m_requestTimeoutTimer->stop(); m_requestTimeoutTimer->stop();
handleNextCommandInQueue();
if (!m_pendingRequests.isEmpty()) { if (m_currentRequest.isValid()) {
QUuid requestId = m_pendingRequests.takeFirst();
if (datagram.contains("done")) { if (datagram.contains("done")) {
emit commandExecuted(requestId, true); qCDebug(dcKebaKeContact()) << "Command" << m_currentRequest.command() << "finished successfully";
emit commandExecuted(m_currentRequest.requestId(), true);
} else { } else {
emit commandExecuted(requestId, false); qCWarning(dcKebaKeContact()) << "Command" << m_currentRequest.command() << "finished with error" << datagram;
emit commandExecuted(m_currentRequest.requestId(), false);
} }
// Schedule pause timer to send next request
m_pauseTimer->start(m_currentRequest.delayUntilNextCommand());
m_currentRequest = KeContactRequest();
} else { } else {
//Probably the response has taken too long and the requestId has been already removed //Probably the response has taken too long and the requestId has been already removed
qCWarning(dcKebaKeContact()) << "Received command OK response without pending request." << datagram;
} }
} else if (datagram.left(8).contains("Firmware")){ } else if (datagram.left(8).contains("Firmware")){
// We received valid data from the address over the data link, so the wallbox must be reachable // We received valid data from the address over the data link, so the wallbox must be reachable
setReachable(true); setReachable(true);
//Command response has been received, now send the next command // Command response has been received, now send the next command
m_deviceBlocked = false;
m_requestTimeoutTimer->stop(); m_requestTimeoutTimer->stop();
handleNextCommandInQueue(); if (m_currentRequest.isValid()) {
// Schedule pause timer to send next request
m_pauseTimer->start(m_currentRequest.delayUntilNextCommand());
m_currentRequest = KeContactRequest();
}
qCDebug(dcKebaKeContact()) << "Firmware information received"; qCDebug(dcKebaKeContact()) << "Firmware information received";
QByteArrayList firmware = datagram.split(':'); QByteArrayList firmware = datagram.split(':');
@ -398,11 +409,13 @@ void KeContact::onReceivedDatagram(const QHostAddress &address, const QByteArray
emit deviceInformationReceived(firmware[1]); emit deviceInformationReceived(firmware[1]);
} }
} else { } else {
//Command response has been received, now send the next command //Command response has been received, now send the next command
m_deviceBlocked = false;
m_requestTimeoutTimer->stop(); m_requestTimeoutTimer->stop();
handleNextCommandInQueue(); if (m_currentRequest.isValid()) {
// Schedule pause timer to send next request
m_pauseTimer->start(m_currentRequest.delayUntilNextCommand());
m_currentRequest = KeContactRequest();
}
// Convert the rawdata to a json document // Convert the rawdata to a json document
QJsonParseError error; QJsonParseError error;
@ -452,15 +465,15 @@ void KeContact::onReceivedDatagram(const QHostAddress &address, const QByteArray
reportTwo.error1 = data.value("Error1").toInt(); reportTwo.error1 = data.value("Error1").toInt();
reportTwo.error2 = data.value("Error2").toInt(); reportTwo.error2 = data.value("Error2").toInt();
reportTwo.plugState = PlugState(data.value("Plug").toInt()); reportTwo.plugState = PlugState(data.value("Plug").toInt());
reportTwo.enableUser = data.value("Enable user").toBool(); reportTwo.enableUser = data.value("Enable user").toBool();
reportTwo.enableSys = data.value("Enable sys").toBool(); reportTwo.enableSys = data.value("Enable sys").toBool();
reportTwo.maxCurrent = data.value("Max curr").toInt()/1000.00; reportTwo.maxCurrent = data.value("Max curr").toInt() / 1000.00;
reportTwo.maxCurrentPercentage = data.value("Max curr %").toInt()/10.00; reportTwo.maxCurrentPercentage = data.value("Max curr %").toInt() / 10.00;
reportTwo.currentHardwareLimitation = data.value("Curr HW").toInt()/1000.00; reportTwo.currentHardwareLimitation = data.value("Curr HW").toInt() / 1000.00;
reportTwo.currentUser = data.value("Curr user").toInt()/1000.00; reportTwo.currentUser = data.value("Curr user").toInt() / 1000.00;
reportTwo.currentFailsafe = data.value("Curr FS").toInt()/1000.00; reportTwo.currentFailsafe = data.value("Curr FS").toInt() / 1000.00;
reportTwo.timeoutFailsafe = data.value("Tmo FS").toInt(); reportTwo.timeoutFailsafe = data.value("Tmo FS").toInt();
reportTwo.setEnergy = data.value("Setenergy").toInt()/10000.00; reportTwo.setEnergy = data.value("Setenergy").toInt() / 10000.00;
reportTwo.output = data.value("Output").toInt(); reportTwo.output = data.value("Output").toInt();
reportTwo.input= data.value("Input").toInt(); reportTwo.input= data.value("Input").toInt();
reportTwo.serialNumber = data.value("Serial").toString(); reportTwo.serialNumber = data.value("Serial").toString();

View File

@ -37,9 +37,32 @@
#include <QByteArray> #include <QByteArray>
#include <QUdpSocket> #include <QUdpSocket>
#include <QUuid> #include <QUuid>
#include <QQueue>
#include "kecontactdatalayer.h" #include "kecontactdatalayer.h"
class KeContactRequest
{
public:
KeContactRequest() = default;
KeContactRequest(const QUuid &requestId, const QByteArray &command) : m_requestId(requestId), m_command(command) { }
QUuid requestId() const { return m_requestId; }
QByteArray command() const { return m_command; }
uint delayUntilNextCommand() const { return m_delayUntilNextCommand; }
void setDelayUntilNextCommand(uint delayUntilNextCommand) { m_delayUntilNextCommand = delayUntilNextCommand; }
bool isValid() { return !m_requestId.isNull() && !m_command.isEmpty(); }
private:
QUuid m_requestId;
QByteArray m_command;
uint m_delayUntilNextCommand = 200;
};
class KeContact : public QObject class KeContact : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -132,7 +155,6 @@ public:
int seconds; // current time when the report was generated int seconds; // current time when the report was generated
}; };
explicit KeContact(const QHostAddress &address, KeContactDataLayer *dataLayer, QObject *parent = nullptr); explicit KeContact(const QHostAddress &address, KeContactDataLayer *dataLayer, QObject *parent = nullptr);
~KeContact(); ~KeContact();
@ -145,7 +167,7 @@ public:
QUuid stop(const QByteArray &rfidToken); // Command “stop” QUuid stop(const QByteArray &rfidToken); // Command “stop”
QUuid enableOutput(bool state); // Command “ena” QUuid enableOutput(bool state); // Command “ena”
QUuid setMaxAmpere(int milliAmpere); // Command “curr” QUuid setMaxAmpere(int milliAmpere); // Command "currtime"
QUuid unlockCharger(); // Command “unlock" QUuid unlockCharger(); // Command “unlock"
QUuid displayMessage(const QByteArray &message); // Command “display” QUuid displayMessage(const QByteArray &message); // Command “display”
QUuid chargeWithEnergyLimit(double energy); // Command “setenergy” QUuid chargeWithEnergyLimit(double energy); // Command “setenergy”
@ -165,18 +187,18 @@ private:
bool m_reachable = false; bool m_reachable = false;
QHostAddress m_address; QHostAddress m_address;
QByteArrayList m_commandList;
bool m_deviceBlocked = false;
QTimer *m_requestTimeoutTimer = nullptr; QTimer *m_requestTimeoutTimer = nullptr;
QTimer *m_pauseTimer = nullptr;
int m_serialNumber = 0; int m_serialNumber = 0;
QList<QUuid> m_pendingRequests;
KeContactRequest m_currentRequest;
QQueue<KeContactRequest> m_requestQueue;
void getReport(int reportNumber); void getReport(int reportNumber);
void sendCommand(const QByteArray &command, const QUuid &requestId);
void sendCommand(const QByteArray &command);
void handleNextCommandInQueue();
void sendCommand(const QByteArray &command);
void sendNextCommand();
void setReachable(bool reachable); void setReachable(bool reachable);
signals: signals: