widget.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #include "widget.h"
  2. #include "./ui_widget.h"
  3. #include <QNetworkReply>
  4. #include <QNetworkAccessManager>
  5. #include <QNetworkRequest>
  6. #include <QTimer>
  7. #include <QNetworkProxy>
  8. #include <iostream>
  9. #include <regex>
  10. #include <stdlib.h>
  11. #include "spdlog/spdlog.h"
  12. #include "LightLog.h"
  13. #include "nlohmann/json.hpp"
  14. #define nJson nlohmann::json
  15. Widget::Widget(QWidget *parent)
  16. : QWidget(parent)
  17. , ui(new Ui::Widget)
  18. {
  19. ui->setupUi(this);
  20. // m_curlFtp.setFtpIPAndPort("192.168.50.100", 21);
  21. // m_curlFtp.setSftpIPAndPort("10.147.18.180", 21);
  22. // m_curlFtp.setUsernameAndPassword("microsoft", "19980714Lq");
  23. // m_curlFtp.setUsernameAndPassword("haxiaoxun@outlook.com", "147258369ztl");
  24. m_curlFtp.setFtpIPAndPort("192.1.2.178", 32021);
  25. m_curlFtp.setUsernameAndPassword("lh", "DWw7V9u0");
  26. // m_curlFtp.setUsernameAndPassword("lh", "DWw7V9u0");
  27. // m_curlFtp.setSftpIPAndPort("192.1.2.49", 32222);
  28. // m_curlFtp.setUsernameAndPassword("lhftp", "8tG!2fP*7bJ@5kQ");
  29. // m_curlFtp.setIgnoreSSLCert(true);
  30. m_curlFtp.enableCurlDebug(true);
  31. SPDLOG_INFO("***** Qt Library *****");
  32. }
  33. Widget::~Widget()
  34. {
  35. delete ui;
  36. }
  37. /* 解析JSON示例 */
  38. void Widget::parseJSON()
  39. {
  40. SPDLOG_INFO("解析JSON");
  41. nJson json0;
  42. json0["name"] = "microsoft";
  43. json0["age"] = 18;
  44. json0["bool"] = true;
  45. json0["null"] = nullptr;
  46. json0["array"] = {1, 2, 3, 4, 5};
  47. json0["object"] = {{"key1", "value1"}, {"key2", "value2"}};
  48. /* 打印json值 */
  49. SPDLOG_INFO("json0: {}", json0.dump());
  50. std::string name = json0["name"];
  51. std::string name2 = json0["name"].get<std::string>();
  52. std::string name1 = json0.at("name").get<std::string>();
  53. std::string name5 = json0.at("name");
  54. std::string name4 = json0.value("name", "default");
  55. int age = json0["age"].get<int>();
  56. bool b = json0["bool"];
  57. nJson null = json0["null"];
  58. nJson array = json0["array"];
  59. nJson object = json0["object"];
  60. /* 如果json中的值是null,在赋值给string时可能会报错,可以使用“?”运算符解决 */
  61. std::string name3 = json0["name"].is_null() ? "" : json0["name"].get<std::string>();
  62. /* 从直接到JSON */
  63. nJson json1 = R"(
  64. {
  65. "name": "microsoft",
  66. "age": 18,
  67. "bool": true,
  68. "null": null,
  69. "array": [1, 2, 3, 4, 5],
  70. "object": {
  71. "key1": "value1",
  72. "key2": "value2"
  73. }
  74. }
  75. )"_json;
  76. /* 或者 */
  77. std::string str = R"(
  78. {
  79. "name": "microsoft",
  80. "age": 18,
  81. "bool": true,
  82. "null": null,
  83. "array": [1, 2, 3, 4, 5],
  84. "object": {
  85. "key1": "value1",
  86. "key2": "value2"
  87. }
  88. }
  89. )";
  90. nJson json2 = nJson::parse(str);
  91. /* 从JSON到字符串 */
  92. std::string str1 = json0.dump();
  93. }
  94. void Widget::on_pBtn_connect_clicked()
  95. {
  96. SPDLOG_INFO("点击了“连接按钮”");
  97. // std::vector<std::string> list;
  98. // m_curlFtp.getFileList("/lh/Recall", list);
  99. // for(const std::string& filename : list)
  100. // {
  101. // QString qfilename = QString::fromLocal8Bit(filename.c_str());
  102. // SPDLOG_INFO("{}", qfilename.toStdString());
  103. // }
  104. /* 创建文件夹 */
  105. // bool ret = m_curlFtp.createDirectories("/SSD2/lh/Recall/FlowChart/Data");
  106. // if(ret)
  107. // {
  108. // SPDLOG_INFO("Data 文件夹创建成功");
  109. // } else
  110. // {
  111. // SPDLOG_INFO("Data 文件夹创建失败");
  112. // }
  113. // ret = m_curlFtp.createDirectories("/lh/Recall/FlowChart/Image");
  114. // if(ret)
  115. // {
  116. // SPDLOG_INFO("Image 文件夹创建成功");
  117. // } else
  118. // {
  119. // SPDLOG_INFO("Image 文件夹创建失败");
  120. // }
  121. // m_curlFtp.createDirectory("/音乐");
  122. // m_curlFtp.createDirectory("/lh/GZHS/mysql/Chn_1/{bf9ae7ab-c85e-488c-8e81-1019b3dddf7a}/{a0d41558-9978-44fc-bae1-5756609622f1}/EQM_FlowChart/Data");
  123. std::vector<std::string> driList;
  124. // m_curlFtp.getDirList("/音乐", driList);
  125. m_curlFtp.getDirList("/lh/GZHS/mysql/Chn_1/{bf9ae7ab-c85e-488c-8e81-1019b3dddf7a}/{a0d41558-9978-44fc-bae1-5756609622f1}/EQM_FlowChart/Data", driList);
  126. for(auto& it : driList)
  127. {
  128. SPDLOG_INFO("{}", it);
  129. }
  130. }
  131. void Widget::on_pBtn_downloadFile_clicked()
  132. {
  133. SPDLOG_INFO("点击了“下载文件”");
  134. std::vector<std::string> fileList;
  135. // bool ret = m_curlFtp.downloadFile("/SSD2/Video/哪吒之魔童降世.mp4", QApplication::applicationDirPath().toStdString() + "/YPM.mp4");
  136. // bool ret = m_curlFtp.downloadFile("/SSD1/Video/v1.mp4", QApplication::applicationDirPath().toStdString() + "/v1.mp4");
  137. std::string remotePath = "/lh/GZHS/mysql/Chn_1/{bf9ae7ab-c85e-488c-8e81-1019b3dddf7a}/{a0d41558-9978-44fc-bae1-5756609622f1}/EQM_FlowChart/Data/Date.json";
  138. bool ret = m_curlFtp.downloadFile(remotePath, QApplication::applicationDirPath().toStdString() + "/Date.json");
  139. if(ret)
  140. {
  141. SPDLOG_INFO("下载成功");
  142. }
  143. else
  144. {
  145. SPDLOG_INFO("下载失败");
  146. }
  147. }
  148. void Widget::on_pBtn_downloadVideo_clicked()
  149. {
  150. SPDLOG_INFO("点击了“下载视频”");
  151. // std::shared_ptr<QtFtp> ftp = std::make_shared<QtFtp>();
  152. // ftp->setHostAndPort("192.168.50.100");
  153. // ftp->setUserPasswd("microsoft", "19980714Lq");
  154. // QString localPath = QApplication::applicationDirPath() + "/v1.mp4";
  155. // ftp->getFile(localPath, "/SSD1/Video/v1.mp4");
  156. // ftp->waitFinished(-1);
  157. // if(ftp->getResult())
  158. // {
  159. // SPDLOG_INFO("下载成功");
  160. // }
  161. // else
  162. // {
  163. // SPDLOG_INFO("下载失败");
  164. // }
  165. bool ret = m_curlFtp.createDirectories("SSD1/DOC/123/234");
  166. if(ret)
  167. {
  168. SPDLOG_INFO("文件夹创建成功");
  169. }else {
  170. SPDLOG_WARN("文件夹创建失败");
  171. }
  172. }
  173. void Widget::on_pBtn_logSpeed_clicked()
  174. {
  175. SPDLOG_INFO("点击了“日志速度”");
  176. /* 开始时间点1 */
  177. std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
  178. for(int i = 0; i < 100000; i++)
  179. {
  180. SPDLOG_INFO("{}", "你好,这是日志速度的对比,这个是SPDLOG在打印日志");
  181. }
  182. /* 结束时间点1 */
  183. std::chrono::steady_clock::time_point spdlogEnd = std::chrono::steady_clock::now();
  184. for(int i = 0; i < 100000; i++)
  185. {
  186. // FMTLOG_INFO("{}", "你好,这是日志速度的对比,这个是FMTLOG在打印日志");
  187. }
  188. std::chrono::steady_clock::time_point fmtlogEnd = std::chrono::steady_clock::now();
  189. /* 使用std::cout */
  190. for(int i = 0; i < 100000; i++)
  191. {
  192. std::cout << "你好,这是日志速度的对比," << "这个是std::cout在打印日志" << std::endl;
  193. }
  194. std::chrono::steady_clock::time_point coutEnd = std::chrono::steady_clock::now();
  195. /* 使用printf */
  196. for(int i = 0; i < 100000; i++)
  197. {
  198. printf("%s\n","你好,这是日志速度的对比,这个是printf在打印日志");
  199. }
  200. std::chrono::steady_clock::time_point printfEnd = std::chrono::steady_clock::now();
  201. /* 使用QDebug */
  202. for(int i = 0; i < 100000; i++)
  203. {
  204. qDebug() << "你好,这是日志速度的对比," << "这个是QDebug在打印日志";
  205. }
  206. std::chrono::steady_clock::time_point qDebugEnd = std::chrono::steady_clock::now();
  207. /* 使用LightLog */
  208. for(int i = 0; i < 100000; i++)
  209. {
  210. QLOG_INFO("你好,这是日志速度的对比," + "这个是LightLog在打印日志");
  211. }
  212. std::chrono::steady_clock::time_point lightLogEnd = std::chrono::steady_clock::now();
  213. /* 计算时间 */
  214. std::chrono::duration<double> spdlogTime = std::chrono::duration_cast<std::chrono::duration<double>>(spdlogEnd - start);
  215. std::chrono::duration<double> fmtlogTime = std::chrono::duration_cast<std::chrono::duration<double>>(fmtlogEnd - spdlogEnd);
  216. std::chrono::duration<double> coutTime = std::chrono::duration_cast<std::chrono::duration<double>>(coutEnd - fmtlogEnd);
  217. std::chrono::duration<double> printfTime = std::chrono::duration_cast<std::chrono::duration<double>>(printfEnd - coutEnd);
  218. std::chrono::duration<double> qDebugTime = std::chrono::duration_cast<std::chrono::duration<double>>(qDebugEnd - printfEnd);
  219. std::chrono::duration<double> lightLogTime = std::chrono::duration_cast<std::chrono::duration<double>>(lightLogEnd - qDebugEnd);
  220. /* 打印时间 */
  221. SPDLOG_INFO("SPDLOG打印耗时: {}", spdlogTime.count());
  222. SPDLOG_INFO("FMTLOG打印耗时: {}", fmtlogTime.count());
  223. SPDLOG_INFO("std::cout打印耗时: {}", coutTime.count());
  224. SPDLOG_INFO("printf打印耗时: {}", printfTime.count());
  225. SPDLOG_INFO("QDebug打印耗时: {}", qDebugTime.count());
  226. SPDLOG_INFO("LightLog打印耗时: {}", lightLogTime.count());
  227. }
  228. void Widget::on_pBtn_upload_clicked()
  229. {
  230. SPDLOG_INFO("点击了“上传”");
  231. bool ret = m_curlFtp.uploadFile(QApplication::applicationDirPath().toStdString() + "/YPM.mp4", "/SSD2/Video/YPM.mp4");
  232. if(ret)
  233. {
  234. SPDLOG_INFO("上传成功");
  235. }
  236. else
  237. {
  238. SPDLOG_INFO("上传失败");
  239. }
  240. }