alarmchannel.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #include "alarmchannel.h"
  2. AlarmChannel::AlarmChannel(QObject *parent, QString ip, quint16 port, QString termId, QString termKey) :
  3. QObject(parent)
  4. {
  5. Ip = ip;
  6. Port = port;
  7. TermId = termId;
  8. TermKey = termKey;
  9. so = new QTcpSocket(this);
  10. connect(so,SIGNAL(connected()),this,SLOT(onConnected()));
  11. connect(so,SIGNAL(readyRead()),this,SLOT(onTextMessageReceived()));
  12. connect(so,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(onStatusChanged(QAbstractSocket::SocketState)));
  13. so->connectToHost(Ip,Port);
  14. }
  15. void AlarmChannel::onConnected(){
  16. QString cmd = QString("{\"AppId\":\"%1\", \"Secert\":\"%2\"}").arg(TermId).arg(TermKey);
  17. printf("%s\n",cmd.toUtf8().data());
  18. so->write(cmd.toUtf8());
  19. so->flush();
  20. }
  21. void AlarmChannel::onStatusChanged(QAbstractSocket::SocketState sta)
  22. {
  23. if(sta==QAbstractSocket::ClosingState)
  24. emit SoClose();
  25. }
  26. void AlarmChannel::onTextMessageReceived()
  27. {
  28. QString msg = QString(so->readAll());
  29. printf("%s\n",msg.toUtf8().data());
  30. emit getData(msg);
  31. }