Add status bar with connection status

This commit is contained in:
Gary Talent 2021-07-25 19:14:00 -05:00
parent d52e31cec5
commit 2274232a92
8 changed files with 116 additions and 5 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
SlideController.pro.user
build

4
consts.hpp Normal file
View File

@ -0,0 +1,4 @@
#pragma once
constexpr auto SlideHost = "turbo";

View File

@ -3,6 +3,7 @@
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QStatusBar>
#include <QVBoxLayout>
#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);
}

View File

@ -1,6 +1,9 @@
#pragma once
#include <cstdint>
#include <QMainWindow>
#include <QTimer>
#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();
};

View File

@ -1,6 +1,13 @@
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QUrl>
#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();
}

View File

@ -2,13 +2,17 @@
#include <QNetworkAccessManager>
#include <QObject>
#include <QTimer>
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();
};

View File

@ -1,5 +1,5 @@
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValueRef>
#include <QNetworkReply>
@ -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()) {

View File

@ -5,6 +5,8 @@
#include <QObject>
#include <QTimer>
#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);