mserial.h 9.0 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 <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/cb/Q6iot/Backlight");
  69. jClass.callMethod<void>("setScreenLight","(Landroid/content/Context;I)V",qtContext.object(),val);
  70. //jClass.callStaticMethod<void>("turnScreenOff", "()V");
  71. }
  72. #if 0
  73. if(val==-255){
  74. QJniObject::callStaticMethod<void>("GetUsbPermission",
  75. "turnScreenOff",
  76. "()V");
  77. }
  78. #endif
  79. }
  80. Q_INVOKABLE QString getId(){
  81. return param::imei;
  82. }
  83. Q_INVOKABLE QString getIccid(){
  84. return param::iccid;
  85. }
  86. Q_INVOKABLE QString getlat(){
  87. return param::gps_lat;
  88. }
  89. Q_INVOKABLE QString getlng(){
  90. return param::gps_lng;
  91. }
  92. Q_INVOKABLE QString get_app_version(){
  93. return APP_VERSION;
  94. }
  95. signals:
  96. void dataReceived(const QByteArray &data);
  97. void dataUpload(const QByteArray &data);
  98. void updatestart(QString filename);
  99. void auto_takephoto(bool sw);
  100. void takephotodone();
  101. void disconnect_485();
  102. void autotestdone();
  103. void translatedone();
  104. void netMessage(const QByteArray &data);
  105. void autoFocusDone();
  106. void autoFocusStart();
  107. void lwjsonStreamDONE(const QByteArray &data);
  108. void signal_movy();
  109. void timeout_mov_stop();
  110. void gpsSuccess();
  111. public slots:
  112. void sendData(const QByteArray &data);
  113. void mqtttoserial(const QByteArray &data);
  114. void getmodemresult(int res);
  115. private slots:
  116. //void readBoardData();
  117. void feedJsonParser(const QByteArray& data)
  118. {
  119. dataBuf.append(data);
  120. for (uint8_t b : data) {
  121. lwjsonr_t ret = lwjson_stream_parse(&lwparser, b);
  122. switch (ret){
  123. case lwjsonERRJSON: // 1, 无效的字符
  124. case lwjsonSTREAMWAITFIRSTCHAR: // 2, 等待首字符
  125. break;
  126. case lwjsonSTREAMINPROG: // 3, 正在解析中
  127. break;
  128. case lwjsonSTREAMDONE: // 4, 解析已完成
  129. if(dataBuf.contains("movy")){
  130. dataBuf.clear();
  131. emit timeout_mov_stop();
  132. emit signal_movy();
  133. }else{
  134. emit lwjsonStreamDONE(dataBuf);
  135. }
  136. lwjson_stream_reset(&lwparser);
  137. break;
  138. case lwjsonERRMEM: // 5, 嵌套的太深("LWJSON_CFG_STREAM_STACK_SIZE")
  139. ERROR("lwjsonERRMEM!");
  140. lwjson_stream_reset(&lwparser);
  141. break;
  142. default: // 流式解析时, 其它的返回值, 都不应该出现
  143. ERROR("未知错误:{}",ret);
  144. break;
  145. }
  146. }
  147. }
  148. private:
  149. void processJsonData(const QByteArray &jsonData);
  150. QTimer m_dat_f;
  151. QTimer* timeout_mov = nullptr;
  152. QSerialPort m_serialPort;
  153. SqliteOperator *sqlConfig;
  154. bool m_running;
  155. bool data_upload;
  156. int connect_board; //主板串口连接状态
  157. JsonFunc m_json;
  158. QString qst_dirPath;
  159. int int_fileCount;
  160. qint64 int_totalSize;
  161. // 用于存储目录统计结果的结构体
  162. struct DirectoryStats {
  163. qint64 totalSize = 0;
  164. int fileCount = 0;
  165. };
  166. lwjson_stream_parser_t lwparser;
  167. void deleteAllFilesInDirectory(const QString &directoryPath)
  168. {
  169. QDir directory(directoryPath);
  170. if (!directory.exists()) {
  171. qDebug() << "Directory does not exist.";
  172. return;
  173. }
  174. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  175. foreach (const QFileInfo &fileInfo, fileInfoList) {
  176. if (fileInfo.isDir()) {
  177. // 递归删除子目录中的文件
  178. deleteAllFilesInDirectory(fileInfo.filePath());
  179. } else {
  180. // 删除文件
  181. QFile::remove(fileInfo.filePath());
  182. }
  183. }
  184. analyzeDirectory(directoryPath);
  185. }
  186. // 递归获取目录统计信息
  187. DirectoryStats getDirectoryStats(const QString &directoryPath)
  188. {
  189. QDir directory(directoryPath);
  190. DirectoryStats stats;
  191. if (!directory.exists()) {
  192. qDebug() << "Directory does not exist.";
  193. }
  194. qint64 totalSize = 0;
  195. int fileCount = 0;
  196. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  197. foreach (const QFileInfo &fileInfo, fileInfoList) {
  198. if (fileInfo.isDir()) {
  199. // 递归获取子目录的统计信息
  200. DirectoryStats subStats = getDirectoryStats(fileInfo.filePath());
  201. stats.fileCount += subStats.fileCount;
  202. stats.totalSize += subStats.totalSize;
  203. } else {
  204. stats.fileCount++;
  205. stats.totalSize += fileInfo.size();
  206. }
  207. }
  208. return stats;
  209. qst_dirPath = directoryPath;
  210. int_fileCount = fileCount;
  211. int_totalSize = totalSize;
  212. LOG("Directory:{}",directoryPath.toStdString());
  213. LOG("File count:{}",fileCount);
  214. LOG("Total size:{} bytes",totalSize);
  215. }
  216. // 使用示例
  217. void analyzeDirectory(const QString &directoryPath) {
  218. qDebug() << "Analyzing directory:" << directoryPath;
  219. DirectoryStats stats = getDirectoryStats(directoryPath);
  220. qst_dirPath = directoryPath;
  221. int_fileCount = stats.fileCount;
  222. int_totalSize = stats.totalSize;
  223. LOG("Total files:{}",int_totalSize);
  224. LOG("Total size:{} MB",stats.totalSize / (1024.0 * 1024.0));
  225. }
  226. };
  227. class Serial_AT : public QSerialPort
  228. {
  229. Q_OBJECT
  230. public:
  231. explicit Serial_AT(QObject *parent = 0);
  232. QTimer *m_5s;
  233. // 发送字节数
  234. int txByte = 0;
  235. // 接收字节数
  236. int rxByte = 0;
  237. // 数据缓存区
  238. QByteArray dataBuf;
  239. // 串口描述内容
  240. QStringList serialDescription;
  241. // 串口扫描函数
  242. QStringList serialScan(void);
  243. bool serialOpen(QString serialName, int baudRate);
  244. void sendData(QByteArray sendData);
  245. void serialClose();
  246. private slots:
  247. void ReadData();
  248. private:
  249. QTextCodec *tc;
  250. QString at_str;
  251. QTimer* timeout_recv = nullptr;
  252. signals:
  253. void readSignal(int recByte);
  254. void sendSuccess(int sendByte);
  255. void imeiDone();
  256. void iccidDone();
  257. };
  258. #endif // MSEIAL_H