mserial.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. Q_INVOKABLE void makeJsonRpc(const QString &method,const QString &value)
  104. {
  105. sendData(m_json.MakeJsonRPC(method,value));
  106. }
  107. signals:
  108. void dataReceived(const QByteArray &data);
  109. void dataUpload(const QByteArray &data);
  110. void updatestart(QString filename);
  111. void auto_takephoto(bool sw);
  112. void takephotodone();
  113. void disconnect_485();
  114. void autotestdone();
  115. void translatedone();
  116. void netMessage(const QByteArray &data);
  117. void autoFocusDone();
  118. void autoFocusStart();
  119. void lwjsonStreamDONE(const QByteArray &data);
  120. void signal_movy(const QString stepcount);
  121. void timeout_mov_stop();
  122. void gpsSuccess();
  123. void ymodemReady(QString mtype);
  124. void ymodemProgress(int val,int tval);
  125. void ymodemFinished(int res);
  126. public slots:
  127. void sendData(const QByteArray &data);
  128. void mqtttoserial(const QByteArray &data);
  129. void getmodemresult(int res);
  130. void startYmodem(const QString path);
  131. void startUpgrade(QString path)
  132. {
  133. if(m_ymodem){
  134. m_upgrading = true;
  135. m_ymodem->start_ymodem(path);
  136. }
  137. }
  138. Q_INVOKABLE void recoverMcuParam();
  139. private slots:
  140. //void readBoardData();
  141. QString selectSerialPortName();
  142. void feedJsonParser(const QByteArray& data)
  143. {
  144. dataBuf.append(data);
  145. for (uint8_t b : data) {
  146. lwjsonr_t ret = lwjson_stream_parse(&lwparser, b);
  147. switch (ret){
  148. case lwjsonERRJSON: // 1, 无效的字符
  149. case lwjsonSTREAMWAITFIRSTCHAR: // 2, 等待首字符
  150. break;
  151. case lwjsonSTREAMINPROG: // 3, 正在解析中
  152. break;
  153. case lwjsonSTREAMDONE: // 4, 解析已完成
  154. LOG("lwjsonSTREAMDONE:{}",dataBuf.constData());
  155. emit lwjsonStreamDONE(dataBuf);
  156. dataBuf.clear();
  157. lwjson_stream_reset(&lwparser);
  158. break;
  159. case lwjsonERRMEM: // 5, 嵌套的太深("LWJSON_CFG_STREAM_STACK_SIZE")
  160. ERROR("lwjsonERRMEM!");
  161. lwjson_stream_reset(&lwparser);
  162. break;
  163. default: // 流式解析时, 其它的返回值, 都不应该出现
  164. ERROR("未知错误:{:d}",static_cast<int>(ret));
  165. break;
  166. }
  167. }
  168. }
  169. private:
  170. bool m_upgrading = false;
  171. void processJsonData(const QByteArray &jsonData);
  172. void saveMcuParam(const QJsonObject &params);
  173. QTimer m_dat_f;
  174. QTimer* timeout_mov = nullptr;
  175. QSerialPort m_serialPort;
  176. YMODEM *m_ymodem; // YMODEM对象
  177. SqliteOperator *sqlConfig;
  178. mutable QMutex m_writeMutex;
  179. bool m_running;
  180. bool data_upload;
  181. int connect_board; //主板串口连接状态
  182. JsonFunc m_json;
  183. QString qst_dirPath;
  184. int int_fileCount;
  185. qint64 int_totalSize;
  186. // 用于存储目录统计结果的结构体
  187. struct DirectoryStats {
  188. qint64 totalSize = 0;
  189. int fileCount = 0;
  190. };
  191. lwjson_stream_parser_t lwparser;
  192. void deleteAllFilesInDirectory(const QString &directoryPath)
  193. {
  194. QDir directory(directoryPath);
  195. if (!directory.exists()) {
  196. qDebug() << "Directory does not exist.";
  197. return;
  198. }
  199. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  200. foreach (const QFileInfo &fileInfo, fileInfoList) {
  201. if (fileInfo.isDir()) {
  202. // 递归删除子目录中的文件
  203. deleteAllFilesInDirectory(fileInfo.filePath());
  204. } else {
  205. // 删除文件
  206. QFile::remove(fileInfo.filePath());
  207. }
  208. }
  209. analyzeDirectory(directoryPath);
  210. }
  211. // 递归获取目录统计信息
  212. DirectoryStats getDirectoryStats(const QString &directoryPath)
  213. {
  214. QDir directory(directoryPath);
  215. DirectoryStats stats;
  216. if (!directory.exists()) {
  217. qDebug() << "Directory does not exist.";
  218. }
  219. qint64 totalSize = 0;
  220. int fileCount = 0;
  221. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  222. foreach (const QFileInfo &fileInfo, fileInfoList) {
  223. if (fileInfo.isDir()) {
  224. // 递归获取子目录的统计信息
  225. DirectoryStats subStats = getDirectoryStats(fileInfo.filePath());
  226. stats.fileCount += subStats.fileCount;
  227. stats.totalSize += subStats.totalSize;
  228. } else {
  229. stats.fileCount++;
  230. stats.totalSize += fileInfo.size();
  231. }
  232. }
  233. return stats;
  234. qst_dirPath = directoryPath;
  235. int_fileCount = fileCount;
  236. int_totalSize = totalSize;
  237. LOG("Directory:{}",directoryPath.toStdString());
  238. LOG("File count:{}",fileCount);
  239. LOG("Total size:{} bytes",totalSize);
  240. }
  241. // 使用示例
  242. void analyzeDirectory(const QString &directoryPath) {
  243. qDebug() << "Analyzing directory:" << directoryPath;
  244. DirectoryStats stats = getDirectoryStats(directoryPath);
  245. qst_dirPath = directoryPath;
  246. int_fileCount = stats.fileCount;
  247. int_totalSize = stats.totalSize;
  248. LOG("Total files:{}",int_totalSize);
  249. LOG("Total size:{} MB",stats.totalSize / (1024.0 * 1024.0));
  250. }
  251. };
  252. #endif // MSEIAL_H