Kstars

node.cpp
1/* EkosLive Node
2
3 SPDX-FileCopyrightText: 2023 Jasem Mutlaq <mutlaqja@ikarustech.com>
4
5 Communication Node
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9
10#include "node.h"
11#include "version.h"
12#include "Options.h"
13#include "ekos_debug.h"
14
15#include <QWebSocket>
16#include <QUrlQuery>
17#include <QTimer>
18#include <QJsonDocument>
19
20#include <KActionCollection>
21#include <basedevice.h>
22#include <QUuid>
23
24namespace EkosLive
25{
26Node::Node(const QString &name) : m_Name(name)
27{
28 connect(&m_WebSocket, &QWebSocket::connected, this, &Node::onConnected);
29 connect(&m_WebSocket, &QWebSocket::disconnected, this, &Node::onDisconnected);
30 connect(&m_WebSocket, static_cast<void(QWebSocket::*)(QAbstractSocket::SocketError)>(&QWebSocket::error), this,
31 &Node::onError);
32
33 m_Path = "/" + m_Name + "/ekos";
34}
35
36void Node::connectServer()
37{
38 QUrl requestURL(m_URL);
39
41 query.addQueryItem("username", m_AuthResponse["username"].toString());
42 query.addQueryItem("token", m_AuthResponse["token"].toString());
43 if (m_AuthResponse.contains("remoteToken"))
44 query.addQueryItem("remoteToken", m_AuthResponse["remoteToken"].toString());
45 query.addQueryItem("cloudEnabled", Options::ekosLiveCloud() ? "true" : "false");
46 query.addQueryItem("email", m_AuthResponse["email"].toString());
47 query.addQueryItem("from_date", m_AuthResponse["from_date"].toString());
48 query.addQueryItem("to_date", m_AuthResponse["to_date"].toString());
49 query.addQueryItem("plan_id", m_AuthResponse["plan_id"].toString());
50 query.addQueryItem("type", m_AuthResponse["type"].toString());
51 query.addQueryItem("version", KSTARS_VERSION);
52
53 requestURL.setPath(m_Path);
54 requestURL.setQuery(query);
55
56 m_WebSocket.open(requestURL);
57
58 qCInfo(KSTARS_EKOS) << "Connecting to " << m_Name << "Websocket server at" << requestURL.toDisplayString();
59}
60
61void Node::disconnectServer()
62{
63 m_WebSocket.close();
64}
65
66void Node::onConnected()
67{
68 //qCInfo(KSTARS_EKOS) << "Connected to" << m_Name << "Websocket server at" << m_URL.toDisplayString();
69
70 m_isConnected = true;
71 m_ReconnectTries = 0;
72
73 connect(&m_WebSocket, &QWebSocket::textMessageReceived, this, &Node::onTextReceived, Qt::UniqueConnection);
74 connect(&m_WebSocket, &QWebSocket::binaryMessageReceived, this, &Node::onBinaryReceived, Qt::UniqueConnection);
75
76 emit connected();
77}
78
79void Node::onDisconnected()
80{
81 qCInfo(KSTARS_EKOS) << "Disconnected from" << m_Name << "Websocket server at" << m_URL.toDisplayString();
82 m_isConnected = false;
83
84 disconnect(&m_WebSocket, &QWebSocket::textMessageReceived, this, &Node::onTextReceived);
85 disconnect(&m_WebSocket, &QWebSocket::binaryMessageReceived, this, &Node::onBinaryReceived);
86
87 emit disconnected();
88}
89
90void Node::onError(QAbstractSocket::SocketError error)
91{
92 qCritical(KSTARS_EKOS) << m_Name << "Websocket connection error from" << m_URL.toDisplayString() << ":" <<
93 m_WebSocket.errorString();
96 {
97 if (m_ReconnectTries++ < RECONNECT_MAX_TRIES)
98 QTimer::singleShot(RECONNECT_INTERVAL, this, &Node::connectServer);
99 }
100}
101
102
103///////////////////////////////////////////////////////////////////////////////////////////
104///
105///////////////////////////////////////////////////////////////////////////////////////////
106void Node::sendResponse(const QString &command, const QJsonObject &payload)
107{
108 if (m_isConnected == false)
109 return;
110
111 m_WebSocket.sendTextMessage(QJsonDocument({{"type", command}, {"payload", payload}}).toJson(QJsonDocument::Compact));
112}
113
114///////////////////////////////////////////////////////////////////////////////////////////
115///
116///////////////////////////////////////////////////////////////////////////////////////////
117void Node::sendResponse(const QString &command, const QJsonArray &payload)
118{
119 if (m_isConnected == false)
120 return;
121
122 m_WebSocket.sendTextMessage(QJsonDocument({{"type", command}, {"payload", payload}}).toJson(QJsonDocument::Compact));
123}
124
125///////////////////////////////////////////////////////////////////////////////////////////
126///
127///////////////////////////////////////////////////////////////////////////////////////////
128void Node::sendResponse(const QString &command, const QString &payload)
129{
130 if (m_isConnected == false)
131 return;
132
133 m_WebSocket.sendTextMessage(QJsonDocument({{"type", command}, {"payload", payload}}).toJson(QJsonDocument::Compact));
134}
135
136///////////////////////////////////////////////////////////////////////////////////////////
137///
138///////////////////////////////////////////////////////////////////////////////////////////
139void Node::sendResponse(const QString &command, bool payload)
140{
141 if (m_isConnected == false)
142 return;
143
144 m_WebSocket.sendTextMessage(QJsonDocument({{"type", command}, {"payload", payload}}).toJson(QJsonDocument::Compact));
145}
146
147///////////////////////////////////////////////////////////////////////////////////////////
148///
149///////////////////////////////////////////////////////////////////////////////////////////
150void Node::sendTextMessage(const QString &message)
151{
152 if (m_isConnected == false)
153 return;
154 m_WebSocket.sendTextMessage(message);
155}
156
157///////////////////////////////////////////////////////////////////////////////////////////
158///
159///////////////////////////////////////////////////////////////////////////////////////////
160void Node::sendBinaryMessage(const QByteArray &message)
161{
162 if (m_isConnected == false)
163 return;
164 m_WebSocket.sendBinaryMessage(message);
165}
166
167}
std::optional< QSqlQuery > query(const QString &queryStatement)
char * toString(const EngineQuery &query)
Generic record interfaces and implementations.
Definition cloud.cpp:23
QString name(StandardAction id)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
UniqueConnection
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void binaryMessageReceived(const QByteArray &message)
void connected()
void disconnected()
QAbstractSocket::SocketError error() const const
void textMessageReceived(const QString &message)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:48:25 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.