-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.cpp
156 lines (140 loc) · 4.93 KB
/
config.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
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
/*
* This file is a part of the relayboard-control project
* Copyright (C) 2021 Dan Leinir Turthra Jensen <[email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <QDebug>
#include <KConfig>
#include <KConfigGroup>
static const QHash<int, char> positionTranslator{
{0, '1'},
{1, '2'},
{2, '3'},
{3, '4'},
{4, '5'},
{5, '6'},
{6, '7'},
{7, '8'}
};
class ConfigPrivate
{
public:
ConfigPrivate() {}
QString configFile;
bool isValid{false};
QStringList toggleTopics;
QStringList statusTopics;
QString mqttHost;
int mqttPort{1883};
QString mqttUsername;
QString mqttPassword;
};
Config::Config(const QString &configFile, QObject *parent)
: QObject(parent)
, d(new ConfigPrivate)
{
d->configFile = configFile;
KConfig configReader(configFile, KConfig::SimpleConfig);
qDebug() << "Reading config from" << configFile;
if (configReader.hasGroup("General")) {
const KConfigGroup generalGroup = configReader.group("General");
if (generalGroup.hasKey("toggleTopics")) {
d->toggleTopics = generalGroup.readEntry("toggleTopics", QStringList{});
qDebug() << "Found toggle topics in the configuration, now set to:" << d->toggleTopics;
}
if (generalGroup.hasKey("statusTopics")) {
d->statusTopics = generalGroup.readEntry("statusTopics", QStringList{});
qDebug() << "Found status topics in the configuration, now set to:" << d->statusTopics;
}
d->mqttHost = generalGroup.readEntry("mqttHost", QString{});
d->mqttPort = generalGroup.readEntry("mqttPort", 1883);
d->mqttUsername = generalGroup.readEntry("mqttUsername", QString{});
d->mqttPassword = generalGroup.readEntry("mqttPassword", QString{});
qDebug() << "Our MQTT host is" << d->mqttHost << d->mqttPort;
// Now read from the Topics group
if (configReader.hasGroup("Topics")) {
static const QLatin1String pathSeparator{"/"};
const KConfigGroup topicsGroup = configReader.group("Topics");
QString topicBase = topicsGroup.readEntry("topicBase", QString());
if (!topicBase.isEmpty() && !topicBase.endsWith(pathSeparator)) {
topicBase += pathSeparator;
}
QString toggleEndpoint = topicsGroup.readEntry("toggleEndpoint", QString("toggle"));
if (!toggleEndpoint.isEmpty() && !toggleEndpoint.startsWith(pathSeparator)) {
toggleEndpoint = pathSeparator + toggleEndpoint;
}
QString statusEndpoint = topicsGroup.readEntry("statusEndpoint", QString("status"));
if (!statusEndpoint.isEmpty() && !statusEndpoint.startsWith(pathSeparator)) {
statusEndpoint = pathSeparator + statusEndpoint;
}
for (int i = 1; i < 9; ++i) {
const QString topic = topicsGroup.readEntry(QString("topic-%1").arg(QString::number(i)), QString());
if (topic.isEmpty()) {
break;
}
d->toggleTopics << QString("%1%2%3").arg(topicBase).arg(topic).arg(toggleEndpoint);
d->statusTopics << QString("%1%2%3").arg(topicBase).arg(topic).arg(statusEndpoint);
}
qDebug() << "Topic section found, set topics to:";
qDebug() << d->toggleTopics;
qDebug() << d->statusTopics;
}
// Sanity check time - make sure we've got everything filled out that we want filled out
if (d->toggleTopics.count() > 0 && !d->mqttHost.isEmpty()) {
d->isValid = true;
}
}
}
Config::~Config()
{
}
QStringList Config::toggleTopics() const
{
return d->toggleTopics;
}
char Config::charForTopic(const QString &topic) const
{
char result{0};
const int index = d->toggleTopics.indexOf(topic);
if (index > -1 && index < positionTranslator.count()) {
result = positionTranslator.value(index);
}
return result;
}
QStringList Config::statusTopics() const
{
return d->statusTopics;
}
bool Config::isValid() const
{
return d->isValid;
}
QString Config::mqttHost() const
{
return d->mqttHost;
}
int Config::mqttPort() const
{
return d->mqttPort;
}
QString Config::mqttUsername() const
{
return d->mqttUsername;
}
QString Config::mqttPassword() const
{
return d->mqttPassword;
}