linethread.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. //前端生产环境
  17. //request.setUrl(QUrl("http://121.40.217.77:8081/pt/ptLinePush"));
  18. reply = naManagerLine->post(request, m_httpData);
  19. }
  20. void LineThread::getData(QNetworkReply*)
  21. {
  22. QString responseData;
  23. reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
  24. reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
  25. QNetworkReply::NetworkError err = reply->error();
  26. if(err != QNetworkReply::NoError) {
  27. responseData.append(QString("Failed: %1").arg(reply->errorString()));
  28. }
  29. else {
  30. // 获取返回内容
  31. QString string = QString::fromUtf8(reply->readAll());
  32. responseData.append(QString::fromUtf8("%1").arg(string));
  33. }
  34. reply->deleteLater();
  35. logList.append(responseData);
  36. QDate today = QDate::currentDate();
  37. 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')));
  38. if(file.open(QIODevice::Text|QIODevice::Append|QIODevice::WriteOnly)){
  39. while (logList.length()>0) {
  40. file.write(logList.first().toUtf8()+"\n");
  41. logList.removeFirst();
  42. usleep(10000);
  43. }
  44. file.close();
  45. }
  46. }
  47. void LineThread::DealAccessDBData(QString lId,QString lName,QString placeId,QString lOrder,QString lNextPlaceTime,QString lStayTime)
  48. {
  49. if(db.open()){
  50. bool found = false;
  51. QStringList tables = db.tables();
  52. for(int i=0;i<tables.length();i++){
  53. if(QString::compare(tables.at(i),"usky_line_data")==0){
  54. found = true;
  55. break;
  56. }
  57. }
  58. if(!found){
  59. if(db.transaction()){
  60. db.exec("create table usky_line_data (lId text,lName text,placeId text,lOrder text,lNextPlaceTime text,lStayTime text,createTime text);");
  61. 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")));
  62. db.commit();
  63. }
  64. }else{
  65. 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")));
  66. db.commit();
  67. }
  68. }
  69. db.close();
  70. }
  71. void LineThread::deleteLineData()
  72. {
  73. //建立现有数据库连接(这个是在已存在数据库的基础上)
  74. db = QSqlDatabase::database("landwell_db");
  75. //直接操作表 在插入数据之前,先清空表中的数据
  76. if(db.open()){
  77. bool found = false;
  78. QStringList tables = db.tables();
  79. for(int i=0;i<tables.length();i++){
  80. if(QString::compare(tables.at(i),"usky_line_data")==0){
  81. found = true;
  82. break;
  83. }
  84. }
  85. if(!found){
  86. }else{
  87. db.exec("delete from usky_line_data");
  88. db.commit();
  89. }
  90. }
  91. db.close();
  92. }