mserial.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #ifndef MSEIAL_H
  2. #define MSEIAL_H
  3. #include <QObject>
  4. #include <QtSerialPort/QSerialPort>
  5. #include <QtSerialPort/QSerialPortInfo>
  6. #include <QStringList>
  7. #include <QString>
  8. #include <QByteArray>
  9. #include <QTextCodec>
  10. #include <QThread>
  11. #include <m_json.h>
  12. #include <QTimer>
  13. #include <QStandardPaths>
  14. #include <QVideoFrame>
  15. #include <mysql.h>
  16. #include <m_loger.h>
  17. class SerialThread: public QThread
  18. {
  19. Q_OBJECT
  20. public:
  21. // 数据缓存区
  22. QByteArray dataBuf;
  23. explicit SerialThread(QObject *parent = nullptr);
  24. ~SerialThread();
  25. void setSerialPortSettings(const QString &portName, QSerialPort::BaudRate baudRate,
  26. QSerialPort::DataBits dataBits, QSerialPort::Parity parity,
  27. QSerialPort::StopBits stopBits, QSerialPort::FlowControl flowControl);
  28. void run() override;
  29. void stop();
  30. void SetLedState(bool sw){
  31. sendData(m_json.setLedState(sw));
  32. }
  33. Q_INVOKABLE void reboot(){
  34. sendData(m_json.CmdReboot());
  35. }
  36. QSerialPort& getSerialPort() {
  37. return m_serialPort;
  38. }
  39. Q_INVOKABLE void readBoardData();
  40. Q_INVOKABLE void update_dat_f_main(QString freq); //更新数据上传间隔
  41. Q_INVOKABLE void sendCmd(QString cmd_type,bool sw,QString setup,QString speed);
  42. Q_INVOKABLE QStringList pathinfo(){
  43. QStringList pf_pic;
  44. pf_pic.append(qst_dirPath);
  45. pf_pic.append(QString::number(int_fileCount));
  46. pf_pic.append(QString::number(int_totalSize/1048576));
  47. return pf_pic;
  48. }
  49. Q_INVOKABLE void readPathinfo(){
  50. analyzeDirectory(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
  51. }
  52. Q_INVOKABLE void removepic(){
  53. deleteAllFilesInDirectory(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
  54. }
  55. Q_INVOKABLE void setCollt(int sh1 ,int eh1,int sh2,int eh2,int sh3,int eh3){
  56. sendData(m_json.CmdParamconf(sh1,eh1,sh2,eh2,sh3,eh3));
  57. }
  58. Q_INVOKABLE void setHt_xy_dis(int xdis ,int ydis){
  59. sendData(m_json.CmdParamconf_xy_dis(xdis,ydis));
  60. }
  61. Q_INVOKABLE void setLightness(int val){
  62. auto qtContext = QJniObject(QNativeInterface::QAndroidApplication::context()); //activity
  63. if (qtContext.isValid()) {
  64. QJniObject jClass("com/yfkj/cb/Q6iot/Backlight");
  65. jClass.callMethod<void>("setScreenLight","(Landroid/content/Context;I)V",qtContext.object(),val);
  66. }
  67. }
  68. Q_INVOKABLE QString getId(){
  69. return param::imei;
  70. }
  71. Q_INVOKABLE QString getIccid(){
  72. return param::iccid;
  73. }
  74. signals:
  75. void dataReceived(const QByteArray &data);
  76. void dataUpload(const QByteArray &data);
  77. void updatestart(QString filename);
  78. void auto_takephoto(bool sw);
  79. void takephotodone();
  80. void disconnect_485();
  81. void autotestdone();
  82. void translatedone();
  83. void netMessage(const QByteArray &data);
  84. void autoFocusDone();
  85. void autoFocusStart();
  86. void signal_movy();
  87. void timeout_mov_stop();
  88. public slots:
  89. void sendData(const QByteArray &data);
  90. void mqtttoserial(const QByteArray &data);
  91. void getmodemresult(int res);
  92. void doWork(QVideoFrame dframe);
  93. private slots:
  94. //void readBoardData();
  95. private:
  96. void processJsonData(QByteArray &jsonData);
  97. QTimer m_dat_f;
  98. QTimer* timeout_mov = nullptr;
  99. QSerialPort m_serialPort;
  100. SqliteOperator *sqlConfig;
  101. bool m_running;
  102. bool data_upload;
  103. int connect_board; //主板串口连接状态
  104. JsonFunc m_json;
  105. QString qst_dirPath;
  106. int int_fileCount;
  107. qint64 int_totalSize;
  108. // 用于存储目录统计结果的结构体
  109. struct DirectoryStats {
  110. qint64 totalSize = 0;
  111. int fileCount = 0;
  112. };
  113. void deleteAllFilesInDirectory(const QString &directoryPath)
  114. {
  115. QDir directory(directoryPath);
  116. if (!directory.exists()) {
  117. qDebug() << "Directory does not exist.";
  118. return;
  119. }
  120. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  121. foreach (const QFileInfo &fileInfo, fileInfoList) {
  122. if (fileInfo.isDir()) {
  123. // 递归删除子目录中的文件
  124. deleteAllFilesInDirectory(fileInfo.filePath());
  125. } else {
  126. // 删除文件
  127. QFile::remove(fileInfo.filePath());
  128. }
  129. }
  130. analyzeDirectory(directoryPath);
  131. }
  132. // 递归获取目录统计信息
  133. DirectoryStats getDirectoryStats(const QString &directoryPath)
  134. {
  135. QDir directory(directoryPath);
  136. DirectoryStats stats;
  137. if (!directory.exists()) {
  138. qDebug() << "Directory does not exist.";
  139. }
  140. qint64 totalSize = 0;
  141. int fileCount = 0;
  142. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  143. foreach (const QFileInfo &fileInfo, fileInfoList) {
  144. if (fileInfo.isDir()) {
  145. // 递归获取子目录的统计信息
  146. DirectoryStats subStats = getDirectoryStats(fileInfo.filePath());
  147. stats.fileCount += subStats.fileCount;
  148. stats.totalSize += subStats.totalSize;
  149. } else {
  150. stats.fileCount++;
  151. stats.totalSize += fileInfo.size();
  152. }
  153. }
  154. return stats;
  155. qst_dirPath = directoryPath;
  156. int_fileCount = fileCount;
  157. int_totalSize = totalSize;
  158. LOG("Directory:{}",directoryPath.toStdString());
  159. LOG("File count:{}",fileCount);
  160. LOG("Total size:{} bytes",totalSize);
  161. }
  162. // 使用示例
  163. void analyzeDirectory(const QString &directoryPath) {
  164. qDebug() << "Analyzing directory:" << directoryPath;
  165. DirectoryStats stats = getDirectoryStats(directoryPath);
  166. qst_dirPath = directoryPath;
  167. int_fileCount = stats.fileCount;
  168. int_totalSize = stats.totalSize;
  169. LOG("Total files:{}",int_totalSize);
  170. LOG("Total size:{} MB",stats.totalSize / (1024.0 * 1024.0));
  171. }
  172. };
  173. class Serial_AT : public QSerialPort
  174. {
  175. Q_OBJECT
  176. public:
  177. explicit Serial_AT(QObject *parent = 0);
  178. QTimer *m_5s;
  179. // 发送字节数
  180. int txByte = 0;
  181. // 接收字节数
  182. int rxByte = 0;
  183. // 数据缓存区
  184. QByteArray dataBuf;
  185. // 串口描述内容
  186. QStringList serialDescription;
  187. // 串口扫描函数
  188. QStringList serialScan(void);
  189. bool serialOpen(QString serialName, int baudRate);
  190. void sendData(QByteArray sendData);
  191. void serialClose();
  192. private slots:
  193. void ReadData();
  194. private:
  195. QTextCodec *tc;
  196. QString at_str;
  197. QTimer* timeout_recv = nullptr;
  198. signals:
  199. void readSignal(int recByte);
  200. void sendSuccess(int sendByte);
  201. void imeiDone();
  202. void iccidDone();
  203. };
  204. #endif // MSEIAL_H