完善升级功能。

This commit is contained in:
luocai 2024-08-08 17:05:32 +08:00
parent 2610c40189
commit 9d495d487d
7 changed files with 251 additions and 198 deletions

View File

@ -12,6 +12,7 @@
#include "VideoFrameProvider.h"
#include "VideoPlayer.h"
#include <QApplication>
#include <QFile>
#include <QFont>
#include <QImage>
#include <QQmlApplicationEngine>
@ -408,7 +409,22 @@ void Application::onVerifyTimeout() {
}
}
void Application::startOta(const QString &path) {
bool Application::startOta(const QString &path) {
if (!QFile::exists(path)) {
emit otaMessage("文件不存在");
return false;
}
auto device = CdcUpdater::searchDevice();
if (device) {
LOG(info) << "device already in ota mode.";
} else {
if (m_communication) {
m_communication->startOta();
} else {
emit otaMessage("请先打开设备");
return false;
}
}
LOG(info) << "start ota, ota path: " << path.toStdString();
m_updater = std::make_shared<CdcUpdater>();
connect(m_updater.get(), &CdcUpdater::deviceDiscovered, this, &Application::onCdcDeviceDiscovered);
@ -416,8 +432,8 @@ void Application::startOta(const QString &path) {
connect(m_updater.get(), &CdcUpdater::progressChanged, this, &Application::otaProgressChanged);
connect(m_updater.get(), &CdcUpdater::message, this, &Application::otaMessage);
m_communication->startOta();
m_updater->start(path);
m_updater->start(path, device ? *device : QSerialPortInfo());
return true;
}
void Application::onCdcDeviceDiscovered(const QSerialPortInfo &info) {

View File

@ -45,7 +45,7 @@ public:
Q_INVOKABLE bool openUVC(const QString &deviceName);
Q_INVOKABLE void close();
Q_INVOKABLE void closeUVC();
Q_INVOKABLE void startOta(const QString &path);
Q_INVOKABLE bool startOta(const QString &path);
Q_INVOKABLE void verify(bool captureImage, uint8_t timeout);
Q_INVOKABLE void enroll(const QString &username, bool persistence, uint8_t timeout);
Q_INVOKABLE void enrollExtended(const QString &username, bool persistence, uint8_t timeout);

View File

@ -5,10 +5,14 @@ import QtQuick.Dialogs
import QtQuick.Layouts
import Analyser
ColumnLayout {
Item {
id: root
ColumnLayout {
anchors.fill: parent
ConnectionItem {}
GroupBox {
id: commandBox
title: "命令"
Layout.columnSpan: 2
enabled: App.connected
@ -50,15 +54,14 @@ ColumnLayout {
}
Button {
property bool enrolling: App.module ? (App.module.currentMessageId === 0x1d)
|| (App.module.currentMessageId
=== 0x1e) : false
property bool enrolling: App.module ? (App.module.currentMessageId === 0x1d) || (App.module.currentMessageId === 0x1e) : false
text: enrolling ? "取消" : "注册"
onClicked: {
if (enrolling) {
App.module.reset()
} else if (extendedMode.checked) {
App.enrollExtended(enrollName.text,
App.enrollExtended(
enrollName.text,
persistence.checked,
parseInt(enrollTimeout.text))
} else {
@ -132,7 +135,8 @@ ColumnLayout {
}
Button {
text: "删除"
onClicked: App.deleteUser(parseInt(deleteUserId.text))
onClicked: App.deleteUser(parseInt(
deleteUserId.text))
}
Button {
text: "删除所有"
@ -176,6 +180,7 @@ ColumnLayout {
onClicked: App.module.requestUniqueId()
}
Button {
id: otaButton
text: "OTA升级"
onClicked: loader.active = true
}
@ -190,7 +195,7 @@ ColumnLayout {
}
}
}
}
FileDialog {
id: fileDialog
nameFilters: ["图片 (*.jpg *.yuv)"]
@ -204,6 +209,19 @@ ColumnLayout {
}
}
MouseArea {
width: otaButton.width
height: otaButton.height
enabled: !commandBox.enabled
onClicked: {
loader.active = true
}
Component.onCompleted: {
x = Qt.binding(() => otaButton.mapToItem(root, 0, 0).x)
y = Qt.binding(() => otaButton.mapToItem(root, 0, 0).y)
}
}
Loader {
id: loader
source: "OtaPage.qml"

View File

@ -45,14 +45,14 @@ Popup {
RowLayout {
spacing: 10
ProgressBar {
id:progressBar
id: progressBar
Layout.fillWidth: true
from: 0
to:100
to: 100
value: 0.0
}
Text {
id:progressText
id: progressText
text: "0%"
verticalAlignment: Text.AlignVCenter
}
@ -72,8 +72,7 @@ Popup {
Layout.alignment: Qt.AlignRight
onClicked: {
otaMessage.color = "black"
App.startOta(otaFile.text)
enabled = false
enabled = !App.startOta(otaFile.text)
}
}
}
@ -106,10 +105,9 @@ Popup {
function onOtaMessage(message) {
otaMessage.text = message
}
function onOtaProgressChanged(progress){
progressBar.value = progress;
function onOtaProgressChanged(progress) {
progressBar.value = progress
progressText.text = `${progress}%`
}
}
}

View File

@ -62,6 +62,10 @@ void Widget::start() {
QMessageBox::warning(this, "升级", "升级文件不存在!");
return;
}
auto device = CdcUpdater::searchDevice();
if (device) {
LOG(info) << "device already in ota mode.";
} else {
auto discovery = std::make_shared<DeviceDiscovery>();
std::error_code error;
@ -73,12 +77,13 @@ void Widget::start() {
}
setMessage("发现设备成功,进入BOOT模式......");
discovery->enterOtaMode(device, error);
}
m_updater = std::make_shared<CdcUpdater>();
connect(m_updater.get(), &CdcUpdater::deviceDiscovered, this, &Widget::onCdcDeviceDiscovered);
connect(m_updater.get(), &CdcUpdater::updateFinished, this, &Widget::onUpdateFinished);
connect(m_updater.get(), &CdcUpdater::progressChanged, this, &Widget::setProgress);
connect(m_updater.get(), &CdcUpdater::message, this, &Widget::setMessage);
m_updater->start(filePath);
m_updater->start(filePath, device ? *device : QSerialPortInfo());
setControlsEnabled(false);
}

View File

@ -3,7 +3,6 @@
#include "StringUtility.h"
#include <QDebug>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <filesystem>
#include <fstream>
#include <mbedtls/md5.h>
@ -41,10 +40,15 @@ CdcUpdater::CdcUpdater(QObject *parent) : QObject(parent) {
CdcUpdater::~CdcUpdater() {
}
void CdcUpdater::start(const QString &path) {
void CdcUpdater::start(const QString &path, const QSerialPortInfo &info) {
if (info.isNull()) {
if (m_timerId < 0) {
m_timerId = startTimer(1000);
}
} else {
open(info);
}
m_path = Amass::StringUtility::UTF8ToGBK(path.toStdString());
LOG(info) << "ota file: " << m_path;
emit progressChanged(++m_progress);
@ -84,20 +88,29 @@ bool CdcUpdater::write(Command command, const uint8_t *data, uint32_t size) {
return m_serialPort->write(reinterpret_cast<const char *>(packet.data()), packet.size());
}
void CdcUpdater::timerEvent(QTimerEvent *event) {
std::optional<QSerialPortInfo> CdcUpdater::searchDevice() {
std::optional<QSerialPortInfo> ret = std::nullopt;
const auto serialPortInfos = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo &portInfo : serialPortInfos) {
LOG(info) << "portName:" << portInfo.portName().toStdString()
<< ", vendorIdentifier: " << portInfo.vendorIdentifier()
<< ", productIdentifier: " << portInfo.productIdentifier();
if (portInfo.vendorIdentifier() == 0xffff) {
LOG(info) << "founded device: " << portInfo.portName().toStdString();
emit deviceDiscovered(portInfo);
killTimer(m_timerId);
m_timerId = -1;
ret = portInfo;
break;
}
}
return ret;
}
void CdcUpdater::timerEvent(QTimerEvent *event) {
auto device = searchDevice();
if (device) {
LOG(info) << "founded device: " << device->portName().toStdString();
emit deviceDiscovered(*device);
killTimer(m_timerId);
m_timerId = -1;
}
LOG(info) << "----------";
}

View File

@ -2,9 +2,10 @@
#define __CDCUPDATER_H__
#include <QObject>
#include <QSerialPortInfo>
#include <optional>
class QSerialPort;
class QSerialPortInfo;
class CdcUpdater : public QObject {
Q_OBJECT
@ -35,8 +36,10 @@ public:
};
CdcUpdater(QObject *parent = nullptr);
~CdcUpdater();
void start(const QString &path);
void start(const QString &path, const QSerialPortInfo &info = QSerialPortInfo());
bool open(const QSerialPortInfo &info);
static std::optional<QSerialPortInfo> searchDevice();
signals:
void deviceDiscovered(const QSerialPortInfo &info);
void updateFinished();