-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.cpp
107 lines (91 loc) · 2.14 KB
/
commands.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
#include "commands.h"
#include <QDebug>
#include <QProcess>
#include <QRegExp>
#include "utils.h"
Commands::Commands(QObject* parent)
:
QObject(parent)
{
}
Commands::~Commands()
{
}
QString Commands::loadCommands()
{
Utils::ErrorOrValue errorOrvalue = Utils::readJsonToVariant("configuration/commands.json");
m_commandToCommandLine.clear();
if (!errorOrvalue.error.isEmpty())
{
return errorOrvalue.error;
}
QMap<QString, QVariant> commandsMap = errorOrvalue.value.toMap();
for (const QString& command : commandsMap.keys())
{
m_commandToCommandLine.insert(command, commandsMap.value(command).toString());
}
return QString();
}
QString Commands::executeCommand(Command commandName, int value)
{
switch(commandName)
{
case SetVolume:
{
QString command = m_commandToCommandLine.value("set_volume");
command.replace("%d", QString::number(value));
execute(command);
execute(m_commandToCommandLine.value("unmute"));
return QString();
}
default:
{
Q_ASSERT(false);
}
}
return QString();
}
Commands::Command Commands::fromString(const QString &command)
{
// TODO refactor
if (command == "power_off")
{
return PowerOff;
}
else
{
return Undefined;
}
}
QString Commands::executeCommand(Command commandName)
{
switch(commandName)
{
case Mute:
{
return execute(m_commandToCommandLine.value("mute"));
break;
}
case PowerOff:
{
execute(m_commandToCommandLine.value("power_off"));
break;
}
default:
{
Q_ASSERT(false);
}
}
return QString();
}
QString Commands::execute(const QString& commandLine)
{
QStringList args = commandLine.split(" ");
QString executable = args.takeFirst();
QProcess process;
qDebug() << "Executing:" << executable << args;
process.start(executable, args);
process.waitForFinished();
QString output(process.readAllStandardOutput());
return output;
}