1.实现图片上报及下发注册的对接实现。

This commit is contained in:
luocai 2024-06-21 11:02:28 +08:00
parent 6e11d190c0
commit d9a9815a89
5 changed files with 44 additions and 27 deletions

View File

@ -17,7 +17,7 @@ class QTimer;
class Application : public QObject {
Q_OBJECT
QML_ELEMENT
Q_PROPERTY(ModuleCommunication *module READ module CONSTANT)
Q_PROPERTY(ModuleCommunication *module READ module NOTIFY connectedChanged)
Q_PROPERTY(bool connected READ connected NOTIFY connectedChanged)
Q_PROPERTY(bool uvcOpened READ uvcOpened NOTIFY uvcOpenedChanged)
Q_PROPERTY(bool persistenceMode READ persistenceMode WRITE setPersistenceMode NOTIFY persistenceModeChanged)

View File

@ -39,8 +39,7 @@ void ModuleCommunication::verify(uint8_t timeout) {
auto [frameData, frameSize] = generateFrame(Verify, reinterpret_cast<const uint8_t *>(&data), sizeof(data));
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
m_currentMessageId = Verify;
emit commandStarted(Verify);
setCurrentMessageIdStatus(Verify);
LOG_CAT(info, GUI) << "发送识别指令: " << protocolDataFormatString(frameData, frameSize);
LOG_CAT(info, GUI) << Separator;
@ -49,6 +48,7 @@ void ModuleCommunication::verify(uint8_t timeout) {
void ModuleCommunication::reset() {
auto [frameData, frameSize] = generateFrame(Reset);
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
setCurrentMessageIdStatus(Reset);
LOG_CAT(info, GUI) << "发送复位指令: " << protocolDataFormatString(frameData, frameSize);
LOG_CAT(info, GUI) << Separator;
}
@ -59,8 +59,7 @@ void ModuleCommunication::enroll(const std::string &username, uint8_t timeout) {
strncpy(reinterpret_cast<char *>(data.username), username.c_str(), sizeof(data.username));
auto [frameData, frameSize] = generateFrame(EnrollSingle, reinterpret_cast<const uint8_t *>(&data), sizeof(data));
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
m_currentMessageId = EnrollSingle;
emit commandStarted(EnrollSingle);
setCurrentMessageIdStatus(EnrollSingle);
LOG_CAT(info, GUI) << "发送注册指令: " << protocolDataFormatString(frameData, frameSize);
LOG_CAT(info, GUI) << "用户名: " << username << ", 超时时间: " << static_cast<int>(timeout) << "s";
@ -83,8 +82,7 @@ void ModuleCommunication::deleteUser(uint16_t userid) {
uint16_t n = htons(userid);
auto [frameData, frameSize] = generateFrame(DeleteUser, reinterpret_cast<const uint8_t *>(&n), sizeof(n));
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
m_currentMessageId = DeleteUser;
emit commandStarted(DeleteUser);
setCurrentMessageIdStatus(DeleteUser);
LOG_CAT(info, GUI) << "发送删除用户指令: " << protocolDataFormatString(frameData, frameSize);
LOG_CAT(info, GUI) << "删除用户ID: " << userid;
LOG_CAT(info, GUI) << Separator;
@ -93,8 +91,7 @@ void ModuleCommunication::deleteUser(uint16_t userid) {
void ModuleCommunication::deleteAll() {
auto [frameData, frameSize] = generateFrame(DeleteAll);
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
m_currentMessageId = DeleteAll;
emit commandStarted(DeleteAll);
setCurrentMessageIdStatus(DeleteAll);
LOG_CAT(info, GUI) << "发送删除所有指令: " << protocolDataFormatString(frameData, frameSize);
LOG_CAT(info, GUI) << Separator;
}
@ -303,6 +300,7 @@ void ModuleCommunication::processPackage(const uint8_t *data, uint16_t size) {
break;
}
m_currentMessageId = Idle;
emit currentMessageIdChanged();
emit commandFinished(static_cast<MessageId>(replyId), static_cast<MessageStatus>(result));
break;
}
@ -400,6 +398,14 @@ std::pair<uint8_t *, uint32_t> ModuleCommunication::generateFrame(MessageId comm
return std::make_pair(sendBuffer, size + 6);
}
void ModuleCommunication::setCurrentMessageIdStatus(MessageId messageId) {
if (m_currentMessageId != messageId) {
m_currentMessageId = messageId;
emit commandStarted(messageId);
emit currentMessageIdChanged();
}
}
std::string ModuleCommunication::protocolDataFormatString(const uint8_t *data, int size) {
std::ostringstream oss;
for (int i = 0; i < size; i++) {

View File

@ -3,13 +3,16 @@
#include "DataStructure.h"
#include <QObject>
#include <QQmlEngine>
#include <QSerialPort>
#include <memory>
class ModuleCommunication : public QObject {
Q_OBJECT
QML_ELEMENT
static constexpr uint32_t UsernameSize = 32;
static constexpr const char *Separator = "----------";
Q_PROPERTY(MessageId currentMessageId READ currentMessageId NOTIFY currentMessageIdChanged)
public:
constexpr static uint16_t InvalidUserId = std::numeric_limits<uint16_t>::max();
@ -29,6 +32,7 @@ public:
RequestPalmFeature = 0xFA,
Idle = 0xFF,
};
Q_ENUM(MessageId)
enum NoteId : uint8_t {
Ready = 0x00,
@ -161,12 +165,14 @@ signals:
void errorOccurred(const QString &error);
void commandStarted(ModuleCommunication::MessageId messageId);
void commandFinished(MessageId messageId, MessageStatus status);
void currentMessageIdChanged();
protected:
void processPackage(const uint8_t *data, uint16_t size);
void onReadyRead();
void onErrorOccurred(QSerialPort::SerialPortError error);
std::pair<uint8_t *, uint32_t> generateFrame(MessageId command, const uint8_t *data = nullptr, uint16_t size = 0);
void setCurrentMessageIdStatus(ModuleCommunication::MessageId messageId);
private:
std::shared_ptr<QSerialPort> m_serialPort;

View File

@ -32,9 +32,12 @@ ColumnLayout {
text: "10"
}
Button {
text: "注册"
onClicked: App.enroll(enrollName.text,
parseInt(enrollTimeout.text))
text: !App.module ? "注册" : App.module.currentMessageId
=== 0x1d ? "取消" : "注册"
onClicked: App.module.currentMessageId
=== 0x1d ? App.module.reset() : App.enroll(
enrollName.text,
parseInt(enrollTimeout.text))
}
}
}
@ -67,11 +70,11 @@ ColumnLayout {
}
Item {}
Button {
text: App.isVerifying?"停止": "识别"
text: App.isVerifying ? "停止" : "识别"
onClicked: {
if(App.isVerifying){
if (App.isVerifying) {
App.module.reset()
}else {
} else {
App.persistenceVerifyInterval = parseInt(
verifyIntetval.text)
App.verify(parseInt(verifyTimeout.text))
@ -107,7 +110,7 @@ ColumnLayout {
columns: 1
Button {
text: "录入图片上报"
onClicked: App.getEnrolledImage("",60)
onClicked: App.getEnrolledImage("", 60)
}
Button {
text: "图片下发注册"

View File

@ -74,11 +74,13 @@ bool CdcUpdater::write(Command command, const uint8_t *data, uint32_t size) {
if (data != nullptr) {
memcpy(&packet[3], data, size);
}
std::ostringstream oss;
for (int i = 0; i < packet.size(); i++) {
oss << "0x" << std::hex << (static_cast<int>(packet[i]) & 0xff) << " ";
if (command != TransferBin) {
std::ostringstream oss;
for (int i = 0; i < packet.size(); i++) {
oss << "0x" << std::hex << (static_cast<int>(packet[i]) & 0xff) << " ";
}
LOG(info) << "write " << oss.str();
}
LOG(info) << "write " << oss.str();
return m_serialPort->write(reinterpret_cast<const char *>(packet.data()), packet.size());
}
@ -121,14 +123,14 @@ void CdcUpdater::transferBin() {
void CdcUpdater::onReadyRead() {
auto data = m_serialPort->readAll();
std::ostringstream oss;
for (int i = 0; i < data.size(); i++) {
oss << "0x" << std::hex << (static_cast<int>(data[i]) & 0xff) << " ";
}
LOG(info) << "onReadyRead, size: " << data.size() << ", " << oss.str();
uint8_t command = static_cast<uint8_t>(data[2]);
if (command != TransferBinReply) {
std::ostringstream oss;
for (int i = 0; i < data.size(); i++) {
oss << "0x" << std::hex << (static_cast<int>(data[i]) & 0xff) << " ";
}
LOG(info) << "onReadyRead, size: " << data.size() << ", " << oss.str();
}
if (command == EnterUpgradeReply) {
write(GetVersion);
emit progressChanged(++m_progress);