-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmbase.cpp
More file actions
187 lines (144 loc) · 4.41 KB
/
bmbase.cpp
File metadata and controls
187 lines (144 loc) · 4.41 KB
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
#include "bmbase.h"
static QString makeJson(QString type, QString action)
{
QJsonObject o;
QJsonObject d;
o.insert("type", type);
d.insert("action", action);
o.insert("data", d);
return QJsonDocument(o).toJson();
}
static QString makeJsonArray(QString type, QString action, QString prop, QJsonArray array)
{
QJsonObject o;
QJsonObject d;
o.insert("type", type);
d.insert("action", action);
d.insert(prop, array);
o.insert("data", d);
return QJsonDocument(o).toJson();
}
BMBase::BMBase(QObject *parent)
: QObject{parent}
{
}
BMBase::~BMBase()
{
m_ws.close();
}
QQmlListProperty<QObject> BMBase::endpoints()
{
return QQmlListProperty<QObject>(this, this, [](QQmlListProperty<QObject>* prop, QObject* obj) {
auto self = static_cast<BMBase*>(prop->data);
obj->setParent(self);
if (auto sub = qobject_cast<QtOpenApiCommon::QOAIBaseApi*>(obj)) {
qDebug() << "OAPI" << self->m_hostname << self->m_protocol << obj << sub->operations();
self->setApiServer(sub);
//QObject::connect(self, &BMBase::serverChanged, sub, &BMBase::setServer);
}
},
nullptr, nullptr, nullptr);
}
void BMBase::setServer(QString hostname, QString protocol)
{
m_hostname=hostname;
m_protocol=protocol;
foreach (auto oa, m_oac) {
setApiServer(oa);
}
}
void BMBase::setApiServer(QtOpenApiCommon::QOAIBaseApi *api) {
auto ops=api->operations();
foreach (auto op, ops) {
api->setServerVariable(op, 0, "hostname", m_hostname);
api->setServerVariable(op, 0, "protocol", m_protocol);
}
}
void BMBase::open()
{
QUrl url;
url.setHost(m_hostname);
url.setScheme("ws");
url.setPath("/control/api/v1/event/websocket");
connect(&m_ws, &QWebSocket::connected, this, &BMBase::onWsConnected);
connect(&m_ws, &QWebSocket::errorOccurred, this, &BMBase::onWsErrorOccurred);
connect(&m_ws, &QWebSocket::disconnected, this, &BMBase::onWsDisconnected);
m_ws.open(url);
}
void BMBase::close()
{
m_ws.close();
m_ws.disconnect();
}
void BMBase::onWsConnected()
{
qDebug("Websocket connected!");
connect(&m_ws, &QWebSocket::textMessageReceived, this, &BMBase::onWsTextMessageReceived);
m_ws.sendTextMessage(makeJson("request", "listProperties"));
}
void BMBase::onWsDisconnected()
{
qDebug("Websocket disconnected");
}
void BMBase::onWsTextMessageReceived(QString message)
{
//qDebug() << "WS" << message;
auto j=QJsonDocument::fromJson(message.toUtf8());
//qDebug() << j;
if (!j.isObject()) {
qWarning("Not an object ?");
emit connectionError();
return;
}
auto d=j.object().value("data").toObject();
auto a=d.value("action");
if (a.toString()=="listProperties") {
auto props=d.value("properties").toArray();
m_ws.sendTextMessage(makeJsonArray("request", "subscribe", "properties", props));
onListProperties(props);
return;
}
if (a.toString()=="subscribe") {
auto v=d.value("values").toObject();
onSubscribeHandler(v);
return;
}
if (a.toString()=="propertyValueChanged") {
auto prop=d.value("property").toString();
auto value=d.value("value");
emit propertyChanged(prop, value);
if (!onPropertyChange(prop, value)) {
qDebug() << "Unhandled property" << prop << value;
}
return;
}
qDebug() << "Unhandled WS message:" << a.toString() << j;
}
void BMBase::onWsErrorOccurred(QAbstractSocket::SocketError error)
{
qDebug() << "WSE" << error;
emit connectionError();
}
void BMBase::onSubscribeHandler(QJsonObject jso)
{
qDebug() << "onSubscribeHandler" << jso;
}
bool BMBase::onPropertyChange(QString property, QJsonValue value)
{
return false;
}
void BMBase::onListProperties(QJsonArray properties)
{
qDebug() << "onListProperties" << properties;
}
QString BMBase::hostname() const
{
return m_hostname;
}
void BMBase::setHostname(const QString &newHostname)
{
if (m_hostname == newHostname)
return;
m_hostname = newHostname;
emit hostnameChanged();
}