Added a wait to prevent premature closing of socket
[irwi] / src / irctrl.cpp
index 2642142..0f98b8a 100644 (file)
@@ -1,6 +1,14 @@
 #include "irctrl.h"
+
 #include <cstdlib>
 
+#include <QTcpSocket>
+#include <QSettings>
+#include <QString>
+#include <QIODevice>
+#include <QHostAddress>
+#include <QAbstractSocket>
+
 IrCtrl::IrCtrl()
 {
     std::system("lircctl start");
@@ -11,9 +19,19 @@ IrCtrl::~IrCtrl()
     std::system("lircctl stop");
 }
 
-void IrCtrl::setRemoteName(const QString &newRemoteName)
+void IrCtrl::genericSendCmd(const QString &commandName)
 {
-    this->remoteName = newRemoteName;
+    QTcpSocket sock(this);
+    sock.setSocketOption(QAbstractSocket::LowDelayOption, 1);
+    sock.connectToHost(QHostAddress::LocalHost, LIRC_PORT,
+            QIODevice::WriteOnly|QIODevice::Unbuffered);
+    sock.waitForConnected(-1);
+    QSettings settings(this);
+    QString remoteName = settings.value("remoteName", "Zyxel_DMA-1000").toString();
+    QString cmd = "SEND_ONCE " + remoteName + " " + commandName + "\n";
+    sock.write(cmd.toAscii());
+    sock.waitForBytesWritten(-1);
+    sock.close();
 }
 
 void IrCtrl::sendCmd0(bool)
@@ -34,6 +52,7 @@ void IrCtrl::sendCmd3(bool)
 
 void IrCtrl::sendCmd4(bool)
 {
+    genericSendCmd("POWER");
 }
 
 void IrCtrl::sendCmd5(bool)