mserial.h 7.3 KB

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