mserial.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. //jClass.callStaticMethod<void>("turnScreenOff", "()V");
  70. }
  71. #if 0
  72. if(val==-255){
  73. QJniObject::callStaticMethod<void>("GetUsbPermission",
  74. "turnScreenOff",
  75. "()V");
  76. }
  77. #endif
  78. }
  79. Q_INVOKABLE QString getId(){
  80. return param::imei;
  81. }
  82. Q_INVOKABLE QString getIccid(){
  83. return param::iccid;
  84. }
  85. Q_INVOKABLE QString getlat(){
  86. return param::gps_lat;
  87. }
  88. Q_INVOKABLE QString getlng(){
  89. return param::gps_lng;
  90. }
  91. Q_INVOKABLE QString get_app_version(){
  92. return APP_VERSION;
  93. }
  94. signals:
  95. void dataReceived(const QByteArray &data);
  96. void dataUpload(const QByteArray &data);
  97. void updatestart(QString filename);
  98. void auto_takephoto(bool sw);
  99. void takephotodone();
  100. void disconnect_485();
  101. void autotestdone();
  102. void translatedone();
  103. void netMessage(const QByteArray &data);
  104. void autoFocusDone();
  105. void autoFocusStart();
  106. void signal_movy();
  107. void timeout_mov_stop();
  108. void gpsSuccess();
  109. public slots:
  110. void sendData(const QByteArray &data);
  111. void mqtttoserial(const QByteArray &data);
  112. void getmodemresult(int res);
  113. private slots:
  114. //void readBoardData();
  115. private:
  116. void processJsonData(QByteArray &jsonData);
  117. QTimer m_dat_f;
  118. QTimer* timeout_mov = nullptr;
  119. QSerialPort m_serialPort;
  120. SqliteOperator *sqlConfig;
  121. bool m_running;
  122. bool data_upload;
  123. int connect_board; //主板串口连接状态
  124. JsonFunc m_json;
  125. QString qst_dirPath;
  126. int int_fileCount;
  127. qint64 int_totalSize;
  128. // 用于存储目录统计结果的结构体
  129. struct DirectoryStats {
  130. qint64 totalSize = 0;
  131. int fileCount = 0;
  132. };
  133. void deleteAllFilesInDirectory(const QString &directoryPath)
  134. {
  135. QDir directory(directoryPath);
  136. if (!directory.exists()) {
  137. qDebug() << "Directory does not exist.";
  138. return;
  139. }
  140. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  141. foreach (const QFileInfo &fileInfo, fileInfoList) {
  142. if (fileInfo.isDir()) {
  143. // 递归删除子目录中的文件
  144. deleteAllFilesInDirectory(fileInfo.filePath());
  145. } else {
  146. // 删除文件
  147. QFile::remove(fileInfo.filePath());
  148. }
  149. }
  150. analyzeDirectory(directoryPath);
  151. }
  152. // 递归获取目录统计信息
  153. DirectoryStats getDirectoryStats(const QString &directoryPath)
  154. {
  155. QDir directory(directoryPath);
  156. DirectoryStats stats;
  157. if (!directory.exists()) {
  158. qDebug() << "Directory does not exist.";
  159. }
  160. qint64 totalSize = 0;
  161. int fileCount = 0;
  162. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  163. foreach (const QFileInfo &fileInfo, fileInfoList) {
  164. if (fileInfo.isDir()) {
  165. // 递归获取子目录的统计信息
  166. DirectoryStats subStats = getDirectoryStats(fileInfo.filePath());
  167. stats.fileCount += subStats.fileCount;
  168. stats.totalSize += subStats.totalSize;
  169. } else {
  170. stats.fileCount++;
  171. stats.totalSize += fileInfo.size();
  172. }
  173. }
  174. return stats;
  175. qst_dirPath = directoryPath;
  176. int_fileCount = fileCount;
  177. int_totalSize = totalSize;
  178. LOG("Directory:{}",directoryPath.toStdString());
  179. LOG("File count:{}",fileCount);
  180. LOG("Total size:{} bytes",totalSize);
  181. }
  182. // 使用示例
  183. void analyzeDirectory(const QString &directoryPath) {
  184. qDebug() << "Analyzing directory:" << directoryPath;
  185. DirectoryStats stats = getDirectoryStats(directoryPath);
  186. qst_dirPath = directoryPath;
  187. int_fileCount = stats.fileCount;
  188. int_totalSize = stats.totalSize;
  189. LOG("Total files:{}",int_totalSize);
  190. LOG("Total size:{} MB",stats.totalSize / (1024.0 * 1024.0));
  191. }
  192. };
  193. class Serial_AT : public QSerialPort
  194. {
  195. Q_OBJECT
  196. public:
  197. explicit Serial_AT(QObject *parent = 0);
  198. QTimer *m_5s;
  199. // 发送字节数
  200. int txByte = 0;
  201. // 接收字节数
  202. int rxByte = 0;
  203. // 数据缓存区
  204. QByteArray dataBuf;
  205. // 串口描述内容
  206. QStringList serialDescription;
  207. // 串口扫描函数
  208. QStringList serialScan(void);
  209. bool serialOpen(QString serialName, int baudRate);
  210. void sendData(QByteArray sendData);
  211. void serialClose();
  212. private slots:
  213. void ReadData();
  214. private:
  215. QTextCodec *tc;
  216. QString at_str;
  217. QTimer* timeout_recv = nullptr;
  218. signals:
  219. void readSignal(int recByte);
  220. void sendSuccess(int sendByte);
  221. void imeiDone();
  222. void iccidDone();
  223. };
  224. #endif // MSEIAL_H