• 643阅读
  • 6回复

Qt编写自助访客app [复制链接]

上一主题 下一主题
离线liudianwu
 

widget编写,将抓拍的照片发给服务器执行人脸识别和比对,比对成功后选择被访人的楼号单元层号房号电话,打通电话,被访人按下确认键,收到后制作人脸通行证,将该访客的人脸信息发送至对应单元下的人脸实时分析设备,访客直接过去自动开门,超时失效。





  1. #ifndef DEVICECLIENT_H
  2. #define DEVICECLIENT_H
  3. #include <QtNetwork>
  4. class DeviceClient : public QObject
  5. {
  6.     Q_OBJECT
  7. public:
  8.     static DeviceClient *Instance();
  9.     explicit DeviceClient(QObject *parent = 0);
  10. private:
  11.     static DeviceClient *self;
  12.     QMutex mutex;
  13.     bool isOk;
  14.     QDateTime lastTime;
  15.     QString serverIP;
  16.     int serverPort;
  17.     QTcpSocket *tcpSocket;
  18.     QByteArray buffer;
  19. private slots:
  20.     void connected();
  21.     void disconnected();
  22.     void checkConn();
  23.     void readData();
  24.     void checkData();
  25. signals:
  26.     void sendData(const QString &ip, const QString &data);
  27.     void receiveData(const QString &ip, const QString &data);
  28.     void receiveFaceFail();
  29.     void receiveFace(const QRect &rect);
  30.     void callFinsh();
  31.     void callError();
  32.     void receiveCall(const QString &tel);
  33.     void receiveKey(const QString &key);
  34. public slots:
  35.     void setServerIP(const QString &serverIP);
  36.     void setServerPort(int serverPort);
  37.     bool start();
  38.     void sendData(const QString &buffer);
  39.     void sendHeart();
  40.     void sendImage(const QImage &image);
  41.     void sendAction(const QString &tel, bool call = true);
  42.     void sendPersonInfo(const QString &personBuild,
  43.                         const QString &personUnit,
  44.                         const QString &personLevel,
  45.                         const QString &personRoom,
  46.                         const QString &personName,
  47.                         const QString &personType,
  48.                         const QString &startTime,
  49.                         const QString &endTime);
  50. };
  51. #endif // DEVICECLIENT_H


  1. #include "deviceclient.h"
  2. #include "quiwidget.h"
  3. #include "devicefun.h"
  4. #include "qandroid.h"
  5. #include "dbdata.h"
  6. DeviceClient *DeviceClient::self = 0;
  7. DeviceClient *DeviceClient::Instance()
  8. {
  9.     if (!self) {
  10.         QMutex mutex;
  11.         QMutexLocker locker(&mutex);
  12.         if (!self) {
  13.             self = new DeviceClient;
  14.         }
  15.     }
  16.     return self;
  17. }
  18. DeviceClient::DeviceClient(QObject *parent) : QObject(parent)
  19. {
  20.     isOk = false;
  21.     lastTime = QDateTime::currentDateTime();
  22.     serverIP = "192.168.1.239";
  23.     serverPort = 6661;
  24.     //实例化连接对象
  25.     tcpSocket = new QTcpSocket(this);
  26.     connect(tcpSocket, SIGNAL(connected()), this, SLOT(connected()));
  27.     connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));
  28.     connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
  29.     //定时器处理数据
  30.     QTimer *timer = new QTimer(this);
  31.     connect(timer, SIGNAL(timeout()), this, SLOT(checkData()));
  32.     timer->start(100);
  33.     //定时器处理重连
  34.     QTimer *timerConn = new QTimer(this);
  35.     connect(timerConn, SIGNAL(timeout()), this, SLOT(checkConn()));
  36.     timerConn->start(5000);
  37.     //定时器发送心跳
  38.     QTimer *timerHeart = new QTimer(this);
  39.     connect(timerHeart, SIGNAL(timeout()), this, SLOT(sendHeart()));
  40.     timerHeart->start(1000);
  41. }
  42. void DeviceClient::connected()
  43. {
  44.     isOk = true;
  45. }
  46. void DeviceClient::disconnected()
  47. {
  48.     isOk = false;
  49. }
  50. void DeviceClient::checkConn()
  51. {
  52.     QDateTime now = QDateTime::currentDateTime();
  53.     int timeout = lastTime.secsTo(now);
  54.     if (timeout >= 10) {
  55.         isOk = false;
  56.         QAndroid::Instance()->makeToast("重新连接服务器");
  57.         start();
  58.     }
  59. }
  60. void DeviceClient::readData()
  61. {
  62.     lastTime = QDateTime::currentDateTime();
  63.     QMutexLocker locker(&mutex);
  64.     buffer.append(tcpSocket->readAll());
  65. }
  66. void DeviceClient::checkData()
  67. {
  68.     QMutexLocker locker(&mutex);
  69.     QDomDocument dom;
  70.     if (!DeviceFun::getReceiveXmlData(buffer, dom)) {
  71.         return;
  72.     }
  73.     QDomElement element = dom.documentElement();
  74.     if (element.tagName() == "Server") {
  75.         QString nowTime = element.attribute("NowTime");
  76.         QString year = nowTime.mid(0, 4);
  77.         QString month = nowTime.mid(5, 2);
  78.         QString day = nowTime.mid(8, 2);
  79.         QString hour = nowTime.mid(11, 2);
  80.         QString min = nowTime.mid(14, 2);
  81.         QString sec = nowTime.mid(17, 2);
  82.         QUIHelper::setSystemDateTime(year, month, day, hour, min, sec);
  83.         //根据子节点处理
  84.         QDomNode childNode = element.firstChild();
  85.         QString name = childNode.nodeName();
  86.         QString value = element.text();
  87.         //qInfo() << TIMEMS << name << value;
  88.         if (name == "OK") {
  89.         } else if (name == "FaceRect") {
  90.             QStringList list = value.split(",");
  91.             if (list.count() == 4) {
  92.                 emit receiveFace(QRect(list.at(0).toInt(), list.at(1).toInt(), list.at(2).toInt(), list.at(3).toInt()));
  93.             } else {
  94.                 emit receiveFaceFail();
  95.             }
  96.         } else if (name == "ActionResult") {
  97.             if (value.startsWith("CallFinsh")) {
  98.                 emit callFinsh();
  99.                 QAndroid::Instance()->makeToast("呼叫结束");
  100.             } else if (value.startsWith("CallError")) {
  101.                 emit callError();
  102.                 QAndroid::Instance()->makeToast("呼叫失败");
  103.             } else if (value.startsWith("ReceiveCall")) {
  104.                 emit receiveCall(value.split(":").at(1));
  105.                 QAndroid::Instance()->makeToast("新电话打入");
  106.             } else if (value.startsWith("ReceiveKey")) {
  107.                 emit receiveKey(value.split(":").at(1));
  108.                 QAndroid::Instance()->makeToast("按键确认");
  109.                 //立即发送访客信息
  110.                 QDateTime now = QDateTime::currentDateTime();
  111.                 QString endTime = now.addSecs(App::Timeout * 60 * 60).toString("yyyy-MM-dd HH:mm:ss");
  112.                 sendPersonInfo(DBData::PersonBuild,
  113.                                DBData::PersonUnit,
  114.                                DBData::PersonLevel,
  115.                                DBData::PersonRoom,
  116.                                DBData::PersonName,
  117.                                "访客", DATETIME, endTime);
  118.             }
  119.         }
  120.     }
  121. }
  122. void DeviceClient::setServerIP(const QString &serverIP)
  123. {
  124.     this->serverIP = serverIP;
  125. }
  126. void DeviceClient::setServerPort(int serverPort)
  127. {
  128.     this->serverPort = serverPort;
  129. }
  130. bool DeviceClient::start()
  131. {
  132.     tcpSocket->abort();
  133.     tcpSocket->connectToHost(serverIP, serverPort);
  134. }
  135. void DeviceClient::sendData(const QString &buffer)
  136. {
  137.     if (!isOk) {
  138.         return;
  139.     }
  140.     tcpSocket->write(buffer.toLatin1());
  141.     emit sendData(App::LocalIP, buffer);
  142.     if (buffer.contains("PersonInfo")) {
  143.         qInfo() << buffer;
  144.     }
  145. }
  146. void DeviceClient::sendHeart()
  147. {
  148.     //构建xml字符串
  149.     QStringList list;
  150.     list.append(QString("<Device DeviceIP=\"%1\">").arg(App::LocalIP));
  151.     list.append("<DeviceHeart/>");
  152.     list.append("</Device>");
  153.     QString buffer = DeviceFun::getSendXmlData(list.join(""));
  154.     sendData(buffer);
  155. }
  156. void DeviceClient::sendImage(const QImage &image)
  157. {
  158.     QByteArray imageData = DeviceFun::getImageData(image);
  159.     //构建xml字符串
  160.     QStringList list;
  161.     list.append(QString("<Device DeviceIP=\"%1\">").arg(App::LocalIP));
  162.     list.append(QString("<Image>%1</Image>").arg(QString(imageData)));
  163.     list.append("</Device>");
  164.     QString buffer = DeviceFun::getSendXmlData(list.join(""));
  165.     sendData(buffer);
  166. }
  167. void DeviceClient::sendAction(const QString &tel, bool call)
  168. {
  169.     //构建xml字符串
  170.     QStringList list;
  171.     list.append(QString("<Device DeviceIP=\"%1\">").arg(App::LocalIP));
  172.     if (call) {
  173.         list.append(QString("<DoAction>StartCall:%1</DoAction>").arg(tel));
  174.     } else {
  175.         list.append(QString("<DoAction>StopCall</DoAction>"));
  176.     }
  177.     list.append("</Device>");
  178.     QString buffer = DeviceFun::getSendXmlData(list.join(""));
  179.     sendData(buffer);
  180. }
  181. void DeviceClient::sendPersonInfo(const QString &personBuild,
  182.                                   const QString &personUnit,
  183.                                   const QString &personLevel,
  184.                                   const QString &personRoom,
  185.                                   const QString &personName,
  186.                                   const QString &personType,
  187.                                   const QString &startTime,
  188.                                   const QString &endTime)
  189. {
  190.     //构建xml字符串
  191.     QStringList list;
  192.     list.append(QString("<Device DeviceIP=\"%1\">").arg(App::LocalIP));
  193.     list.append("<PersonInfo>");
  194.     list.append(QString("<PersonBuild>%1</PersonBuild>").arg(DeviceFun::getBase64(personBuild)));
  195.     list.append(QString("<PersonUnit>%1</PersonUnit>").arg(DeviceFun::getBase64(personUnit)));
  196.     list.append(QString("<PersonLevel>%1</PersonLevel>").arg(DeviceFun::getBase64(personLevel)));
  197.     list.append(QString("<PersonRoom>%1</PersonRoom>").arg(DeviceFun::getBase64(personRoom)));
  198.     list.append(QString("<PersonName>%1</PersonName>").arg(DeviceFun::getBase64(personName)));
  199.     list.append(QString("<PersonType>%1</PersonType>").arg(DeviceFun::getBase64(personType)));
  200.     list.append(QString("<StartTime>%1</StartTime>").arg(startTime));
  201.     list.append(QString("<EndTime>%1</EndTime>").arg(endTime));
  202.     list.append("</PersonInfo>");
  203.     list.append("</Device>");
  204.     QString buffer = DeviceFun::getSendXmlData(list.join(""));
  205.     sendData(buffer);
  206. }

离线圣域天子

只看该作者 1楼 发表于: 04-07
怎么现在迷上用QWidget开发app了???
离线liudianwu

只看该作者 2楼 发表于: 04-07
回 圣域天子 的帖子
圣域天子:怎么现在迷上用QWidget开发app了??? (2018-04-07 08:23) 

关键是不会用qml
离线圣域天子

只看该作者 3楼 发表于: 04-07
回 liudianwu 的帖子
liudianwu:关键是不会用qm[表情]l (2018-04-07 08:28) 

彼此彼此,...
我觉得 qml 都不是C++ 了
它感觉还没有定型
目前我也用不到,所以也不太关注
离线return

只看该作者 4楼 发表于: 04-07
      
离线liuchangyin

只看该作者 5楼 发表于: 04-08

只看该作者 6楼 发表于: 04-10
挺好的大神带带我
快速回复
限100 字节
 
上一个 下一个