widget.cpp 8.2 KB

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