clientthread.cpp 6.4 KB

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