修复早期T009版本不回复不支持的命令,导致上位机不发送后续命令的问题。
All checks were successful
Release tag / build (push) Successful in 3m5s

This commit is contained in:
luocai 2024-12-03 17:56:24 +08:00
parent 95b68bc67d
commit ef7aa73c8d
3 changed files with 51 additions and 26 deletions

View File

@ -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)

View File

@ -4,6 +4,7 @@
#include <QFileInfo>
#include <QPointF>
#include <QTimer>
#include <QTimerEvent>
#include <WinSock2.h>
#include <boost/json/object.hpp>
#include <boost/json/parse.hpp>
@ -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<bool> 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<QTcpSocket *>(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<uint32_t *>(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<milliseconds>(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++;
}
}

View File

@ -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<void()> task;
std::shared_ptr<QTimer> timeoutTimer = nullptr;
std::shared_ptr<QFutureInterface<bool>> future;
@ -135,7 +137,7 @@ private:
H264FrameCallback m_frameCallback;
std::queue<Task> m_requests;
int m_timerId = -1;
int m_heartbeatTimerId = -1;
int heartbeats = 0;
NetworkInfomation m_networkInfomation;
QString m_firmware;