mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Details coming soon.
This commit is contained in:
@@ -1,33 +1,38 @@
|
||||
// project includes
|
||||
#include <udplistener/UDPListener.h>
|
||||
|
||||
// hyperion includes
|
||||
#include <hyperion/Hyperion.h>
|
||||
#include <bonjour/bonjourserviceregister.h>
|
||||
|
||||
// hyperion util includes
|
||||
#include "hyperion/ImageProcessorFactory.h"
|
||||
#include "hyperion/ImageProcessor.h"
|
||||
#include "utils/ColorRgb.h"
|
||||
#include "HyperionConfig.h"
|
||||
|
||||
// qt includes
|
||||
#include <QUdpSocket>
|
||||
|
||||
using namespace hyperion;
|
||||
|
||||
UDPListener::UDPListener(const int priority, const int timeout, const QString& address, quint16 listenPort, bool shared) :
|
||||
UDPListener::UDPListener(const QJsonDocument& config) :
|
||||
QObject(),
|
||||
_hyperion(Hyperion::getInstance()),
|
||||
_server(),
|
||||
_server(new QUdpSocket(this)),
|
||||
_openConnections(),
|
||||
_priority(priority),
|
||||
_timeout(timeout),
|
||||
_priority(0),
|
||||
_timeout(0),
|
||||
_log(Logger::getInstance("UDPLISTENER")),
|
||||
_isActive(false),
|
||||
_listenPort(listenPort),
|
||||
_bondage(shared ? QAbstractSocket::ShareAddress : QAbstractSocket::DefaultForPlatform)
|
||||
_listenPort(0)
|
||||
{
|
||||
_server = new QUdpSocket(this);
|
||||
_listenAddress = address.isEmpty()? QHostAddress::AnyIPv4 : QHostAddress(address);
|
||||
|
||||
Debug(_log, "Instance created");
|
||||
// listen for comp changes
|
||||
connect(_hyperion, SIGNAL(componentStateChanged(hyperion::Components,bool)), this, SLOT(componentStateChanged(hyperion::Components,bool)));
|
||||
// Set trigger for incoming connections
|
||||
connect(_server, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
|
||||
|
||||
_hyperion->registerPriority("UDPLISTENER", _priority);
|
||||
// init
|
||||
handleSettingsUpdate(settings::UDPLISTENER, config);
|
||||
}
|
||||
|
||||
UDPListener::~UDPListener()
|
||||
@@ -51,7 +56,7 @@ void UDPListener::start()
|
||||
|
||||
if (!_server->bind(_listenAddress, _listenPort, _bondage))
|
||||
{
|
||||
Warning(_log, "Could not bind to %s:%d", _listenAddress.toString().toStdString().c_str(), _listenPort);
|
||||
Error(_log, "Could not bind to %s:%d", _listenAddress.toString().toStdString().c_str(), _listenPort);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -62,7 +67,13 @@ void UDPListener::start()
|
||||
WarningIf( ! joinGroupOK, _log, "Multicast failed");
|
||||
}
|
||||
_isActive = true;
|
||||
emit statusChanged(_isActive);
|
||||
_hyperion->getComponentRegister().componentStateChanged(COMP_UDPLISTENER, _isActive);
|
||||
|
||||
if(_bonjourService == nullptr)
|
||||
{
|
||||
_bonjourService = new BonjourServiceRegister();
|
||||
_bonjourService->registerService("_hyperiond-udp._udp", _listenPort);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +84,9 @@ void UDPListener::stop()
|
||||
|
||||
_server->close();
|
||||
_isActive = false;
|
||||
emit statusChanged(_isActive);
|
||||
Info(_log, "Stopped");
|
||||
_hyperion->clear(_priority);
|
||||
_hyperion->getComponentRegister().componentStateChanged(COMP_UDPLISTENER, _isActive);
|
||||
}
|
||||
|
||||
void UDPListener::componentStateChanged(const hyperion::Components component, bool enable)
|
||||
@@ -84,9 +97,7 @@ void UDPListener::componentStateChanged(const hyperion::Components component, bo
|
||||
{
|
||||
if (enable) start();
|
||||
else stop();
|
||||
Info(_log, "change state to %s", (enable ? "enabled" : "disabled") );
|
||||
}
|
||||
_hyperion->getComponentRegister().componentStateChanged(component, enable);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,9 +116,7 @@ void UDPListener::readPendingDatagrams()
|
||||
quint16 senderPort;
|
||||
|
||||
_server->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
|
||||
|
||||
processTheDatagram(&datagram, &sender);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,9 +135,26 @@ void UDPListener::processTheDatagram(const QByteArray * datagram, const QHostAdd
|
||||
rgb.green = datagram->at(ledIndex*3+1);
|
||||
rgb.blue = datagram->at(ledIndex*3+2);
|
||||
}
|
||||
|
||||
_hyperion->setColors(_priority, _ledColors, _timeout, -1, hyperion::COMP_UDPLISTENER, sender->toString());
|
||||
// TODO provide a setInput with origin arg to overwrite senders smarter
|
||||
_hyperion->registerInput(_priority, hyperion::COMP_UDPLISTENER, QString("UDPListener@%1").arg(sender->toString()));
|
||||
_hyperion->setInput(_priority, _ledColors, _timeout);
|
||||
}
|
||||
|
||||
void UDPListener::handleSettingsUpdate(const settings::type& type, const QJsonDocument& config)
|
||||
{
|
||||
if(type == settings::UDPLISTENER)
|
||||
{
|
||||
QJsonObject obj = config.object();
|
||||
// if we change the prio we need to make sure the old one is cleared before we apply the new one!
|
||||
stop();
|
||||
|
||||
|
||||
QString addr = obj["address"].toString("");
|
||||
_priority = obj["priority"].toInt();
|
||||
_listenPort = obj["port"].toInt();
|
||||
_listenAddress = addr.isEmpty()? QHostAddress::AnyIPv4 : QHostAddress(addr);
|
||||
_bondage = (obj["shared"].toBool(false)) ? QAbstractSocket::ShareAddress : QAbstractSocket::DefaultForPlatform;
|
||||
_timeout = obj["timeout"].toInt(10000);
|
||||
if(obj["enable"].toBool())
|
||||
start();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user