widget.cpp 7.7 KB

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