mserial.h 9.3 KB

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