1234567891011121314151617181920212223242526272829303132333435 |
- #include "alarmchannel.h"
- AlarmChannel::AlarmChannel(QObject *parent, QString ip, quint16 port, QString termId, QString termKey) :
- QObject(parent)
- {
- Ip = ip;
- Port = port;
- TermId = termId;
- TermKey = termKey;
- so = new QTcpSocket(this);
- connect(so,SIGNAL(connected()),this,SLOT(onConnected()));
- connect(so,SIGNAL(readyRead()),this,SLOT(onTextMessageReceived()));
- connect(so,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(onStatusChanged(QAbstractSocket::SocketState)));
- so->connectToHost(Ip,Port);
- }
- void AlarmChannel::onConnected(){
- QString cmd = QString("{\"AppId\":\"%1\", \"Secert\":\"%2\"}").arg(TermId).arg(TermKey);
- printf("%s\n",cmd.toUtf8().data());
- so->write(cmd.toUtf8());
- so->flush();
- }
- void AlarmChannel::onStatusChanged(QAbstractSocket::SocketState sta)
- {
- if(sta==QAbstractSocket::ClosingState)
- emit SoClose();
- }
- void AlarmChannel::onTextMessageReceived()
- {
- QString msg = QString(so->readAll());
- printf("%s\n",msg.toUtf8().data());
- emit getData(msg);
- }
|