-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
104 lines (82 loc) · 2.34 KB
/
client.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
#include "client.h"
#include <QJsonObject>
#include <QJsonDocument>
Client::~Client()
{
if(m_client)
{
delete m_client;
m_client = nullptr;
}
}
Client::Client(QObject *parent)
: QObject(parent),
m_client(nullptr)
{
try
{
m_client = new Mere::RPC::Client("mms://display");
m_ready = true;
}
catch(...)
{
m_ready = false;
}
}
void Client::authenticate(const std::string &user, const std::string &pass)
{
if (!m_ready)
{
emit action(false, "Client and server connection is not ready.");
return;
}
m_client->service("auth")->method("authenticate")->with({QString::fromStdString(user), QString::fromStdString(pass)})->call([](QVariant res, QVariant err, void *context){
Client *client = static_cast<Client*>(context);
if (!client) return;
bool ok = res.toBool();
std::string message;
if(ok)
message = "Succeed to authenticate user.";
else
message = "Unable to authenticate user.";
client->action(ok, message);
}, (void*) this);
}
void Client::reboot(int time)
{
if (!m_ready)
{
emit action(false, "Client and server connection is not ready.");
return;
}
m_client->service("power")->method("reboot")->with({QVariant(time)})->call([](QVariant res, QVariant err, void *context){
Client *client = static_cast<Client*>(context);
if (!client) return;
bool ok = res.toBool();
std::string message;
if(ok)
message = "System is rebooting now....";
else
message = "Unable to reboot the system.";
client->action(ok, message);
}, (void*) this);
}
void Client::shutdown(int time)
{
if (!m_ready)
{
emit action(false, "Client and server connection is not ready.");
return;
}
m_client->service("power")->method("reboot")->with({QVariant(time)})->call([](QVariant res, QVariant err, void *context){
Client *client = static_cast<Client*>(context);
if (!client) return;
bool ok = res.toBool();
std::string message;
if(ok)
message = "System is shuting down now....";
else
message = "Unable to shutdown the system.";
client->action(ok, message);
}, (void*) this);
}