2013-08-17 19:20:19 +02:00
|
|
|
// system includes
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <cassert>
|
2016-05-30 22:39:12 +02:00
|
|
|
#include <iomanip>
|
2016-09-15 20:42:58 +02:00
|
|
|
#include <unistd.h>
|
2013-08-17 19:20:19 +02:00
|
|
|
|
2013-08-17 15:39:29 +02:00
|
|
|
// stl includes
|
|
|
|
#include <iostream>
|
2013-08-17 19:20:19 +02:00
|
|
|
#include <sstream>
|
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
// Qt includes
|
|
|
|
#include <QResource>
|
2013-08-19 20:33:36 +02:00
|
|
|
#include <QDateTime>
|
2014-10-25 22:35:53 +02:00
|
|
|
#include <QCryptographicHash>
|
|
|
|
#include <QHostInfo>
|
2016-08-06 08:28:42 +02:00
|
|
|
#include <QString>
|
2016-08-15 22:32:01 +02:00
|
|
|
#include <QFile>
|
2016-11-18 18:39:21 +01:00
|
|
|
#include <QFileInfo>
|
2016-09-15 20:42:58 +02:00
|
|
|
#include <QCoreApplication>
|
2016-10-09 22:22:17 +02:00
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QVariantMap>
|
2016-10-24 23:52:53 +02:00
|
|
|
#include <QDir>
|
2016-12-18 19:00:14 +01:00
|
|
|
#include <QImage>
|
|
|
|
#include <QBuffer>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QIODevice>
|
|
|
|
#include <QDateTime>
|
2013-08-17 15:39:29 +02:00
|
|
|
|
2013-08-18 12:21:07 +02:00
|
|
|
// hyperion util includes
|
2013-11-22 11:48:10 +01:00
|
|
|
#include <hyperion/ImageProcessorFactory.h>
|
|
|
|
#include <hyperion/ImageProcessor.h>
|
2016-02-15 20:53:03 +01:00
|
|
|
#include <hyperion/MessageForwarder.h>
|
2016-04-02 00:04:11 +02:00
|
|
|
#include <hyperion/ColorAdjustment.h>
|
2017-01-06 14:25:55 +01:00
|
|
|
#include <utils/ColorSys.h>
|
2013-11-22 11:48:10 +01:00
|
|
|
#include <utils/ColorRgb.h>
|
2016-08-23 20:07:12 +02:00
|
|
|
#include <leddevice/LedDevice.h>
|
2016-06-10 18:32:50 +02:00
|
|
|
#include <HyperionConfig.h>
|
2016-10-09 22:22:17 +02:00
|
|
|
#include <utils/jsonschema/QJsonFactory.h>
|
2016-09-15 20:42:58 +02:00
|
|
|
#include <utils/Process.h>
|
2013-08-18 12:21:07 +02:00
|
|
|
|
2013-08-17 19:20:19 +02:00
|
|
|
// project includes
|
2013-08-17 15:39:29 +02:00
|
|
|
#include "JsonClientConnection.h"
|
|
|
|
|
2016-08-11 07:13:55 +02:00
|
|
|
using namespace hyperion;
|
|
|
|
|
2016-07-31 22:21:35 +02:00
|
|
|
JsonClientConnection::JsonClientConnection(QTcpSocket *socket)
|
|
|
|
: QObject()
|
|
|
|
, _socket(socket)
|
|
|
|
, _imageProcessor(ImageProcessorFactory::getInstance().newImageProcessor())
|
|
|
|
, _hyperion(Hyperion::getInstance())
|
|
|
|
, _receiveBuffer()
|
|
|
|
, _webSocketHandshakeDone(false)
|
2016-11-26 22:46:16 +01:00
|
|
|
, _log(Logger::getInstance("JSONCLIENTCONNECTION"))
|
2016-08-11 07:13:55 +02:00
|
|
|
, _forwarder_enabled(true)
|
2016-11-26 22:34:46 +01:00
|
|
|
, _streaming_logging_activated(false)
|
2016-12-18 19:00:14 +01:00
|
|
|
, _image_stream_timeout(0)
|
2013-08-17 15:39:29 +02:00
|
|
|
{
|
2013-08-17 19:20:19 +02:00
|
|
|
// connect internal signals and slots
|
2013-08-17 15:39:29 +02:00
|
|
|
connect(_socket, SIGNAL(disconnected()), this, SLOT(socketClosed()));
|
|
|
|
connect(_socket, SIGNAL(readyRead()), this, SLOT(readData()));
|
2016-09-07 20:10:37 +02:00
|
|
|
connect(_hyperion, SIGNAL(componentStateChanged(hyperion::Components,bool)), this, SLOT(componentStateChanged(hyperion::Components,bool)));
|
2016-11-26 22:46:16 +01:00
|
|
|
|
2016-09-07 20:10:37 +02:00
|
|
|
_timer_ledcolors.setSingleShot(false);
|
|
|
|
connect(&_timer_ledcolors, SIGNAL(timeout()), this, SLOT(streamLedcolorsUpdate()));
|
2016-12-18 19:00:14 +01:00
|
|
|
_image_stream_mutex.unlock();
|
2013-08-17 15:39:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JsonClientConnection::~JsonClientConnection()
|
|
|
|
{
|
|
|
|
delete _socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonClientConnection::readData()
|
|
|
|
{
|
|
|
|
_receiveBuffer += _socket->readAll();
|
2014-10-25 22:35:53 +02:00
|
|
|
|
2014-11-08 21:01:46 +01:00
|
|
|
if (_webSocketHandshakeDone)
|
|
|
|
{
|
|
|
|
// websocket mode, data frame
|
|
|
|
handleWebSocketFrame();
|
2016-12-05 11:10:44 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-08 21:01:46 +01:00
|
|
|
{
|
|
|
|
// might be a handshake request or raw socket data
|
|
|
|
if(_receiveBuffer.contains("Upgrade: websocket"))
|
|
|
|
{
|
|
|
|
doWebSocketHandshake();
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
// raw socket data, handling as usual
|
2014-10-25 22:35:53 +02:00
|
|
|
int bytes = _receiveBuffer.indexOf('\n') + 1;
|
|
|
|
while(bytes > 0)
|
|
|
|
{
|
|
|
|
// create message string
|
|
|
|
std::string message(_receiveBuffer.data(), bytes);
|
|
|
|
|
|
|
|
// remove message data from buffer
|
|
|
|
_receiveBuffer = _receiveBuffer.mid(bytes);
|
|
|
|
|
|
|
|
// handle message
|
2016-10-09 22:22:17 +02:00
|
|
|
handleMessage(QString::fromStdString(message));
|
2014-10-25 22:35:53 +02:00
|
|
|
|
|
|
|
// try too look up '\n' again
|
|
|
|
bytes = _receiveBuffer.indexOf('\n') + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-17 15:39:29 +02:00
|
|
|
}
|
|
|
|
|
2014-11-08 21:01:46 +01:00
|
|
|
void JsonClientConnection::handleWebSocketFrame()
|
|
|
|
{
|
2016-12-05 11:10:44 +01:00
|
|
|
if ((_receiveBuffer.at(0) & BHB0_FIN) == BHB0_FIN)
|
2014-11-08 21:01:46 +01:00
|
|
|
{
|
|
|
|
// final bit found, frame complete
|
|
|
|
quint8 * maskKey = NULL;
|
2016-12-05 11:10:44 +01:00
|
|
|
quint8 opCode = _receiveBuffer.at(0) & BHB0_OPCODE;
|
|
|
|
bool isMasked = (_receiveBuffer.at(1) & BHB0_FIN) == BHB0_FIN;
|
|
|
|
quint64 payloadLength = _receiveBuffer.at(1) & BHB1_PAYLOAD;
|
2014-11-08 21:01:46 +01:00
|
|
|
quint32 index = 2;
|
|
|
|
|
|
|
|
switch (payloadLength)
|
|
|
|
{
|
2016-12-05 11:10:44 +01:00
|
|
|
case payload_size_code_16bit:
|
2014-11-08 21:01:46 +01:00
|
|
|
payloadLength = ((_receiveBuffer.at(2) << 8) & 0xFF00) | (_receiveBuffer.at(3) & 0xFF);
|
|
|
|
index += 2;
|
|
|
|
break;
|
2016-12-05 11:10:44 +01:00
|
|
|
case payload_size_code_64bit:
|
2014-11-08 21:01:46 +01:00
|
|
|
payloadLength = 0;
|
2016-12-05 11:10:44 +01:00
|
|
|
for (uint i=0; i < 8; i++)
|
|
|
|
{
|
2014-11-08 21:01:46 +01:00
|
|
|
payloadLength |= ((quint64)(_receiveBuffer.at(index+i) & 0xFF)) << (8*(7-i));
|
|
|
|
}
|
|
|
|
index += 8;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isMasked)
|
|
|
|
{
|
|
|
|
// if the data is masked we need to get the key for unmasking
|
|
|
|
maskKey = new quint8[4];
|
|
|
|
for (uint i=0; i < 4; i++)
|
|
|
|
{
|
|
|
|
maskKey[i] = _receiveBuffer.at(index + i);
|
|
|
|
}
|
|
|
|
index += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the type of data frame
|
|
|
|
switch (opCode)
|
|
|
|
{
|
2016-12-05 11:10:44 +01:00
|
|
|
case OPCODE::TEXT:
|
2014-11-08 21:01:46 +01:00
|
|
|
{
|
|
|
|
// frame contains text, extract it
|
|
|
|
QByteArray result = _receiveBuffer.mid(index, payloadLength);
|
|
|
|
_receiveBuffer.clear();
|
|
|
|
|
|
|
|
// unmask data if necessary
|
|
|
|
if (isMasked)
|
|
|
|
{
|
|
|
|
for (uint i=0; i < payloadLength; i++)
|
|
|
|
{
|
|
|
|
result[i] = (result[i] ^ maskKey[i % 4]);
|
|
|
|
}
|
|
|
|
if (maskKey != NULL)
|
|
|
|
{
|
|
|
|
delete[] maskKey;
|
|
|
|
maskKey = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
handleMessage(QString(result));
|
2014-11-08 21:01:46 +01:00
|
|
|
}
|
|
|
|
break;
|
2016-12-05 11:10:44 +01:00
|
|
|
case OPCODE::CLOSE:
|
2014-11-08 21:01:46 +01:00
|
|
|
{
|
|
|
|
// close request, confirm
|
2016-12-05 11:10:44 +01:00
|
|
|
quint8 close[] = {0x88, 0};
|
2014-11-08 21:01:46 +01:00
|
|
|
_socket->write((const char*)close, 2);
|
|
|
|
_socket->flush();
|
|
|
|
_socket->close();
|
|
|
|
}
|
|
|
|
break;
|
2016-12-05 11:10:44 +01:00
|
|
|
case OPCODE::PING:
|
2014-11-08 21:01:46 +01:00
|
|
|
{
|
|
|
|
// ping received, send pong
|
2016-12-05 11:10:44 +01:00
|
|
|
quint8 pong[] = {OPCODE::PONG, 0};
|
2014-11-08 21:01:46 +01:00
|
|
|
_socket->write((const char*)pong, 2);
|
|
|
|
_socket->flush();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-12-05 11:10:44 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-08 21:01:46 +01:00
|
|
|
{
|
2016-07-11 17:08:22 +02:00
|
|
|
Error(_log, "Someone is sending very big messages over several frames... it's not supported yet");
|
2014-11-08 21:01:46 +01:00
|
|
|
quint8 close[] = {0x88, 0};
|
|
|
|
_socket->write((const char*)close, 2);
|
|
|
|
_socket->flush();
|
|
|
|
_socket->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonClientConnection::doWebSocketHandshake()
|
|
|
|
{
|
|
|
|
// http header, might not be a very reliable check...
|
2016-07-11 17:08:22 +02:00
|
|
|
Debug(_log, "Websocket handshake");
|
2014-11-08 21:01:46 +01:00
|
|
|
|
|
|
|
// get the key to prepare an answer
|
|
|
|
int start = _receiveBuffer.indexOf("Sec-WebSocket-Key") + 19;
|
|
|
|
std::string value(_receiveBuffer.mid(start, _receiveBuffer.indexOf("\r\n", start) - start).data());
|
|
|
|
_receiveBuffer.clear();
|
|
|
|
|
|
|
|
// must be always appended
|
|
|
|
value += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
|
|
|
|
|
|
|
// generate sha1 hash
|
|
|
|
QByteArray hash = QCryptographicHash::hash(value.c_str(), QCryptographicHash::Sha1);
|
|
|
|
|
|
|
|
// prepare an answer
|
|
|
|
std::ostringstream h;
|
|
|
|
h << "HTTP/1.1 101 Switching Protocols\r\n" <<
|
|
|
|
"Upgrade: websocket\r\n" <<
|
|
|
|
"Connection: Upgrade\r\n" <<
|
|
|
|
"Sec-WebSocket-Accept: " << QString(hash.toBase64()).toStdString() << "\r\n\r\n";
|
|
|
|
|
|
|
|
_socket->write(h.str().c_str());
|
|
|
|
_socket->flush();
|
|
|
|
// we are in WebSocket mode, data frames should follow next
|
|
|
|
_webSocketHandshakeDone = true;
|
|
|
|
}
|
|
|
|
|
2013-08-17 15:39:29 +02:00
|
|
|
void JsonClientConnection::socketClosed()
|
|
|
|
{
|
2014-10-25 22:35:53 +02:00
|
|
|
_webSocketHandshakeDone = false;
|
2013-08-17 15:39:29 +02:00
|
|
|
emit connectionClosed(this);
|
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::handleMessage(const QString& messageString)
|
2013-08-17 15:39:29 +02:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
QString errors;
|
|
|
|
|
2016-08-10 19:53:53 +02:00
|
|
|
try
|
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(messageString.toUtf8(), &error);
|
|
|
|
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
2016-08-10 19:53:53 +02:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
// report to the user the failure and their locations in the document.
|
|
|
|
int errorLine(0), errorColumn(0);
|
|
|
|
|
|
|
|
for( int i=0, count=qMin( error.offset,messageString.size()); i<count; ++i )
|
|
|
|
{
|
|
|
|
++errorColumn;
|
|
|
|
if(messageString.at(i) == '\n' )
|
|
|
|
{
|
|
|
|
errorColumn = 0;
|
|
|
|
++errorLine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream sstream;
|
|
|
|
sstream << "Error while parsing json: " << error.errorString().toStdString() << " at Line: " << errorLine << ", Column: " << errorColumn;
|
|
|
|
sendErrorReply(QString::fromStdString(sstream.str()));
|
2016-08-10 19:53:53 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-10-09 22:22:17 +02:00
|
|
|
|
|
|
|
const QJsonObject message = doc.object();
|
2016-08-10 19:53:53 +02:00
|
|
|
|
|
|
|
// check basic message
|
|
|
|
if (!checkJson(message, ":schema", errors))
|
|
|
|
{
|
|
|
|
sendErrorReply("Error while validating json: " + errors);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check specific message
|
2016-10-09 22:22:17 +02:00
|
|
|
const QString command = message["command"].toString();
|
|
|
|
if (!checkJson(message, QString(":schema-%1").arg(command), errors))
|
2016-08-10 19:53:53 +02:00
|
|
|
{
|
|
|
|
sendErrorReply("Error while validating json: " + errors);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
int tan = message["tan"].toInt(0);
|
2016-08-10 19:53:53 +02:00
|
|
|
// switch over all possible commands and handle them
|
|
|
|
if (command == "color")
|
2016-08-17 11:46:36 +02:00
|
|
|
handleColorCommand(message, command, tan);
|
2016-08-10 19:53:53 +02:00
|
|
|
else if (command == "image")
|
2016-08-17 11:46:36 +02:00
|
|
|
handleImageCommand(message, command, tan);
|
2016-08-10 19:53:53 +02:00
|
|
|
else if (command == "effect")
|
2016-08-17 11:46:36 +02:00
|
|
|
handleEffectCommand(message, command, tan);
|
2016-10-24 23:52:53 +02:00
|
|
|
else if (command == "create-effect")
|
|
|
|
handleCreateEffectCommand(message, command, tan);
|
2016-11-18 18:39:21 +01:00
|
|
|
else if (command == "delete-effect")
|
|
|
|
handleDeleteEffectCommand(message, command, tan);
|
2016-08-10 19:53:53 +02:00
|
|
|
else if (command == "serverinfo")
|
2016-08-17 11:46:36 +02:00
|
|
|
handleServerInfoCommand(message, command, tan);
|
2016-08-10 19:53:53 +02:00
|
|
|
else if (command == "clear")
|
2016-08-17 11:46:36 +02:00
|
|
|
handleClearCommand(message, command, tan);
|
2016-08-10 19:53:53 +02:00
|
|
|
else if (command == "clearall")
|
2016-08-17 11:46:36 +02:00
|
|
|
handleClearallCommand(message, command, tan);
|
2016-08-10 19:53:53 +02:00
|
|
|
else if (command == "adjustment")
|
2016-08-17 11:46:36 +02:00
|
|
|
handleAdjustmentCommand(message, command, tan);
|
2016-08-10 19:53:53 +02:00
|
|
|
else if (command == "sourceselect")
|
2016-08-17 11:46:36 +02:00
|
|
|
handleSourceSelectCommand(message, command, tan);
|
2016-08-15 22:32:01 +02:00
|
|
|
else if (command == "config")
|
2016-08-17 11:46:36 +02:00
|
|
|
handleConfigCommand(message, command, tan);
|
2016-08-10 19:53:53 +02:00
|
|
|
else if (command == "componentstate")
|
2016-08-17 11:46:36 +02:00
|
|
|
handleComponentStateCommand(message, command, tan);
|
2016-09-05 17:26:29 +02:00
|
|
|
else if (command == "ledcolors")
|
|
|
|
handleLedColorsCommand(message, command, tan);
|
2016-11-26 22:34:46 +01:00
|
|
|
else if (command == "logging")
|
|
|
|
handleLoggingCommand(message, command, tan);
|
2016-12-19 23:59:50 +01:00
|
|
|
else if (command == "processing")
|
|
|
|
handleProcessingCommand(message, command, tan);
|
2016-08-10 19:53:53 +02:00
|
|
|
else
|
|
|
|
handleNotImplemented();
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
sendErrorReply("Error while processing incoming json message: " + QString(e.what()) + " " + errors );
|
|
|
|
Warning(_log, "Error while processing incoming json message: %s (%s)", e.what(), errors.toStdString().c_str());
|
2016-08-10 19:53:53 +02:00
|
|
|
}
|
2013-08-18 12:02:17 +02:00
|
|
|
}
|
|
|
|
|
2016-08-11 07:13:55 +02:00
|
|
|
void JsonClientConnection::componentStateChanged(const hyperion::Components component, bool enable)
|
|
|
|
{
|
|
|
|
if (component == COMP_FORWARDER && _forwarder_enabled != enable)
|
|
|
|
{
|
|
|
|
_forwarder_enabled = enable;
|
|
|
|
Info(_log, "forwarder change state to %s", (enable ? "enabled" : "disabled") );
|
|
|
|
}
|
|
|
|
}
|
2016-02-15 18:25:18 +01:00
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::forwardJsonMessage(const QJsonObject & message)
|
2016-02-15 18:25:18 +01:00
|
|
|
{
|
2016-08-11 07:13:55 +02:00
|
|
|
if (_forwarder_enabled)
|
2016-02-15 18:25:18 +01:00
|
|
|
{
|
2016-08-11 07:13:55 +02:00
|
|
|
QTcpSocket client;
|
|
|
|
QList<MessageForwarder::JsonSlaveAddress> list = _hyperion->getForwarder()->getJsonSlaves();
|
|
|
|
|
|
|
|
for ( int i=0; i<list.size(); i++ )
|
2016-02-15 20:53:03 +01:00
|
|
|
{
|
2016-08-11 07:13:55 +02:00
|
|
|
client.connectToHost(list.at(i).addr, list.at(i).port);
|
|
|
|
if ( client.waitForConnected(500) )
|
|
|
|
{
|
|
|
|
sendMessage(message,&client);
|
|
|
|
client.close();
|
|
|
|
}
|
2016-02-15 20:53:03 +01:00
|
|
|
}
|
2016-02-15 18:25:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::handleColorCommand(const QJsonObject& message, const QString& command, const int tan)
|
2013-08-18 12:02:17 +02:00
|
|
|
{
|
2016-02-15 18:25:18 +01:00
|
|
|
forwardJsonMessage(message);
|
|
|
|
|
2013-08-18 12:21:07 +02:00
|
|
|
// extract parameters
|
2016-10-09 22:22:17 +02:00
|
|
|
int priority = message["priority"].toInt();
|
|
|
|
int duration = message["duration"].toInt(-1);
|
2013-12-12 23:39:17 +01:00
|
|
|
|
|
|
|
std::vector<ColorRgb> colorData(_hyperion->getLedCount());
|
2016-10-09 22:22:17 +02:00
|
|
|
const QJsonArray & jsonColor = message["color"].toArray();
|
2016-10-13 21:59:58 +02:00
|
|
|
unsigned int i = 0;
|
2016-10-09 22:22:17 +02:00
|
|
|
for (; i < unsigned(jsonColor.size()/3) && i < _hyperion->getLedCount(); ++i)
|
2013-12-12 23:39:17 +01:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
colorData[i].red = uint8_t(jsonColor.at(3u*i).toInt());
|
|
|
|
colorData[i].green = uint8_t(jsonColor.at(3u*i+1u).toInt());
|
|
|
|
colorData[i].blue = uint8_t(jsonColor.at(3u*i+2u).toInt());
|
2013-12-12 23:39:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy full blocks of led colors
|
|
|
|
unsigned size = i;
|
|
|
|
while (i + size < _hyperion->getLedCount())
|
|
|
|
{
|
|
|
|
memcpy(&(colorData[i]), colorData.data(), size * sizeof(ColorRgb));
|
|
|
|
i += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy remaining block of led colors
|
|
|
|
if (i < _hyperion->getLedCount())
|
|
|
|
{
|
|
|
|
memcpy(&(colorData[i]), colorData.data(), (_hyperion->getLedCount()-i) * sizeof(ColorRgb));
|
|
|
|
}
|
2013-08-18 12:21:07 +02:00
|
|
|
|
|
|
|
// set output
|
2017-01-10 19:58:41 +01:00
|
|
|
_hyperion->setColors(priority, colorData, duration, true, hyperion::COMP_COLOR);
|
2013-08-18 12:21:07 +02:00
|
|
|
|
|
|
|
// send reply
|
2016-08-17 11:46:36 +02:00
|
|
|
sendSuccessReply(command, tan);
|
2013-08-18 12:02:17 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::handleImageCommand(const QJsonObject& message, const QString& command, const int tan)
|
2013-08-18 12:02:17 +02:00
|
|
|
{
|
2016-02-15 18:25:18 +01:00
|
|
|
forwardJsonMessage(message);
|
|
|
|
|
2013-08-18 20:55:59 +02:00
|
|
|
// extract parameters
|
2016-10-09 22:22:17 +02:00
|
|
|
int priority = message["priority"].toInt();
|
|
|
|
int duration = message["duration"].toInt(-1);
|
|
|
|
int width = message["imagewidth"].toInt();
|
|
|
|
int height = message["imageheight"].toInt();
|
|
|
|
QByteArray data = QByteArray::fromBase64(QByteArray(message["imagedata"].toString().toUtf8()));
|
2013-08-18 20:55:59 +02:00
|
|
|
|
|
|
|
// check consistency of the size of the received data
|
|
|
|
if (data.size() != width*height*3)
|
|
|
|
{
|
2016-08-17 11:46:36 +02:00
|
|
|
sendErrorReply("Size of image data does not match with the width and height", command, tan);
|
2013-08-18 20:55:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set width and height of the image processor
|
|
|
|
_imageProcessor->setSize(width, height);
|
|
|
|
|
2013-11-11 10:00:37 +01:00
|
|
|
// create ImageRgb
|
|
|
|
Image<ColorRgb> image(width, height);
|
2013-08-18 20:55:59 +02:00
|
|
|
memcpy(image.memptr(), data.data(), data.size());
|
|
|
|
|
|
|
|
// process the image
|
2013-11-11 10:00:37 +01:00
|
|
|
std::vector<ColorRgb> ledColors = _imageProcessor->process(image);
|
2013-08-18 20:55:59 +02:00
|
|
|
_hyperion->setColors(priority, ledColors, duration);
|
|
|
|
|
|
|
|
// send reply
|
2016-08-17 11:46:36 +02:00
|
|
|
sendSuccessReply(command, tan);
|
2013-08-17 15:39:29 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::handleEffectCommand(const QJsonObject& message, const QString& command, const int tan)
|
2013-11-24 16:10:48 +01:00
|
|
|
{
|
2016-02-15 18:25:18 +01:00
|
|
|
forwardJsonMessage(message);
|
|
|
|
|
2013-11-24 16:10:48 +01:00
|
|
|
// extract parameters
|
2016-10-09 22:22:17 +02:00
|
|
|
int priority = message["priority"].toInt();
|
|
|
|
int duration = message["duration"].toInt(-1);
|
2016-10-30 22:59:45 +01:00
|
|
|
QString pythonScript = message["pythonScript"].toString("");
|
2016-10-09 22:22:17 +02:00
|
|
|
const QJsonObject & effect = message["effect"].toObject();
|
|
|
|
const QString & effectName = effect["name"].toString();
|
2013-11-24 16:10:48 +01:00
|
|
|
|
|
|
|
// set output
|
2016-10-09 22:22:17 +02:00
|
|
|
if (effect.contains("args"))
|
2013-12-01 14:09:01 +01:00
|
|
|
{
|
2016-10-30 22:59:45 +01:00
|
|
|
_hyperion->setEffect(effectName, effect["args"].toObject(), priority, duration, pythonScript);
|
2013-12-01 14:09:01 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_hyperion->setEffect(effectName, priority, duration);
|
|
|
|
}
|
2013-11-24 16:10:48 +01:00
|
|
|
|
|
|
|
// send reply
|
2016-08-17 11:46:36 +02:00
|
|
|
sendSuccessReply(command, tan);
|
2013-11-24 16:10:48 +01:00
|
|
|
}
|
|
|
|
|
2016-10-24 23:52:53 +02:00
|
|
|
void JsonClientConnection::handleCreateEffectCommand(const QJsonObject& message, const QString &command, const int tan)
|
|
|
|
{
|
|
|
|
if(message.size() > 0)
|
|
|
|
{
|
|
|
|
if (!message["args"].toObject().isEmpty())
|
|
|
|
{
|
|
|
|
QString scriptName;
|
|
|
|
(message["script"].toString().mid(0, 1) == ":" )
|
|
|
|
? scriptName = ":/effects//" + message["script"].toString().mid(1)
|
|
|
|
: scriptName = message["script"].toString();
|
|
|
|
|
|
|
|
std::list<EffectSchema> effectsSchemas = _hyperion->getEffectSchemas();
|
|
|
|
std::list<EffectSchema>::iterator it = std::find_if(effectsSchemas.begin(), effectsSchemas.end(), find_schema(scriptName));
|
|
|
|
|
|
|
|
if (it != effectsSchemas.end())
|
|
|
|
{
|
|
|
|
QString errors;
|
|
|
|
|
|
|
|
if (!checkJson(message["args"].toObject(), it->schemaFile, errors))
|
|
|
|
{
|
|
|
|
sendErrorReply("Error while validating json: " + errors, command, tan);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject effectJson;
|
|
|
|
QJsonArray effectArray;
|
|
|
|
effectArray = _hyperion->getQJsonConfig()["effects"].toObject()["paths"].toArray();
|
|
|
|
|
|
|
|
if (effectArray.size() > 0)
|
|
|
|
{
|
2016-12-21 18:24:03 +01:00
|
|
|
if (message["name"].toString().trimmed().isEmpty() || message["name"].toString().trimmed().startsWith("."))
|
2016-12-08 23:39:41 +01:00
|
|
|
{
|
2016-12-21 18:24:03 +01:00
|
|
|
sendErrorReply("Can't save new effect. Effect name is empty or begins with a dot.", command, tan);
|
2016-12-08 23:39:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-24 23:52:53 +02:00
|
|
|
effectJson["name"] = message["name"].toString();
|
|
|
|
effectJson["script"] = message["script"].toString();
|
|
|
|
effectJson["args"] = message["args"].toObject();
|
2016-11-18 18:39:21 +01:00
|
|
|
|
2016-12-16 19:48:15 +01:00
|
|
|
std::list<EffectDefinition> availableEffects = _hyperion->getEffects();
|
|
|
|
std::list<EffectDefinition>::iterator iter = std::find_if(availableEffects.begin(), availableEffects.end(), find_effect(message["name"].toString()));
|
2016-11-18 18:39:21 +01:00
|
|
|
|
2016-12-16 19:48:15 +01:00
|
|
|
QFileInfo newFileName;
|
|
|
|
if (iter != availableEffects.end())
|
|
|
|
{
|
|
|
|
newFileName.setFile(iter->file);
|
|
|
|
if (newFileName.absoluteFilePath().mid(0, 1) == ":")
|
|
|
|
{
|
|
|
|
sendErrorReply("The effect name '" + message["name"].toString() + "' is assigned to an internal effect. Please rename your effekt.", command, tan);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
2016-11-18 18:39:21 +01:00
|
|
|
{
|
2016-12-16 19:48:15 +01:00
|
|
|
newFileName.setFile(effectArray[0].toString() + QDir::separator() + message["name"].toString().replace(QString(" "), QString("")) + QString(".json"));
|
|
|
|
|
|
|
|
while(newFileName.exists())
|
|
|
|
{
|
|
|
|
newFileName.setFile(effectArray[0].toString() + QDir::separator() + newFileName.baseName() + QString::number(qrand() % ((10) - 0) + 0) + QString(".json"));
|
|
|
|
}
|
2016-11-18 18:39:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QJsonFactory::writeJson(newFileName.absoluteFilePath(), effectJson);
|
2016-11-20 18:41:10 +01:00
|
|
|
Info(_log, "Reload effect list");
|
|
|
|
_hyperion->reloadEffects();
|
|
|
|
sendSuccessReply(command, tan);
|
2016-10-24 23:52:53 +02:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
sendErrorReply("Can't save new effect. Effect path empty", command, tan);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
sendErrorReply("Missing schema file for Python script " + message["script"].toString(), command, tan);
|
|
|
|
} else
|
|
|
|
sendErrorReply("Missing or empty Object 'args'", command, tan);
|
|
|
|
} else
|
|
|
|
sendErrorReply("Error while parsing json: Message size " + QString(message.size()), command, tan);
|
|
|
|
}
|
|
|
|
|
2016-11-18 18:39:21 +01:00
|
|
|
void JsonClientConnection::handleDeleteEffectCommand(const QJsonObject& message, const QString& command, const int tan)
|
|
|
|
{
|
|
|
|
if(message.size() > 0)
|
|
|
|
{
|
|
|
|
QString effectName = message["name"].toString();
|
|
|
|
std::list<EffectDefinition> effectsDefinition = _hyperion->getEffects();
|
|
|
|
std::list<EffectDefinition>::iterator it = std::find_if(effectsDefinition.begin(), effectsDefinition.end(), find_effect(effectName));
|
|
|
|
|
|
|
|
if (it != effectsDefinition.end())
|
|
|
|
{
|
|
|
|
QFileInfo effectConfigurationFile(it->file);
|
|
|
|
if (effectConfigurationFile.absoluteFilePath().mid(0, 1) != ":" )
|
|
|
|
{
|
|
|
|
if (effectConfigurationFile.exists())
|
|
|
|
{
|
|
|
|
bool result = QFile::remove(effectConfigurationFile.absoluteFilePath());
|
2016-11-20 18:41:10 +01:00
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
Info(_log, "Reload effect list");
|
|
|
|
_hyperion->reloadEffects();
|
|
|
|
sendSuccessReply(command, tan);
|
|
|
|
} else
|
|
|
|
sendErrorReply("Can't delete effect configuration file: " + effectConfigurationFile.absoluteFilePath() + ". Please check permissions", command, tan);
|
2016-11-18 18:39:21 +01:00
|
|
|
} else
|
|
|
|
sendErrorReply("Can't find effect configuration file: " + effectConfigurationFile.absoluteFilePath(), command, tan);
|
|
|
|
} else
|
|
|
|
sendErrorReply("Can't delete internal effect: " + message["name"].toString(), command, tan);
|
|
|
|
} else
|
|
|
|
sendErrorReply("Effect " + message["name"].toString() + " not found", command, tan);
|
|
|
|
} else
|
|
|
|
sendErrorReply("Error while parsing json: Message size " + QString(message.size()), command, tan);
|
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::handleServerInfoCommand(const QJsonObject&, const QString& command, const int tan)
|
2013-08-18 12:02:17 +02:00
|
|
|
{
|
2013-08-19 20:33:36 +02:00
|
|
|
// create result
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonObject result;
|
2013-08-19 20:33:36 +02:00
|
|
|
result["success"] = true;
|
2016-08-17 11:46:36 +02:00
|
|
|
result["command"] = command;
|
|
|
|
result["tan"] = tan;
|
2014-10-25 22:35:53 +02:00
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonObject info;
|
|
|
|
|
2014-10-25 22:35:53 +02:00
|
|
|
// add host name for remote clients
|
2016-10-09 22:22:17 +02:00
|
|
|
info["hostname"] = QHostInfo::localHostName();
|
2013-08-19 20:33:36 +02:00
|
|
|
|
|
|
|
// collect priority information
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonArray priorities;
|
2013-08-19 20:33:36 +02:00
|
|
|
uint64_t now = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
QList<int> activePriorities = _hyperion->getActivePriorities();
|
2016-07-27 22:52:59 +02:00
|
|
|
Hyperion::PriorityRegister priorityRegister = _hyperion->getPriorityRegister();
|
2016-07-31 22:21:35 +02:00
|
|
|
int currentPriority = _hyperion->getCurrentPriority();
|
2016-10-09 22:22:17 +02:00
|
|
|
|
2013-08-19 20:33:36 +02:00
|
|
|
foreach (int priority, activePriorities) {
|
|
|
|
const Hyperion::InputInfo & priorityInfo = _hyperion->getPriorityInfo(priority);
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonObject item;
|
2013-08-19 20:33:36 +02:00
|
|
|
item["priority"] = priority;
|
|
|
|
if (priorityInfo.timeoutTime_ms != -1)
|
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
item["duration_ms"] = int(priorityInfo.timeoutTime_ms - now);
|
2013-08-19 20:33:36 +02:00
|
|
|
}
|
2016-07-16 22:51:31 +02:00
|
|
|
|
2017-01-10 19:58:41 +01:00
|
|
|
item["owner"] = QString(hyperion::componentToIdString(priorityInfo.componentId));
|
|
|
|
item["componentId"] = priorityInfo.componentId;
|
|
|
|
item["component"] = QString(hyperion::componentToString(priorityInfo.componentId));
|
2016-07-31 22:21:35 +02:00
|
|
|
item["active"] = true;
|
|
|
|
item["visible"] = (priority == currentPriority);
|
2016-07-27 22:52:59 +02:00
|
|
|
foreach(auto const &entry, priorityRegister)
|
2016-07-16 22:51:31 +02:00
|
|
|
{
|
|
|
|
if (entry.second == priority)
|
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
item["owner"] = QString::fromStdString(entry.first);
|
2016-07-27 22:52:59 +02:00
|
|
|
priorityRegister.erase(entry.first);
|
2016-07-16 22:51:31 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-10-09 22:22:17 +02:00
|
|
|
|
2017-01-17 21:53:35 +01:00
|
|
|
if(priorityInfo.componentId == 9)
|
|
|
|
{
|
|
|
|
QJsonObject LEDcolor;
|
|
|
|
|
|
|
|
// add RGB Value to Array
|
|
|
|
QJsonArray RGBValue;
|
|
|
|
RGBValue.append(priorityInfo.ledColors.begin()->red);
|
|
|
|
RGBValue.append(priorityInfo.ledColors.begin()->green);
|
|
|
|
RGBValue.append(priorityInfo.ledColors.begin()->blue);
|
|
|
|
LEDcolor.insert("RGB", RGBValue);
|
|
|
|
|
|
|
|
uint16_t Hue;
|
|
|
|
float Saturation, Luminace;
|
|
|
|
|
|
|
|
// add HSL Value to Array
|
|
|
|
QJsonArray HSLValue;
|
|
|
|
ColorSys::rgb2hsl(priorityInfo.ledColors.begin()->red,
|
|
|
|
priorityInfo.ledColors.begin()->green,
|
|
|
|
priorityInfo.ledColors.begin()->blue,
|
|
|
|
Hue, Saturation, Luminace);
|
|
|
|
|
|
|
|
HSLValue.append(Hue);
|
|
|
|
HSLValue.append(Saturation);
|
|
|
|
HSLValue.append(Luminace);
|
|
|
|
LEDcolor.insert("HSL", HSLValue);
|
|
|
|
|
|
|
|
// add HEX Value to Array ["HEX Value"]
|
|
|
|
QJsonArray HEXValue;
|
|
|
|
std::stringstream hex;
|
|
|
|
hex << "0x"
|
|
|
|
<< std::uppercase << std::setw(2) << std::setfill('0')
|
|
|
|
<< std::hex << unsigned(priorityInfo.ledColors.begin()->red)
|
|
|
|
<< std::uppercase << std::setw(2) << std::setfill('0')
|
|
|
|
<< std::hex << unsigned(priorityInfo.ledColors.begin()->green)
|
|
|
|
<< std::uppercase << std::setw(2) << std::setfill('0')
|
|
|
|
<< std::hex << unsigned(priorityInfo.ledColors.begin()->blue);
|
|
|
|
|
|
|
|
HEXValue.append(QString::fromStdString(hex.str()));
|
|
|
|
LEDcolor.insert("HEX", HEXValue);
|
|
|
|
|
|
|
|
item["value"] = LEDcolor;
|
|
|
|
}
|
2016-10-09 22:22:17 +02:00
|
|
|
// priorities[priorities.size()] = item;
|
|
|
|
priorities.append(item);
|
2013-08-19 20:33:36 +02:00
|
|
|
}
|
2016-10-09 22:22:17 +02:00
|
|
|
|
2016-07-27 22:52:59 +02:00
|
|
|
foreach(auto const &entry, priorityRegister)
|
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonObject item;
|
2016-07-27 22:52:59 +02:00
|
|
|
item["priority"] = entry.second;
|
2016-07-31 22:21:35 +02:00
|
|
|
item["active"] = false;
|
|
|
|
item["visible"] = false;
|
2016-10-09 22:22:17 +02:00
|
|
|
item["owner"] = QString::fromStdString(entry.first);
|
|
|
|
priorities.append(item);
|
2016-07-27 22:52:59 +02:00
|
|
|
}
|
2016-03-21 17:04:02 +01:00
|
|
|
|
2016-10-10 18:29:54 +02:00
|
|
|
info["priorities"] = priorities;
|
2016-12-21 18:24:03 +01:00
|
|
|
info["priorities_autoselect"] = _hyperion->sourceAutoSelectEnabled();
|
2013-08-19 20:33:36 +02:00
|
|
|
|
2013-11-24 16:10:48 +01:00
|
|
|
// collect effect info
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonArray effects;
|
2013-12-01 14:09:01 +01:00
|
|
|
const std::list<EffectDefinition> & effectsDefinitions = _hyperion->getEffects();
|
|
|
|
for (const EffectDefinition & effectDefinition : effectsDefinitions)
|
2013-11-24 16:10:48 +01:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonObject effect;
|
2013-12-01 14:09:01 +01:00
|
|
|
effect["name"] = effectDefinition.name;
|
2016-11-18 18:39:21 +01:00
|
|
|
effect["file"] = effectDefinition.file;
|
2013-12-01 14:09:01 +01:00
|
|
|
effect["script"] = effectDefinition.script;
|
|
|
|
effect["args"] = effectDefinition.args;
|
2013-11-24 16:10:48 +01:00
|
|
|
effects.append(effect);
|
|
|
|
}
|
2016-04-24 17:07:31 +02:00
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
info["effects"] = effects;
|
2013-11-24 16:10:48 +01:00
|
|
|
|
2016-08-23 20:07:12 +02:00
|
|
|
// get available led devices
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonObject ledDevices;
|
|
|
|
ledDevices["active"] = QString::fromStdString(LedDevice::activeDevice());
|
|
|
|
QJsonArray available;
|
|
|
|
for (auto dev: LedDevice::getDeviceMap())
|
2016-08-23 20:07:12 +02:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
available.append(QString::fromStdString(dev.first));
|
2016-08-23 20:07:12 +02:00
|
|
|
}
|
2016-10-09 22:22:17 +02:00
|
|
|
|
|
|
|
ledDevices["available"] = available;
|
|
|
|
info["ledDevices"] = ledDevices;
|
2016-08-23 20:07:12 +02:00
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
// get available components
|
|
|
|
QJsonArray component;
|
2016-09-07 20:10:37 +02:00
|
|
|
std::map<hyperion::Components, bool> components = _hyperion->getComponentRegister().getRegister();
|
|
|
|
for(auto comp : components)
|
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonObject item;
|
2016-09-07 20:10:37 +02:00
|
|
|
item["id"] = comp.first;
|
2016-10-09 22:22:17 +02:00
|
|
|
item["name"] = QString::fromStdString(hyperion::componentToIdString(comp.first));
|
|
|
|
item["title"] = QString::fromStdString(hyperion::componentToString(comp.first));
|
2016-09-07 20:10:37 +02:00
|
|
|
item["enabled"] = comp.second;
|
2016-10-09 22:22:17 +02:00
|
|
|
|
|
|
|
component.append(item);
|
2016-09-07 20:10:37 +02:00
|
|
|
}
|
2016-08-23 20:07:12 +02:00
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
info["components"] = component;
|
2016-12-20 19:55:54 +01:00
|
|
|
info["ledMAppingType"] = ImageProcessor::mappingTypeToStr(_hyperion->getLedMappingType());
|
2016-10-09 22:22:17 +02:00
|
|
|
|
2016-06-10 18:32:50 +02:00
|
|
|
// Add Hyperion Version, build time
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonArray hyperion;
|
|
|
|
QJsonObject ver;
|
|
|
|
ver["jsonrpc_version"] = QString(HYPERION_JSON_VERSION);
|
|
|
|
ver["version"] = QString(HYPERION_VERSION);
|
|
|
|
ver["build"] = QString(HYPERION_BUILD_ID);
|
|
|
|
ver["time"] = QString(__DATE__ " " __TIME__);
|
2016-09-14 13:51:28 +02:00
|
|
|
ver["config_modified"] = _hyperion->configModified();
|
2016-12-14 22:45:00 +01:00
|
|
|
ver["config_writeable"] = _hyperion->configWriteable();
|
2016-06-10 18:32:50 +02:00
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
hyperion.append(ver);
|
|
|
|
info["hyperion"] = hyperion;
|
|
|
|
|
2013-08-19 20:33:36 +02:00
|
|
|
// send the result
|
2016-10-09 22:22:17 +02:00
|
|
|
result["info"] = info;
|
2013-08-19 20:33:36 +02:00
|
|
|
sendMessage(result);
|
2013-08-18 12:02:17 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::handleClearCommand(const QJsonObject& message, const QString& command, const int tan)
|
2013-08-18 12:02:17 +02:00
|
|
|
{
|
2016-02-15 18:25:18 +01:00
|
|
|
forwardJsonMessage(message);
|
|
|
|
|
2013-08-18 13:33:56 +02:00
|
|
|
// extract parameters
|
2016-10-09 22:22:17 +02:00
|
|
|
int priority = message["priority"].toInt();
|
2013-08-18 13:33:56 +02:00
|
|
|
|
|
|
|
// clear priority
|
|
|
|
_hyperion->clear(priority);
|
|
|
|
|
|
|
|
// send reply
|
2016-08-17 11:46:36 +02:00
|
|
|
sendSuccessReply(command, tan);
|
2013-08-18 12:02:17 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::handleClearallCommand(const QJsonObject& message, const QString& command, const int tan)
|
2013-08-18 12:02:17 +02:00
|
|
|
{
|
2016-02-15 18:25:18 +01:00
|
|
|
forwardJsonMessage(message);
|
|
|
|
|
2013-08-18 13:33:56 +02:00
|
|
|
// clear priority
|
|
|
|
_hyperion->clearall();
|
|
|
|
|
|
|
|
// send reply
|
2016-08-17 11:46:36 +02:00
|
|
|
sendSuccessReply(command, tan);
|
2013-08-18 12:02:17 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::handleAdjustmentCommand(const QJsonObject& message, const QString& command, const int tan)
|
2016-04-02 00:04:11 +02:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
const QJsonObject & adjustment = message["adjustment"].toObject();
|
2016-04-02 00:04:11 +02:00
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
const QString adjustmentId = adjustment["id"].toString(QString::fromStdString(_hyperion->getAdjustmentIds().front()));
|
|
|
|
ColorAdjustment * colorAdjustment = _hyperion->getAdjustment(adjustmentId.toStdString());
|
2016-04-02 00:04:11 +02:00
|
|
|
if (colorAdjustment == nullptr)
|
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
Warning(_log, "Incorrect adjustment identifier: %s", adjustmentId.toStdString().c_str());
|
2016-04-02 00:04:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-24 21:29:39 +01:00
|
|
|
if (adjustment.contains("red"))
|
2016-04-02 00:04:11 +02:00
|
|
|
{
|
2017-01-24 21:29:39 +01:00
|
|
|
const QJsonArray & values = adjustment["red"].toArray();
|
2016-10-09 22:22:17 +02:00
|
|
|
colorAdjustment->_rgbRedAdjustment.setAdjustmentR(values[0u].toInt());
|
|
|
|
colorAdjustment->_rgbRedAdjustment.setAdjustmentG(values[1u].toInt());
|
|
|
|
colorAdjustment->_rgbRedAdjustment.setAdjustmentB(values[2u].toInt());
|
2016-04-02 00:04:11 +02:00
|
|
|
}
|
|
|
|
|
2017-01-24 21:29:39 +01:00
|
|
|
if (adjustment.contains("green"))
|
2016-04-02 00:04:11 +02:00
|
|
|
{
|
2017-01-24 21:29:39 +01:00
|
|
|
const QJsonArray & values = adjustment["green"].toArray();
|
2016-10-09 22:22:17 +02:00
|
|
|
colorAdjustment->_rgbGreenAdjustment.setAdjustmentR(values[0u].toInt());
|
|
|
|
colorAdjustment->_rgbGreenAdjustment.setAdjustmentG(values[1u].toInt());
|
|
|
|
colorAdjustment->_rgbGreenAdjustment.setAdjustmentB(values[2u].toInt());
|
2016-04-02 00:04:11 +02:00
|
|
|
}
|
|
|
|
|
2017-01-24 21:29:39 +01:00
|
|
|
if (adjustment.contains("blue"))
|
2016-04-02 00:04:11 +02:00
|
|
|
{
|
2017-01-24 21:29:39 +01:00
|
|
|
const QJsonArray & values = adjustment["blue"].toArray();
|
2016-10-09 22:22:17 +02:00
|
|
|
colorAdjustment->_rgbBlueAdjustment.setAdjustmentR(values[0u].toInt());
|
|
|
|
colorAdjustment->_rgbBlueAdjustment.setAdjustmentG(values[1u].toInt());
|
|
|
|
colorAdjustment->_rgbBlueAdjustment.setAdjustmentB(values[2u].toInt());
|
2016-04-02 00:04:11 +02:00
|
|
|
}
|
2017-01-24 21:29:39 +01:00
|
|
|
if (adjustment.contains("cyan"))
|
2017-01-06 14:25:55 +01:00
|
|
|
{
|
2017-01-24 21:29:39 +01:00
|
|
|
const QJsonArray & values = adjustment["cyan"].toArray();
|
2017-01-06 14:25:55 +01:00
|
|
|
colorAdjustment->_rgbCyanAdjustment.setAdjustmentR(values[0u].toInt());
|
|
|
|
colorAdjustment->_rgbCyanAdjustment.setAdjustmentG(values[1u].toInt());
|
|
|
|
colorAdjustment->_rgbCyanAdjustment.setAdjustmentB(values[2u].toInt());
|
|
|
|
}
|
2017-01-24 21:29:39 +01:00
|
|
|
if (adjustment.contains("magenta"))
|
2017-01-06 14:25:55 +01:00
|
|
|
{
|
2017-01-24 21:29:39 +01:00
|
|
|
const QJsonArray & values = adjustment["magenta"].toArray();
|
2017-01-06 14:25:55 +01:00
|
|
|
colorAdjustment->_rgbMagentaAdjustment.setAdjustmentR(values[0u].toInt());
|
|
|
|
colorAdjustment->_rgbMagentaAdjustment.setAdjustmentG(values[1u].toInt());
|
|
|
|
colorAdjustment->_rgbMagentaAdjustment.setAdjustmentB(values[2u].toInt());
|
|
|
|
}
|
2017-01-24 21:29:39 +01:00
|
|
|
if (adjustment.contains("yellow"))
|
2017-01-06 14:25:55 +01:00
|
|
|
{
|
2017-01-24 21:29:39 +01:00
|
|
|
const QJsonArray & values = adjustment["yellow"].toArray();
|
2017-01-06 14:25:55 +01:00
|
|
|
colorAdjustment->_rgbYellowAdjustment.setAdjustmentR(values[0u].toInt());
|
|
|
|
colorAdjustment->_rgbYellowAdjustment.setAdjustmentG(values[1u].toInt());
|
|
|
|
colorAdjustment->_rgbYellowAdjustment.setAdjustmentB(values[2u].toInt());
|
|
|
|
}
|
2017-01-24 21:29:39 +01:00
|
|
|
if (adjustment.contains("black"))
|
2017-01-06 14:25:55 +01:00
|
|
|
{
|
2017-01-24 21:29:39 +01:00
|
|
|
const QJsonArray & values = adjustment["black"].toArray();
|
2017-01-06 14:25:55 +01:00
|
|
|
colorAdjustment->_rgbBlackAdjustment.setAdjustmentR(values[0u].toInt());
|
|
|
|
colorAdjustment->_rgbBlackAdjustment.setAdjustmentG(values[1u].toInt());
|
|
|
|
colorAdjustment->_rgbBlackAdjustment.setAdjustmentB(values[2u].toInt());
|
|
|
|
}
|
2017-01-24 21:29:39 +01:00
|
|
|
if (adjustment.contains("white"))
|
2017-01-06 14:25:55 +01:00
|
|
|
{
|
2017-01-24 21:29:39 +01:00
|
|
|
const QJsonArray & values = adjustment["white"].toArray();
|
2017-01-06 14:25:55 +01:00
|
|
|
colorAdjustment->_rgbWhiteAdjustment.setAdjustmentR(values[0u].toInt());
|
|
|
|
colorAdjustment->_rgbWhiteAdjustment.setAdjustmentG(values[1u].toInt());
|
|
|
|
colorAdjustment->_rgbWhiteAdjustment.setAdjustmentB(values[2u].toInt());
|
|
|
|
}
|
|
|
|
|
2017-01-24 21:29:39 +01:00
|
|
|
if (adjustment.contains("gammaRed"))
|
2017-01-06 14:25:55 +01:00
|
|
|
{
|
2017-01-24 21:29:39 +01:00
|
|
|
colorAdjustment->_rgbTransform.setGamma(adjustment["gammaRed"].toDouble(), colorAdjustment->_rgbTransform.getGammaG(), colorAdjustment->_rgbTransform.getGammaB());
|
2017-01-06 14:25:55 +01:00
|
|
|
}
|
2017-01-24 21:29:39 +01:00
|
|
|
if (adjustment.contains("gammaGreen"))
|
2017-01-06 14:25:55 +01:00
|
|
|
{
|
2017-01-24 21:29:39 +01:00
|
|
|
colorAdjustment->_rgbTransform.setGamma(colorAdjustment->_rgbTransform.getGammaR(), adjustment["gammaGreen"].toDouble(), colorAdjustment->_rgbTransform.getGammaB());
|
2017-01-06 14:25:55 +01:00
|
|
|
}
|
2017-01-24 21:29:39 +01:00
|
|
|
if (adjustment.contains("gammaBlue"))
|
2017-01-06 14:25:55 +01:00
|
|
|
{
|
2017-01-24 21:29:39 +01:00
|
|
|
colorAdjustment->_rgbTransform.setGamma(colorAdjustment->_rgbTransform.getGammaR(), colorAdjustment->_rgbTransform.getGammaG(), adjustment["gammaBlue"].toDouble());
|
2017-01-06 14:25:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (adjustment.contains("brightnessMin"))
|
|
|
|
{
|
|
|
|
colorAdjustment->_rgbTransform.setBrightnessMin(adjustment["brightnessMin"].toDouble());
|
|
|
|
}
|
|
|
|
if (adjustment.contains("brightness"))
|
|
|
|
{
|
|
|
|
colorAdjustment->_rgbTransform.setBrightness(adjustment["brightness"].toDouble());
|
|
|
|
}
|
|
|
|
|
2016-04-02 00:04:11 +02:00
|
|
|
// commit the changes
|
|
|
|
_hyperion->adjustmentsUpdated();
|
|
|
|
|
2016-08-17 11:46:36 +02:00
|
|
|
sendSuccessReply(command, tan);
|
2016-04-02 00:04:11 +02:00
|
|
|
}
|
2016-07-31 22:21:35 +02:00
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::handleSourceSelectCommand(const QJsonObject& message, const QString& command, const int tan)
|
2016-07-31 22:21:35 +02:00
|
|
|
{
|
|
|
|
bool success = false;
|
2016-10-09 22:22:17 +02:00
|
|
|
if (message["auto"].toBool(false))
|
2016-07-31 22:21:35 +02:00
|
|
|
{
|
|
|
|
_hyperion->setSourceAutoSelectEnabled(true);
|
|
|
|
success = true;
|
|
|
|
}
|
2016-10-09 22:22:17 +02:00
|
|
|
else if (message.contains("priority"))
|
2016-07-31 22:21:35 +02:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
success = _hyperion->setCurrentSourcePriority(message["priority"].toInt());
|
2016-07-31 22:21:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
{
|
2016-08-17 11:46:36 +02:00
|
|
|
sendSuccessReply(command, tan);
|
2016-07-31 22:21:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-17 11:46:36 +02:00
|
|
|
sendErrorReply("setting current priority failed", command, tan);
|
2016-07-31 22:21:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::handleConfigCommand(const QJsonObject& message, const QString& command, const int tan)
|
2016-08-15 22:32:01 +02:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
QString subcommand = message["subcommand"].toString("");
|
|
|
|
QString full_command = command + "-" + subcommand;
|
2016-08-15 22:32:01 +02:00
|
|
|
if (subcommand == "getschema")
|
|
|
|
{
|
2016-09-03 15:54:33 +02:00
|
|
|
handleSchemaGetCommand(message, full_command, tan);
|
2016-08-15 22:32:01 +02:00
|
|
|
}
|
|
|
|
else if (subcommand == "getconfig")
|
|
|
|
{
|
2016-09-03 15:54:33 +02:00
|
|
|
handleConfigGetCommand(message, full_command, tan);
|
2016-08-15 22:32:01 +02:00
|
|
|
}
|
2016-09-15 20:42:58 +02:00
|
|
|
else if (subcommand == "reload")
|
|
|
|
{
|
2016-10-10 23:08:01 +02:00
|
|
|
_hyperion->freeObjects();
|
2016-09-15 20:42:58 +02:00
|
|
|
Process::restartHyperion();
|
|
|
|
sendErrorReply("failed to restart hyperion", full_command, tan);
|
|
|
|
}
|
2016-08-15 22:32:01 +02:00
|
|
|
else
|
|
|
|
{
|
2016-09-03 15:54:33 +02:00
|
|
|
sendErrorReply("unknown or missing subcommand", full_command, tan);
|
2016-08-15 22:32:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::handleConfigGetCommand(const QJsonObject& message, const QString& command, const int tan)
|
2016-08-03 22:03:19 +02:00
|
|
|
{
|
|
|
|
// create result
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonObject result;
|
2016-08-03 22:03:19 +02:00
|
|
|
result["success"] = true;
|
2016-08-17 11:46:36 +02:00
|
|
|
result["command"] = command;
|
|
|
|
result["tan"] = tan;
|
2016-12-03 21:11:52 +01:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
result["result"] = QJsonFactory::readJson(QString::fromStdString(_hyperion->getConfigFileName()));
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
result["result"] = _hyperion->getQJsonConfig();
|
|
|
|
}
|
2016-10-09 22:22:17 +02:00
|
|
|
|
2016-08-03 22:03:19 +02:00
|
|
|
// send the result
|
|
|
|
sendMessage(result);
|
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::handleSchemaGetCommand(const QJsonObject& message, const QString& command, const int tan)
|
2016-08-15 22:32:01 +02:00
|
|
|
{
|
|
|
|
// create result
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonObject result, schemaJson, alldevices, properties;
|
2016-08-15 22:32:01 +02:00
|
|
|
result["success"] = true;
|
2016-08-17 11:46:36 +02:00
|
|
|
result["command"] = command;
|
|
|
|
result["tan"] = tan;
|
2016-09-10 19:08:08 +02:00
|
|
|
|
2016-08-15 22:32:01 +02:00
|
|
|
// make sure the resources are loaded (they may be left out after static linking)
|
|
|
|
Q_INIT_RESOURCE(resource);
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonParseError error;
|
2016-08-15 22:32:01 +02:00
|
|
|
|
2016-10-24 23:52:53 +02:00
|
|
|
// read the hyperion json schema from the resource
|
2016-12-23 19:37:35 +01:00
|
|
|
QFile schemaData(":/hyperion-schema-"+QString::number(_hyperion->getConfigVersionId()));
|
2016-10-09 22:22:17 +02:00
|
|
|
|
|
|
|
if (!schemaData.open(QIODevice::ReadOnly))
|
2016-08-15 22:32:01 +02:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
std::stringstream error;
|
|
|
|
error << "Schema not found: " << schemaData.errorString().toStdString();
|
|
|
|
throw std::runtime_error(error.str());
|
2016-08-15 22:32:01 +02:00
|
|
|
}
|
2016-10-09 22:22:17 +02:00
|
|
|
|
|
|
|
QByteArray schema = schemaData.readAll();
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(schema, &error);
|
|
|
|
schemaData.close();
|
|
|
|
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
// report to the user the failure and their locations in the document.
|
|
|
|
int errorLine(0), errorColumn(0);
|
|
|
|
|
|
|
|
for( int i=0, count=qMin( error.offset,schema.size()); i<count; ++i )
|
|
|
|
{
|
|
|
|
++errorColumn;
|
|
|
|
if(schema.at(i) == '\n' )
|
|
|
|
{
|
|
|
|
errorColumn = 0;
|
|
|
|
++errorLine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream sstream;
|
|
|
|
sstream << "ERROR: Json schema wrong: " << error.errorString().toStdString() << " at Line: " << errorLine << ", Column: " << errorColumn;
|
|
|
|
|
|
|
|
throw std::runtime_error(sstream.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaJson = doc.object();
|
|
|
|
|
2016-10-24 23:52:53 +02:00
|
|
|
// collect all LED Devices
|
2016-10-09 22:22:17 +02:00
|
|
|
properties = schemaJson["properties"].toObject();
|
|
|
|
alldevices = LedDevice::getLedDeviceSchemas();
|
|
|
|
properties.insert("alldevices", alldevices);
|
2016-10-24 23:52:53 +02:00
|
|
|
|
|
|
|
// collect all available effect schemas
|
|
|
|
QJsonObject pyEffectSchemas, pyEffectSchema;
|
|
|
|
QJsonArray in, ex;
|
|
|
|
const std::list<EffectSchema> & effectsSchemas = _hyperion->getEffectSchemas();
|
|
|
|
for (const EffectSchema & effectSchema : effectsSchemas)
|
|
|
|
{
|
|
|
|
if (effectSchema.pyFile.mid(0, 1) == ":")
|
|
|
|
{
|
|
|
|
QJsonObject internal;
|
|
|
|
internal.insert("script", effectSchema.pyFile);
|
2016-10-30 17:54:38 +01:00
|
|
|
internal.insert("schemaLocation", effectSchema.schemaFile);
|
|
|
|
internal.insert("schemaContent", effectSchema.pySchema);
|
2016-10-24 23:52:53 +02:00
|
|
|
in.append(internal);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QJsonObject external;
|
|
|
|
external.insert("script", effectSchema.pyFile);
|
2016-10-30 17:54:38 +01:00
|
|
|
external.insert("schemaLocation", effectSchema.schemaFile);
|
|
|
|
external.insert("schemaContent", effectSchema.pySchema);
|
2016-10-24 23:52:53 +02:00
|
|
|
ex.append(external);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in.empty())
|
|
|
|
pyEffectSchema.insert("internal", in);
|
|
|
|
if (!ex.empty())
|
|
|
|
pyEffectSchema.insert("external", ex);
|
|
|
|
|
|
|
|
pyEffectSchemas = pyEffectSchema;
|
|
|
|
properties.insert("effectSchemas", pyEffectSchemas);
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
schemaJson.insert("properties", properties);
|
|
|
|
|
|
|
|
result["result"] = schemaJson;
|
2016-08-15 22:32:01 +02:00
|
|
|
|
|
|
|
// send the result
|
|
|
|
sendMessage(result);
|
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::handleComponentStateCommand(const QJsonObject& message, const QString &command, const int tan)
|
2016-08-04 13:10:53 +02:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
const QJsonObject & componentState = message["componentstate"].toObject();
|
|
|
|
Components component = stringToComponent(componentState["component"].toString("invalid"));
|
2016-08-04 13:10:53 +02:00
|
|
|
|
2016-08-11 07:13:55 +02:00
|
|
|
if (component != COMP_INVALID)
|
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
_hyperion->setComponentState(component, componentState["state"].toBool(true));
|
2016-08-17 11:46:36 +02:00
|
|
|
sendSuccessReply(command, tan);
|
2016-08-11 07:13:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-17 11:46:36 +02:00
|
|
|
sendErrorReply("invalid component name", command, tan);
|
2016-08-11 07:13:55 +02:00
|
|
|
}
|
2016-08-04 13:10:53 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::handleLedColorsCommand(const QJsonObject& message, const QString &command, const int tan)
|
2016-09-05 17:26:29 +02:00
|
|
|
{
|
|
|
|
// create result
|
2016-10-09 22:22:17 +02:00
|
|
|
QString subcommand = message["subcommand"].toString("");
|
2016-12-18 19:00:14 +01:00
|
|
|
|
2016-09-07 20:10:37 +02:00
|
|
|
if (subcommand == "ledstream-start")
|
2016-09-05 17:26:29 +02:00
|
|
|
{
|
2016-12-18 19:00:14 +01:00
|
|
|
_streaming_leds_reply["success"] = true;
|
2016-09-07 20:10:37 +02:00
|
|
|
_streaming_leds_reply["command"] = command+"-ledstream-update";
|
2016-12-18 19:00:14 +01:00
|
|
|
_streaming_leds_reply["tan"] = tan;
|
2016-09-07 20:10:37 +02:00
|
|
|
_timer_ledcolors.start(125);
|
2016-09-05 17:26:29 +02:00
|
|
|
}
|
2016-09-07 20:10:37 +02:00
|
|
|
else if (subcommand == "ledstream-stop")
|
|
|
|
{
|
|
|
|
_timer_ledcolors.stop();
|
|
|
|
}
|
2016-12-18 19:00:14 +01:00
|
|
|
else if (subcommand == "imagestream-start")
|
|
|
|
{
|
|
|
|
_streaming_image_reply["success"] = true;
|
|
|
|
_streaming_image_reply["command"] = command+"-imagestream-update";
|
|
|
|
_streaming_image_reply["tan"] = tan;
|
|
|
|
connect(_hyperion, SIGNAL(emitImage(int, const Image<ColorRgb>&, const int)), this, SLOT(setImage(int, const Image<ColorRgb>&, const int)) );
|
|
|
|
}
|
|
|
|
else if (subcommand == "imagestream-stop")
|
|
|
|
{
|
|
|
|
disconnect(_hyperion, SIGNAL(emitImage(int, const Image<ColorRgb>&, const int)), this, 0 );
|
|
|
|
}
|
2016-09-07 20:10:37 +02:00
|
|
|
else
|
|
|
|
{
|
2016-12-18 19:00:14 +01:00
|
|
|
sendErrorReply("unknown subcommand \""+subcommand+"\"",command,tan);
|
2016-09-07 20:10:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendSuccessReply(command+"-"+subcommand,tan);
|
2016-09-05 17:26:29 +02:00
|
|
|
}
|
|
|
|
|
2016-11-26 22:34:46 +01:00
|
|
|
void JsonClientConnection::handleLoggingCommand(const QJsonObject& message, const QString &command, const int tan)
|
|
|
|
{
|
|
|
|
// create result
|
|
|
|
QString subcommand = message["subcommand"].toString("");
|
|
|
|
_streaming_logging_reply["success"] = true;
|
|
|
|
_streaming_logging_reply["command"] = command;
|
|
|
|
_streaming_logging_reply["tan"] = tan;
|
|
|
|
|
|
|
|
if (subcommand == "start")
|
|
|
|
{
|
|
|
|
if (!_streaming_logging_activated)
|
|
|
|
{
|
|
|
|
_streaming_logging_reply["command"] = command+"-update";
|
2016-11-26 22:46:16 +01:00
|
|
|
connect(LoggerManager::getInstance(),SIGNAL(newLogMessage(Logger::T_LOG_MESSAGE)), this, SLOT(incommingLogMessage(Logger::T_LOG_MESSAGE)));
|
|
|
|
Debug(_log, "log streaming activated"); // needed to trigger log sending
|
2016-11-26 22:34:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (subcommand == "stop")
|
|
|
|
{
|
|
|
|
if (_streaming_logging_activated)
|
|
|
|
{
|
2016-11-26 22:46:16 +01:00
|
|
|
disconnect(LoggerManager::getInstance(), SIGNAL(newLogMessage(Logger::T_LOG_MESSAGE)), this, 0);
|
2016-11-26 22:34:46 +01:00
|
|
|
_streaming_logging_activated = false;
|
2016-11-26 22:46:16 +01:00
|
|
|
Debug(_log, "log streaming deactivated");
|
|
|
|
|
2016-11-26 22:34:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sendErrorReply("unknown subcommand",command,tan);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendSuccessReply(command+"-"+subcommand,tan);
|
|
|
|
}
|
|
|
|
|
2016-12-19 23:59:50 +01:00
|
|
|
void JsonClientConnection::handleProcessingCommand(const QJsonObject& message, const QString &command, const int tan)
|
|
|
|
{
|
|
|
|
_hyperion->setLedMappingType(ImageProcessor::mappingTypeToInt( message["mappingType"].toString("multicolor_mean")) );
|
|
|
|
|
|
|
|
sendSuccessReply(command, tan);
|
|
|
|
}
|
|
|
|
|
2016-11-26 22:34:46 +01:00
|
|
|
void JsonClientConnection::incommingLogMessage(Logger::T_LOG_MESSAGE msg)
|
|
|
|
{
|
2016-12-17 22:14:30 +01:00
|
|
|
QJsonObject result, message;
|
|
|
|
QJsonArray messageArray;
|
2016-11-26 22:46:16 +01:00
|
|
|
|
2016-11-26 22:34:46 +01:00
|
|
|
if (!_streaming_logging_activated)
|
|
|
|
{
|
|
|
|
_streaming_logging_activated = true;
|
2016-11-26 22:46:16 +01:00
|
|
|
QVector<Logger::T_LOG_MESSAGE>* logBuffer = LoggerManager::getInstance()->getLogMessageBuffer();
|
2016-11-26 22:34:46 +01:00
|
|
|
for(int i=0; i<logBuffer->length(); i++)
|
|
|
|
{
|
2016-12-17 22:14:30 +01:00
|
|
|
message["appName"] = logBuffer->at(i).appName;
|
|
|
|
message["loggerName"] = logBuffer->at(i).loggerName;
|
|
|
|
message["function"] = logBuffer->at(i).function;
|
|
|
|
message["line"] = QString::number(logBuffer->at(i).line);
|
|
|
|
message["fileName"] = logBuffer->at(i).fileName;
|
|
|
|
message["message"] = logBuffer->at(i).message;
|
|
|
|
message["levelString"] = logBuffer->at(i).levelString;
|
|
|
|
|
|
|
|
messageArray.append(message);
|
2016-11-26 22:34:46 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-26 22:46:16 +01:00
|
|
|
else
|
|
|
|
{
|
2016-12-17 22:14:30 +01:00
|
|
|
message["appName"] = msg.appName;
|
|
|
|
message["loggerName"] = msg.loggerName;
|
|
|
|
message["function"] = msg.function;
|
|
|
|
message["line"] = QString::number(msg.line);
|
|
|
|
message["fileName"] = msg.fileName;
|
|
|
|
message["message"] = msg.message;
|
|
|
|
message["levelString"] = msg.levelString;
|
|
|
|
|
|
|
|
messageArray.append(message);
|
2016-11-26 22:46:16 +01:00
|
|
|
}
|
2016-11-26 22:34:46 +01:00
|
|
|
|
2016-12-17 22:14:30 +01:00
|
|
|
result.insert("messages", messageArray);
|
2016-11-26 22:46:16 +01:00
|
|
|
_streaming_logging_reply["result"] = result;
|
|
|
|
|
|
|
|
// send the result
|
|
|
|
sendMessage(_streaming_logging_reply);
|
2016-11-26 22:34:46 +01:00
|
|
|
}
|
|
|
|
|
2013-08-18 12:02:17 +02:00
|
|
|
void JsonClientConnection::handleNotImplemented()
|
2013-08-17 15:39:29 +02:00
|
|
|
{
|
|
|
|
sendErrorReply("Command not implemented");
|
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::sendMessage(const QJsonObject &message)
|
2013-08-17 15:39:29 +02:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonDocument writer(message);
|
|
|
|
QByteArray serializedReply = writer.toJson(QJsonDocument::Compact) + "\n";
|
2014-10-25 22:35:53 +02:00
|
|
|
|
2014-11-08 21:01:46 +01:00
|
|
|
if (!_webSocketHandshakeDone)
|
|
|
|
{
|
|
|
|
// raw tcp socket mode
|
2014-10-25 22:35:53 +02:00
|
|
|
_socket->write(serializedReply.data(), serializedReply.length());
|
2014-11-08 21:01:46 +01:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
// websocket mode
|
2014-10-25 22:35:53 +02:00
|
|
|
quint32 size = serializedReply.length();
|
|
|
|
|
|
|
|
// prepare data frame
|
|
|
|
QByteArray response;
|
|
|
|
response.append(0x81);
|
2014-11-08 21:01:46 +01:00
|
|
|
if (size > 125)
|
|
|
|
{
|
2014-10-25 22:35:53 +02:00
|
|
|
response.append(0x7E);
|
|
|
|
response.append((size >> 8) & 0xFF);
|
|
|
|
response.append(size & 0xFF);
|
|
|
|
} else {
|
|
|
|
response.append(size);
|
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
response.append(serializedReply, serializedReply.length());
|
2014-10-25 22:35:53 +02:00
|
|
|
|
2016-02-15 18:25:18 +01:00
|
|
|
_socket->write(response.data(), response.length());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::sendMessage(const QJsonObject & message, QTcpSocket * socket)
|
2016-02-15 18:25:18 +01:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
// serialize message
|
|
|
|
QJsonDocument writer(message);
|
|
|
|
QByteArray serializedMessage = writer.toJson(QJsonDocument::Compact) + "\n";
|
2016-02-15 18:25:18 +01:00
|
|
|
|
|
|
|
// write message
|
2016-10-09 22:22:17 +02:00
|
|
|
socket->write(serializedMessage);
|
2016-02-15 18:25:18 +01:00
|
|
|
if (!socket->waitForBytesWritten())
|
|
|
|
{
|
2016-07-11 17:08:22 +02:00
|
|
|
Debug(_log, "Error while writing data to host");
|
2016-02-15 18:25:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read reply data
|
|
|
|
QByteArray serializedReply;
|
|
|
|
while (!serializedReply.contains('\n'))
|
|
|
|
{
|
|
|
|
// receive reply
|
|
|
|
if (!socket->waitForReadyRead())
|
|
|
|
{
|
2016-07-11 17:08:22 +02:00
|
|
|
Debug(_log, "Error while writing data from host");
|
2016-02-15 18:25:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
serializedReply += socket->readAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse reply data
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument reply = QJsonDocument::fromJson(serializedReply ,&error);
|
|
|
|
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
2016-02-15 18:25:18 +01:00
|
|
|
{
|
2016-07-11 17:08:22 +02:00
|
|
|
Error(_log, "Error while parsing reply: invalid json");
|
2016-02-15 18:25:18 +01:00
|
|
|
return;
|
2014-10-25 22:35:53 +02:00
|
|
|
}
|
2016-02-15 18:25:18 +01:00
|
|
|
|
2013-08-17 15:39:29 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::sendSuccessReply(const QString &command, const int tan)
|
2013-08-18 12:21:07 +02:00
|
|
|
{
|
|
|
|
// create reply
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonObject reply;
|
2013-08-18 12:21:07 +02:00
|
|
|
reply["success"] = true;
|
2016-08-17 11:46:36 +02:00
|
|
|
reply["command"] = command;
|
|
|
|
reply["tan"] = tan;
|
2013-08-18 12:21:07 +02:00
|
|
|
|
|
|
|
// send reply
|
|
|
|
sendMessage(reply);
|
|
|
|
}
|
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
void JsonClientConnection::sendErrorReply(const QString &error, const QString &command, const int tan)
|
2013-08-17 15:39:29 +02:00
|
|
|
{
|
|
|
|
// create reply
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonObject reply;
|
2013-08-17 15:39:29 +02:00
|
|
|
reply["success"] = false;
|
|
|
|
reply["error"] = error;
|
2016-08-17 11:46:36 +02:00
|
|
|
reply["command"] = command;
|
|
|
|
reply["tan"] = tan;
|
2013-08-17 15:39:29 +02:00
|
|
|
|
|
|
|
// send reply
|
|
|
|
sendMessage(reply);
|
|
|
|
}
|
2013-08-18 12:02:17 +02:00
|
|
|
|
2016-10-09 22:22:17 +02:00
|
|
|
bool JsonClientConnection::checkJson(const QJsonObject& message, const QString& schemaResource, QString& errorMessage, bool ignoreRequired)
|
2013-08-18 12:02:17 +02:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
// make sure the resources are loaded (they may be left out after static linking)
|
|
|
|
Q_INIT_RESOURCE(JsonSchemas);
|
|
|
|
QJsonParseError error;
|
|
|
|
|
2013-08-18 12:02:17 +02:00
|
|
|
// read the json schema from the resource
|
2016-10-09 22:22:17 +02:00
|
|
|
QFile schemaData(schemaResource);
|
|
|
|
if (!schemaData.open(QIODevice::ReadOnly))
|
2013-08-18 12:02:17 +02:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
errorMessage = "Schema error: " + schemaData.errorString();
|
2016-08-10 19:53:53 +02:00
|
|
|
return false;
|
2013-08-18 12:02:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// create schema checker
|
2016-10-09 22:22:17 +02:00
|
|
|
QByteArray schema = schemaData.readAll();
|
|
|
|
QJsonDocument schemaJson = QJsonDocument::fromJson(schema, &error);
|
|
|
|
schemaData.close();
|
|
|
|
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
// report to the user the failure and their locations in the document.
|
|
|
|
int errorLine(0), errorColumn(0);
|
|
|
|
|
|
|
|
for( int i=0, count=qMin( error.offset,schema.size()); i<count; ++i )
|
|
|
|
{
|
|
|
|
++errorColumn;
|
|
|
|
if(schema.at(i) == '\n' )
|
|
|
|
{
|
|
|
|
errorColumn = 0;
|
|
|
|
++errorLine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream sstream;
|
|
|
|
sstream << "Schema error: " << error.errorString().toStdString() << " at Line: " << errorLine << ", Column: " << errorColumn;
|
|
|
|
errorMessage = QString::fromStdString(sstream.str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonSchemaChecker schemaChecker;
|
|
|
|
schemaChecker.setSchema(schemaJson.object());
|
2013-08-18 12:02:17 +02:00
|
|
|
|
|
|
|
// check the message
|
2016-10-09 22:22:17 +02:00
|
|
|
if (!schemaChecker.validate(message, ignoreRequired))
|
2013-08-18 12:02:17 +02:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
const std::list<std::string> & errors = schemaChecker.getMessages();
|
2013-08-18 12:02:17 +02:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << "{";
|
2016-10-09 22:22:17 +02:00
|
|
|
foreach (const std::string & error, errors)
|
|
|
|
{
|
2013-08-18 12:02:17 +02:00
|
|
|
ss << error << " ";
|
|
|
|
}
|
|
|
|
ss << "}";
|
2016-10-09 22:22:17 +02:00
|
|
|
errorMessage = QString::fromStdString(ss.str());
|
2013-08-18 12:02:17 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-07 20:10:37 +02:00
|
|
|
|
|
|
|
void JsonClientConnection::streamLedcolorsUpdate()
|
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonObject result;
|
|
|
|
QJsonArray leds;
|
2016-09-08 16:32:42 +02:00
|
|
|
|
2016-09-07 20:10:37 +02:00
|
|
|
const PriorityMuxer::InputInfo & priorityInfo = _hyperion->getPriorityInfo(_hyperion->getCurrentPriority());
|
2016-10-09 22:22:17 +02:00
|
|
|
for(auto color = priorityInfo.ledColors.begin(); color != priorityInfo.ledColors.end(); ++color)
|
2016-09-07 20:10:37 +02:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonObject item;
|
|
|
|
item["index"] = int(color - priorityInfo.ledColors.begin());
|
|
|
|
item["red"] = color->red;
|
|
|
|
item["green"] = color->green;
|
|
|
|
item["blue"] = color->blue;
|
|
|
|
leds.append(item);
|
2016-09-07 20:10:37 +02:00
|
|
|
}
|
2016-10-09 22:22:17 +02:00
|
|
|
|
|
|
|
result["leds"] = leds;
|
|
|
|
_streaming_leds_reply["result"] = result;
|
2016-09-07 20:10:37 +02:00
|
|
|
|
|
|
|
// send the result
|
|
|
|
sendMessage(_streaming_leds_reply);
|
2016-12-18 19:00:14 +01:00
|
|
|
}
|
2016-09-07 20:10:37 +02:00
|
|
|
|
2016-12-18 19:00:14 +01:00
|
|
|
void JsonClientConnection::setImage(int priority, const Image<ColorRgb> & image, int duration_ms)
|
|
|
|
{
|
|
|
|
if ( (_image_stream_timeout+250) < QDateTime::currentMSecsSinceEpoch() && _image_stream_mutex.tryLock(0) )
|
|
|
|
{
|
|
|
|
_image_stream_timeout = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
|
|
|
|
QImage jpgImage((const uint8_t *) image.memptr(), image.width(), image.height(), 3*image.width(), QImage::Format_RGB888);
|
|
|
|
QByteArray ba;
|
|
|
|
QBuffer buffer(&ba);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
jpgImage.save(&buffer, "jpg");
|
|
|
|
|
|
|
|
QJsonObject result;
|
|
|
|
result["image"] = "data:image/jpg;base64,"+QString(ba.toBase64());
|
|
|
|
_streaming_image_reply["result"] = result;
|
|
|
|
sendMessage(_streaming_image_reply);
|
|
|
|
|
|
|
|
_image_stream_mutex.unlock();
|
|
|
|
}
|
2016-09-07 20:10:37 +02:00
|
|
|
}
|
2016-12-18 19:00:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
|