diff --git a/.gitignore b/.gitignore index 2a23370..e77caf8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ SlideController.pro.user +build diff --git a/consts.hpp b/consts.hpp new file mode 100644 index 0000000..b98d93b --- /dev/null +++ b/consts.hpp @@ -0,0 +1,4 @@ + +#pragma once + +constexpr auto SlideHost = "turbo"; diff --git a/mainwindow.cpp b/mainwindow.cpp index 1ccce5e..3e537e6 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "slideview.hpp" @@ -63,7 +64,55 @@ MainWindow::MainWindow(QWidget *parent): QMainWindow(parent) { connect(btnObsHideSlides, &QPushButton::clicked, &m_obsClient, &OBSClient::hideSlides); connect(btnObsShowSlides, &QPushButton::clicked, &m_obsClient, &OBSClient::showSlides); connect(btnObsShowSlides, &QPushButton::clicked, &m_openlpClient, &OpenLPClient::showSlides); + // setup status bar + auto statusBar = new QStatusBar(this); + setStatusBar(statusBar); + connect(&m_openlpClient, &OpenLPClient::pollUpdate, this, &MainWindow::openLpConnectionInit); + connect(&m_openlpClient, &OpenLPClient::pollUpdate, [this] { ++m_openLPUpdates; }); + connect(&m_obsClient, &OBSClient::pollUpdate, this, &MainWindow::obsConnectionInit); + connect(&m_obsClient, &OBSClient::pollUpdate, [this] { ++m_obsUpdates; }); + m_statusBarTimer.start(5000); + connect(&m_statusBarTimer, &QTimer::timeout, this, &MainWindow::refreshStatusBar); + refreshStatusBar(); } MainWindow::~MainWindow() { } + +void MainWindow::openLpConnectionInit() { + disconnect(&m_openlpClient, &OpenLPClient::pollUpdate, this, &MainWindow::openLpConnectionInit); + ++m_openLPUpdates; + refreshStatusBar(); +} + +void MainWindow::openLpConnectionLost() { + connect(&m_openlpClient, &OpenLPClient::pollUpdate, this, &MainWindow::openLpConnectionInit); +} + +void MainWindow::obsConnectionInit() { + disconnect(&m_obsClient, &OBSClient::pollUpdate, this, &MainWindow::obsConnectionInit); + ++m_openLPUpdates; + refreshStatusBar(); +} + +void MainWindow::obsConnectionLost() { + connect(&m_obsClient, &OBSClient::pollUpdate, this, &MainWindow::obsConnectionInit); +} + +void MainWindow::refreshStatusBar() { + QString openLpStatus; + QString obsStatus; + if (m_openLPUpdates > 0) { + openLpStatus = tr("OpenLP: Connected"); + } else { + openLpStatus = tr("OpenLP: Not Connected"); + } + if (m_obsUpdates > 0) { + obsStatus = tr("OBS: Connected"); + } else { + obsStatus = tr("OBS: Not Connected"); + } + m_openLPUpdates = 0; + m_obsUpdates = 0; + statusBar()->showMessage(openLpStatus + " | " + obsStatus); +} diff --git a/mainwindow.hpp b/mainwindow.hpp index 13f0545..583e130 100644 --- a/mainwindow.hpp +++ b/mainwindow.hpp @@ -1,6 +1,9 @@ #pragma once +#include + #include +#include #include "obsclient.hpp" #include "openlpclient.hpp" @@ -11,11 +14,23 @@ class MainWindow: public QMainWindow { private: OBSClient m_obsClient; OpenLPClient m_openlpClient; + QTimer m_statusBarTimer; + uint64_t m_openLPUpdates = 0; + uint64_t m_obsUpdates = 0; public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: + void openLpConnectionInit(); + + void openLpConnectionLost(); + + void obsConnectionInit(); + + void obsConnectionLost(); + + void refreshStatusBar(); }; diff --git a/obsclient.cpp b/obsclient.cpp index 2aa421a..2b41814 100644 --- a/obsclient.cpp +++ b/obsclient.cpp @@ -1,6 +1,13 @@ +#include +#include +#include + #include "obsclient.hpp" OBSClient::OBSClient(QObject *parent): QObject(parent) { + m_pollTimer.start(1000); + connect(&m_pollTimer, &QTimer::timeout, this, &OBSClient::poll); + connect(m_pollingNam, &QNetworkAccessManager::finished, this, &OBSClient::handlePollResponse); } void OBSClient::setScene(QString scene) { @@ -28,3 +35,19 @@ void OBSClient::get(QString urlExt) { QNetworkRequest rqst(url); m_nam->get(rqst); } + +void OBSClient::poll() { + QUrl url(QString(BaseUrl) + "/api/poll?_=1626628079579"); + QNetworkRequest rqst(url); + m_pollingNam->get(rqst); +} + +void OBSClient::handlePollResponse(QNetworkReply *reply) { + if (reply->error()) { + qDebug() << reply->errorString(); + emit pollFailed(); + return; + } + emit pollUpdate(); +} + diff --git a/obsclient.hpp b/obsclient.hpp index 3d6d17d..3b0a572 100644 --- a/obsclient.hpp +++ b/obsclient.hpp @@ -2,13 +2,17 @@ #include #include +#include -class OBSClient : public QObject -{ +#include "consts.hpp" + +class OBSClient: public QObject { Q_OBJECT private: - static constexpr auto BaseUrl = "http://127.0.0.1:9302"; + const QString BaseUrl = QString("http://") + SlideHost + ":9302"; QNetworkAccessManager *m_nam = new QNetworkAccessManager(this); + QNetworkAccessManager *m_pollingNam = new QNetworkAccessManager(this); + QTimer m_pollTimer; public: explicit OBSClient(QObject *parent = nullptr); @@ -25,5 +29,14 @@ class OBSClient : public QObject private: void get(QString url); + void poll(); + + void handlePollResponse(QNetworkReply *reply); + + signals: + void pollUpdate(); + + void pollFailed(); + }; diff --git a/openlpclient.cpp b/openlpclient.cpp index a4e79eb..61a6f3f 100644 --- a/openlpclient.cpp +++ b/openlpclient.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include #include #include @@ -85,6 +85,8 @@ void OpenLPClient::handleGeneralResponse(QNetworkReply *reply) { void OpenLPClient::handlePollResponse(QNetworkReply *reply) { if (reply->error()) { qDebug() << reply->errorString(); + emit pollFailed(); + return; } auto data = reply->readAll(); if (data.isEmpty()) { diff --git a/openlpclient.hpp b/openlpclient.hpp index 1a4bec4..2efb200 100644 --- a/openlpclient.hpp +++ b/openlpclient.hpp @@ -5,6 +5,8 @@ #include #include +#include "consts.hpp" + class OpenLPClient: public QObject { Q_OBJECT private: @@ -12,7 +14,7 @@ class OpenLPClient: public QObject { QString name; QString id; }; - static constexpr auto BaseUrl = "http://127.0.0.1:4316"; + const QString BaseUrl = QString("http://") + SlideHost + ":4316"; QNetworkAccessManager *m_nam = new QNetworkAccessManager(this); QNetworkAccessManager *m_pollingNam = new QNetworkAccessManager(this); QNetworkAccessManager *m_songListNam = new QNetworkAccessManager(this); @@ -63,6 +65,8 @@ class OpenLPClient: public QObject { signals: void pollUpdate(QString songId, int slideNum); + void pollFailed(); + void songListUpdate(QStringList); void slideListUpdate(QStringList, QStringList);