// stl includes #include #include #include #include // Qt includes #include #include #include #include // hyperion-remote includes #include "JsonConnection.h" JsonConnection::JsonConnection(const QString & address, bool printJson) : _printJson(printJson) , _socket() { QStringList parts = address.split(":"); if (parts.size() != 2) { throw std::runtime_error(QString("Wrong address: unable to parse address (%1)").arg(address).toStdString()); } bool ok; uint16_t port = parts[1].toUShort(&ok); if (!ok) { throw std::runtime_error(QString("Wrong address: Unable to parse the port number (%1)").arg(parts[1]).toStdString()); } _socket.connectToHost(parts[0], port); if (!_socket.waitForConnected()) { throw std::runtime_error("Unable to connect to host"); } qDebug() << "Connected to:" << address; } JsonConnection::~JsonConnection() { _socket.close(); } void JsonConnection::setColor(std::vector colors, int priority, int duration) { qDebug() << "Set color to " << colors[0].red() << " " << colors[0].green() << " " << colors[0].blue() << (colors.size() > 1 ? " + ..." : ""); // create command QJsonObject command; command["command"] = QString("color"); command["priority"] = priority; QJsonArray rgbValue; for (const QColor & color : colors) { rgbValue.append(color.red()); rgbValue.append(color.green()); rgbValue.append(color.blue()); } command["color"] = rgbValue; if (duration > 0) { command["duration"] = duration; } // send command message QJsonObject reply = sendMessage(command); // parse reply message parseReply(reply); } void JsonConnection::setImage(QImage &image, int priority, int duration) { qDebug() << "Set image has size: " << image.width() << "x" << image.height(); // ensure the image has RGB888 format image = image.convertToFormat(QImage::Format_ARGB32_Premultiplied); QByteArray binaryImage; binaryImage.reserve(image.width() * image.height() * 3); for (int i = 0; i < image.height(); ++i) { const QRgb * scanline = reinterpret_cast(image.scanLine(i)); for (int j = 0; j < image.width(); ++j) { binaryImage.append((char) qRed(scanline[j])); binaryImage.append((char) qGreen(scanline[j])); binaryImage.append((char) qBlue(scanline[j])); } } const QByteArray base64Image = binaryImage.toBase64(); // create command QJsonObject command; command["command"] = QString("image"); command["priority"] = priority; command["imagewidth"] = image.width(); command["imageheight"] = image.height(); command["imagedata"] = QString(base64Image.data()); if (duration > 0) { command["duration"] = duration; } // send command message QJsonObject reply = sendMessage(command); // parse reply message parseReply(reply); } void JsonConnection::setEffect(const QString &effectName, const QString & effectArgs, int priority, int duration) { qDebug() << "Start effect " << effectName; // create command QJsonObject command, effect; command["command"] = QString("effect"); command["priority"] = priority; effect["name"] = effectName; if (effectArgs.size() > 0) { QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(effectArgs.toUtf8() ,&error); 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,effectArgs.size()); i 0) { command["duration"] = duration; } // send command message QJsonObject reply = sendMessage(command); // parse reply message parseReply(reply); } QString JsonConnection::getServerInfo() { qDebug() << "Get server info"; // create command QJsonObject command; command["command"] = QString("serverinfo"); // send command message QJsonObject reply = sendMessage(command); // parse reply message if (parseReply(reply)) { if (!reply.contains("info") || !reply["info"].isObject()) { throw std::runtime_error("No info available in result"); } QJsonDocument doc(reply["info"].toObject()); QString info(doc.toJson(QJsonDocument::Indented)); return info; } return QString(); } void JsonConnection::clear(int priority) { qDebug() << "Clear priority channel " << priority; // create command QJsonObject command; command["command"] = QString("clear"); command["priority"] = priority; // send command message QJsonObject reply = sendMessage(command); // parse reply message parseReply(reply); } void JsonConnection::clearAll() { qDebug() << "Clear all priority channels"; // create command QJsonObject command; command["command"] = QString("clearall"); // send command message QJsonObject reply = sendMessage(command); // parse reply message parseReply(reply); } void JsonConnection::setComponentState(const QString & component, const bool state) { qDebug() << (state ? "Enable" : "Disable") << "Component" << component; // create command QJsonObject command, parameter; command["command"] = QString("componentstate"); parameter["component"] = component; parameter["state"] = state; command["componentstate"] = parameter; // send command message QJsonObject reply = sendMessage(command); // parse reply message parseReply(reply); } void JsonConnection::setSource(int priority) { // create command QJsonObject command; command["command"] = QString("sourceselect"); command["priority"] = priority; // send command message QJsonObject reply = sendMessage(command); // parse reply message parseReply(reply); } void JsonConnection::setSourceAutoSelect() { // create command QJsonObject command; command["command"] = QString("sourceselect"); command["auto"] = true; // send command message QJsonObject reply = sendMessage(command); // parse reply message parseReply(reply); } QString JsonConnection::getConfig(std::string type) { assert( type == "schema" || type == "config" ); qDebug() << "Get configuration file from Hyperion Server"; // create command QJsonObject command; command["command"] = QString("config"); command["subcommand"] = (type == "schema") ? QString("getschema") : QString("getconfig"); // send command message QJsonObject reply = sendMessage(command); // parse reply message if (parseReply(reply)) { if (!reply.contains("result") || !reply["result"].isObject()) { throw std::runtime_error("No configuration file available in result"); } QJsonDocument doc(reply["result"].toObject()); QString result(doc.toJson(QJsonDocument::Indented)); return result; } return QString(); } void JsonConnection::setConfig(const QString &jsonString) { // create command QJsonObject command; command["command"] = QString("config"); command["subcommand"] = QString("setconfig"); if (jsonString.size() > 0) { QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(jsonString.toUtf8() ,&error); 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,jsonString.size()); i