-
Notifications
You must be signed in to change notification settings - Fork 1
/
LcdClient.cpp
367 lines (311 loc) · 13 KB
/
LcdClient.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "LcdClient.hpp"
// Constructor and initialization routines (Opening files, connecting to LCDd, ...)
LcdClient::LcdClient(QObject *parent)
: QObject(parent)
{
for (int i = 0; i < 8; i++) {
universeAllZeroes[i] = 1;
}
for (int i = 0; i < 8; i++) {
universeOffset[i] = 0;
}
connect(&updateTimer, &QTimer::timeout, this, &LcdClient::update);
connect(&qnam, &QNetworkAccessManager::finished, this, &LcdClient::handleHttpResponse);
fileTemp.setFileName("/sys/devices/virtual/thermal/thermal_zone0/temp");
if (!fileTemp.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Failed to open /sys/devices/virtual/thermal/thermal_zone0/temp";
}
fileStat.setFileName("/proc/stat");
if (!fileStat.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Failed to open /proc/stat";
}
fileVoltAlarm.setFileName("/sys/devices/platform/soc/soc:firmware/raspberrypi-hwmon/hwmon/hwmon0/in0_lcrit_alarm");
if (!fileVoltAlarm.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Failed to open /sys/devices/platform/soc/soc:firmware/raspberrypi-hwmon/hwmon/hwmon0/in0_lcrit_alarm";
}
fileTempThrottle.setFileName("/sys/devices/platform/soc/soc:firmware/get_throttled");
if (!fileTempThrottle.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Failed to open /sys/devices/platform/soc/soc:firmware/get_throttled";
}
connect(&lcdSocket, &QIODevice::readyRead, this, &LcdClient::readServerResponse);
connect(&lcdSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), this, &LcdClient::handleSocketError);
lcdSocket.abort();
lcdSocket.connectToHost("127.0.0.1", 13666);
lcdSocket.write("hello\n");
}
// Update the currently visible screen content
void LcdClient::update()
{
if (currentScreen == "time") {
lcdSocket.write(QString("widget_set time line1 Uhrzeit\n").toLatin1());
lcdSocket.write(QString("widget_set time line2 1 2 \"%1\"\n")
.arg(QDateTime::currentDateTime().toString("dd.MM. HH:mm:ss"))
.toLatin1()
);
} else if (currentScreen == "net") {
lcdSocket.write(QString("widget_set net line1 Netzwerk\n").toLatin1());
lcdSocket.write(QString("widget_set net line2 1 2 15 2 m 2 \"%1\"\n")
.arg(getMachineIPs())
.toLatin1()
);
} else if (currentScreen == "sys") {
lcdSocket.write(QString("widget_set sys line1 \"Status:%1\"\n")
.arg(getRPiStatus())
.toLatin1()
);
lcdSocket.write(QString("widget_set sys line2 1 2 \"CPU:%1 T:%2°C\"\n")
.arg(getMachineCPULoad())
.arg(getMachineTemp())
.toLatin1()
);
} else if (currentScreen.startsWith("universe")) {
int universe = currentScreen.right(1).toInt();
updateDmxUniverse(universe);
}
// Check the RPi status to set the machineProblem flag
getRPiStatus();
// Update the backlight depending on machineProblem flag
// and DMX values
// When we just triggered the data fetching from OLA, the backlight
// will be updated again after the response has arrived
updateBacklight();
}
// Calculate the current CPU usage
QString LcdClient::getMachineCPULoad()
{
fileStat.seek(0);
QString line = fileStat.readLine().trimmed();
QStringList parts = line.split(' ', QString::SkipEmptyParts);
loadStruct currentLoad;
unsigned long diffUser, diffSystem, diffNice, diffTotal;
currentLoad.user = parts[1].toULong();
currentLoad.nice = parts[2].toULong();
currentLoad.system = parts[3].toULong();
currentLoad.idle = parts[4].toULong();
currentLoad.idle += parts[5].toULong(); // iowait
currentLoad.system += parts[6].toULong(); // irq
currentLoad.system += parts[7].toULong(); // softirq
currentLoad.total = currentLoad.user + currentLoad.nice + currentLoad.system + currentLoad.idle;
diffUser = currentLoad.user - lastLoad.user;
diffSystem = currentLoad.system - lastLoad.system;
diffNice = currentLoad.nice - lastLoad.nice;
diffTotal = currentLoad.total - lastLoad.total;
lastLoad = currentLoad;
int cpuLoad = qRound(100.0 * (((double) diffUser + (double) diffSystem + (double) diffNice) / (double) diffTotal));
return QString("%1\%").arg((int)cpuLoad, 3, 10, QLatin1Char('0'));
}
// Read the current thermal_zone temperature
QString LcdClient::getMachineTemp()
{
fileTemp.seek(0);
float temp = QString(fileTemp.readAll()).toFloat() / 1000;
return QString("%1°C").arg(qRound(temp));
}
// Generates a string containing all "external" network interfaces and their IPv4 addresses
QString LcdClient::getMachineIPs()
{
// Step 1: Get a list of all network interfaces
QHash<QString, QList<QHostAddress>> ifaceIPs;
QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces();
QNetworkInterface iface;
QString machineIPs;
// Step 2: For each interface, get a list of all Addresses of that interface
foreach(iface, allInterfaces) {
QList<QNetworkAddressEntry> allEntries = iface.addressEntries();
QList<QHostAddress> addresses;
QNetworkAddressEntry entry;
// Step 3: For each address, find all
// Non-Loopback, Non-Multicast IPv4
// and also interface name must not start with "docker"
foreach (entry, allEntries) {
if (!entry.ip().isLoopback() &&
!entry.ip().isMulticast() &&
(entry.ip().protocol() == QAbstractSocket::IPv4Protocol) &&
!iface.name().startsWith("docker")
) {
addresses.append(entry.ip());
}
}
// Step 4: If an address remained for that interface,
// add it + the IPs to the final QHash (currently unused)
// and a string-representation thereof (that is being returned)
if (addresses.count()) {
ifaceIPs.insert(iface.name(), addresses);
machineIPs += " " + iface.name() + ":";
QHostAddress adr;
foreach (adr, addresses) {
machineIPs += adr.toString() + ",";
}
if (machineIPs.endsWith(',')) {
machineIPs.chop(1);
}
}
}
machineIPs = machineIPs.trimmed();
return machineIPs;
}
// Get the RPi status (temp throttle and undervoltage)
QString LcdClient::getRPiStatus()
{
fileVoltAlarm.seek(0);
fileTempThrottle.seek(0);
int voltAlarm = QString(fileVoltAlarm.readAll()).toInt();
int tempThrottle = QString(fileTempThrottle.readAll()).toInt();
machineProblem = 1;
if (voltAlarm && !tempThrottle) {
return QString("Saft");
} else if (!voltAlarm && tempThrottle) {
return QString("Hitz");
} else if (voltAlarm && tempThrottle) {
return QString("H&S");
} else {
machineProblem = 0;
return QString("Gut");
}
}
// Parse responses from LCDd (via TCP socket)
void LcdClient::readServerResponse()
{
QString response = lcdSocket.readAll();
QStringList lines = response.split("\n", QString::SkipEmptyParts);
QString line;
foreach(line, lines) {
if (line == "success") {
continue;
}
qDebug() << "LCDd resp:" << line;
if (line.startsWith("connect ")) {
// Set client name
lcdSocket.write("client_set -name Kiste3000\n");
// Add info screens
lcdSocket.write("screen_add time\n");
lcdSocket.write("widget_add time line1 title\n");
lcdSocket.write("widget_add time line2 string\n");
lcdSocket.write("screen_add sys\n");
lcdSocket.write("widget_add sys line1 title\n");
lcdSocket.write("widget_add sys line2 string\n");
lcdSocket.write("screen_add net\n");
lcdSocket.write("widget_add net line1 title\n");
lcdSocket.write("widget_add net line2 scroller\n");
for (int i = 1; i <= 8; i++) {
lcdSocket.write(QString("screen_add universe%1\n").arg(i).toLatin1());
lcdSocket.write(QString("widget_add universe%1 line1 title\n").arg(i).toLatin1());
lcdSocket.write(QString("widget_add universe%1 line2 string\n").arg(i).toLatin1());
lcdSocket.write(QString("widget_set universe%1 line1 \"U_%1 %2->\"\n")
.arg(i)
.arg(universeOffset[i - 1] + 1, 3, 10, QLatin1Char('0'))
.toLatin1());
}
// Add menu structure
lcdSocket.write("menu_add_item \"\" system menu System\n");
lcdSocket.write("menu_add_item system shutdown action \"Nobb foahre\" -menu_result close\n");
lcdSocket.write("menu_add_item system startqlcplus action \"QLC+ stadde\" -menu_result close\n");
lcdSocket.write("menu_add_item \"\" display menu \"Anzeige\"\n");
for (int i = 1; i <= 8; i++) {
lcdSocket.write(QString("menu_add_item display offset%1 numeric \"U%2 Start\" -value \"1\" -minvalue \"1\" -maxvalue \"505\"\n")
.arg(i)
.arg(i)
.toLatin1());
}
// Start the periodic updating of info screens and RPi status
updateTimer.start(250);
} else if (line.startsWith("listen")) {
currentScreen = line.split(" ")[1].trimmed();
update();
} else if (line.startsWith("menuevent update offset")) {
int universe = line.mid(23, 1).toInt();
int val = line.split(" ")[3].toInt();
universeOffset[universe - 1] = val - 1;
lcdSocket.write(QString("widget_set universe%1 line1 \"U_%1 %2->\"\n")
.arg(universe)
.arg(universeOffset[universe - 1] + 1, 3, 10, QLatin1Char('0'))
.toLatin1());
} else if (line == "menuevent select shutdown") {
// TODO: Shut down. Honestly
proc.start("poweroff");
} else if (line == "menuevent select startqlcplus") {
// Start X and QLC+. As user "kiste"
}
}
}
// Request the current DMX values of a given universe ID from OLAd
void LcdClient::updateDmxUniverse(int universe) {
request.setUrl(QUrl(QString("http://127.0.0.1:9090/get_dmx?u=%1").arg(universe)));
qnam.get(request);
}
// Parse the HTTP response sent from OLAd (DMX values)
void LcdClient::handleHttpResponse(QNetworkReply *reply)
{
if (reply->error()) {
qDebug() << reply->errorString();
return;
}
int universe = reply->url().toString().right(1).toInt();
QString answer = reply->readAll();
QJsonArray jsonArray = QJsonDocument::fromJson(answer.toUtf8())["dmx"].toArray();
// Check if the array is empty or all zero. If not, save that info
universeAllZeroes[universe - 1] = 1;
if (jsonArray.size() != 0) {
foreach (const QJsonValue & value, jsonArray) {
if (value.toInt() != 0) {
universeAllZeroes[universe - 1] = 0;
break;
}
}
}
updateBacklight();
int offset = universeOffset[universe - 1];
lcdSocket.write(QString("widget_set universe%1 line1 \"U_%1 %2->\"\n")
.arg(universe)
.arg(offset + 1, 3, 10, QLatin1Char('0'))
.toLatin1());
lcdSocket.write(QString("widget_set universe%1 line2 1 2 \"%2%3%4%5%6%7%8%9\"\n")
.arg(universe)
.arg(jsonArray[offset + 0].toInt(), 2, 16, QLatin1Char('0'))
.arg(jsonArray[offset + 1].toInt(), 2, 16, QLatin1Char('0'))
.arg(jsonArray[offset + 2].toInt(), 2, 16, QLatin1Char('0'))
.arg(jsonArray[offset + 3].toInt(), 2, 16, QLatin1Char('0'))
.arg(jsonArray[offset + 4].toInt(), 2, 16, QLatin1Char('0'))
.arg(jsonArray[offset + 5].toInt(), 2, 16, QLatin1Char('0'))
.arg(jsonArray[offset + 6].toInt(), 2, 16, QLatin1Char('0'))
.arg(jsonArray[offset + 7].toInt(), 2, 16, QLatin1Char('0'))
.toLatin1()
);
}
// Handle socket errors on LCDd communication socket
void LcdClient::handleSocketError(QAbstractSocket::SocketError socketError)
{
qWarning() << "LCDd socket error: " << socketError;
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
break;
case QAbstractSocket::ConnectionRefusedError:
break;
default:
break;
}
}
// Sets the backlight depending on current RPi status
// and DMX universe values (ALL zeroes?)
void LcdClient::updateBacklight()
{
lcdSocket.write("backlight off\n");
if (machineProblem) {
lcdSocket.write("backlight red\n");
}
// Now check if all 8 universes are allZero
int allZero = 1;
for (int i = 0; i < 8; i++) {
if (universeAllZeroes[i] != 1) {
allZero = 0;
break;
}
}
if (allZero) {
lcdSocket.write("backlight blue\n");
} else {
lcdSocket.write("backlight green\n");
}
}