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("observatory", Options::ekosLiveObservatory());
42 query.addQueryItem("username", m_AuthResponse["username"].toString());
43 query.addQueryItem("token", m_AuthResponse["token"].toString());
44 if (m_AuthResponse.contains("remoteToken"))
45 query.addQueryItem("remoteToken", m_AuthResponse["remoteToken"].toString());
46 if (m_AuthResponse.contains("machine_id"))
47 query.addQueryItem("machine_id", m_AuthResponse["machine_id"].toString());
48 query.addQueryItem("cloudEnabled", Options::ekosLiveCloud() ? "true" : "false");
49 query.addQueryItem("email", m_AuthResponse["email"].toString());
50 query.addQueryItem("from_date", m_AuthResponse["from_date"].toString());
51 query.addQueryItem("to_date", m_AuthResponse["to_date"].toString());
52 query.addQueryItem("plan_id", m_AuthResponse["plan_id"].toString());
53 query.addQueryItem("type", m_AuthResponse["type"].toString());
54 query.addQueryItem("version", KSTARS_VERSION);
55
56
57 requestURL.setPath(m_Path);
58 requestURL.setQuery(query);
59
60 m_WebSocket.open(requestURL);
61
62 qCInfo(KSTARS_EKOS) << "Connecting to " << m_Name << "Websocket server at" << requestURL.toDisplayString();
63}
64
65void Node::disconnectServer()
66{
67 m_WebSocket.close();
68}
69
70void Node::onConnected()
71{
72 //qCInfo(KSTARS_EKOS) << "Connected to" << m_Name << "Websocket server at" << m_URL.toDisplayString();
73
74 m_isConnected = true;
75 m_ReconnectTries = 0;
76
77 connect(&m_WebSocket, &QWebSocket::textMessageReceived, this, &Node::onTextReceived, Qt::UniqueConnection);
78 connect(&m_WebSocket, &QWebSocket::binaryMessageReceived, this, &Node::onBinaryReceived, Qt::UniqueConnection);
79
80 emit connected();
81}
82
83void Node::onDisconnected()
84{
85 qCInfo(KSTARS_EKOS) << "Disconnected from" << m_Name << "Websocket server at" << m_URL.toDisplayString();
86 m_isConnected = false;
87
88 disconnect(&m_WebSocket, &QWebSocket::textMessageReceived, this, &Node::onTextReceived);
89 disconnect(&m_WebSocket, &QWebSocket::binaryMessageReceived, this, &Node::onBinaryReceived);
90
91 emit disconnected();
92}
93
94void Node::onError(QAbstractSocket::SocketError error)
95{
96 qCritical(KSTARS_EKOS) << m_Name << "Websocket connection error from" << m_URL.toDisplayString() << ":" <<
97 m_WebSocket.errorString();
100 {
101 if (m_ReconnectTries++ < RECONNECT_MAX_TRIES)
102 QTimer::singleShot(RECONNECT_INTERVAL, this, &Node::connectServer);
103 }
104}
105
106
107///////////////////////////////////////////////////////////////////////////////////////////
108///
109///////////////////////////////////////////////////////////////////////////////////////////
110void Node::sendResponse(const QString &command, const QJsonObject &payload)
111{
112 if (m_isConnected == false)
113 return;
114
115 m_WebSocket.sendTextMessage(QJsonDocument({{"type", command}, {"payload", payload}}).toJson(QJsonDocument::Compact));
116}
117
118///////////////////////////////////////////////////////////////////////////////////////////
119///
120///////////////////////////////////////////////////////////////////////////////////////////
121void Node::sendResponse(const QString &command, const QJsonArray &payload)
122{
123 if (m_isConnected == false)
124 return;
125
126 m_WebSocket.sendTextMessage(QJsonDocument({{"type", command}, {"payload", payload}}).toJson(QJsonDocument::Compact));
127}
128
129///////////////////////////////////////////////////////////////////////////////////////////
130///
131///////////////////////////////////////////////////////////////////////////////////////////
132void Node::sendResponse(const QString &command, const QString &payload)
133{
134 if (m_isConnected == false)
135 return;
136
137 m_WebSocket.sendTextMessage(QJsonDocument({{"type", command}, {"payload", payload}}).toJson(QJsonDocument::Compact));
138}
139
140///////////////////////////////////////////////////////////////////////////////////////////
141///
142///////////////////////////////////////////////////////////////////////////////////////////
143void Node::sendResponse(const QString &command, bool payload)
144{
145 if (m_isConnected == false)
146 return;
147
148 m_WebSocket.sendTextMessage(QJsonDocument({{"type", command}, {"payload", payload}}).toJson(QJsonDocument::Compact));
149}
150
151///////////////////////////////////////////////////////////////////////////////////////////
152///
153///////////////////////////////////////////////////////////////////////////////////////////
154void Node::sendTextMessage(const QString &message)
155{
156 if (m_isConnected == false)
157 return;
158 m_WebSocket.sendTextMessage(message);
159}
160
161///////////////////////////////////////////////////////////////////////////////////////////
162///
163///////////////////////////////////////////////////////////////////////////////////////////
164void Node::sendBinaryMessage(const QByteArray &message)
165{
166 if (m_isConnected == false)
167 return;
168 m_WebSocket.sendBinaryMessage(message);
169}
170
171}
std::optional< QSqlQuery > query(const QString &queryStatement)
char * toString(const EngineQuery &query)
Generic record interfaces and implementations.
Definition cloud.cpp:22
QString name(StandardAction id)
QByteArray toJson(JsonFormat format) const const
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 Sat Dec 21 2024 17:04:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.