2 #include <score/serialization/StringConstants.hpp>
4 #include <QAbstractListModel>
5 #include <QQmlApplicationEngine>
6 #include <QtWebSockets/QtWebSockets>
8 #include <wobjectdefs.h>
17 friend bool operator==(
const SyncInfo& tn,
const QJsonValue& rhs)
19 return tn.path == rhs;
21 friend bool operator!=(
const SyncInfo& tn,
const QJsonValue& rhs)
23 return tn.path != rhs;
30 template <
typename Fun>
38 int rowCount(
const QModelIndex&)
const override {
return timeSyncs.size(); }
40 QVariant data(
const QModelIndex& index,
int)
const override
42 if(index.row() >= timeSyncs.size())
45 return timeSyncs[index.row()].prettyName;
48 QHash<int, QByteArray> roleNames()
const override
50 QHash<int, QByteArray> hash;
51 hash.insert(Qt::DisplayRole,
"name");
54 std::vector<SyncInfo> timeSyncs;
66 ossia::hash_map<QString, std::function<void(
const QJsonObject&)>> m_answers;
71 m_answers.insert(std::make_pair(
"TriggerAdded", [
this](
const QJsonObject& obj) {
73 auto json_it = obj.find(
"Path");
74 if(json_it == obj.end())
78 m_activeSyncs.timeSyncs.begin(), m_activeSyncs.timeSyncs.end(), *json_it);
79 if(it == m_activeSyncs.timeSyncs.end())
81 m_activeSyncs.apply([=]() {
82 m_activeSyncs.timeSyncs.emplace_back(
83 SyncInfo{*json_it, obj[score::StringConstant().Name].toString()});
88 m_answers.insert(std::make_pair(
"TriggerRemoved", [
this](
const QJsonObject& obj) {
90 auto json_it = obj.find(
"Path");
91 if(json_it == obj.end())
95 m_activeSyncs.timeSyncs.begin(), m_activeSyncs.timeSyncs.end(), *json_it);
96 if(it != m_activeSyncs.timeSyncs.end())
98 m_activeSyncs.apply([=]() { m_activeSyncs.timeSyncs.erase(it); });
103 &m_server, &QWebSocket::textMessageReceived,
this,
104 &WebSocketHandler::processTextMessage);
106 &m_server, &QWebSocket::binaryMessageReceived,
this,
107 &WebSocketHandler::processBinaryMessage);
108 connect(&m_server, &QWebSocket::connected,
this, [] { qDebug(
"yolooo"); });
111 static_cast<void (QWebSocket::*)(QAbstractSocket::SocketError)
>(
113 this, [=](QAbstractSocket::SocketError) { qDebug() << m_server.errorString(); });
115 m_server.open(QUrl(
"ws://147.210.128.72:10212"));
120 void processTextMessage(
const QString& message)
122 processBinaryMessage(message.toLatin1());
125 void processBinaryMessage(QByteArray message)
127 QJsonParseError error;
128 auto doc = QJsonDocument::fromJson(std::move(message), &error);
132 auto obj = doc.object();
133 auto it = obj.find(
"Message");
137 auto mess = it->toString();
138 auto answer_it = m_answers.find(mess);
139 if(answer_it == m_answers.end())
142 answer_it->second(obj);
145 void on_rowPressed(
int i)
147 if(i >= m_activeSyncs.timeSyncs.size())
150 auto tn = m_activeSyncs.timeSyncs[i];
153 mess[score::StringConstant().Message] =
"Trigger";
154 mess[score::StringConstant().Path] = tn.path;
155 QJsonDocument doc{mess};
156 auto json = doc.toJson();
158 m_server.sendTextMessage(json);
160 W_SLOT(on_rowPressed)
165 mess[score::StringConstant().Message] =
"Play";
166 m_server.sendTextMessage(QJsonDocument{mess}.toJson());
173 mess[score::StringConstant().Message] =
"Pause";
174 m_server.sendTextMessage(QJsonDocument{mess}.toJson());
181 mess[score::StringConstant().Message] =
"Stop";
182 m_server.sendTextMessage(QJsonDocument{mess}.toJson());
186 void on_addressChanged(QString addr)
190 m_activeSyncs.apply([
this]() { m_activeSyncs.timeSyncs.clear(); });
192 m_server.open(QUrl{addr});
194 W_SLOT(on_addressChanged)
208 QQmlApplicationEngine engine;
Definition: RemoteApplication.hpp:198
Definition: RemoteApplication.hpp:28
Definition: RemoteApplication.hpp:14
Definition: RemoteApplication.hpp:58