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 { class Application : public QObject {
Q_OBJECT Q_OBJECT
QML_ELEMENT 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 connected READ connected NOTIFY connectedChanged)
Q_PROPERTY(bool uvcOpened READ uvcOpened NOTIFY uvcOpenedChanged) Q_PROPERTY(bool uvcOpened READ uvcOpened NOTIFY uvcOpenedChanged)
Q_PROPERTY(bool persistenceMode READ persistenceMode WRITE setPersistenceMode NOTIFY persistenceModeChanged) 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)); auto [frameData, frameSize] = generateFrame(Verify, reinterpret_cast<const uint8_t *>(&data), sizeof(data));
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize); m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
m_currentMessageId = Verify; setCurrentMessageIdStatus(Verify);
emit commandStarted(Verify);
LOG_CAT(info, GUI) << "发送识别指令: " << protocolDataFormatString(frameData, frameSize); LOG_CAT(info, GUI) << "发送识别指令: " << protocolDataFormatString(frameData, frameSize);
LOG_CAT(info, GUI) << Separator; LOG_CAT(info, GUI) << Separator;
@ -49,6 +48,7 @@ void ModuleCommunication::verify(uint8_t timeout) {
void ModuleCommunication::reset() { void ModuleCommunication::reset() {
auto [frameData, frameSize] = generateFrame(Reset); auto [frameData, frameSize] = generateFrame(Reset);
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize); m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
setCurrentMessageIdStatus(Reset);
LOG_CAT(info, GUI) << "发送复位指令: " << protocolDataFormatString(frameData, frameSize); LOG_CAT(info, GUI) << "发送复位指令: " << protocolDataFormatString(frameData, frameSize);
LOG_CAT(info, GUI) << Separator; 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)); 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)); auto [frameData, frameSize] = generateFrame(EnrollSingle, reinterpret_cast<const uint8_t *>(&data), sizeof(data));
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize); m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
m_currentMessageId = EnrollSingle; setCurrentMessageIdStatus(EnrollSingle);
emit commandStarted(EnrollSingle);
LOG_CAT(info, GUI) << "发送注册指令: " << protocolDataFormatString(frameData, frameSize); LOG_CAT(info, GUI) << "发送注册指令: " << protocolDataFormatString(frameData, frameSize);
LOG_CAT(info, GUI) << "用户名: " << username << ", 超时时间: " << static_cast<int>(timeout) << "s"; 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); uint16_t n = htons(userid);
auto [frameData, frameSize] = generateFrame(DeleteUser, reinterpret_cast<const uint8_t *>(&n), sizeof(n)); auto [frameData, frameSize] = generateFrame(DeleteUser, reinterpret_cast<const uint8_t *>(&n), sizeof(n));
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize); m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
m_currentMessageId = DeleteUser; setCurrentMessageIdStatus(DeleteUser);
emit commandStarted(DeleteUser);
LOG_CAT(info, GUI) << "发送删除用户指令: " << protocolDataFormatString(frameData, frameSize); LOG_CAT(info, GUI) << "发送删除用户指令: " << protocolDataFormatString(frameData, frameSize);
LOG_CAT(info, GUI) << "删除用户ID: " << userid; LOG_CAT(info, GUI) << "删除用户ID: " << userid;
LOG_CAT(info, GUI) << Separator; LOG_CAT(info, GUI) << Separator;
@ -93,8 +91,7 @@ void ModuleCommunication::deleteUser(uint16_t userid) {
void ModuleCommunication::deleteAll() { void ModuleCommunication::deleteAll() {
auto [frameData, frameSize] = generateFrame(DeleteAll); auto [frameData, frameSize] = generateFrame(DeleteAll);
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize); m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
m_currentMessageId = DeleteAll; setCurrentMessageIdStatus(DeleteAll);
emit commandStarted(DeleteAll);
LOG_CAT(info, GUI) << "发送删除所有指令: " << protocolDataFormatString(frameData, frameSize); LOG_CAT(info, GUI) << "发送删除所有指令: " << protocolDataFormatString(frameData, frameSize);
LOG_CAT(info, GUI) << Separator; LOG_CAT(info, GUI) << Separator;
} }
@ -303,6 +300,7 @@ void ModuleCommunication::processPackage(const uint8_t *data, uint16_t size) {
break; break;
} }
m_currentMessageId = Idle; m_currentMessageId = Idle;
emit currentMessageIdChanged();
emit commandFinished(static_cast<MessageId>(replyId), static_cast<MessageStatus>(result)); emit commandFinished(static_cast<MessageId>(replyId), static_cast<MessageStatus>(result));
break; break;
} }
@ -400,6 +398,14 @@ std::pair<uint8_t *, uint32_t> ModuleCommunication::generateFrame(MessageId comm
return std::make_pair(sendBuffer, size + 6); 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::string ModuleCommunication::protocolDataFormatString(const uint8_t *data, int size) {
std::ostringstream oss; std::ostringstream oss;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {

View File

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

View File

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

View File

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