clientthread.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "clientthread.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. ClientThread::ClientThread(QObject *parent) : QThread(parent)
  4. {
  5. keep = false;
  6. CmdList.clear();
  7. devCode.clear();
  8. switch_status = 0;
  9. }
  10. void ClientThread::stop()
  11. {
  12. keep = false;
  13. }
  14. void ClientThread::deviceStatus()
  15. {
  16. for(int i=0;i<1024;i++){
  17. if((agBoxShm->device[i].Enabled == 0x01)&&(agBoxShm->device[i].device_type == 510)){
  18. int cur=0;
  19. unsigned char data[256];
  20. data[cur++] = 0x00;
  21. data[cur++] = 0x00;
  22. data[cur++] = 0x00;
  23. data[cur++] = 0x00;
  24. data[cur++] = 0x00;
  25. data[cur++] = 0x06;
  26. data[cur++] = 0x01;
  27. data[cur++] = 0x03;
  28. QByteArray deviceCode = QByteArray::fromHex(QString(agBoxShm->device[i].device_code).toLatin1());
  29. emit dataListLog(QString("deviceCode %1").arg(agBoxShm->device[i].device_code));
  30. printf("deviceCode %s\n",agBoxShm->device[i].device_code);
  31. data[cur++] = deviceCode.at(0)&0xff;
  32. data[cur++] = deviceCode.at(1)&0xff;
  33. data[cur++] = 0x00;
  34. data[cur++] = 0x01;
  35. QString DataStr;
  36. QByteArray rtn = QByteArray((const char *)data,12);
  37. for(int i=0;i<rtn.length();i++){
  38. DataStr.append(QString("%1").arg(rtn.at(i)&0xff,2,16,QChar('0')));
  39. }
  40. // printf("4444 %s\n",DataStr.toUtf8().data());
  41. CmdList.append(rtn);
  42. }
  43. }
  44. }
  45. void ClientThread::controlData(QString deviceId,int switchStatus)
  46. {
  47. int cur=0;
  48. unsigned char data[256];
  49. data[cur++] = 0x00;
  50. data[cur++] = 0x00;
  51. data[cur++] = 0x00;
  52. data[cur++] = 0x00;
  53. data[cur++] = 0x00;
  54. data[cur++] = 0x09;
  55. data[cur++] = 0x01;
  56. data[cur++] = 0x10;
  57. for(int i=0;i<1024;i++){
  58. if(deviceId.compare(agBoxShm->device[i].device_id)==0){
  59. QByteArray deviceCode = QByteArray::fromHex(QString(agBoxShm->device[i].device_code).toLatin1());
  60. emit dataListLog(QString("deviceCode %1").arg(agBoxShm->device[i].device_code));
  61. printf("deviceCode %s\n",agBoxShm->device[i].device_code);
  62. data[cur++] = deviceCode.at(0)&0xff;
  63. data[cur++] = deviceCode.at(1)&0xff;
  64. break;
  65. }
  66. }
  67. data[cur++] = 0x00;
  68. data[cur++] = 0x01;
  69. data[cur++] = 0x02;
  70. switch_status = switchStatus;
  71. if(switchStatus == 1){
  72. data[cur++] = 0x00;
  73. data[cur++] = 0x01;
  74. }else{
  75. data[cur++] = 0x00;
  76. data[cur++] = 0x00;
  77. }
  78. QString DataStr;
  79. QByteArray rtn = QByteArray((const char *)data,15);
  80. for(int i=0;i<rtn.length();i++){
  81. DataStr.append(QString("%1").arg(rtn.at(i)&0xff,2,16,QChar('0')));
  82. }
  83. printf("777777 %s\n",DataStr.toUtf8().data());
  84. CmdList.append(rtn);
  85. }
  86. void ClientThread::readData(QTcpSocket *so,QString devCode)
  87. {
  88. QByteArray Rtn = so->readAll();
  89. QString DataStr;
  90. for(int i=0;i<Rtn.length();i++){
  91. DataStr.append(QString("%1").arg(Rtn.at(i)&0xff,2,16,QChar('0')));
  92. }
  93. emit dataListLog(QString("[%1] devCode %2 %3").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(devCode).arg(DataStr));
  94. if((Rtn.at(7)&0xff) == 0x03){
  95. for(int i=0;i<1024;i++){
  96. if(devCode.compare(agBoxShm->device[i].device_code)==0){
  97. int switchStatus = ((Rtn.at(9)&0x00ff)<<8)|(Rtn.at(10)&0x00ff);
  98. agBoxShm->device[i].lastTime = QDateTime::currentDateTime().toTime_t();
  99. emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{\"switch_status\":%5,\"device_status\":1},\"device_type\":\"%6-lg\"}")
  100. .arg(agBoxShm->device[i].device_id).arg(agBoxShm->device[i].device_code).arg(agBoxShm->device[i].product_code).arg(QDateTime::currentDateTime().toTime_t()).arg(switchStatus).arg(agBoxShm->device[i].device_type));
  101. break;
  102. }
  103. }
  104. }else if((Rtn.at(7)&0xff) == 0x10){
  105. for(int i=0;i<1024;i++){
  106. if(devCode.compare(agBoxShm->device[i].device_code)==0){
  107. agBoxShm->device[i].lastTime = QDateTime::currentDateTime().toTime_t();
  108. emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{\"switch_status\":%5,\"device_status\":1},\"device_type\":\"%6-lg\"}")
  109. .arg(agBoxShm->device[i].device_id).arg(agBoxShm->device[i].device_code).arg(agBoxShm->device[i].product_code).arg(QDateTime::currentDateTime().toTime_t()).arg(switch_status).arg(agBoxShm->device[i].device_type));
  110. emit responseMsg(QString("{\"code\":\"0\",\"msg\":\"success\",\"data\":\"\"}"));
  111. break;
  112. }
  113. }
  114. }
  115. }
  116. void ClientThread::run()
  117. {
  118. bool connect = false;
  119. keep = true;
  120. so = new QTcpSocket;
  121. while (keep) {
  122. if(!connect){
  123. so->connectToHost(QHostAddress("172.17.203.45"),502);
  124. if(so->waitForConnected(3000)){
  125. printf("connect success\n");
  126. connect = true;
  127. }else{
  128. printf("connect failed\n");
  129. so->abort();
  130. }
  131. }else{
  132. if(so->waitForReadyRead(100)){
  133. readData(so,devCode);
  134. }else if(CmdList.length()>0){
  135. QByteArray cmd = CmdList.first();
  136. QString DataStr;
  137. for(int i=0;i<cmd.length();i++){
  138. DataStr.append(QString("%1").arg(cmd.at(i)&0xff,2,16,QChar('0')));
  139. }
  140. devCode.clear();
  141. devCode = (QString("%1%2").arg(cmd.at(8)&0xff,2,16,QChar('0')).arg(cmd.at(9)&0xff,2,16,QChar('0'))).toUpper();
  142. emit dataListLog(QString("[%1] CmdList.first() %2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(DataStr));
  143. so->write(cmd);
  144. if(!so->waitForBytesWritten(1000)){
  145. so->abort();
  146. connect = false;
  147. }else{
  148. CmdList.removeFirst();
  149. }
  150. }
  151. }
  152. usleep(100000);
  153. }
  154. }