Refactor: Remove HyperionClient dependency
This commit is contained in:
parent
c45cba611d
commit
5919ee7ae7
@ -1,279 +0,0 @@
|
||||
#include "hyperionclient.h"
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
|
||||
int HyperionClient::_idCounter = 0;
|
||||
|
||||
// public
|
||||
|
||||
HyperionClient::HyperionClient(QString host, ushort port, QString priority)
|
||||
{
|
||||
_sock_p = new QTcpSocket(this);
|
||||
// _sock_p->setReadBufferSize(1); // Commented out
|
||||
_host_m = host;
|
||||
_port_m = port;
|
||||
_priority_m = priority;
|
||||
|
||||
connect(_sock_p, &QTcpSocket::connected, this, &HyperionClient::_connected);
|
||||
connect(_sock_p, &QTcpSocket::disconnected, this, &HyperionClient::_disconnected);
|
||||
connect(_sock_p, &QTcpSocket::readyRead, this, &HyperionClient::_readyRead);
|
||||
connect(_sock_p, &QTcpSocket::errorOccurred, this, &HyperionClient::_socketError); // Connect error signal
|
||||
|
||||
_connectHost();
|
||||
qDebug() << "HyperionClient: Connection attempt initiated.";
|
||||
}
|
||||
|
||||
HyperionClient::~HyperionClient()
|
||||
{
|
||||
qDebug() << "HyperionClient destructor called.";
|
||||
clearLeds();
|
||||
if (_sock_p->state() == QAbstractSocket::ConnectedState) {
|
||||
while(_sock_p->waitForBytesWritten()) {}
|
||||
_sock_p->disconnectFromHost();
|
||||
}
|
||||
delete _sock_p;
|
||||
}
|
||||
|
||||
void HyperionClient::clearLeds()
|
||||
{
|
||||
QJsonObject fullCommand;
|
||||
fullCommand["command"] = "clearall";
|
||||
fullCommand["id"] = ++_idCounter;
|
||||
|
||||
QJsonObject params;
|
||||
params["priority"] = _priority_m.toInt();
|
||||
fullCommand["params"] = params;
|
||||
|
||||
_cmd_m = QJsonDocument(fullCommand).toJson(QJsonDocument::Compact);
|
||||
_cmd_m.append("\n");
|
||||
_sendCommand();
|
||||
}
|
||||
|
||||
void HyperionClient::setLedColor(quint8 R, quint8 G, quint8 B)
|
||||
{
|
||||
QJsonObject fullCommand;
|
||||
fullCommand["command"] = "color";
|
||||
fullCommand["id"] = ++_idCounter;
|
||||
|
||||
QJsonObject params;
|
||||
QJsonArray colorArray;
|
||||
colorArray.append(R);
|
||||
colorArray.append(G);
|
||||
colorArray.append(B);
|
||||
params["color"] = colorArray;
|
||||
params["priority"] = _priority_m.toInt();
|
||||
fullCommand["params"] = params;
|
||||
|
||||
_cmd_m = QJsonDocument(fullCommand).toJson(QJsonDocument::Compact);
|
||||
_cmd_m.append("\n");
|
||||
_sendCommand();
|
||||
}
|
||||
|
||||
void HyperionClient::ledAdjustments(QString red, QString green, QString blue, QString temp, QString thres, QString trans)
|
||||
{
|
||||
_redAdjust_m = red;
|
||||
_greenAdjust_m = green;
|
||||
_blueAdjust_m = blue;
|
||||
_temperature_m = temp;
|
||||
_threshold_m = thres;
|
||||
_transform_m = trans;
|
||||
_ledAdjustments();
|
||||
}
|
||||
|
||||
// private
|
||||
|
||||
|
||||
|
||||
void HyperionClient::_connectHost()
|
||||
{
|
||||
_sock_p->connectToHost(_host_m, _port_m);
|
||||
}
|
||||
|
||||
void HyperionClient::_sendCommand()
|
||||
{
|
||||
if (_sock_p->state() == QAbstractSocket::ConnectedState) {
|
||||
qDebug() << "_sendCommand() called. Command size: " << _cmd_m.size() << ", Data: " << _cmd_m.left(1000) << "...";
|
||||
_sock_p->write(_cmd_m);
|
||||
while (_sock_p->bytesToWrite()) {
|
||||
_sock_p->waitForBytesWritten();
|
||||
}
|
||||
} else {
|
||||
qWarning() << "Socket not connected. Cannot send command.";
|
||||
}
|
||||
}
|
||||
|
||||
void HyperionClient::_ledAdjustments()
|
||||
{
|
||||
_colorAdjustmentType_m = 0;
|
||||
if (_redAdjust_m != "") {
|
||||
_colorAdjustmentType_m |= REDADJUST;
|
||||
}
|
||||
if (_greenAdjust_m != "") {
|
||||
_colorAdjustmentType_m |= GREENADJUST;
|
||||
}
|
||||
if (_blueAdjust_m != "") {
|
||||
_colorAdjustmentType_m |= BLUEADJUST;
|
||||
}
|
||||
_colorAdjustment();
|
||||
_thresholdAdjustment();
|
||||
_transformdAdjustment();
|
||||
_temperatureAdjustment(); // Ensure temperature adjustment is called
|
||||
}
|
||||
|
||||
void HyperionClient::_colorAdjustment()
|
||||
{
|
||||
if (!_colorAdjustmentType_m) {
|
||||
return;
|
||||
}
|
||||
QJsonObject fullCommand;
|
||||
fullCommand["command"] = "adjustment";
|
||||
fullCommand["id"] = ++_idCounter;
|
||||
|
||||
QJsonObject params;
|
||||
QJsonObject adjustmentObject;
|
||||
|
||||
if (_colorAdjustmentType_m & REDADJUST) {
|
||||
adjustmentObject["redAdjust"] = _redAdjust_m.toDouble();
|
||||
}
|
||||
if (_colorAdjustmentType_m & GREENADJUST) {
|
||||
adjustmentObject["greenAdjust"] = _greenAdjust_m.toDouble();
|
||||
}
|
||||
if (_colorAdjustmentType_m & BLUEADJUST) {
|
||||
adjustmentObject["blueAdjust"] = _blueAdjust_m.toDouble();
|
||||
}
|
||||
params["adjustment"] = adjustmentObject;
|
||||
params["priority"] = _priority_m.toInt();
|
||||
fullCommand["params"] = params;
|
||||
|
||||
_cmd_m = QJsonDocument(fullCommand).toJson(QJsonDocument::Compact);
|
||||
_cmd_m.append("\n");
|
||||
_sendCommand();
|
||||
}
|
||||
|
||||
void HyperionClient::_thresholdAdjustment()
|
||||
{
|
||||
if (_threshold_m == "") {
|
||||
return;
|
||||
}
|
||||
QJsonObject fullCommand;
|
||||
fullCommand["command"] = "transform";
|
||||
fullCommand["id"] = ++_idCounter;
|
||||
|
||||
QJsonObject params;
|
||||
QJsonObject transformObject;
|
||||
transformObject["threshold"] = _threshold_m.toDouble();
|
||||
params["transform"] = transformObject;
|
||||
params["priority"] = _priority_m.toInt();
|
||||
fullCommand["params"] = params;
|
||||
|
||||
_cmd_m = QJsonDocument(fullCommand).toJson(QJsonDocument::Compact);
|
||||
_cmd_m.append("\n");
|
||||
_sendCommand();
|
||||
}
|
||||
|
||||
void HyperionClient::_transformdAdjustment()
|
||||
{
|
||||
if (_transform_m == "") {
|
||||
return;
|
||||
}
|
||||
QStringList values = _transform_m.split(',');
|
||||
if (values.size() != 3) {
|
||||
qWarning() << "Invalid transform values:" << _transform_m;
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject fullCommand;
|
||||
fullCommand["command"] = "transform";
|
||||
fullCommand["id"] = ++_idCounter;
|
||||
|
||||
QJsonObject params;
|
||||
QJsonObject transformObject;
|
||||
transformObject["luminanceGain"] = values.at(0).toDouble();
|
||||
transformObject["luminanceMinimum"] = values.at(1).toDouble();
|
||||
transformObject["saturationGain"] = values.at(2).toDouble();
|
||||
params["transform"] = transformObject;
|
||||
params["priority"] = _priority_m.toInt();
|
||||
fullCommand["params"] = params;
|
||||
|
||||
_cmd_m = QJsonDocument(fullCommand).toJson(QJsonDocument::Compact);
|
||||
_cmd_m.append("\n");
|
||||
_sendCommand();
|
||||
}
|
||||
|
||||
void HyperionClient::_temperatureAdjustment()
|
||||
{
|
||||
if (_temperature_m == "") {
|
||||
return;
|
||||
}
|
||||
QJsonObject fullCommand;
|
||||
fullCommand["command"] = "temperature";
|
||||
fullCommand["id"] = ++_idCounter;
|
||||
|
||||
QJsonObject params;
|
||||
params["temperature"] = _temperature_m.toDouble();
|
||||
params["priority"] = _priority_m.toInt();
|
||||
fullCommand["params"] = params;
|
||||
|
||||
_cmd_m = QJsonDocument(fullCommand).toJson(QJsonDocument::Compact);
|
||||
_cmd_m.append("\n");
|
||||
_sendCommand();
|
||||
}
|
||||
|
||||
// public slots
|
||||
|
||||
void HyperionClient::sendImage(const uchar *data, int size)
|
||||
{
|
||||
QJsonObject fullCommand;
|
||||
fullCommand["command"] = "image";
|
||||
fullCommand["id"] = ++_idCounter;
|
||||
|
||||
_imageParams_m["imagedata"] = QString(QByteArray::fromRawData(reinterpret_cast<const char*>(data), size).toBase64());
|
||||
|
||||
fullCommand["params"] = _imageParams_m;
|
||||
|
||||
_cmd_m = QJsonDocument(fullCommand).toJson(QJsonDocument::Compact);
|
||||
_cmd_m.append("\n"); // Add newline terminator
|
||||
|
||||
qDebug() << "Sending image command: " << _cmd_m.left(500) << "..."; // Log first 500 chars
|
||||
_sendCommand();
|
||||
}
|
||||
|
||||
void HyperionClient::setImgSize(int width, int height)
|
||||
{
|
||||
_imageParams_m["priority"] = _priority_m.toInt();
|
||||
_imageParams_m["imageheight"] = height;
|
||||
_imageParams_m["imagewidth"] = width;
|
||||
_imageParams_m["duration"] = -1; // Default value
|
||||
_imageParams_m["scale"] = -1; // Default value
|
||||
_imageParams_m["format"] = "rgb"; // Default value
|
||||
_imageParams_m["name"] = ""; // Default value
|
||||
}
|
||||
|
||||
|
||||
// private slots
|
||||
|
||||
void HyperionClient::_connected()
|
||||
{
|
||||
qDebug() << "Successfully connected to Hyperion server at" << _host_m << ":" << _port_m;
|
||||
_ledAdjustments(); // Send initial adjustments after connection
|
||||
emit clientConnected(); // Emit signal
|
||||
}
|
||||
|
||||
void HyperionClient::_disconnected()
|
||||
{
|
||||
qDebug() << "Disconnected from Hyperion server. Attempting to reconnect in 5 seconds...";
|
||||
QTimer::singleShot(5000, this, &HyperionClient::_connectHost); // Reconnect after a delay
|
||||
}
|
||||
|
||||
void HyperionClient::_readyRead()
|
||||
{
|
||||
QByteArray data = _sock_p->readAll();
|
||||
qDebug() << "Received from Hyperion: " << data;
|
||||
}
|
||||
|
||||
void HyperionClient::_socketError(QAbstractSocket::SocketError socketError)
|
||||
{
|
||||
Q_UNUSED(socketError);
|
||||
qWarning() << "Socket Error:" << _sock_p->errorString();
|
||||
}
|
||||
@ -1,66 +0,0 @@
|
||||
#ifndef HYPERIONCLIENT_H
|
||||
#define HYPERIONCLIENT_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QTcpSocket>
|
||||
#include <QByteArray>
|
||||
#include <QTimer> // Added for QTimer::singleShot
|
||||
#include <QJsonObject>
|
||||
|
||||
class HyperionClient : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit HyperionClient(QString host, ushort port, QString priority);
|
||||
~HyperionClient();
|
||||
|
||||
void clearLeds();
|
||||
void setLedColor(quint8 R, quint8 G, quint8 B);
|
||||
void ledAdjustments(QString red, QString green, QString blue, QString temp, QString thres, QString trans);
|
||||
void sendImage(const uchar *data, int size);
|
||||
void setImgSize(int width, int height);
|
||||
|
||||
signals:
|
||||
void clientConnected();
|
||||
|
||||
private slots:
|
||||
void _connected();
|
||||
void _disconnected();
|
||||
void _readyRead();
|
||||
void _socketError(QAbstractSocket::SocketError socketError);
|
||||
|
||||
private:
|
||||
QTcpSocket* _sock_p;
|
||||
QString _host_m;
|
||||
ushort _port_m;
|
||||
QString _priority_m;
|
||||
QByteArray _cmd_m;
|
||||
QByteArray imgCmdBuf;
|
||||
QJsonObject _imageParams_m;
|
||||
|
||||
void _connectHost();
|
||||
void _sendCommand();
|
||||
void _ledAdjustments();
|
||||
void _colorAdjustment();
|
||||
void _thresholdAdjustment();
|
||||
void _transformdAdjustment();
|
||||
void _temperatureAdjustment();
|
||||
|
||||
enum ColorAdjustmentType {
|
||||
REDADJUST = 1,
|
||||
GREENADJUST = 2,
|
||||
BLUEADJUST = 4
|
||||
};
|
||||
quint8 _colorAdjustmentType_m;
|
||||
QString _redAdjust_m;
|
||||
QString _greenAdjust_m;
|
||||
QString _blueAdjust_m;
|
||||
QString _temperature_m;
|
||||
QString _threshold_m;
|
||||
QString _transform_m;
|
||||
|
||||
static int _idCounter;
|
||||
};
|
||||
|
||||
#endif // HYPERIONCLIENT_H
|
||||
Loading…
x
Reference in New Issue
Block a user