-
Notifications
You must be signed in to change notification settings - Fork 5
/
connectionapi.cpp
120 lines (86 loc) · 4.41 KB
/
connectionapi.cpp
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
#include "connectionapi.h"
ConnectionApi::ConnectionApi(QObject *parent) :
QObject(parent)
{
}
void ConnectionApi::requestStatus()
{
QNetworkReply * reply = fbx()->get(fbx()->myCreateRequest("connection/"));
connect(reply,SIGNAL(finished()),this,SLOT(requestStatusFinished()));
connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),fbx(),SLOT(errorReceived(QNetworkReply::NetworkError)));
}
void ConnectionApi::requestConfiguration()
{
QNetworkReply * reply = fbx()->get(fbx()->myCreateRequest("connection/config/"));
connect(reply,SIGNAL(finished()),this,SLOT(requestStatusFinished()));
connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),fbx(),SLOT(errorReceived(QNetworkReply::NetworkError)));
}
void ConnectionApi::requestUpdateConfigurationFinished(bool ping, bool remoteAccess, int remoteAccessPort,
bool wool, bool adblock, bool allowTokenRequest)
{
QNetworkRequest request = fbx()->myCreateRequest("connection/config/");
QJsonObject json;
json.insert("ping",ping);
json.insert("remote_access",remoteAccess);
json.insert("remote_access_port",remoteAccessPort);
json.insert("wool",wool);
json.insert("adblock",adblock);
json.insert("allow_token_request",allowTokenRequest);
QJsonDocument doc(json);
QNetworkReply * reply = fbx()->put(request, doc.toJson());
connect(reply,SIGNAL(finished()),this,SLOT(requestUpdateConfigurationFinished()));
connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),fbx(),SLOT(errorReceived(QNetworkReply::NetworkError)));
}
void ConnectionApi::requestStatusFinished()
{
QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
if(fbx()->parseResult(doc))
{
QJsonValue item = doc.object().value("result");
ConnectionStatus status;
status.bytesDownPriv = item.toObject().value("bytes_down_priv").toDouble();
status.bytesUpPriv = item.toObject().value("bytes_up_priv").toDouble();
status.bytesUp= item.toObject().value("bytes_up").toDouble();
status.bytesDown = item.toObject().value("bytes_down").toDouble();
status.rateDown = item.toObject().value("rate_down").toDouble();
status.rateUp= item.toObject().value("rate_up").toDouble();
status.rateUpPriv= item.toObject().value("bytes_up_priv").toDouble();
status.rateDownPriv= item.toObject().value("rate_down_priv").toDouble();
status.bandwidthUp= item.toObject().value("bandwidth_up").toDouble();
status.ipv4= item.toObject().value("ipv4").toString();
status.ipv6= item.toObject().value("ipv6").toString();
status.state = status.stateFromString(item.toObject().value("ipv6").toString());
status.type = status.typeFromString(item.toObject().value("type").toString());
status.media = status.mediaFromString(item.toObject().value("media").toString());
emit statusReceived(status);
}
}
void ConnectionApi::requestConfigurationFinished()
{
QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
if(fbx()->parseResult(doc))
{
QJsonValue item = doc.object().value("result");
ConnectionConfiguration config;
config.ping = item.toObject().value("ping").toBool();
config.isSecurePass = item.toObject().value("is_secure_pass").toBool();
config.remoteAccessPort = item.toObject().value("remote_access_port").toDouble();
config.remoteAccess = item.toObject().value("remote_access").toBool();
config.wol = item.toObject().value("wol").toBool();
config.adblock = item.toObject().value("adblock").toBool();
config.adblockNotSet = item.toObject().value("adblock_not_set").toBool();
config.apiRemoteAccess = item.toObject().value("api_remote_access").toBool();
config.allowTokenRequest = item.toObject().value("allow_token_request").toBool();
config.remoteAccessIp = item.toObject().value("remote_access_ip").toString();
emit configurationReceived(config);
}
}
void ConnectionApi::requestUpdateConfigurationFinished()
{
QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
if(fbx()->parseResult(doc))
emit updateConfigurationFinished();
}