// 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); } void JsonConnection::createEffect(const QString &effectName, const QString &effectScript, const QString & effectArgs) { qDebug() << "Create effect " << effectName; // create command QJsonObject effect; effect["command"] = QString("create-effect"); effect["name"] = effectName; effect["script"] = effectScript; 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) { 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