mserial.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 setCollt_min(int sh1 ,int eh1,int sh2,int eh2,int sh3,int eh3){
  65. sendData(m_json.CmdParamconf_min(sh1,eh1,sh2,eh2,sh3,eh3));
  66. }
  67. Q_INVOKABLE void setTempconf(int tpl ,int tph){
  68. sendData(m_json.CmdParamconf_temp(tpl,tph));
  69. }
  70. Q_INVOKABLE void setHt_xy_dis(int xdis ,int ydis){
  71. sendData(m_json.CmdParamconf_xy_dis(xdis,ydis));
  72. }
  73. Q_INVOKABLE void setLightness(int val){
  74. auto qtContext = QJniObject(QNativeInterface::QAndroidApplication::context()); //activity
  75. if (qtContext.isValid()) {
  76. QJniObject jClass("com/yfkj/bzyv1/Backlight");
  77. jClass.callMethod<void>("setScreenLight","(Landroid/content/Context;I)V",qtContext.object(),val);
  78. //jClass.callStaticMethod<void>("turnScreenOff", "()V");
  79. }
  80. #if 0
  81. if(val==-255){
  82. QJniObject::callStaticMethod<void>("GetUsbPermission",
  83. "turnScreenOff",
  84. "()V");
  85. }
  86. #endif
  87. }
  88. Q_INVOKABLE QString getId(){
  89. return param::imei;
  90. }
  91. Q_INVOKABLE QString getIccid(){
  92. return param::iccid;
  93. }
  94. Q_INVOKABLE QString getlat(){
  95. return param::gps_lat;
  96. }
  97. Q_INVOKABLE QString getlng(){
  98. return param::gps_lng;
  99. }
  100. Q_INVOKABLE QString get_app_version(){
  101. return APP_VERSION;
  102. }
  103. signals:
  104. void dataReceived(const QByteArray &data);
  105. void dataUpload(const QByteArray &data);
  106. void updatestart(QString filename);
  107. void auto_takephoto(bool sw);
  108. void takephotodone();
  109. void disconnect_485();
  110. void autotestdone();
  111. void translatedone();
  112. void netMessage(const QByteArray &data);
  113. void autoFocusDone();
  114. void autoFocusStart();
  115. void lwjsonStreamDONE(const QByteArray &data);
  116. void signal_movy(const QString stepcount);
  117. void timeout_mov_stop();
  118. void gpsSuccess();
  119. void ymodemReady(QString mtype);
  120. void ymodemProgress(int val,int tval);
  121. void ymodemFinished(int res);
  122. public slots:
  123. void sendData(const QByteArray &data);
  124. void mqtttoserial(const QByteArray &data);
  125. void getmodemresult(int res);
  126. void startYmodem(const QString path);
  127. void startUpgrade(QString path)
  128. {
  129. if(m_ymodem){
  130. m_upgrading = true;
  131. m_ymodem->start_ymodem(path);
  132. }
  133. }
  134. void recoverMcuParam();
  135. private slots:
  136. //void readBoardData();
  137. QString selectSerialPortName();
  138. void feedJsonParser(const QByteArray& data)
  139. {
  140. dataBuf.append(data);
  141. for (uint8_t b : data) {
  142. lwjsonr_t ret = lwjson_stream_parse(&lwparser, b);
  143. switch (ret){
  144. case lwjsonERRJSON: // 1, 无效的字符
  145. case lwjsonSTREAMWAITFIRSTCHAR: // 2, 等待首字符
  146. break;
  147. case lwjsonSTREAMINPROG: // 3, 正在解析中
  148. break;
  149. case lwjsonSTREAMDONE: // 4, 解析已完成
  150. LOG("lwjsonSTREAMDONE:{}",dataBuf.constData());
  151. emit lwjsonStreamDONE(dataBuf);
  152. dataBuf.clear();
  153. lwjson_stream_reset(&lwparser);
  154. break;
  155. case lwjsonERRMEM: // 5, 嵌套的太深("LWJSON_CFG_STREAM_STACK_SIZE")
  156. ERROR("lwjsonERRMEM!");
  157. lwjson_stream_reset(&lwparser);
  158. break;
  159. default: // 流式解析时, 其它的返回值, 都不应该出现
  160. ERROR("未知错误:{:d}",static_cast<int>(ret));
  161. break;
  162. }
  163. }
  164. }
  165. private:
  166. bool m_upgrading = false;
  167. void processJsonData(const QByteArray &jsonData);
  168. void saveMcuParam(const QJsonObject &params);
  169. QTimer m_dat_f;
  170. QTimer* timeout_mov = nullptr;
  171. QSerialPort m_serialPort;
  172. YMODEM *m_ymodem; // YMODEM对象
  173. SqliteOperator *sqlConfig;
  174. mutable QMutex m_writeMutex;
  175. bool m_running;
  176. bool data_upload;
  177. int connect_board; //主板串口连接状态
  178. JsonFunc m_json;
  179. QString qst_dirPath;
  180. int int_fileCount;
  181. qint64 int_totalSize;
  182. // 用于存储目录统计结果的结构体
  183. struct DirectoryStats {
  184. qint64 totalSize = 0;
  185. int fileCount = 0;
  186. };
  187. lwjson_stream_parser_t lwparser;
  188. void deleteAllFilesInDirectory(const QString &directoryPath)
  189. {
  190. QDir directory(directoryPath);
  191. if (!directory.exists()) {
  192. qDebug() << "Directory does not exist.";
  193. return;
  194. }
  195. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  196. foreach (const QFileInfo &fileInfo, fileInfoList) {
  197. if (fileInfo.isDir()) {
  198. // 递归删除子目录中的文件
  199. deleteAllFilesInDirectory(fileInfo.filePath());
  200. } else {
  201. // 删除文件
  202. QFile::remove(fileInfo.filePath());
  203. }
  204. }
  205. analyzeDirectory(directoryPath);
  206. }
  207. // 递归获取目录统计信息
  208. DirectoryStats getDirectoryStats(const QString &directoryPath)
  209. {
  210. QDir directory(directoryPath);
  211. DirectoryStats stats;
  212. if (!directory.exists()) {
  213. qDebug() << "Directory does not exist.";
  214. }
  215. qint64 totalSize = 0;
  216. int fileCount = 0;
  217. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  218. foreach (const QFileInfo &fileInfo, fileInfoList) {
  219. if (fileInfo.isDir()) {
  220. // 递归获取子目录的统计信息
  221. DirectoryStats subStats = getDirectoryStats(fileInfo.filePath());
  222. stats.fileCount += subStats.fileCount;
  223. stats.totalSize += subStats.totalSize;
  224. } else {
  225. stats.fileCount++;
  226. stats.totalSize += fileInfo.size();
  227. }
  228. }
  229. return stats;
  230. qst_dirPath = directoryPath;
  231. int_fileCount = fileCount;
  232. int_totalSize = totalSize;
  233. LOG("Directory:{}",directoryPath.toStdString());
  234. LOG("File count:{}",fileCount);
  235. LOG("Total size:{} bytes",totalSize);
  236. }
  237. // 使用示例
  238. void analyzeDirectory(const QString &directoryPath) {
  239. qDebug() << "Analyzing directory:" << directoryPath;
  240. DirectoryStats stats = getDirectoryStats(directoryPath);
  241. qst_dirPath = directoryPath;
  242. int_fileCount = stats.fileCount;
  243. int_totalSize = stats.totalSize;
  244. LOG("Total files:{}",int_totalSize);
  245. LOG("Total size:{} MB",stats.totalSize / (1024.0 * 1024.0));
  246. }
  247. };
  248. #endif // MSEIAL_H