mserial.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #include "lwjson/lwjson.h"
  18. class SerialThread: public QThread
  19. {
  20. Q_OBJECT
  21. public:
  22. // 数据缓存区
  23. QByteArray dataBuf;
  24. explicit SerialThread(QObject *parent = nullptr);
  25. ~SerialThread();
  26. void setSerialPortSettings(const QString &portName, QSerialPort::BaudRate baudRate,
  27. QSerialPort::DataBits dataBits, QSerialPort::Parity parity,
  28. QSerialPort::StopBits stopBits, QSerialPort::FlowControl flowControl);
  29. void run() override;
  30. void stop();
  31. void SetLedState(bool sw){
  32. sendData(m_json.setLedState(sw));
  33. }
  34. Q_INVOKABLE void reboot(){
  35. sendData(m_json.CmdReboot());
  36. }
  37. QSerialPort& getSerialPort() {
  38. return m_serialPort;
  39. }
  40. Q_INVOKABLE void readBoardData();
  41. Q_INVOKABLE void uploadData_Mqtt(){
  42. m_dat_f.setInterval(0);
  43. };
  44. Q_INVOKABLE void update_dat_f_main(QString freq); //更新数据上传间隔
  45. Q_INVOKABLE void sendCmd(QString cmd_type,bool sw,QString setup,QString speed);
  46. Q_INVOKABLE QStringList pathinfo(){
  47. QStringList pf_pic;
  48. pf_pic.append(qst_dirPath);
  49. pf_pic.append(QString::number(int_fileCount));
  50. pf_pic.append(QString::number(int_totalSize/1048576));
  51. return pf_pic;
  52. }
  53. Q_INVOKABLE void readPathinfo(){
  54. analyzeDirectory(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
  55. }
  56. Q_INVOKABLE void removepic(){
  57. deleteAllFilesInDirectory(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
  58. }
  59. Q_INVOKABLE void setCollt(int sh1 ,int eh1,int sh2,int eh2,int sh3,int eh3){
  60. sendData(m_json.CmdParamconf(sh1,eh1,sh2,eh2,sh3,eh3));
  61. }
  62. Q_INVOKABLE void setHt_xy_dis(int xdis ,int ydis){
  63. sendData(m_json.CmdParamconf_xy_dis(xdis,ydis));
  64. }
  65. Q_INVOKABLE void setLightness(int val){
  66. auto qtContext = QJniObject(QNativeInterface::QAndroidApplication::context()); //activity
  67. if (qtContext.isValid()) {
  68. QJniObject jClass("com/yfkj/bzyv1/Backlight");
  69. jClass.callMethod<void>("setScreenLight","(Landroid/content/Context;I)V",qtContext.object(),val);
  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 lwjsonStreamDONE(const QByteArray &data);
  107. void signal_movy(const QString stepcount);
  108. void timeout_mov_stop();
  109. void gpsSuccess();
  110. public slots:
  111. void sendData(const QByteArray &data);
  112. void mqtttoserial(const QByteArray &data);
  113. void getmodemresult(int res);
  114. private slots:
  115. //void readBoardData();
  116. void feedJsonParser(const QByteArray& data)
  117. {
  118. dataBuf.append(data);
  119. for (uint8_t b : data) {
  120. lwjsonr_t ret = lwjson_stream_parse(&lwparser, b);
  121. switch (ret){
  122. case lwjsonERRJSON: // 1, 无效的字符
  123. case lwjsonSTREAMWAITFIRSTCHAR: // 2, 等待首字符
  124. break;
  125. case lwjsonSTREAMINPROG: // 3, 正在解析中
  126. break;
  127. case lwjsonSTREAMDONE: // 4, 解析已完成
  128. LOG("lwjsonSTREAMDONE:{}",dataBuf.constData());
  129. emit lwjsonStreamDONE(dataBuf);
  130. dataBuf.clear();
  131. lwjson_stream_reset(&lwparser);
  132. break;
  133. case lwjsonERRMEM: // 5, 嵌套的太深("LWJSON_CFG_STREAM_STACK_SIZE")
  134. ERROR("lwjsonERRMEM!");
  135. lwjson_stream_reset(&lwparser);
  136. break;
  137. default: // 流式解析时, 其它的返回值, 都不应该出现
  138. ERROR("未知错误:%d",(int)ret);
  139. break;
  140. }
  141. }
  142. }
  143. private:
  144. void processJsonData(const QByteArray &jsonData);
  145. QTimer m_dat_f;
  146. QTimer* timeout_mov = nullptr;
  147. QSerialPort m_serialPort;
  148. SqliteOperator *sqlConfig;
  149. bool m_running;
  150. bool data_upload;
  151. int connect_board; //主板串口连接状态
  152. JsonFunc m_json;
  153. QString qst_dirPath;
  154. int int_fileCount;
  155. qint64 int_totalSize;
  156. // 用于存储目录统计结果的结构体
  157. struct DirectoryStats {
  158. qint64 totalSize = 0;
  159. int fileCount = 0;
  160. };
  161. lwjson_stream_parser_t lwparser;
  162. void deleteAllFilesInDirectory(const QString &directoryPath)
  163. {
  164. QDir directory(directoryPath);
  165. if (!directory.exists()) {
  166. qDebug() << "Directory does not exist.";
  167. return;
  168. }
  169. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  170. foreach (const QFileInfo &fileInfo, fileInfoList) {
  171. if (fileInfo.isDir()) {
  172. // 递归删除子目录中的文件
  173. deleteAllFilesInDirectory(fileInfo.filePath());
  174. } else {
  175. // 删除文件
  176. QFile::remove(fileInfo.filePath());
  177. }
  178. }
  179. analyzeDirectory(directoryPath);
  180. }
  181. // 递归获取目录统计信息
  182. DirectoryStats getDirectoryStats(const QString &directoryPath)
  183. {
  184. QDir directory(directoryPath);
  185. DirectoryStats stats;
  186. if (!directory.exists()) {
  187. qDebug() << "Directory does not exist.";
  188. }
  189. qint64 totalSize = 0;
  190. int fileCount = 0;
  191. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  192. foreach (const QFileInfo &fileInfo, fileInfoList) {
  193. if (fileInfo.isDir()) {
  194. // 递归获取子目录的统计信息
  195. DirectoryStats subStats = getDirectoryStats(fileInfo.filePath());
  196. stats.fileCount += subStats.fileCount;
  197. stats.totalSize += subStats.totalSize;
  198. } else {
  199. stats.fileCount++;
  200. stats.totalSize += fileInfo.size();
  201. }
  202. }
  203. return stats;
  204. qst_dirPath = directoryPath;
  205. int_fileCount = fileCount;
  206. int_totalSize = totalSize;
  207. LOG("Directory:{}",directoryPath.toStdString());
  208. LOG("File count:{}",fileCount);
  209. LOG("Total size:{} bytes",totalSize);
  210. }
  211. // 使用示例
  212. void analyzeDirectory(const QString &directoryPath) {
  213. qDebug() << "Analyzing directory:" << directoryPath;
  214. DirectoryStats stats = getDirectoryStats(directoryPath);
  215. qst_dirPath = directoryPath;
  216. int_fileCount = stats.fileCount;
  217. int_totalSize = stats.totalSize;
  218. LOG("Total files:{}",int_totalSize);
  219. LOG("Total size:{} MB",stats.totalSize / (1024.0 * 1024.0));
  220. }
  221. };
  222. class Serial_AT : public QSerialPort
  223. {
  224. Q_OBJECT
  225. public:
  226. explicit Serial_AT(QObject *parent = 0);
  227. QTimer *m_5s;
  228. // 发送字节数
  229. int txByte = 0;
  230. // 接收字节数
  231. int rxByte = 0;
  232. // 数据缓存区
  233. QByteArray dataBuf;
  234. // 串口描述内容
  235. QStringList serialDescription;
  236. // 串口扫描函数
  237. QStringList serialScan(void);
  238. bool serialOpen(QString serialName, int baudRate);
  239. void sendData(QByteArray sendData);
  240. void serialClose();
  241. private slots:
  242. void ReadData();
  243. private:
  244. QTextCodec *tc;
  245. QString at_str;
  246. QTimer* timeout_recv = nullptr;
  247. signals:
  248. void readSignal(int recByte);
  249. void sendSuccess(int sendByte);
  250. void imeiDone();
  251. void iccidDone();
  252. };
  253. #endif // MSEIAL_H