-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevicemanager.cpp
More file actions
211 lines (166 loc) · 6 KB
/
devicemanager.cpp
File metadata and controls
211 lines (166 loc) · 6 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "devicemanager.h"
//TODO handle mobile lock on mdns lib (manually stop and start on screen lock/unlock
//call device action
//UI device List
DeviceManager::DeviceManager(QObject *parent)
: QObject (parent)
, manager(new QNetworkAccessManager(this))
, zeroconf(new QZeroConf())
{
//HTTP Handling
QObject::connect(manager, &QNetworkAccessManager::finished,
this, &DeviceManager::HandleGetConfigResponse);
//MDNS Handling
zeroconf->startBrowser("_vika._tcp");
QObject::connect(zeroconf, &QZeroConf::serviceAdded,
this, &DeviceManager::GetConfig);
QObject::connect(zeroconf, &QZeroConf::serviceRemoved,
this, &DeviceManager::RemoveDevice);
//IsAlive Timer connect
}
DeviceManager::~DeviceManager(){
delete manager;
zeroconf->stopBrowser();
delete zeroconf;
}
void DeviceManager::RemoveDevice(QZeroConfService &service)
{
for (int i = 0; i < deviceList.size(); ++i) {
if (deviceList[i].address == service->ip()) {
deviceList.removeAt(i);
emit DeviceMissing();
}
}
}
//Fix pls
void DeviceManager::isAlive() {
// qDebug() << "isAlive";
// QVector<int> toRemove;
// QNetworkAccessManager *isAliveManager = new QNetworkAccessManager(this);
//
// for(int i = 0; i < deviceList.length(); i++) {
//
// QNetworkRequest req;
//
// QString url = "http://" + deviceList[i].address.toString() + "/";
// req.setUrl(QUrl(url));
// req.setRawHeader("User-Agent", "Vika Volatile Server");
//
// qDebug() << "isAlive - on url : " << url;
// QNetworkReply *reply = isAliveManager->get(req);
// if(reply->error()){
// qDebug() << "isAlive error";
// toRemove.push_back(i);
// }
// }
//
// if(toRemove.length() != 0) {
// foreach(const int idx, toRemove) {
// toRemove.removeAt(idx);
// qDebug() << "isAlive removed at idx " << idx;
// }
// }
}
void DeviceManager::CallAction(const QModelIndex &index) const {
QNetworkAccessManager *mngr = new QNetworkAccessManager();
QEventLoop loop;
QObject::connect(mngr, &QNetworkAccessManager::finished, &loop, &QEventLoop::quit);
//get action url
QNetworkRequest req;
req.setUrl(QUrl(index.data(Qt::UserRole + 2).toString()));
qDebug() << "calling action on row " << index.row();
qDebug() << " with url " << req.url();
QByteArray qb = "";
req.setHeader(QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"data\""));
req.setHeader(QNetworkRequest::ContentTypeHeader,
QVariant("application/json"));
QNetworkReply *postReply = manager->post(req, qb);
loop.exec();
qDebug() << postReply->readAll();
QString state = "";
while(postReply->isFinished() == false) {
qDebug() << ".";
}
if(postReply->error()) {
qDebug() << "http post reply error";
}
qDebug() << "callaction response : state: " << state;
emit ActionCalled(index, state);
//delete http;
delete postReply;
delete mngr;
}
void DeviceManager::HandleGetConfigResponse(QNetworkReply *reply) {
qDebug() << "DeviceManager - Handling GetConfig";
if(reply->error()){
qDebug() << " - HTTP reponse handling error: "
<< reply->errorString();
return;
}
QJsonParseError parseError;
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll()
, &parseError);
if(parseError.error != QJsonParseError::NoError) {
qDebug() << " - Error while parsing Http response";
return;
}
//parse host addr from url
QHostAddress replyHost = QHostAddress(QUrl(reply->url()).host());
QVector<Action> actions;
//Parse actions from json repsonse and build a device
QJsonArray jsonActions = doc["a"].toArray();
for (int i = 0; i < jsonActions.size(); i++) {
QJsonObject ac = jsonActions[i].toObject();
VikaSyntax syntax;
foreach(const QJsonValue & verb, ac["vrb"].toArray()){
syntax.Verbs.append(verb.toString());
}
foreach(const QJsonValue & obj, ac["obj"].toArray()){
syntax.Objects.append(obj.toString());
}
foreach(const QJsonValue & adj, ac["adj"].toArray()){
syntax.Adjectives.append(adj.toString());
}
syntax.Localisation = ac["loc"].toString();
QString description = ac["desc"].toString();
QString uri = "http://" + replyHost.toString() + "/handlePin?a=" + i;
ActionType actionType;
QString actionTypeStr = ac["type"].toString();
if(actionTypeStr == "Toggle") {
actionType = ActionType::Toggle;
} else if(actionTypeStr == "Analog") {
actionType = ActionType::Analog;
} else if(actionTypeStr == "Measure") {
actionType = ActionType::Measure;
} else {
qDebug() << "DeviceManager - Undefined Action type, str: " << ac["type"].toString();
actionType = ActionType::Undefined;
}
actions.push_back(Action(actionType, syntax, uri, description));
}
Device d = Device(replyHost, actions);
//Add device to device list
deviceList.push_back(d);
emit DeviceDiscovered(d);
}
void DeviceManager::GetConfig(QZeroConfService service) {
if(DeviceAddrIndexOf(service->ip()) != -1) {
qDebug() << "DeviceManager - GetConfig abort : device already exists";
return;
}
QString url = "http://" + service->ip().toString() + "/getConfig";
qDebug() << "DeviceManager - Getting config on url " << url;
QNetworkRequest req;
req.setUrl(QUrl(url));
req.setRawHeader("User-Agent", "Vika Volatile Server");
manager->get(req);
}
int DeviceManager::DeviceAddrIndexOf(const QHostAddress &addr) const {
for (int i = 0; i < deviceList.length(); ++i) {
if(deviceList[i].address == addr) {
return i;
}
}
return -1;
}