#include "clientthread.h" #include "../AGBoxDog/boxshm.h" ClientThread::ClientThread(QObject *parent) : QThread(parent) { keep = false; CmdList.clear(); devCode.clear(); switch_status = 0; } void ClientThread::stop() { keep = false; } void ClientThread::deviceStatus() { for(int i=0;i<1024;i++){ if((agBoxShm->device[i].Enabled == 0x01)&&(agBoxShm->device[i].device_type == 510)){ int cur=0; unsigned char data[256]; data[cur++] = 0x00; data[cur++] = 0x00; data[cur++] = 0x00; data[cur++] = 0x00; data[cur++] = 0x00; data[cur++] = 0x06; data[cur++] = 0x01; data[cur++] = 0x03; QByteArray deviceCode = QByteArray::fromHex(QString(agBoxShm->device[i].device_code).toLatin1()); emit dataListLog(QString("deviceCode %1").arg(agBoxShm->device[i].device_code)); printf("deviceCode %s\n",agBoxShm->device[i].device_code); data[cur++] = deviceCode.at(0)&0xff; data[cur++] = deviceCode.at(1)&0xff; data[cur++] = 0x00; data[cur++] = 0x01; QString DataStr; QByteArray rtn = QByteArray((const char *)data,12); for(int i=0;idevice[i].device_id)==0){ QByteArray deviceCode = QByteArray::fromHex(QString(agBoxShm->device[i].device_code).toLatin1()); emit dataListLog(QString("deviceCode %1").arg(agBoxShm->device[i].device_code)); printf("deviceCode %s\n",agBoxShm->device[i].device_code); data[cur++] = deviceCode.at(0)&0xff; data[cur++] = deviceCode.at(1)&0xff; break; } } data[cur++] = 0x00; data[cur++] = 0x01; data[cur++] = 0x02; switch_status = switchStatus; if(switchStatus == 1){ data[cur++] = 0x00; data[cur++] = 0x01; }else{ data[cur++] = 0x00; data[cur++] = 0x00; } QString DataStr; QByteArray rtn = QByteArray((const char *)data,15); for(int i=0;ireadAll(); QString DataStr; for(int i=0;idevice[i].device_code)==0){ int switchStatus = ((Rtn.at(9)&0x00ff)<<8)|(Rtn.at(10)&0x00ff); agBoxShm->device[i].lastTime = QDateTime::currentDateTime().toTime_t(); 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\"}") .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)); break; } } }else if((Rtn.at(7)&0xff) == 0x10){ for(int i=0;i<1024;i++){ if(devCode.compare(agBoxShm->device[i].device_code)==0){ agBoxShm->device[i].lastTime = QDateTime::currentDateTime().toTime_t(); 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\"}") .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)); emit responseMsg(QString("{\"code\":\"0\",\"msg\":\"success\",\"data\":\"\"}")); break; } } } } void ClientThread::run() { bool connect = false; keep = true; so = new QTcpSocket; while (keep) { if(!connect){ so->connectToHost(QHostAddress("172.17.203.45"),502); if(so->waitForConnected(3000)){ printf("connect success\n"); connect = true; }else{ printf("connect failed\n"); so->abort(); } }else{ if(so->waitForReadyRead(100)){ readData(so,devCode); }else if(CmdList.length()>0){ QByteArray cmd = CmdList.first(); QString DataStr; for(int i=0;iwrite(cmd); if(!so->waitForBytesWritten(1000)){ so->abort(); connect = false; }else{ CmdList.removeFirst(); } } } usleep(100000); } }