hyperion.ng/libsrc/boblightserver/BoblightServer.cpp
redPanther 2beccb0912 Webui: extend led hardware config + connection lost page (#226)
* split content and js
tune leds config

* implement connection lost page

* split js/html in huebridge

* add js action for connection lost

* extend led config
make connection loss nicer

* tune led code
add menu entry for grabber

* more tuning of webui

* switch back to botstrap textarea
add v4l to components

* add icon

* extend schema for jsoneditor

* implement ledcolors streaming with 4fps

* implement component state
2016-09-07 20:10:37 +02:00

104 lines
2.3 KiB
C++

// system includes
#include <stdexcept>
// project includes
#include <boblightserver/BoblightServer.h>
#include "BoblightClientConnection.h"
using namespace hyperion;
BoblightServer::BoblightServer(const int priority, uint16_t port)
: QObject()
, _hyperion(Hyperion::getInstance())
, _server()
, _openConnections()
, _priority(priority)
, _log(Logger::getInstance("BOBLIGHT"))
, _isActive(false)
, _port(port)
{
// Set trigger for incoming connections
connect(&_server, SIGNAL(newConnection()), this, SLOT(newConnection()));
}
BoblightServer::~BoblightServer()
{
stop();
}
void BoblightServer::start()
{
if ( active() )
return;
if (!_server.listen(QHostAddress::Any, _port))
{
throw std::runtime_error("BOBLIGHT ERROR: server could not bind to port");
}
Info(_log, "Boblight server started on port %d", _port);
_isActive = true;
emit statusChanged(_isActive);
_hyperion->registerPriority("Boblight", _priority);
}
void BoblightServer::stop()
{
if ( ! active() )
return;
foreach (BoblightClientConnection * connection, _openConnections) {
delete connection;
}
_server.close();
_isActive = false;
emit statusChanged(_isActive);
_hyperion->unRegisterPriority("Boblight");
}
void BoblightServer::componentStateChanged(const hyperion::Components component, bool enable)
{
if (component == COMP_BOBLIGHTSERVER)
{
if (_isActive != enable)
{
if (enable) start();
else stop();
Info(_log, "change state to %s", (_isActive ? "enabled" : "disabled") );
}
_hyperion->getComponentRegister().componentStateChanged(component, _isActive);
}
}
uint16_t BoblightServer::getPort() const
{
return _server.serverPort();
}
void BoblightServer::newConnection()
{
QTcpSocket * socket = _server.nextPendingConnection();
if (socket != nullptr)
{
Info(_log, "new connection");
BoblightClientConnection * connection = new BoblightClientConnection(socket, _priority);
_openConnections.insert(connection);
// register slot for cleaning up after the connection closed
connect(connection, SIGNAL(connectionClosed(BoblightClientConnection*)), this, SLOT(closedConnection(BoblightClientConnection*)));
}
}
void BoblightServer::closedConnection(BoblightClientConnection *connection)
{
Debug(_log, "connection closed");
_openConnections.remove(connection);
// schedule to delete the connection object
connection->deleteLater();
}