mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
migrate std::string to qstring + add sysinfo via json (#412)
* std::string -> qstring part 1 * more string migration * more string migration ... * ... * more qstring mogrations add sysinfo via json * remove unneccessary includes * integrate sysinfo into webui
This commit is contained in:
@@ -41,6 +41,7 @@
|
||||
#include <HyperionConfig.h>
|
||||
#include <utils/jsonschema/QJsonFactory.h>
|
||||
#include <utils/Process.h>
|
||||
#include <utils/SysInfo.h>
|
||||
|
||||
// project includes
|
||||
#include "JsonClientConnection.h"
|
||||
@@ -98,13 +99,13 @@ void JsonClientConnection::readData()
|
||||
while(bytes > 0)
|
||||
{
|
||||
// create message string
|
||||
std::string message(_receiveBuffer.data(), bytes);
|
||||
QString message(QByteArray(_receiveBuffer.data(), bytes));
|
||||
|
||||
// remove message data from buffer
|
||||
_receiveBuffer = _receiveBuffer.mid(bytes);
|
||||
|
||||
// handle message
|
||||
handleMessage(QString::fromStdString(message));
|
||||
handleMessage(message);
|
||||
|
||||
// try too look up '\n' again
|
||||
bytes = _receiveBuffer.indexOf('\n') + 1;
|
||||
@@ -215,14 +216,14 @@ void JsonClientConnection::doWebSocketHandshake()
|
||||
|
||||
// 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());
|
||||
QByteArray value = _receiveBuffer.mid(start, _receiveBuffer.indexOf("\r\n", start) - start);
|
||||
_receiveBuffer.clear();
|
||||
|
||||
// must be always appended
|
||||
value += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
||||
|
||||
// generate sha1 hash
|
||||
QByteArray hash = QCryptographicHash::hash(value.c_str(), QCryptographicHash::Sha1);
|
||||
QByteArray hash = QCryptographicHash::hash(value, QCryptographicHash::Sha1);
|
||||
|
||||
// prepare an answer
|
||||
std::ostringstream h;
|
||||
@@ -292,38 +293,23 @@ void JsonClientConnection::handleMessage(const QString& messageString)
|
||||
|
||||
int tan = message["tan"].toInt(0);
|
||||
// switch over all possible commands and handle them
|
||||
if (command == "color")
|
||||
handleColorCommand(message, command, tan);
|
||||
else if (command == "image")
|
||||
handleImageCommand(message, command, tan);
|
||||
else if (command == "effect")
|
||||
handleEffectCommand(message, command, tan);
|
||||
else if (command == "create-effect")
|
||||
handleCreateEffectCommand(message, command, tan);
|
||||
else if (command == "delete-effect")
|
||||
handleDeleteEffectCommand(message, command, tan);
|
||||
else if (command == "serverinfo")
|
||||
handleServerInfoCommand(message, command, tan);
|
||||
else if (command == "clear")
|
||||
handleClearCommand(message, command, tan);
|
||||
else if (command == "clearall")
|
||||
handleClearallCommand(message, command, tan);
|
||||
else if (command == "adjustment")
|
||||
handleAdjustmentCommand(message, command, tan);
|
||||
else if (command == "sourceselect")
|
||||
handleSourceSelectCommand(message, command, tan);
|
||||
else if (command == "config")
|
||||
handleConfigCommand(message, command, tan);
|
||||
else if (command == "componentstate")
|
||||
handleComponentStateCommand(message, command, tan);
|
||||
else if (command == "ledcolors")
|
||||
handleLedColorsCommand(message, command, tan);
|
||||
else if (command == "logging")
|
||||
handleLoggingCommand(message, command, tan);
|
||||
else if (command == "processing")
|
||||
handleProcessingCommand(message, command, tan);
|
||||
else
|
||||
handleNotImplemented();
|
||||
if (command == "color") handleColorCommand (message, command, tan);
|
||||
else if (command == "image") handleImageCommand (message, command, tan);
|
||||
else if (command == "effect") handleEffectCommand (message, command, tan);
|
||||
else if (command == "create-effect") handleCreateEffectCommand (message, command, tan);
|
||||
else if (command == "delete-effect") handleDeleteEffectCommand (message, command, tan);
|
||||
else if (command == "serverinfo") handleServerInfoCommand (message, command, tan);
|
||||
else if (command == "sysinfo") handleSysInfoCommand (message, command, tan);
|
||||
else if (command == "clear") handleClearCommand (message, command, tan);
|
||||
else if (command == "clearall") handleClearallCommand (message, command, tan);
|
||||
else if (command == "adjustment") handleAdjustmentCommand (message, command, tan);
|
||||
else if (command == "sourceselect") handleSourceSelectCommand (message, command, tan);
|
||||
else if (command == "config") handleConfigCommand (message, command, tan);
|
||||
else if (command == "componentstate") handleComponentStateCommand(message, command, tan);
|
||||
else if (command == "ledcolors") handleLedColorsCommand (message, command, tan);
|
||||
else if (command == "logging") handleLoggingCommand (message, command, tan);
|
||||
else if (command == "processing") handleProcessingCommand (message, command, tan);
|
||||
else handleNotImplemented ();
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
@@ -571,6 +557,41 @@ void JsonClientConnection::handleDeleteEffectCommand(const QJsonObject& message,
|
||||
sendErrorReply("Error while parsing json: Message size " + QString(message.size()), command, tan);
|
||||
}
|
||||
|
||||
|
||||
void JsonClientConnection::handleSysInfoCommand(const QJsonObject&, const QString& command, const int tan)
|
||||
{
|
||||
// create result
|
||||
QJsonObject result;
|
||||
QJsonObject info;
|
||||
result["success"] = true;
|
||||
result["command"] = command;
|
||||
result["tan"] = tan;
|
||||
|
||||
SysInfo::HyperionSysInfo data = SysInfo::get();
|
||||
QJsonObject system;
|
||||
system["kernelType" ] = data.kernelType;
|
||||
system["kernelVersion" ] = data.kernelVersion;
|
||||
system["architecture" ] = data.architecture;
|
||||
system["wordSize" ] = data.wordSize;
|
||||
system["productType" ] = data.productType;
|
||||
system["productVersion"] = data.productVersion;
|
||||
system["prettyName" ] = data.prettyName;
|
||||
system["hostName" ] = data.hostName;
|
||||
info["system"] = system;
|
||||
|
||||
QJsonObject hyperion;
|
||||
hyperion["jsonrpc_version" ] = QString(HYPERION_JSON_VERSION);
|
||||
hyperion["version" ] = QString(HYPERION_VERSION);
|
||||
hyperion["build" ] = QString(HYPERION_BUILD_ID);
|
||||
hyperion["time" ] = QString(__DATE__ " " __TIME__);
|
||||
info["hyperion"] = hyperion;
|
||||
|
||||
// send the result
|
||||
result["info" ] = info;
|
||||
sendMessage(result);
|
||||
}
|
||||
|
||||
|
||||
void JsonClientConnection::handleServerInfoCommand(const QJsonObject&, const QString& command, const int tan)
|
||||
{
|
||||
// create result
|
||||
@@ -581,9 +602,6 @@ void JsonClientConnection::handleServerInfoCommand(const QJsonObject&, const QSt
|
||||
|
||||
QJsonObject info;
|
||||
|
||||
// add host name for remote clients
|
||||
info["hostname"] = QHostInfo::localHostName();
|
||||
|
||||
// collect priority information
|
||||
QJsonArray priorities;
|
||||
uint64_t now = QDateTime::currentMSecsSinceEpoch();
|
||||
@@ -677,17 +695,17 @@ void JsonClientConnection::handleServerInfoCommand(const QJsonObject&, const QSt
|
||||
|
||||
// collect adjustment information
|
||||
QJsonArray adjustmentArray;
|
||||
for (const std::string& adjustmentId : _hyperion->getAdjustmentIds())
|
||||
for (const QString& adjustmentId : _hyperion->getAdjustmentIds())
|
||||
{
|
||||
const ColorAdjustment * colorAdjustment = _hyperion->getAdjustment(adjustmentId);
|
||||
if (colorAdjustment == nullptr)
|
||||
{
|
||||
Error(_log, "Incorrect color adjustment id: %s", adjustmentId.c_str());
|
||||
Error(_log, "Incorrect color adjustment id: %s", QSTRING_CSTR(adjustmentId));
|
||||
continue;
|
||||
}
|
||||
|
||||
QJsonObject adjustment;
|
||||
adjustment["id"] = QString::fromStdString(adjustmentId);
|
||||
adjustment["id"] = adjustmentId;
|
||||
|
||||
QJsonArray blackAdjust;
|
||||
blackAdjust.append(colorAdjustment->_rgbBlackAdjustment.getAdjustmentR());
|
||||
@@ -766,11 +784,11 @@ void JsonClientConnection::handleServerInfoCommand(const QJsonObject&, const QSt
|
||||
|
||||
// get available led devices
|
||||
QJsonObject ledDevices;
|
||||
ledDevices["active"] = QString::fromStdString(LedDevice::activeDevice());
|
||||
ledDevices["active"] =LedDevice::activeDevice();
|
||||
QJsonArray availableLedDevices;
|
||||
for (auto dev: LedDevice::getDeviceMap())
|
||||
{
|
||||
availableLedDevices.append(QString::fromStdString(dev.first));
|
||||
availableLedDevices.append(dev.first);
|
||||
}
|
||||
|
||||
ledDevices["available"] = availableLedDevices;
|
||||
@@ -805,17 +823,10 @@ void JsonClientConnection::handleServerInfoCommand(const QJsonObject&, const QSt
|
||||
info["components"] = component;
|
||||
info["ledMAppingType"] = ImageProcessor::mappingTypeToStr(_hyperion->getLedMappingType());
|
||||
|
||||
// Add Hyperion Version, build time
|
||||
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__);
|
||||
ver["config_modified"] = _hyperion->configModified();
|
||||
ver["config_writeable"] = _hyperion->configWriteable();
|
||||
|
||||
hyperion.append(ver);
|
||||
// Add Hyperion
|
||||
QJsonObject hyperion;
|
||||
hyperion["config_modified" ] = _hyperion->configModified();
|
||||
hyperion["config_writeable"] = _hyperion->configWriteable();
|
||||
info["hyperion"] = hyperion;
|
||||
|
||||
// send the result
|
||||
@@ -852,8 +863,8 @@ void JsonClientConnection::handleAdjustmentCommand(const QJsonObject& message, c
|
||||
{
|
||||
const QJsonObject & adjustment = message["adjustment"].toObject();
|
||||
|
||||
const QString adjustmentId = adjustment["id"].toString(QString::fromStdString(_hyperion->getAdjustmentIds().front()));
|
||||
ColorAdjustment * colorAdjustment = _hyperion->getAdjustment(adjustmentId.toStdString());
|
||||
const QString adjustmentId = adjustment["id"].toString(_hyperion->getAdjustmentIds().first());
|
||||
ColorAdjustment * colorAdjustment = _hyperion->getAdjustment(adjustmentId);
|
||||
if (colorAdjustment == nullptr)
|
||||
{
|
||||
Warning(_log, "Incorrect adjustment identifier: %s", adjustmentId.toStdString().c_str());
|
||||
@@ -1008,7 +1019,7 @@ void JsonClientConnection::handleConfigGetCommand(const QJsonObject& message, co
|
||||
|
||||
try
|
||||
{
|
||||
result["result"] = QJsonFactory::readConfig(QString::fromStdString(_hyperion->getConfigFileName()));
|
||||
result["result"] = QJsonFactory::readConfig(_hyperion->getConfigFileName());
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
@@ -1387,9 +1398,7 @@ bool JsonClientConnection::checkJson(const QJsonObject& message, const QString&
|
||||
}
|
||||
}
|
||||
|
||||
std::stringstream sstream;
|
||||
sstream << "Schema error: " << error.errorString().toStdString() << " at Line: " << errorLine << ", Column: " << errorColumn;
|
||||
errorMessage = QString::fromStdString(sstream.str());
|
||||
errorMessage = "Schema error: " + error.errorString() + " at Line: " + QString::number(errorLine) + ", Column: " + QString::number(errorColumn);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1399,15 +1408,13 @@ bool JsonClientConnection::checkJson(const QJsonObject& message, const QString&
|
||||
// check the message
|
||||
if (!schemaChecker.validate(message, ignoreRequired))
|
||||
{
|
||||
const std::list<std::string> & errors = schemaChecker.getMessages();
|
||||
std::stringstream ss;
|
||||
ss << "{";
|
||||
foreach (const std::string & error, errors)
|
||||
const QStringList & errors = schemaChecker.getMessages();
|
||||
errorMessage = "{";
|
||||
foreach (auto & error, errors)
|
||||
{
|
||||
ss << error << " ";
|
||||
errorMessage += error + " ";
|
||||
}
|
||||
ss << "}";
|
||||
errorMessage = QString::fromStdString(ss.str());
|
||||
errorMessage += "}";
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@@ -1,13 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
// stl includes
|
||||
#include <string>
|
||||
|
||||
// Qt includes
|
||||
#include <QByteArray>
|
||||
#include <QTcpSocket>
|
||||
#include <QMutex>
|
||||
#include <QHostAddress>
|
||||
#include <QString>
|
||||
|
||||
// Hyperion includes
|
||||
#include <hyperion/Hyperion.h>
|
||||
@@ -189,6 +187,13 @@ private:
|
||||
///
|
||||
void handleServerInfoCommand(const QJsonObject & message, const QString &command, const int tan);
|
||||
|
||||
///
|
||||
/// Handle an incoming JSON System info message
|
||||
///
|
||||
/// @param message the incoming message
|
||||
///
|
||||
void handleSysInfoCommand(const QJsonObject & message, const QString &command, const int tan);
|
||||
|
||||
///
|
||||
/// Handle an incoming JSON Clear message
|
||||
///
|
||||
|
@@ -4,6 +4,7 @@
|
||||
<file alias="schema-color">schema/schema-color.json</file>
|
||||
<file alias="schema-image">schema/schema-image.json</file>
|
||||
<file alias="schema-serverinfo">schema/schema-serverinfo.json</file>
|
||||
<file alias="schema-sysinfo">schema/schema-sysinfo.json</file>
|
||||
<file alias="schema-clear">schema/schema-clear.json</file>
|
||||
<file alias="schema-clearall">schema/schema-clearall.json</file>
|
||||
<file alias="schema-adjustment">schema/schema-adjustment.json</file>
|
||||
|
15
libsrc/jsonserver/schema/schema-sysinfo.json
Normal file
15
libsrc/jsonserver/schema/schema-sysinfo.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"type":"object",
|
||||
"required":true,
|
||||
"properties":{
|
||||
"command": {
|
||||
"type" : "string",
|
||||
"required" : true,
|
||||
"enum" : ["sysinfo"]
|
||||
},
|
||||
"tan" : {
|
||||
"type" : "integer"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
}
|
@@ -5,7 +5,7 @@
|
||||
"command": {
|
||||
"type" : "string",
|
||||
"required" : true,
|
||||
"enum" : ["color", "image", "effect", "create-effect", "delete-effect", "serverinfo", "clear", "clearall", "adjustment", "sourceselect", "config", "componentstate", "ledcolors", "logging", "processing"]
|
||||
"enum" : ["color", "image", "effect", "create-effect", "delete-effect", "serverinfo", "clear", "clearall", "adjustment", "sourceselect", "config", "componentstate", "ledcolors", "logging", "processing", "sysinfo"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user