hyperion.ng/libsrc/boblightserver/BoblightServer.cpp
LordGrey 160c5d0b3a
UI and Web updates (#1421)
* Stop Web-Capture when priority changes

* Remote control UI: Treat duration=0 as endless

* Stop Web-Capture on non-Image events changes

* LED Matrix Layout - Support vertical cabling direction

* Additional Yeelight models

* Treat http headers case insensitive

* Update change log

* Treat http headers case insensitive (consider Qt version)

* API - Consider provided format when setImage

* UI - Support Boblight configuration per LED instance

* Support multiple Boblight clients with different priorities

* Update changelog

* Simplify isGUI rules allowing for QT only builds

* Sysinfo: Fix indents

* LED-Devices: Show warning, if get properties failed

* Qt-Grabber: Fixed position handling of multiple monitors

* LED layout: Remove indention limitations

* Yeelight: Test YLTD003

* hyperion-remote: Provide image filename to muxer/UI

* Refactor PriorityMuxer and related

* Temp: Build under Windows 2019

* Yeelight: Remove YLTD003 as it is not working without additional changes

* Test Windows-latest with out removing redistributables/new MSVC

* correct workflows

* correct CI script

* Build Windows with Qt 5.15.2

* Priority Muxer: Updates after testing

* Fix Typo

* Update BGHandler

* QTGrabber - Reactivate windows code to avoid cursor issues

* Emit prioritiesChanged when autoselect was changed by user

Co-authored-by: Paulchen Panther <Paulchen-Panter@protonmail.com>
2022-02-22 20:58:59 +01:00

130 lines
3.0 KiB
C++

// system includes
#include <stdexcept>
// project includes
#include <boblightserver/BoblightServer.h>
#include "BoblightClientConnection.h"
// hyperion includes
#include <hyperion/Hyperion.h>
// qt incl
#include <QTcpServer>
// netUtil
#include <utils/NetUtils.h>
using namespace hyperion;
BoblightServer::BoblightServer(Hyperion* hyperion,const QJsonDocument& config)
: QObject()
, _hyperion(hyperion)
, _server(new QTcpServer(this))
, _openConnections()
, _priority(0)
, _log(nullptr)
, _port(0)
{
QString subComponent = _hyperion->property("instance").toString();
_log= Logger::getInstance("BOBLIGHT", subComponent);
Debug(_log, "Instance created");
// listen for component change
connect(_hyperion, &Hyperion::compStateChangeRequest, this, &BoblightServer::compStateChangeRequest);
// listen new connection signal from server
connect(_server, &QTcpServer::newConnection, this, &BoblightServer::newConnection);
// init
handleSettingsUpdate(settings::BOBLSERVER, config);
}
BoblightServer::~BoblightServer()
{
stop();
}
void BoblightServer::start()
{
if ( _server->isListening() )
return;
if (NetUtils::portAvailable(_port, _log))
_server->listen(QHostAddress::Any, _port);
Info(_log, "Started on port: %d", _port);
_hyperion->setNewComponentState(COMP_BOBLIGHTSERVER, _server->isListening());
}
void BoblightServer::stop()
{
if ( ! _server->isListening() )
return;
qDeleteAll(_openConnections);
_server->close();
Info(_log, "Stopped");
_hyperion->setNewComponentState(COMP_BOBLIGHTSERVER, _server->isListening());
}
bool BoblightServer::active() const
{
return _server->isListening();
}
void BoblightServer::compStateChangeRequest(hyperion::Components component, bool enable)
{
if (component == COMP_BOBLIGHTSERVER)
{
if (_server->isListening() != enable)
{
if (enable) start();
else stop();
}
}
}
uint16_t BoblightServer::getPort() const
{
return _server->serverPort();
}
void BoblightServer::newConnection()
{
QTcpSocket * socket = _server->nextPendingConnection();
if (socket != nullptr)
{
Info(_log, "New connection from %s ", QSTRING_CSTR(QString("Boblight@%1").arg(socket->peerAddress().toString())));
BoblightClientConnection * connection = new BoblightClientConnection(_hyperion, socket, _priority);
_openConnections.insert(connection);
// register slot for cleaning up after the connection closed
connect(connection, &BoblightClientConnection::connectionClosed, this, &BoblightServer::closedConnection);
}
}
void BoblightServer::closedConnection(BoblightClientConnection *connection)
{
Debug(_log, "Connection closed for %s", QSTRING_CSTR(QString("Boblight@%1").arg(connection->getClientAddress())));
_openConnections.remove(connection);
// schedule to delete the connection object
connection->deleteLater();
}
void BoblightServer::handleSettingsUpdate(settings::type type, const QJsonDocument& config)
{
if(type == settings::BOBLSERVER)
{
QJsonObject obj = config.object();
_port = obj["port"].toInt();
_priority = obj["priority"].toInt();
stop();
if(obj["enable"].toBool())
start();
}
}