| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- #ifndef MSEIAL_H
- #define MSEIAL_H
- #include <QObject>
- #include <QtSerialPort/QSerialPort>
- #include <QtSerialPort/QSerialPortInfo>
- #include <QStringList>
- #include <QString>
- #include <QByteArray>
- #include <QThread>
- #include <m_json.h>
- #include <QTimer>
- #include <QStandardPaths>
- #include <QVideoFrame>
- #include <mysql.h>
- #include <m_loger.h>
- #include "lwjson/lwjson.h"
- #include "ymodem.h"
- class SerialThread: public QThread
- {
- Q_OBJECT
- public:
- // 数据缓存区
- QByteArray dataBuf;
- YMODEM* getYmodem(); // 声明 getter
- explicit SerialThread(QObject *parent = nullptr);
- ~SerialThread();
- void setSerialPortSettings(const QString &portName, QSerialPort::BaudRate baudRate,
- QSerialPort::DataBits dataBits, QSerialPort::Parity parity,
- QSerialPort::StopBits stopBits, QSerialPort::FlowControl flowControl);
- void init();
- void run() override;
- void stop();
- void SetLedState(bool sw){
- sendData(m_json.setLedState(sw));
- }
- Q_INVOKABLE void reboot(){
- sendData(m_json.CmdReboot());
- }
- QSerialPort& getSerialPort() {
- return m_serialPort;
- }
- Q_INVOKABLE void readBoardData();
- Q_INVOKABLE void uploadData_Mqtt(){
- m_dat_f.setInterval(0);
- };
- Q_INVOKABLE void update_dat_f_main(QString freq); //更新数据上传间隔
- Q_INVOKABLE void sendCmd(QString cmd_type,bool sw,QString setup,QString speed);
- Q_INVOKABLE QStringList pathinfo(){
- QStringList pf_pic;
- pf_pic.append(qst_dirPath);
- pf_pic.append(QString::number(int_fileCount));
- pf_pic.append(QString::number(int_totalSize/1048576));
- return pf_pic;
- }
- Q_INVOKABLE void readPathinfo(){
- analyzeDirectory(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/Pictures");
- }
- Q_INVOKABLE void removepic(){
- deleteAllFilesInDirectory(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/Pictures");
- }
- Q_INVOKABLE void setCollt(int sh1 ,int eh1,int sh2,int eh2,int sh3,int eh3){
- sendData(m_json.CmdParamconf(sh1,eh1,sh2,eh2,sh3,eh3));
- }
- Q_INVOKABLE void setCollt_min(int sh1 ,int eh1,int sh2,int eh2,int sh3,int eh3){
- sendData(m_json.CmdParamconf_min(sh1,eh1,sh2,eh2,sh3,eh3));
- }
- Q_INVOKABLE void setTempconf(int tpl ,int tph){
- sendData(m_json.CmdParamconf_temp(tpl,tph));
- }
- Q_INVOKABLE void setHt_xy_dis(int xdis ,int ydis){
- sendData(m_json.CmdParamconf_xy_dis(xdis,ydis));
- }
- Q_INVOKABLE void setLightness(int val){
- auto qtContext = QJniObject(QNativeInterface::QAndroidApplication::context()); //activity
- if (qtContext.isValid()) {
- QJniObject jClass("com/yfkj/bzyv1/Backlight");
- jClass.callMethod<void>("setScreenLight","(Landroid/content/Context;I)V",qtContext.object(),val);
- //jClass.callStaticMethod<void>("turnScreenOff", "()V");
- }
- #if 0
- if(val==-255){
- QJniObject::callStaticMethod<void>("GetUsbPermission",
- "turnScreenOff",
- "()V");
- }
- #endif
- }
- Q_INVOKABLE QString getId(){
- return param::imei;
- }
- Q_INVOKABLE QString getIccid(){
- return param::iccid;
- }
- Q_INVOKABLE QString getlat(){
- return param::gps_lat;
- }
- Q_INVOKABLE QString getlng(){
- return param::gps_lng;
- }
- Q_INVOKABLE QString get_app_version(){
- return APP_VERSION;
- }
- Q_INVOKABLE void makeJsonRpc(const QString &method,const QString &value)
- {
- sendData(m_json.MakeJsonRPC(method,value));
- }
- signals:
- void dataReceived(const QByteArray &data);
- void dataUpload(const QByteArray &data);
- void updatestart(QString filename);
- void auto_takephoto(bool sw);
- void takephotodone();
- void disconnect_485();
- void autotestdone();
- void translatedone();
- void netMessage(const QByteArray &data);
- void autoFocusDone();
- void autoFocusStart();
- void lwjsonStreamDONE(const QByteArray &data);
- void signal_movy(const QString stepcount);
- void timeout_mov_stop();
- void gpsSuccess();
- void ymodemReady(QString mtype);
- void ymodemProgress(int val,int tval);
- void ymodemFinished(int res);
- public slots:
- void sendData(const QByteArray &data);
- void mqtttoserial(const QByteArray &data);
- void getmodemresult(int res);
- void startYmodem(const QString path);
- void startUpgrade(QString path)
- {
- if(m_ymodem){
- m_upgrading = true;
- m_ymodem->start_ymodem(path);
- }
- }
- Q_INVOKABLE void recoverMcuParam();
- private slots:
- //void readBoardData();
- QString selectSerialPortName();
- void feedJsonParser(const QByteArray& data)
- {
- dataBuf.append(data);
- for (uint8_t b : data) {
- lwjsonr_t ret = lwjson_stream_parse(&lwparser, b);
- switch (ret){
- case lwjsonERRJSON: // 1, 无效的字符
- case lwjsonSTREAMWAITFIRSTCHAR: // 2, 等待首字符
- break;
- case lwjsonSTREAMINPROG: // 3, 正在解析中
- break;
- case lwjsonSTREAMDONE: // 4, 解析已完成
- LOG("lwjsonSTREAMDONE:{}",dataBuf.constData());
- emit lwjsonStreamDONE(dataBuf);
- dataBuf.clear();
- lwjson_stream_reset(&lwparser);
- break;
- case lwjsonERRMEM: // 5, 嵌套的太深("LWJSON_CFG_STREAM_STACK_SIZE")
- ERROR("lwjsonERRMEM!");
- lwjson_stream_reset(&lwparser);
- break;
- default: // 流式解析时, 其它的返回值, 都不应该出现
- ERROR("未知错误:{:d}",static_cast<int>(ret));
- break;
- }
- }
- }
- private:
- bool m_upgrading = false;
- void processJsonData(const QByteArray &jsonData);
- void saveMcuParam(const QJsonObject ¶ms);
- QTimer m_dat_f;
- QTimer* timeout_mov = nullptr;
- QSerialPort m_serialPort;
- YMODEM *m_ymodem; // YMODEM对象
- SqliteOperator *sqlConfig;
- mutable QMutex m_writeMutex;
- bool m_running;
- bool data_upload;
- int connect_board; //主板串口连接状态
- JsonFunc m_json;
- QString qst_dirPath;
- int int_fileCount;
- qint64 int_totalSize;
- // 用于存储目录统计结果的结构体
- struct DirectoryStats {
- qint64 totalSize = 0;
- int fileCount = 0;
- };
- lwjson_stream_parser_t lwparser;
- void deleteAllFilesInDirectory(const QString &directoryPath)
- {
- QDir directory(directoryPath);
- if (!directory.exists()) {
- qDebug() << "Directory does not exist.";
- return;
- }
- QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
- foreach (const QFileInfo &fileInfo, fileInfoList) {
- if (fileInfo.isDir()) {
- // 递归删除子目录中的文件
- deleteAllFilesInDirectory(fileInfo.filePath());
- } else {
- // 删除文件
- QFile::remove(fileInfo.filePath());
- }
- }
- analyzeDirectory(directoryPath);
- }
- // 递归获取目录统计信息
- DirectoryStats getDirectoryStats(const QString &directoryPath)
- {
- QDir directory(directoryPath);
- DirectoryStats stats;
- if (!directory.exists()) {
- qDebug() << "Directory does not exist.";
- }
- qint64 totalSize = 0;
- int fileCount = 0;
- QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
- foreach (const QFileInfo &fileInfo, fileInfoList) {
- if (fileInfo.isDir()) {
- // 递归获取子目录的统计信息
- DirectoryStats subStats = getDirectoryStats(fileInfo.filePath());
- stats.fileCount += subStats.fileCount;
- stats.totalSize += subStats.totalSize;
- } else {
- stats.fileCount++;
- stats.totalSize += fileInfo.size();
- }
- }
- return stats;
- qst_dirPath = directoryPath;
- int_fileCount = fileCount;
- int_totalSize = totalSize;
- LOG("Directory:{}",directoryPath.toStdString());
- LOG("File count:{}",fileCount);
- LOG("Total size:{} bytes",totalSize);
- }
- // 使用示例
- void analyzeDirectory(const QString &directoryPath) {
- qDebug() << "Analyzing directory:" << directoryPath;
- DirectoryStats stats = getDirectoryStats(directoryPath);
- qst_dirPath = directoryPath;
- int_fileCount = stats.fileCount;
- int_totalSize = stats.totalSize;
- LOG("Total files:{}",int_totalSize);
- LOG("Total size:{} MB",stats.totalSize / (1024.0 * 1024.0));
- }
- };
- #endif // MSEIAL_H
|