diff --git a/keba/integrationpluginkeba.cpp b/keba/integrationpluginkeba.cpp index be661728..4f37edc8 100644 --- a/keba/integrationpluginkeba.cpp +++ b/keba/integrationpluginkeba.cpp @@ -428,7 +428,7 @@ void IntegrationPluginKeba::onReportTwoReceived(const KeContact::ReportTwo &repo thing->setStateValue(wallboxOutputX2StateTypeId, reportTwo.output); thing->setStateValue(wallboxInputStateTypeId, reportTwo.input); - thing->setStateValue(wallboxUptimeStateTypeId, reportTwo.seconds/60); + thing->setStateValue(wallboxUptimeStateTypeId, reportTwo.seconds / 60); } else { qCWarning(dcKebaKeContact()) << "Received report but the serial number didn't match"; } diff --git a/keba/kecontact.cpp b/keba/kecontact.cpp index 5cf75023..ccfa7c84 100644 --- a/keba/kecontact.cpp +++ b/keba/kecontact.cpp @@ -41,13 +41,25 @@ KeContact::KeContact(const QHostAddress &address, KeContactDataLayer *dataLayer, qCDebug(dcKebaKeContact()) << "Creating KeContact connection for address" << m_address; m_requestTimeoutTimer = new QTimer(this); 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 setReachable(false); - //Try to send the next command - handleNextCommandInQueue(); - m_deviceBlocked = false; + if (m_currentRequest.isValid()) { + // Schedule pause timer to send next request + 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); @@ -71,12 +83,12 @@ QUuid KeContact::start(const QByteArray &rfidToken, const QByteArray &rfidClassi return QUuid(); } - QUuid requestId = QUuid::createUuid(); - m_pendingRequests.append(requestId); QByteArray datagram = "start " + rfidToken + " " + rfidClassifier; - qCDebug(dcKebaKeContact()) << "Datagram : " << datagram; - sendCommand(datagram, requestId);; - return requestId; + KeContactRequest request(QUuid::createUuid(), datagram); + qCDebug(dcKebaKeContact()) << "Start: Datagram:" << datagram; + m_requestQueue.enqueue(request); + sendNextCommand(); + return request.requestId(); } QUuid KeContact::stop(const QByteArray &rfidToken) @@ -87,12 +99,12 @@ QUuid KeContact::stop(const QByteArray &rfidToken) return QUuid(); } - QUuid requestId = QUuid::createUuid(); - m_pendingRequests.append(requestId); QByteArray datagram = "stop " + rfidToken; - qCDebug(dcKebaKeContact()) << "Datagram : " << datagram; - sendCommand(datagram, requestId); - return requestId; + KeContactRequest request(QUuid::createUuid(), datagram); + qCDebug(dcKebaKeContact()) << "Stop: Datagram:" << datagram; + m_requestQueue.enqueue(request); + sendNextCommand(); + return request.requestId(); } void KeContact::setAddress(const QHostAddress &address) @@ -109,18 +121,6 @@ bool KeContact::reachable() const 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) { if (!m_dataLayer) { @@ -129,32 +129,23 @@ void KeContact::sendCommand(const QByteArray &command) return; } - if (m_deviceBlocked) { - // Add command to queue - m_commandList.append(command); - } else { - qCDebug(dcKebaKeContact()) << "Writing datagram to" << m_address.toString() << command; - m_dataLayer->write(m_address, command); - m_requestTimeoutTimer->start(5000); - m_deviceBlocked = true; - } + qCDebug(dcKebaKeContact()) << "--> Writing datagram to" << m_address.toString() << command; + m_dataLayer->write(m_address, command); + m_requestTimeoutTimer->start(5000); } -void KeContact::handleNextCommandInQueue() +void KeContact::sendNextCommand() { - if (!m_dataLayer) { - qCWarning(dcKebaKeContact()) << "Data layer not initialized"; - setReachable(false); + // No message left, we are done + if (m_requestQueue.isEmpty()) return; - } - qCDebug(dcKebaKeContact()) << "Handle Command Queue - Pending commands" << m_commandList.length() << "Pending requestIds" << m_pendingRequests.length(); - if (!m_commandList.isEmpty()) { - QByteArray command = m_commandList.takeFirst(); - qCDebug(dcKebaKeContact()) << "Writing datagram to" << m_address.toString() << command; - m_dataLayer->write( m_address, command); - m_requestTimeoutTimer->start(5000); - } + // Still a request pending + if (m_currentRequest.isValid()) + return; + + m_currentRequest = m_requestQueue.dequeue(); + sendCommand(m_currentRequest.command()); } void KeContact::setReachable(bool reachable) @@ -180,8 +171,6 @@ QUuid KeContact::enableOutput(bool state) return QUuid(); } - QUuid requestId = QUuid::createUuid(); - m_pendingRequests.append(requestId); // Print information that we are executing now the update action; QByteArray datagram; if (state){ @@ -190,9 +179,12 @@ QUuid KeContact::enableOutput(bool state) datagram.append("ena 0"); } - qCDebug(dcKebaKeContact()) << "Enable output, command:" << datagram; - sendCommand(datagram, requestId); - return requestId; + KeContactRequest request(QUuid::createUuid(), datagram); + request.setDelayUntilNextCommand(2000); + qCDebug(dcKebaKeContact()) << "Enable output: Datagram:" << datagram; + m_requestQueue.enqueue(request); + sendNextCommand(); + return request.requestId(); } QUuid KeContact::setMaxAmpere(int milliAmpere) @@ -208,15 +200,15 @@ QUuid KeContact::setMaxAmpere(int milliAmpere) return QUuid(); } - QUuid requestId = QUuid::createUuid(); - m_pendingRequests.append(requestId); // Print information that we are executing now the update action qCDebug(dcKebaKeContact()) << "Update max current to : " << milliAmpere; - QString commandLine = QString("currtime %1 %2").arg(milliAmpere, 1); - QByteArray data = commandLine.toUtf8(); - qCDebug(dcKebaKeContact()) << "Set max. ampere, command: " << data; - sendCommand(data, requestId); - return requestId; + QString commandLine = QString("currtime %1 0").arg(milliAmpere); + QByteArray datagram = commandLine.toUtf8(); + KeContactRequest request(QUuid::createUuid(), datagram); + qCDebug(dcKebaKeContact()) << "Set max charging amps: Datagram:" << datagram; + m_requestQueue.enqueue(request); + sendNextCommand(); + return request.requestId(); } QUuid KeContact::displayMessage(const QByteArray &message) @@ -232,19 +224,20 @@ QUuid KeContact::displayMessage(const QByteArray &message) $ == blank , == comma */ - QUuid requestId = QUuid::createUuid(); - m_pendingRequests.append(requestId); + qCDebug(dcKebaKeContact()) << "Set display message: " << message; - QByteArray data; + QByteArray datagram; QByteArray modifiedMessage = message; modifiedMessage.replace(" ", "$"); if (modifiedMessage.size() > 23) { modifiedMessage.resize(23); } - data.append("display 0 0 0 0 " + modifiedMessage); - qCDebug(dcKebaKeContact()) << "Display message, command: " << data; - sendCommand(data, requestId); - return requestId; + datagram.append("display 0 0 0 0 " + modifiedMessage); + KeContactRequest request(QUuid::createUuid(), datagram); + qCDebug(dcKebaKeContact()) << "Display message: Datagram:" << datagram; + m_requestQueue.enqueue(request); + sendNextCommand(); + return request.requestId(); } QUuid KeContact::chargeWithEnergyLimit(double energy) @@ -255,14 +248,13 @@ QUuid KeContact::chargeWithEnergyLimit(double energy) return QUuid(); } - QUuid requestId = QUuid::createUuid(); - m_pendingRequests.append(requestId); - - QByteArray data; - data.append("setenergy " + QVariant(static_cast(energy*10000)).toByteArray()); - qCDebug(dcKebaKeContact()) << "Charge with energy limit, command: " << data; - sendCommand(data, requestId); - return requestId; + QByteArray datagram; + datagram.append("setenergy " + QVariant(static_cast(energy*10000)).toByteArray()); + KeContactRequest request(QUuid::createUuid(), datagram); + qCDebug(dcKebaKeContact()) << "Charge with energy limit: Datagram: " << datagram; + m_requestQueue.enqueue(request); + sendNextCommand(); + return request.requestId(); } QUuid KeContact::setFailsafe(int timeout, int current, bool save) @@ -273,25 +265,26 @@ QUuid KeContact::setFailsafe(int timeout, int current, bool save) return QUuid(); } - QUuid requestId = QUuid::createUuid(); - m_pendingRequests.append(requestId); - QByteArray data; data.append("failsave"); data.append(" "+QVariant(timeout).toByteArray()); data.append(" "+QVariant(current).toByteArray()); data.append((save ? " 1":" 0")); - qCDebug(dcKebaKeContact()) << "Set failsafe mode, command: " << data; - sendCommand(data, requestId); - return requestId; + KeContactRequest request(QUuid::createUuid(), data); + qCDebug(dcKebaKeContact()) << "Set failsafe mode: Datagram: " << data; + m_requestQueue.enqueue(request); + sendNextCommand(); + return request.requestId(); } void KeContact::getDeviceInformation() { QByteArray data; data.append("i"); - qCDebug(dcKebaKeContact()) << "Get device information, command: " << data; - sendCommand(data); + KeContactRequest request(QUuid::createUuid(), data); + qCDebug(dcKebaKeContact()) << "Get device information: Datagram: " << data; + m_requestQueue.enqueue(request); + sendNextCommand(); } void KeContact::getReport1() @@ -322,21 +315,32 @@ QUuid KeContact::setOutputX2(bool state) return QUuid(); } - QUuid requestId = QUuid::createUuid(); - m_pendingRequests.append(requestId); - QByteArray data; - data.append("output "+QVariant((state ? 1 : 0)).toByteArray()); - qCDebug(dcKebaKeContact()) << "Set Output X2, state:" << state << "Command:" << data; - sendCommand(data, requestId); - return requestId; + + QByteArray datagram; + datagram.append("output " + QVariant((state ? 1 : 0)).toByteArray()); + + KeContactRequest request(QUuid::createUuid(), datagram); + qCDebug(dcKebaKeContact()) << "Set Output X2, state:" << state << "Datagram:" << datagram; + m_requestQueue.enqueue(request); + sendNextCommand(); + return request.requestId(); } void KeContact::getReport(int reportNumber) { - QByteArray data; - data.append("report "+QVariant(reportNumber).toByteArray()); - qCDebug(dcKebaKeContact()) << "Get report" << reportNumber << "Command:" << data; - sendCommand(data); + if (!m_dataLayer) { + qCWarning(dcKebaKeContact()) << "UDP socket not initialized"; + setReachable(false); + 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() @@ -347,13 +351,14 @@ QUuid KeContact::unlockCharger() return QUuid(); } - QUuid requestId = QUuid::createUuid(); - m_pendingRequests.append(requestId); - QByteArray data; - data.append("unlock"); - qCDebug(dcKebaKeContact()) << "Unlock charger, command: " << data; - sendCommand(data); - return requestId; + QByteArray datagram; + datagram.append("unlock"); + + KeContactRequest request(QUuid::createUuid(), datagram); + qCDebug(dcKebaKeContact()) << "Unlock charger: Datagram:" << datagram; + m_requestQueue.enqueue(request); + sendNextCommand(); + return request.requestId(); } void KeContact::onReceivedDatagram(const QHostAddress &address, const QByteArray &datagram) @@ -368,29 +373,35 @@ void KeContact::onReceivedDatagram(const QHostAddress &address, const QByteArray setReachable(true); //Command response has been received, now send the next command - m_deviceBlocked = false; m_requestTimeoutTimer->stop(); - handleNextCommandInQueue(); - if (!m_pendingRequests.isEmpty()) { - QUuid requestId = m_pendingRequests.takeFirst(); + if (m_currentRequest.isValid()) { if (datagram.contains("done")) { - emit commandExecuted(requestId, true); + qCDebug(dcKebaKeContact()) << "Command" << m_currentRequest.command() << "finished successfully"; + emit commandExecuted(m_currentRequest.requestId(), true); } 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 { //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")){ // We received valid data from the address over the data link, so the wallbox must be reachable setReachable(true); - //Command response has been received, now send the next command - m_deviceBlocked = false; + // Command response has been received, now send the next command 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"; QByteArrayList firmware = datagram.split(':'); @@ -398,11 +409,13 @@ void KeContact::onReceivedDatagram(const QHostAddress &address, const QByteArray emit deviceInformationReceived(firmware[1]); } } else { - //Command response has been received, now send the next command - m_deviceBlocked = false; 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 QJsonParseError error; @@ -452,15 +465,15 @@ void KeContact::onReceivedDatagram(const QHostAddress &address, const QByteArray reportTwo.error1 = data.value("Error1").toInt(); reportTwo.error2 = data.value("Error2").toInt(); reportTwo.plugState = PlugState(data.value("Plug").toInt()); - reportTwo.enableUser = data.value("Enable user").toBool(); - reportTwo.enableSys = data.value("Enable sys").toBool(); - reportTwo.maxCurrent = data.value("Max curr").toInt()/1000.00; - reportTwo.maxCurrentPercentage = data.value("Max curr %").toInt()/10.00; - reportTwo.currentHardwareLimitation = data.value("Curr HW").toInt()/1000.00; - reportTwo.currentUser = data.value("Curr user").toInt()/1000.00; - reportTwo.currentFailsafe = data.value("Curr FS").toInt()/1000.00; + reportTwo.enableUser = data.value("Enable user").toBool(); + reportTwo.enableSys = data.value("Enable sys").toBool(); + reportTwo.maxCurrent = data.value("Max curr").toInt() / 1000.00; + reportTwo.maxCurrentPercentage = data.value("Max curr %").toInt() / 10.00; + reportTwo.currentHardwareLimitation = data.value("Curr HW").toInt() / 1000.00; + reportTwo.currentUser = data.value("Curr user").toInt() / 1000.00; + reportTwo.currentFailsafe = data.value("Curr FS").toInt() / 1000.00; 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.input= data.value("Input").toInt(); reportTwo.serialNumber = data.value("Serial").toString(); diff --git a/keba/kecontact.h b/keba/kecontact.h index 3622130a..8468e55a 100644 --- a/keba/kecontact.h +++ b/keba/kecontact.h @@ -37,9 +37,32 @@ #include #include #include +#include #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 { Q_OBJECT @@ -132,7 +155,6 @@ public: int seconds; // current time when the report was generated }; - explicit KeContact(const QHostAddress &address, KeContactDataLayer *dataLayer, QObject *parent = nullptr); ~KeContact(); @@ -145,7 +167,7 @@ public: QUuid stop(const QByteArray &rfidToken); // Command “stop” QUuid enableOutput(bool state); // Command “ena” - QUuid setMaxAmpere(int milliAmpere); // Command “curr” + QUuid setMaxAmpere(int milliAmpere); // Command "currtime" QUuid unlockCharger(); // Command “unlock" QUuid displayMessage(const QByteArray &message); // Command “display” QUuid chargeWithEnergyLimit(double energy); // Command “setenergy” @@ -165,18 +187,18 @@ private: bool m_reachable = false; QHostAddress m_address; - QByteArrayList m_commandList; - bool m_deviceBlocked = false; QTimer *m_requestTimeoutTimer = nullptr; + QTimer *m_pauseTimer = nullptr; int m_serialNumber = 0; - QList m_pendingRequests; + + KeContactRequest m_currentRequest; + QQueue m_requestQueue; 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); signals: