widget.cpp 8.1 KB

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