mserial.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 <QThread>
  10. #include <m_json.h>
  11. #include <QTimer>
  12. #include <QStandardPaths>
  13. #include <QVideoFrame>
  14. #include <mysql.h>
  15. #include <m_loger.h>
  16. #include "lwjson/lwjson.h"
  17. #include "ymodem.h"
  18. class SerialThread: public QThread
  19. {
  20. Q_OBJECT
  21. public:
  22. // 数据缓存区
  23. QByteArray dataBuf;
  24. YMODEM* getYmodem(); // 声明 getter
  25. explicit SerialThread(QObject *parent = nullptr);
  26. ~SerialThread();
  27. void setSerialPortSettings(const QString &portName, QSerialPort::BaudRate baudRate,
  28. QSerialPort::DataBits dataBits, QSerialPort::Parity parity,
  29. QSerialPort::StopBits stopBits, QSerialPort::FlowControl flowControl);
  30. void init();
  31. void run() override;
  32. void stop();
  33. void SetLedState(bool sw){
  34. sendData(m_json.setLedState(sw));
  35. }
  36. Q_INVOKABLE void reboot(){
  37. sendData(m_json.CmdReboot());
  38. }
  39. QSerialPort& getSerialPort() {
  40. return m_serialPort;
  41. }
  42. Q_INVOKABLE void readBoardData();
  43. Q_INVOKABLE void uploadData_Mqtt(){
  44. m_dat_f.setInterval(0);
  45. };
  46. Q_INVOKABLE void update_dat_f_main(QString freq); //更新数据上传间隔
  47. Q_INVOKABLE void sendCmd(QString cmd_type,bool sw,QString setup,QString speed);
  48. Q_INVOKABLE QStringList pathinfo(){
  49. QStringList pf_pic;
  50. pf_pic.append(qst_dirPath);
  51. pf_pic.append(QString::number(int_fileCount));
  52. pf_pic.append(QString::number(int_totalSize/1048576));
  53. return pf_pic;
  54. }
  55. Q_INVOKABLE void readPathinfo(){
  56. analyzeDirectory(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/Pictures");
  57. }
  58. Q_INVOKABLE void removepic(){
  59. deleteAllFilesInDirectory(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/Pictures");
  60. }
  61. Q_INVOKABLE void setCollt(int sh1 ,int eh1,int sh2,int eh2,int sh3,int eh3){
  62. sendData(m_json.CmdParamconf(sh1,eh1,sh2,eh2,sh3,eh3));
  63. }
  64. Q_INVOKABLE void setHt_xy_dis(int xdis ,int ydis){
  65. sendData(m_json.CmdParamconf_xy_dis(xdis,ydis));
  66. }
  67. Q_INVOKABLE void setLightness(int val){
  68. auto qtContext = QJniObject(QNativeInterface::QAndroidApplication::context()); //activity
  69. if (qtContext.isValid()) {
  70. QJniObject jClass("com/yfkj/bzyv1/Backlight");
  71. jClass.callMethod<void>("setScreenLight","(Landroid/content/Context;I)V",qtContext.object(),val);
  72. //jClass.callStaticMethod<void>("turnScreenOff", "()V");
  73. }
  74. #if 0
  75. if(val==-255){
  76. QJniObject::callStaticMethod<void>("GetUsbPermission",
  77. "turnScreenOff",
  78. "()V");
  79. }
  80. #endif
  81. }
  82. Q_INVOKABLE QString getId(){
  83. return param::imei;
  84. }
  85. Q_INVOKABLE QString getIccid(){
  86. return param::iccid;
  87. }
  88. Q_INVOKABLE QString getlat(){
  89. return param::gps_lat;
  90. }
  91. Q_INVOKABLE QString getlng(){
  92. return param::gps_lng;
  93. }
  94. Q_INVOKABLE QString get_app_version(){
  95. return APP_VERSION;
  96. }
  97. signals:
  98. void dataReceived(const QByteArray &data);
  99. void dataUpload(const QByteArray &data);
  100. void updatestart(QString filename);
  101. void auto_takephoto(bool sw);
  102. void takephotodone();
  103. void disconnect_485();
  104. void autotestdone();
  105. void translatedone();
  106. void netMessage(const QByteArray &data);
  107. void autoFocusDone();
  108. void autoFocusStart();
  109. void lwjsonStreamDONE(const QByteArray &data);
  110. void signal_movy(const QString stepcount);
  111. void timeout_mov_stop();
  112. void gpsSuccess();
  113. void ymodemReady(QString mtype);
  114. void ymodemProgress(int val,int tval);
  115. void ymodemFinished(int res);
  116. public slots:
  117. void sendData(const QByteArray &data);
  118. void mqtttoserial(const QByteArray &data);
  119. void getmodemresult(int res);
  120. void startYmodem(const QString path);
  121. void startUpgrade(QString path)
  122. {
  123. if(m_ymodem){
  124. m_upgrading = true;
  125. m_ymodem->start_ymodem(path);
  126. }
  127. }
  128. void recoverMcuParam();
  129. private slots:
  130. //void readBoardData();
  131. QString selectSerialPortName();
  132. void feedJsonParser(const QByteArray& data)
  133. {
  134. dataBuf.append(data);
  135. for (uint8_t b : data) {
  136. lwjsonr_t ret = lwjson_stream_parse(&lwparser, b);
  137. switch (ret){
  138. case lwjsonERRJSON: // 1, 无效的字符
  139. case lwjsonSTREAMWAITFIRSTCHAR: // 2, 等待首字符
  140. break;
  141. case lwjsonSTREAMINPROG: // 3, 正在解析中
  142. break;
  143. case lwjsonSTREAMDONE: // 4, 解析已完成
  144. LOG("lwjsonSTREAMDONE:{}",dataBuf.constData());
  145. emit lwjsonStreamDONE(dataBuf);
  146. dataBuf.clear();
  147. lwjson_stream_reset(&lwparser);
  148. break;
  149. case lwjsonERRMEM: // 5, 嵌套的太深("LWJSON_CFG_STREAM_STACK_SIZE")
  150. ERROR("lwjsonERRMEM!");
  151. lwjson_stream_reset(&lwparser);
  152. break;
  153. default: // 流式解析时, 其它的返回值, 都不应该出现
  154. ERROR("未知错误:{:d}",static_cast<int>(ret));
  155. break;
  156. }
  157. }
  158. }
  159. private:
  160. bool m_upgrading = false;
  161. void processJsonData(const QByteArray &jsonData);
  162. void saveMcuParam(const QJsonObject &params);
  163. QTimer m_dat_f;
  164. QTimer* timeout_mov = nullptr;
  165. QSerialPort m_serialPort;
  166. YMODEM *m_ymodem; // YMODEM对象
  167. SqliteOperator *sqlConfig;
  168. mutable QMutex m_writeMutex;
  169. bool m_running;
  170. bool data_upload;
  171. int connect_board; //主板串口连接状态
  172. JsonFunc m_json;
  173. QString qst_dirPath;
  174. int int_fileCount;
  175. qint64 int_totalSize;
  176. // 用于存储目录统计结果的结构体
  177. struct DirectoryStats {
  178. qint64 totalSize = 0;
  179. int fileCount = 0;
  180. };
  181. lwjson_stream_parser_t lwparser;
  182. void deleteAllFilesInDirectory(const QString &directoryPath)
  183. {
  184. QDir directory(directoryPath);
  185. if (!directory.exists()) {
  186. qDebug() << "Directory does not exist.";
  187. return;
  188. }
  189. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  190. foreach (const QFileInfo &fileInfo, fileInfoList) {
  191. if (fileInfo.isDir()) {
  192. // 递归删除子目录中的文件
  193. deleteAllFilesInDirectory(fileInfo.filePath());
  194. } else {
  195. // 删除文件
  196. QFile::remove(fileInfo.filePath());
  197. }
  198. }
  199. analyzeDirectory(directoryPath);
  200. }
  201. // 递归获取目录统计信息
  202. DirectoryStats getDirectoryStats(const QString &directoryPath)
  203. {
  204. QDir directory(directoryPath);
  205. DirectoryStats stats;
  206. if (!directory.exists()) {
  207. qDebug() << "Directory does not exist.";
  208. }
  209. qint64 totalSize = 0;
  210. int fileCount = 0;
  211. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  212. foreach (const QFileInfo &fileInfo, fileInfoList) {
  213. if (fileInfo.isDir()) {
  214. // 递归获取子目录的统计信息
  215. DirectoryStats subStats = getDirectoryStats(fileInfo.filePath());
  216. stats.fileCount += subStats.fileCount;
  217. stats.totalSize += subStats.totalSize;
  218. } else {
  219. stats.fileCount++;
  220. stats.totalSize += fileInfo.size();
  221. }
  222. }
  223. return stats;
  224. qst_dirPath = directoryPath;
  225. int_fileCount = fileCount;
  226. int_totalSize = totalSize;
  227. LOG("Directory:{}",directoryPath.toStdString());
  228. LOG("File count:{}",fileCount);
  229. LOG("Total size:{} bytes",totalSize);
  230. }
  231. // 使用示例
  232. void analyzeDirectory(const QString &directoryPath) {
  233. qDebug() << "Analyzing directory:" << directoryPath;
  234. DirectoryStats stats = getDirectoryStats(directoryPath);
  235. qst_dirPath = directoryPath;
  236. int_fileCount = stats.fileCount;
  237. int_totalSize = stats.totalSize;
  238. LOG("Total files:{}",int_totalSize);
  239. LOG("Total size:{} MB",stats.totalSize / (1024.0 * 1024.0));
  240. }
  241. };
  242. #endif // MSEIAL_H