-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbackendconnection.h
228 lines (184 loc) · 6.72 KB
/
backendconnection.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
** Copyright (C) 2023 Victron Energy B.V.
** See LICENSE.txt for license information.
*/
#ifndef BACKENDCONNECTION_H
#define BACKENDCONNECTION_H
#include <QObject>
#include <QQmlEngine>
#include <QNetworkAccessManager>
#include <QMqttClient>
#include "veutil/qt/ve_qitems_mqtt.hpp"
class VeQItemDbusProducer;
class AlarmBusitem;
namespace Victron {
namespace VenusOS {
class BackendConnection : public QObject
{
Q_OBJECT
QML_ELEMENT
QML_SINGLETON
Q_PROPERTY(State state READ state NOTIFY stateChanged FINAL)
Q_PROPERTY(SourceType type READ type NOTIFY typeChanged FINAL)
Q_PROPERTY(MqttClientError mqttClientError READ mqttClientError NOTIFY mqttClientErrorChanged FINAL)
Q_PROPERTY(QString username READ username WRITE setUsername NOTIFY usernameChanged FINAL)
Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged FINAL)
Q_PROPERTY(QString portalId READ portalId WRITE setPortalId NOTIFY portalIdChanged FINAL)
Q_PROPERTY(QString shard READ shard WRITE setShard NOTIFY shardChanged FINAL)
Q_PROPERTY(QString token READ token WRITE setToken NOTIFY tokenChanged FINAL)
Q_PROPERTY(QUrl demoImageFileName READ demoImageFileName CONSTANT FINAL)
Q_PROPERTY(int idUser READ idUser WRITE setIdUser NOTIFY idUserChanged FINAL)
Q_PROPERTY(bool vrm READ isVrm WRITE setVrm NOTIFY vrmChanged FINAL)
Q_PROPERTY(bool applicationVisible READ isApplicationVisible WRITE setApplicationVisible NOTIFY applicationVisibleChanged FINAL)
Q_PROPERTY(bool needsWasmKeyboardHandler READ needsWasmKeyboardHandler WRITE setNeedsWasmKeyboardHandler NOTIFY needsWasmKeyboardHandlerChanged FINAL)
friend class BackendConnectionTester;
public:
enum SourceType {
UnknownSource,
DBusSource,
MqttSource,
MockSource
};
Q_ENUM(SourceType)
// Same as VeQItemMqttProducer::ConnectionState
enum State {
Idle,
Connecting,
Connected,
Initializing,
Ready,
Disconnected,
Reconnecting,
Failed
};
Q_ENUM(State)
enum MqttClientError {
MqttClient_NoError = QMqttClient::NoError,
MqttClient_InvalidProtocolVersion = QMqttClient::InvalidProtocolVersion,
MqttClient_IdRejected = QMqttClient::IdRejected,
MqttClient_ServerUnavailable = QMqttClient::ServerUnavailable,
MqttClient_BadUsernameOrPassword = QMqttClient::BadUsernameOrPassword,
MqttClient_NotAuthorized = QMqttClient::NotAuthorized,
MqttClient_TransportInvalid = QMqttClient::TransportInvalid,
MqttClient_ProtocolViolation = QMqttClient::ProtocolViolation,
MqttClient_UnknownError = QMqttClient::UnknownError,
MqttClient_Mqtt5SpecificError = QMqttClient::Mqtt5SpecificError
};
Q_ENUM(MqttClientError)
static BackendConnection* create(QQmlEngine *engine = nullptr, QJSEngine *jsEngine = nullptr);
State state() const;
MqttClientError mqttClientError() const;
void loadConfiguration();
SourceType type() const;
void setType(SourceType type, const QString &address = QString());
QString username() const;
void setUsername(const QString &username);
QString password() const;
void setPassword(const QString &password);
QString portalId() const;
void setPortalId(const QString &portalId);
QString shard() const;
void setShard(const QString &shard);
QString token() const;
void setToken(const QString &tok);
int idUser() const;
void setIdUser(int id);
void loginVrmApi();
void requestShardFromVrmApi();
bool isVrm() const;
void setVrm(bool v);
bool isApplicationVisible() const;
void setApplicationVisible(bool v);
bool needsWasmKeyboardHandler() const;
void setNeedsWasmKeyboardHandler(bool needsWasmKeyboardHandler);
QUrl demoImageFileName() const;
// Each service type (system, settings, battery, etc.) has a base uid, which has different
// forms on D-Bus and MQTT:
// - D-Bus uid: "dbus/<serviceName>"
// E.g. "dbus/com.victronenergy.system", "dbus/com.victronenergy.battery.lynxparallel"
// (The serviceName format is "com.victronenergy.<serviceType>[.suffix]/*".)
// - MQTT uid: "mqtt/<serviceType>/<deviceInstance>"
// E.g. "mqtt/system/0", "mqtt/battery/256"
// (Unlike for D-Bus, MQTT uids never have a suffix after the serviceType, so the device
// instance number must be available to create the MQTT uid.)
Q_INVOKABLE QString serviceUidForType(const QString &serviceType) const;
Q_INVOKABLE QString serviceTypeFromUid(const QString &uid) const;
Q_INVOKABLE QString serviceUidFromName(const QString &serviceName, int deviceInstance) const;
Q_INVOKABLE QString uidPrefix() const;
Q_INVOKABLE void logout();
Q_INVOKABLE void securityProtocolChanged();
Q_INVOKABLE void reloadPage();
Q_INVOKABLE void openUrl(const QString &url);
// Move this to some mock data manager when available
Q_INVOKABLE void setMockValue(const QString &uid, const QVariant &value);
Q_INVOKABLE QVariant mockValue(const QString &uid) const;
#if defined(VENUS_WEBASSEMBLY_BUILD)
Q_INVOKABLE void hitWatchdog();
#endif
Q_SIGNALS:
void stateChanged();
void typeChanged();
void mqttClientErrorChanged();
void usernameChanged();
void passwordChanged();
void portalIdChanged();
void shardChanged();
void tokenChanged();
void idUserChanged();
void vrmChanged();
void applicationVisibleChanged();
void needsWasmKeyboardHandlerChanged();
private Q_SLOTS:
void onNetworkConfigChanged(const QVariant var);
void onReloadPageTimerExpired();
private:
explicit BackendConnection(QObject *parent = nullptr);
void setState(State backendConnectionState);
void setState(VeQItemMqttProducer::ConnectionState backendConnectionState);
void setState(bool connected);
void mqttErrorChanged();
void addSettings(VeQItemSettingsInfo *info);
#if !defined(VENUS_WEBASSEMBLY_BUILD)
void initDBusConnection(const QString &address);
#endif
void initMqttConnection(const QString &address);
void initMockConnection();
QString m_username;
QString m_password;
QString m_portalId;
QString m_shard;
QString m_token;
int m_idUser = -1;
bool m_vrm = false;
bool m_applicationVisible = true;
bool m_needsWasmKeyboardHandler = false;
State m_state = BackendConnection::State::Idle;
SourceType m_type = UnknownSource;
QMqttClient::ClientError m_mqttClientError = QMqttClient::NoError;
QTimer *mRestartDelayTimer = nullptr;
VeQItemProducer *m_producer = nullptr;
#if !defined(VENUS_WEBASSEMBLY_BUILD)
AlarmBusitem *m_alarmBusItem = nullptr;
#endif
QNetworkAccessManager *m_network = nullptr;
};
class BackendConnectionTester : public QObject
{
Q_OBJECT
public:
BackendConnectionTester();
Victron::VenusOS::BackendConnection mqttBackend, dbusBackend;
public slots:
void applicationAvailable()
{
// Initialization that only requires the QGuiApplication object to be available
}
void qmlEngineAvailable(QQmlEngine *engine); // Initialization requiring the QQmlEngine to be constructed
void cleanupTestCase()
{
// Implement custom resource cleanup
}
};
}
}
#endif // BACKENDCONNECTION_H