-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.cpp
448 lines (384 loc) · 15 KB
/
mainwindow.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include <QDesktopWidget>
#include <QFileDialog>
#include <QScrollBar>
#include <QDebug>
#include "digitalin.h"
#include "dualbutton.h"
#include "dualsensor.h"
#include "lcd.h"
#include "lcddisplayarea.h"
#include "ledbutton.h"
#include "ledstrip.h"
#include "motionsensor.h"
#include "multisensor.h"
#include "outdoorsensors.h"
#include "oled.h"
#include "piezospeaker.h"
#include "relay.h"
#include "sensor.h"
#include "touchpad.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <stubserver/stubserver.h>
#include <stubserver/SimulatedDevice.h>
#include <bindings/device_identifiers.h>
#include <utils/utils.h>
#include <utils/Log.h>
/**
* Number of columns in the GridLayout:
* 24 cols with 50 pixels => 1200 pixels
* 30 cols with 50 pixels => 1500 pixels
*/
static int COLS_PER_ROW = 24;
static int COLUMN_WIDTH = 50;
MainWindow::MainWindow(const char *configFile, int _port)
: QMainWindow(nullptr)
, ui(new Ui::MainWindow)
, configFileName(configFile ? configFile : "")
, serverThread(nullptr)
, serverOK(false)
, port(_port)
{
ui->setupUi(this);
ui->action_Stop->setEnabled(false);
ui->action_Run->setEnabled(false);
ui->centralWidget->setWidgetResizable(true);
setWindowTitle("TF Stub-Visualization");
QDesktopWidget dw;
if (dw.width() > 1600) {
// set width 1420 instead of 1210
this->setGeometry(20, 40, 1420, 1000);
COLS_PER_ROW = 30;
}
if (configFileName.length() > 0)
loadConfig();
else
statusBar()->showMessage(tr("Server cannot be started, no configuration selected"));
}
MainWindow::~MainWindow()
{
if (serverThread)
{
on_action_Stop_triggered();
serverThread->wait();
delete serverThread;
serverThread = nullptr;
}
delete ui;
}
void MainWindow::loadConfig()
{
if (configFileName.length() > 0) {
statusBar()->showMessage(tr("Using configuration: ") + configFileName);
ui->action_Run->setEnabled(true);
}
}
void MainWindow::serverStopped()
{
statusBar()->showMessage(tr("Server stopped"));
ui->action_Stop->setEnabled(false);
ui->action_Run->setEnabled(true);
QGridLayout* layout = static_cast<QGridLayout*>(ui->scrollAreaWidgetContents->layout());
if (layout) {
// remove old widgets
QLayoutItem *child;
while ((child = layout->takeAt(0)) != nullptr)
{
delete child->widget();
delete child;
}
}
serverThread->wait();
delete serverThread;
serverThread = nullptr;
}
void MainWindow::serverError(const QString &err)
{
serverOK = false;
statusBar()->showMessage(err);
}
void MainWindow::on_action_Exit_triggered()
{
on_action_Stop_triggered();
close();
qApp->quit();
}
void MainWindow::on_action_Load_stub_config_triggered()
{
configFileName = QFileDialog::getOpenFileName(this,
tr("Open Config Properties"), ".", tr("Properties Files (*.properties)"));
loadConfig();
}
/**
* Calculate where to put the next widget in the GridLayout
*/
static void calculatePositionAndAdd(int &row, int &col, QGridLayout *layout, QWidget *w)
{
int numCols = w->width() / COLUMN_WIDTH;
if (w->width() % COLUMN_WIDTH != 0)
++numCols;
if (col + numCols > COLS_PER_ROW) {
++row;
col = 0;
}
layout->addWidget(w, row, col, 1, numCols, Qt::AlignHCenter | Qt::AlignVCenter);
col += numCols;
}
/**
* @brief MainWindow::on_action_Run_triggered - trigger when server should be started
* Create new widgets and layout all the stuff
*/
void MainWindow::on_action_Run_triggered()
{
if (serverThread)
return;
ui->action_Stop->setEnabled(true);
ui->action_Run->setEnabled(false);
serverThread = new ServerThread(configFileName, port);
connect(serverThread, &ServerThread::error, this, &MainWindow::serverError);
connect(serverThread, &ServerThread::finished, this, &MainWindow::serverStopped);
serverThread->start();
serverOK = true;
statusBar()->showMessage(tr("Server started"));
utils::msleep(300);
if (!serverOK || !serverThread)
return;
QGridLayout* layout = static_cast<QGridLayout*>(ui->scrollAreaWidgetContents->layout());
if (!layout) {
layout = new QGridLayout();
layout->setVerticalSpacing(20);
ui->scrollAreaWidgetContents->setLayout(layout);
}
int row = 0;
int col = 0;
std::list<const stubserver::SimulatedDevice*> devices = stubserver::getUiDevices();
// add the items in the order they appear: strongly recommended to use UI_UIDS in the config
for (auto it : devices)
{
const char *deviceTitle = it->getTitle().c_str();
const char *uidStr = it->getUidStr().c_str();
VisualizationWidget *vzw = nullptr;
// large items
switch (it->getDeviceTypeId()) {
case MULTI_TOUCH_V2_DEVICE_IDENTIFIER:
case MULTI_TOUCH_DEVICE_IDENTIFIER: {
TouchPad *touch = new TouchPad(this, deviceTitle);
calculatePositionAndAdd(row, col, layout, touch);
touch->setLabels(it->getLabel());
it->setVisualizationClient(*touch);
vzw = touch;
break;
}
case LCD_20X4_DEVICE_IDENTIFIER: {
LCD *lcd = new LCD(this, uidStr);
calculatePositionAndAdd(row, col, layout, lcd);
lcd->setLabels(it->getLabel());
it->setVisualizationClient(*(lcd->getLCDDisplayArea()));
vzw = lcd;
break;
}
case LCD_128X64_DEVICE_IDENTIFIER: {
OLED *w = new OLED(this, deviceTitle);
calculatePositionAndAdd(row, col, layout, w);
it->setVisualizationClient(*w);
vzw = w;
break;
}
case OLED_128X64_DEVICE_IDENTIFIER:
case OLED_64X48_DEVICE_IDENTIFIER: {
OLED *w = new OLED(this, deviceTitle, it->getDeviceTypeId() == OLED_64X48_DEVICE_IDENTIFIER);
calculatePositionAndAdd(row, col, layout, w);
it->setVisualizationClient(*w);
vzw = w;
break;
}
case LED_STRIP_DEVICE_IDENTIFIER: {
LedStrip *w = new LedStrip(this, uidStr);
calculatePositionAndAdd(row, col, layout, w);
it->setVisualizationClient(*w);
vzw = w;
break;
}
case AIR_QUALITY_DEVICE_IDENTIFIER: {
MultiSensor *w = new MultiSensor(this, deviceTitle, 4);
w->setValueLabel(AIR_QUALITY_IAQ, "IAQ");
w->setValueLabel(AIR_QUALITY_TEMP, "°C * 100");
w->setValueLabel(AIR_QUALITY_HUMIDITY, "%rH * 100");
w->setValueLabel(AIR_QUALITY_PRESSURE, "mBar * 100");
calculatePositionAndAdd(row, col, layout, w);
it->setVisualizationClient(*w);
vzw = w;
break;
}
//-------------------------- add small items
case DUAL_RELAY_DEVICE_IDENTIFIER:
case REMOTE_SWITCH_DEVICE_IDENTIFIER:
case INDUSTRIAL_QUAD_RELAY_DEVICE_IDENTIFIER:
case INDUSTRIAL_QUAD_RELAY_V2_DEVICE_IDENTIFIER:
case INDUSTRIAL_DIGITAL_OUT_4_DEVICE_IDENTIFIER:
case INDUSTRIAL_DIGITAL_OUT_4_V2_DEVICE_IDENTIFIER: {
Relay *relay = new Relay(this, deviceTitle, it->getDeviceTypeId() == DUAL_RELAY_DEVICE_IDENTIFIER);
calculatePositionAndAdd(row, col, layout, relay);
it->setVisualizationClient(*relay);
vzw = relay;
break;
}
case AMBIENT_LIGHT_DEVICE_IDENTIFIER:
case AMBIENT_LIGHT_V2_DEVICE_IDENTIFIER:
case ANALOG_IN_V2_DEVICE_IDENTIFIER:
case ANALOG_OUT_DEVICE_IDENTIFIER:
case BAROMETER_DEVICE_IDENTIFIER:
case CO2_V2_DEVICE_IDENTIFIER:
case DISTANCE_IR_DEVICE_IDENTIFIER:
case DISTANCE_US_DEVICE_IDENTIFIER:
case DUST_DETECTOR_DEVICE_IDENTIFIER:
case HALL_EFFECT_V2_DEVICE_IDENTIFIER:
case HUMIDITY_DEVICE_IDENTIFIER:
case LINEAR_POTI_DEVICE_IDENTIFIER:
case LINE_DEVICE_IDENTIFIER:
case LOAD_CELL_DEVICE_IDENTIFIER:
case LOAD_CELL_V2_DEVICE_IDENTIFIER:
case MOISTURE_DEVICE_IDENTIFIER:
case PTC_DEVICE_IDENTIFIER:
case SOUND_INTENSITY_DEVICE_IDENTIFIER:
case TEMPERATURE_DEVICE_IDENTIFIER:
case TEMPERATURE_V2_DEVICE_IDENTIFIER:
case UV_LIGHT_DEVICE_IDENTIFIER: {
Sensor *widget = new Sensor(this, deviceTitle, false);
calculatePositionAndAdd(row, col, layout, widget);
if (it->getDeviceTypeId() == LOAD_CELL_DEVICE_IDENTIFIER ||
it->getDeviceTypeId() == LOAD_CELL_V2_DEVICE_IDENTIFIER ||
it->getDeviceTypeId() == HALL_EFFECT_V2_DEVICE_IDENTIFIER) {
widget->setLedOn(true);
}
widget->setValueLabel(it->getLabel());
it->setVisualizationClient(*widget);
vzw = widget;
break;
}
case HAT_DEVICE_IDENTIFIER:
case MASTER_DEVICE_IDENTIFIER:
case DC_DEVICE_IDENTIFIER:
case SERVO_DEVICE_IDENTIFIER:
case VOLTAGE_CURRENT_DEVICE_IDENTIFIER: {
DualSensor *widget = new DualSensor(this, deviceTitle, uidStr);
calculatePositionAndAdd(row, col, layout, widget);
widget->setValueLabels(it->getLabel(), "Current mA");
widget->setLed1On(true);
it->setVisualizationClient(*widget);
vzw = widget;
break;
}
case INDUSTRIAL_DUAL_ANALOG_IN_DEVICE_IDENTIFIER:
case INDUSTRIAL_DUAL_ANALOG_IN_V2_DEVICE_IDENTIFIER: {
DualSensor *widget = new DualSensor(this, deviceTitle, uidStr);
calculatePositionAndAdd(row, col, layout, widget);
widget->setValueLabels("Ch 0 (mV)", "Ch 1 (mV)");
if (it->getDeviceTypeId() == INDUSTRIAL_DUAL_ANALOG_IN_V2_DEVICE_IDENTIFIER) {
widget->setLed1On(true);
widget->setLed2On(true);
}
it->setVisualizationClient(*widget);
vzw = widget;
break;
}
case OUTDOOR_WEATHER_DEVICE_IDENTIFIER: {
OutdoorSensors *widget = new OutdoorSensors(this, uidStr);
calculatePositionAndAdd(row, col, layout, widget);
widget->setLedOn(true);
it->setVisualizationClient(*widget);
vzw = widget;
break;
}
case TEMPERATURE_IR_DEVICE_IDENTIFIER: {
DualSensor *widget = new DualSensor(this, deviceTitle, "Object / Ambient");
calculatePositionAndAdd(row, col, layout, widget);
widget->setValueLabels(it->getLabel(), "Ambient °C * 100");
it->setVisualizationClient(*widget);
vzw = widget;
break;
}
case HUMIDITY_V2_DEVICE_IDENTIFIER: {
DualSensor *widget = new DualSensor(this, deviceTitle, "Humidity / Temperature");
calculatePositionAndAdd(row, col, layout, widget);
widget->setValueLabels(it->getLabel(), "°C * 100");
it->setVisualizationClient(*widget);
vzw = widget;
break;
}
case BAROMETER_V2_DEVICE_IDENTIFIER: {
DualSensor *widget = new DualSensor(this, deviceTitle, "AirPressure / Temperature");
calculatePositionAndAdd(row, col, layout, widget);
widget->setValueLabels(it->getLabel(), "°C * 100");
it->setVisualizationClient(*widget);
vzw = widget;
break;
}
case ROTARY_POTI_DEVICE_IDENTIFIER: {
Sensor *widget = new Sensor(this, deviceTitle, true);
calculatePositionAndAdd(row, col, layout, widget);
it->setVisualizationClient(*widget);
vzw = widget;
break;
}
case PIEZO_SPEAKER_V2_DEVICE_IDENTIFIER:
case PIEZO_SPEAKER_DEVICE_IDENTIFIER: {
PiezoSpeaker *widget = new PiezoSpeaker(this, deviceTitle, it->getDeviceTypeId() != PIEZO_SPEAKER_DEVICE_IDENTIFIER);
calculatePositionAndAdd(row, col, layout, widget);
it->setVisualizationClient(*widget);
vzw = widget;
break;
}
case INDUSTRIAL_DIGITAL_IN_4_DEVICE_IDENTIFIER:
case INDUSTRIAL_DIGITAL_IN_4_V2_DEVICE_IDENTIFIER: {
DigitalIn *widget = new DigitalIn(this, deviceTitle);
calculatePositionAndAdd(row, col, layout, widget);
it->setVisualizationClient(*widget);
vzw = widget;
break;
}
case DUAL_BUTTON_DEVICE_IDENTIFIER: {
DualButton *widget = new DualButton(this, deviceTitle);
calculatePositionAndAdd(row, col, layout, widget);
it->setVisualizationClient(*widget);
vzw = widget;
break;
}
case RGB_LED_V2_DEVICE_IDENTIFIER:
case RGB_LED_BUTTON_DEVICE_IDENTIFIER: {
LedButton *widget = new LedButton(this, it->getDeviceTypeId() == RGB_LED_V2_DEVICE_IDENTIFIER, deviceTitle);
calculatePositionAndAdd(row, col, layout, widget);
it->setVisualizationClient(*widget);
vzw = widget;
break;
}
case HALL_EFFECT_DEVICE_IDENTIFIER:
case TILT_DEVICE_IDENTIFIER:
case MOTION_DETECTOR_DEVICE_IDENTIFIER:
case MOTION_DETECTOR_V2_DEVICE_IDENTIFIER: {
MotionSensor *widget = new MotionSensor(this, deviceTitle);
calculatePositionAndAdd(row, col, layout, widget);
if (it->getDeviceTypeId() == MOTION_DETECTOR_V2_DEVICE_IDENTIFIER)
widget->showAllLeds();
if (it->getDeviceTypeId() == HALL_EFFECT_DEVICE_IDENTIFIER)
widget->showUseCounter();
else
widget->setLedOn(true);
it->setVisualizationClient(*widget);
vzw = widget;
break;
}
default:
utils::Log() << "UI: ignored device of type " << it->getDeviceTypeName() << " (uid=" << it->getUidStr() << ')';
}
if (vzw)
vzw->setStackParameter(it->getPosition(), it->getConnectedUidStr());
}
ui->scrollAreaWidgetContents->setMinimumHeight(315 * (row+1));
}
void MainWindow::on_action_Stop_triggered()
{
serverOK = false;
if (serverThread)
serverThread->doStop();
}