linethread.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "linethread.h"
  2. #include <QSettings>
  3. LineThread::LineThread(QObject *parent) : QThread(parent)
  4. {
  5. naManagerLine = new QNetworkAccessManager(this);
  6. //QNetworkAccessManager 向前台发送消息,等消息finished之后,才可以触发收前台返回的数据(不能把发送前台消息操作和前台返回数据操作放在一个函数里处理,有先后顺序)
  7. QObject::connect(naManagerLine, &QNetworkAccessManager::finished, this, &LineThread::getData);
  8. }
  9. void LineThread::SendJSONData(QByteArray m_httpData)
  10. {
  11. QNetworkRequest request;
  12. QSettings sets("sys.ini", QSettings::IniFormat);
  13. QString lineUrl = sets.value("LINE_HTTPURL").toString();
  14. //前端测试环境
  15. //request.setUrl(QUrl(lineUrl));
  16. QSslConfiguration config = request.sslConfiguration();
  17. config.setPeerVerifyMode(QSslSocket::VerifyNone);
  18. config.setProtocol(QSsl::TlsV1SslV3);
  19. request.setSslConfiguration(config);
  20. request.setUrl(QUrl(lineUrl));
  21. //request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));
  22. //前端生产环境
  23. //request.setUrl(QUrl("http://121.40.217.77:8081/pt/ptLinePush"));
  24. reply = naManagerLine->post(request, m_httpData);
  25. }
  26. void LineThread::getData(QNetworkReply*)
  27. {
  28. QString responseData;
  29. reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
  30. reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
  31. QNetworkReply::NetworkError err = reply->error();
  32. if(err != QNetworkReply::NoError) {
  33. responseData.append(QString("Failed: %1").arg(reply->errorString()));
  34. }
  35. else {
  36. // 获取返回内容
  37. QString string = QString::fromUtf8(reply->readAll());
  38. responseData.append(QString::fromUtf8("%1").arg(string));
  39. }
  40. reply->deleteLater();
  41. logList.append(responseData);
  42. QDate today = QDate::currentDate();
  43. QFile file(QString("log/responseLineData-%1%2%3.txt").arg(today.year(),4,10,QChar('0')).arg(today.month(),2,10,QChar('0')).arg(today.day(),2,10,QChar('0')));
  44. if(file.open(QIODevice::Text|QIODevice::Append|QIODevice::WriteOnly)){
  45. while (logList.length()>0) {
  46. file.write(logList.first().toUtf8()+"\n");
  47. logList.removeFirst();
  48. usleep(10000);
  49. }
  50. file.close();
  51. }
  52. }
  53. void LineThread::DealAccessDBData(QString lId,QString lName,QString placeId,QString lOrder,QString lNextPlaceTime,QString lStayTime)
  54. {
  55. if(db.open()){
  56. bool found = false;
  57. QStringList tables = db.tables();
  58. for(int i=0;i<tables.length();i++){
  59. if(QString::compare(tables.at(i),"usky_line_data")==0){
  60. found = true;
  61. break;
  62. }
  63. }
  64. if(!found){
  65. if(db.transaction()){
  66. db.exec("create table usky_line_data (lId text,lName text,placeId text,lOrder text,lNextPlaceTime text,lStayTime text,createTime text);");
  67. db.exec(QString("insert into usky_line_data (lId, lName, placeId,lOrder,lNextPlaceTime,lStayTime,createTime) values ('%1', '%2', '%3', '%4', '%5', '%6', '%7');").arg(lId).arg(lName).arg(placeId).arg(lOrder).arg(lNextPlaceTime).arg(lStayTime).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")));
  68. db.commit();
  69. }
  70. }else{
  71. db.exec(QString("insert into usky_line_data (lId, lName, placeId,lOrder,lNextPlaceTime,lStayTime,createTime) values ('%1', '%2', '%3', '%4', '%5', '%6', '%7');").arg(lId).arg(lName).arg(placeId).arg(lOrder).arg(lNextPlaceTime).arg(lStayTime).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")));
  72. db.commit();
  73. }
  74. }
  75. db.close();
  76. }
  77. void LineThread::deleteLineData()
  78. {
  79. //建立现有数据库连接(这个是在已存在数据库的基础上)
  80. db = QSqlDatabase::database("landwell_db");
  81. //直接操作表 在插入数据之前,先清空表中的数据
  82. if(db.open()){
  83. bool found = false;
  84. QStringList tables = db.tables();
  85. for(int i=0;i<tables.length();i++){
  86. if(QString::compare(tables.at(i),"usky_line_data")==0){
  87. found = true;
  88. break;
  89. }
  90. }
  91. if(!found){
  92. }else{
  93. db.exec("delete from usky_line_data");
  94. db.commit();
  95. }
  96. }
  97. db.close();
  98. }