diff --git a/CMakeLists.txt b/CMakeLists.txt index b51f118..6c81d19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.16) -project(AntiClipSettings VERSION 1.5 LANGUAGES C CXX) +project(AntiClipSettings VERSION 1.6 LANGUAGES C CXX) set(APPLICATION_NAME "视觉防夹设备上位机工具") set(CMAKE_CXX_STANDARD 17) diff --git a/DeviceConnection.cpp b/DeviceConnection.cpp index 4030996..b1f4d32 100644 --- a/DeviceConnection.cpp +++ b/DeviceConnection.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -24,9 +25,9 @@ void DeviceConnection::close() { m_otaTimer->deleteLater(); m_otaTimer = nullptr; } - if (m_timerId > 0) { - killTimer(m_timerId); - m_timerId = -1; + if (m_heartbeatTimerId > 0) { + killTimer(m_heartbeatTimerId); + m_heartbeatTimerId = -1; } if (m_commandSocket != nullptr) { m_commandSocket->deleteLater(); @@ -387,9 +388,9 @@ QFuture DeviceConnection::updateNetworkInfomation(bool dhcp, const QString void DeviceConnection::requestOta(const QString &firmware, const QString &file) { m_otaProgress = 0; emit otaProgressChanged(true, m_otaProgress, "正在向设备发起OTA请求......"); - if (m_timerId > 0) { - killTimer(m_timerId); - m_timerId = -1; + if (m_heartbeatTimerId > 0) { + killTimer(m_heartbeatTimerId); + m_heartbeatTimerId = -1; } if (!m_requests.empty()) { m_requests.pop(); @@ -480,7 +481,7 @@ void DeviceConnection::transferBinContent() { m_h264Socket->close(); }, type); - m_otaTimer->start(60 * 1000); + m_otaTimer->start(5 * 60 * 1000); // 固件升级五分钟,正常升级2.5分钟左右(包含算法模型) } } @@ -613,7 +614,7 @@ QString DeviceConnection::handleCommand(const std::string_view &replyText, const emit otaProgressChanged(true, m_otaProgress, "设备正在升级中,请稍后......"); } else { m_otaTimer->stop(); // 这里不需要再超时了 - emit otaProgressChanged(true, 100, "设备正在升级中,请于一分钟后重新连接wifi搜索设备"); + emit otaProgressChanged(true, 100, "设备正在升级中,请于五分钟后重新连接wifi搜索设备"); } } else { const char *message = nullptr; @@ -671,7 +672,7 @@ QString DeviceConnection::handleCommand(const std::string_view &replyText, const } else { LOG(warning) << "unknown reply: " << replyText; } - return QString::fromStdString(std::string(std::move(function))); + return QString::fromStdString(std::string(function)); } void DeviceConnection::onConnected() { @@ -684,7 +685,7 @@ void DeviceConnection::onConnected() { requestNetworkInfomation(); requestVideoInformation(); emit connected(); - m_timerId = startTimer(2500); + m_heartbeatTimerId = startTimer(2500); if (m_otaProgress == 99) { m_otaProgress = -1; emit otaProgressChanged(true, 100, "设备升级成功!"); @@ -702,9 +703,9 @@ void DeviceConnection::onConnected() { void DeviceConnection::onDisconnected() { auto socket = dynamic_cast(sender()); if (socket == m_commandSocket) { - if (m_timerId > 0) { - killTimer(m_timerId); - m_timerId = -1; + if (m_heartbeatTimerId > 0) { + killTimer(m_heartbeatTimerId); + m_heartbeatTimerId = -1; } emit disconnected(); if ((m_otaProgress >= 0) && (m_otaProgress <= 98)) { @@ -743,7 +744,6 @@ void DeviceConnection::onH264ReadyRead() { void DeviceConnection::onCommandReadyRead() { auto data = m_commandSocket->readAll(); m_commandBuffer.push_back(data); - while (!m_commandBuffer.isEmpty()) { auto packageSize = ntohl(*reinterpret_cast(m_commandBuffer.data())); if (m_commandBuffer.size() < (packageSize + sizeof(uint32_t))) break; @@ -755,9 +755,13 @@ void DeviceConnection::onCommandReadyRead() { auto &task = m_requests.front(); if (task.command == command) { m_requests.pop(); + } else { + LOG(warning) << "current command[" << command.toStdString() << "] is no the task queue's head[" + << task.command.toStdString() << "]"; } if (!m_requests.empty()) { - m_requests.front().task(); + auto &command = m_requests.front(); + command.task(); } } } @@ -769,15 +773,34 @@ void DeviceConnection::onErrorOccurred(QAbstractSocket::SocketError socketError) } void DeviceConnection::timerEvent(QTimerEvent *event) { - if (isConnected()) { - int index = heartbeats % 3; - if (index == 0) { - requestOpenDoorArea(); - } else if (index == 1) { - requestShieldedArea(); - } else if (index == 2) { - requestAntiClipArea(); + using namespace std::chrono; + if (event->timerId() == m_heartbeatTimerId) { + if (isConnected()) { + int index = heartbeats % 3; + if (index == 0) { + requestOpenDoorArea(); + } else if (index == 1) { + requestShieldedArea(); + } else if (index == 2) { + requestAntiClipArea(); + } + heartbeats++; + } + if (!m_requests.empty()) { + auto &command = m_requests.front(); + auto elapsed = duration_cast(system_clock::now() - command.time); + if (elapsed > (HeartbeatInterval * 2)) { + LOG(info) << "not received command[" << command.command.toStdString() << "] more than " + << (HeartbeatInterval * 2).count() << " ms, consider it failed, send next command."; + m_requests.pop(); + if (!m_requests.empty()) { + m_requests.front().task(); + } + } else if (elapsed > HeartbeatInterval) { + LOG(info) << "not received command[" << command.command.toStdString() << "] more than " + << HeartbeatInterval.count() << " ms, resend it."; + command.task(); + } } - heartbeats++; } } diff --git a/DeviceConnection.h b/DeviceConnection.h index 592d05f..bc6108d 100644 --- a/DeviceConnection.h +++ b/DeviceConnection.h @@ -19,6 +19,7 @@ class DeviceConnection : public QObject { public: constexpr static auto WirelessAddress = "192.168.10.2"; + constexpr static auto HeartbeatInterval = std::chrono::milliseconds(2500); enum Resolution { Video_360P = 0, Video_720P, @@ -103,6 +104,7 @@ protected: class Task { public: QString command; + std::chrono::system_clock::time_point time = std::chrono::system_clock::now(); std::function task; std::shared_ptr timeoutTimer = nullptr; std::shared_ptr> future; @@ -135,7 +137,7 @@ private: H264FrameCallback m_frameCallback; std::queue m_requests; - int m_timerId = -1; + int m_heartbeatTimerId = -1; int heartbeats = 0; NetworkInfomation m_networkInfomation; QString m_firmware;