| 92 | } |
| 93 | |
| 94 | void VisConnector::webSocketMessageReceived(const QString& message) |
| 95 | { |
| 96 | auto* socket = qobject_cast<QWebSocket*>(sender()); |
| 97 | |
| 98 | QJsonParseError error; |
| 99 | auto json = QJsonDocument::fromJson(message.toUtf8(), &error); |
| 100 | auto msg = json.object(); |
| 101 | auto event = msg["event"].toString(); |
| 102 | if (event == "solve") { |
| 103 | auto mf = msg["modelFile"].toString(); |
| 104 | QStringList dfs; |
| 105 | auto dataFiles = msg["dataFiles"]; |
| 106 | bool dataFilesGiven = dataFiles.isArray(); |
| 107 | for (auto v : dataFiles.toArray()) { |
| 108 | dfs << v.toString(); |
| 109 | } |
| 110 | auto opts = msg["options"].toObject().toVariantMap(); |
| 111 | emit solveRequested(mf, dataFilesGiven, dfs, opts); |
| 112 | } else if (event == "getNumSolutions") { |
| 113 | QJsonObject obj({{"event", "response"}, |
| 114 | {"id", msg["id"]}, |
| 115 | {"window", msg["window"]}, |
| 116 | {"payload", _solutions.isEmpty() ? 0 : _solutions.first().size()}}); |
| 117 | socket->sendTextMessage(QString::fromUtf8(QJsonDocument(obj).toJson())); |
| 118 | } else if (event == "getSolution") { |
| 119 | auto window_id = msg["window"].toString(); |
| 120 | if (!_solutions.contains(window_id)) { |
| 121 | QJsonObject obj({{"event", "error"}, |
| 122 | {"id", msg["id"]}, |
| 123 | {"window", msg["window"]}, |
| 124 | {"message", "Invalid window."}}); |
| 125 | socket->sendTextMessage(QString::fromUtf8(QJsonDocument(obj).toJson())); |
| 126 | return; |
| 127 | } |
| 128 | auto idx = msg["index"].toInt(); |
| 129 | if (idx < 0) { |
| 130 | idx += _solutions[window_id].count(); |
| 131 | } |
| 132 | if (idx < 0 || idx >= _solutions[window_id].count()) { |
| 133 | QJsonObject obj({{"event", "error"}, |
| 134 | {"id", msg["id"]}, |
| 135 | {"window", msg["window"]}, |
| 136 | {"message", "Index out of range."}}); |
| 137 | socket->sendTextMessage(QString::fromUtf8(QJsonDocument(obj).toJson())); |
| 138 | return; |
| 139 | } |
| 140 | QJsonObject obj({{"event", "response"}, |
| 141 | {"id", msg["id"]}, |
| 142 | {"window", msg["window"]}, |
| 143 | {"payload", _solutions[window_id][idx]}}); |
| 144 | socket->sendTextMessage(QString::fromUtf8(QJsonDocument(obj).toJson())); |
| 145 | } else if (event == "getAllSolutions") { |
| 146 | auto window_id = msg["window"].toString(); |
| 147 | if (!_solutions.contains(window_id)) { |
| 148 | QJsonObject obj({{"event", "error"}, |
| 149 | {"id", msg["id"]}, |
| 150 | {"window", msg["window"]}, |
| 151 | {"message", "Invalid window."}}); |