-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
309 lines (269 loc) · 11.6 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtMqtt/QMqttClient>
#include <QTime>
#include <QTimer>
#include <QtQml/QQmlContext>
#include <QQmlProperty>
#include <QMetaObject>
#include <cstdlib>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <fstream>
#include <QFile>
#include <QtMath>
bool connectedToMQTT = false;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// init components
// add graph into widgets gui
bool openGLSupported = QQuickWindow::graphicsApi() == QSGRendererInterface::OpenGLRhi;
if (!openGLSupported) {
qWarning() << "OpenGL is not set as the graphics backend, so AbstractSeries.useOpenGL will not work.";
qWarning() << "Set QSG_RHI_BACKEND=opengl environment variable to force the OpenGL backend to be used.";
}
this->scopeView = new QQuickView();
this->dataSource = new DataSource(scopeView);
scopeView->rootContext()->setContextProperty("dataSource", dataSource);
scopeView->rootContext()->setContextProperty("openGLSupported", openGLSupported);
//oscilliscope
QWidget *scopeContainer = QWidget::createWindowContainer(this->scopeView, this);
scopeView->setColor(QColor("#2A2C3A")); // I can't make it transparent, so I set it to the same color
scopeView->setSource(QUrl("qrc:/styles/ScopeView.qml"));
ui->oscilloscopeLayout->addWidget(scopeContainer);
//Voltage Control
this->voltageTumblerView = new QQuickView();
QWidget *voltageTumblerContainer = QWidget::createWindowContainer(this->voltageTumblerView, this);
this->voltageTumblerView->setColor(QColor("#2A2C3A")); // I can't make it transparent, so I set it to the same color
this->voltageTumblerView->setSource(QUrl("qrc:/styles/NumberTumbler.qml"));
ui->VoltageOutputTumbler->addWidget(voltageTumblerContainer);
//Current Control
this->currentTumblerView = new QQuickView();
QWidget *currentTumblerContainer = QWidget::createWindowContainer(this->currentTumblerView, this);
this->currentTumblerView->setColor(QColor("#2A2C3A")); // I can't make it transparent, so I set it to the same color
this->currentTumblerView->setSource(QUrl("qrc:/styles/NumberTumbler.qml"));
ui->CurrentOutputTumbler->addWidget(currentTumblerContainer);
ui->MainPages->setCurrentIndex(0);
//start timer
QTimer *timer = new QTimer(); // for starting the clock
connect(timer, &QTimer::timeout, this, &MainWindow::updateTime);
timer->start(1000); // clock update frequency (1 update/s)
// setOutputCurrent = ui->currentSpinBox->text().toInt();
// setOutputVoltage = ui->voltageSpinBox->text().toInt();
updateSetOutputStatus();
//connect to mqtt
m_client = new QMqttClient();
m_client->setKeepAlive(10000);
m_client->setHostname("137.184.70.171"); // test mde signal
m_client->setPort(1883);
m_client->setUsername("mde_test");
m_client->setPassword("mde_test");
connect(m_client, &QMqttClient::messageReceived, this, &MainWindow::updateOnMessageReceived);
connect(m_client, &QMqttClient::disconnected, this, [this](){
// update the icon here
connectedToMQTT = false;
ui->ConnectionSymbol->setStyleSheet("border-image: url(:/pictures/disconnected.png) no-repeat");
qDebug() << "disconnected\n";
QTimer::singleShot(1000, this, [this](){this->m_client->connectToHost();});
// while (true) {
// if (try to reconnect) {
// break;
// }
// std::sleep(1); // Wait for 5 seconds before attempting to reconnect
// }
});
connect(m_client, &QMqttClient::connected, this, [this](){
// update the icon here
connectedToMQTT = true;
ui->ConnectionSymbol->setStyleSheet("border-image: url(:/pictures/connected.png) no-repeat");
qDebug() << "connected\n";
auto subscription = m_client->subscribe(QString("/pebb/voltage"), 0);
if (!subscription) {
qDebug() << QLatin1String("Error:") << QLatin1String("Could not subscribe. Is there a valid connection?");
}
subscription = m_client->subscribe(QString("/pebb/current"), 0);
if (!subscription) {
qDebug() << QLatin1String("Error:") << QLatin1String("Could not subscribe. Is there a valid connection?");
}
// TODO: Ask communication team to send us power status of the PEBB (ON/OFF) when initializing connection
m_client->subscribe(QString("/pebb/state"), 0);
m_client->subscribe(QString("/pebb/fault"), 0);
});
// UI interaction signals/slots
connect(ui->SetOutputButton, &QPushButton::clicked, this, &MainWindow::goToSetCurrentVoltagePage);
connect(ui->CancelButton, &QPushButton::clicked, this, &MainWindow::cancelSetCurrentVoltage);
connect(ui->OffButton, &QPushButton::clicked, this, &MainWindow::turnOnOff);
connect(ui->SaveButton, &QPushButton::clicked, this, &MainWindow::saveSetCurrentVoltage);
connect(ui->StateComboBox, &QComboBox::currentTextChanged, this, &MainWindow::changeState);
connect(ui->FaultComboBox, &QComboBox::currentTextChanged, this, &MainWindow::changeFault);
m_client->connectToHost();
//battery
// ui->MainPages->setCurrentIndex(0);
// QTimer *timer2 = new QTimer(); // for starting the clock
// connect(timer2, &QTimer::timeout, this, &MainWindow::BatteryBar_Update);
// timer2->start(1000); // clock update frequency (1 update/s)
//ui->BatteryBar->setValue(deviceInfo->batteryLevel());
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::goToSetCurrentVoltagePage()
{
ui->MainPages->setCurrentIndex(1);
}
void MainWindow::cancelSetCurrentVoltage()
{
updateSetOutputStatus();
ui->MainPages->setCurrentIndex(0);
// ui->currentSpinBox->setValue(setOutputCurrent);
// ui->voltageSpinBox->setValue(setOutputVoltage);
QVariant returnedValue;
QMetaObject::invokeMethod((QObject*) this->voltageTumblerView->rootObject(), "setValue",
Q_RETURN_ARG(QVariant, returnedValue),
Q_ARG(QVariant, setOutputVoltage));
QMetaObject::invokeMethod((QObject*) this->currentTumblerView->rootObject(), "setValue",
Q_RETURN_ARG(QVariant, returnedValue),
Q_ARG(QVariant, setOutputCurrent));
}
void MainWindow::saveSetCurrentVoltage()
{
// set(save) values
// send mqtt messages
setOutputCurrent = QQmlProperty::read((QObject*)this->currentTumblerView->rootObject(), "value").toInt();
setOutputVoltage = QQmlProperty::read((QObject*)this->voltageTumblerView->rootObject(), "value").toInt();
int setOutputCurrentPeak = qSqrt(2) * setOutputCurrent;
int setOutputVoltagePeak = qSqrt(2) * setOutputVoltage;
m_client->publish(QMqttTopicName("/pebb/setVoltage"), QString::number(setOutputVoltagePeak).toUtf8());
m_client->publish(QMqttTopicName("/pebb/setCurrent"), QString::number(setOutputCurrentPeak).toUtf8());
updateSetOutputStatus();
ui->MainPages->setCurrentIndex(0);
}
void MainWindow::setDCCurrentLabel(double currentValue)
{
ui->DCCurrentLabel->setText(QString("DC Curr.: %1A").arg(currentValue, 0, 'f', 1));
}
void MainWindow::setDCVoltageLabel(double voltageValue)
{
ui->DCVoltageLabel->setText(QString("DC Volt.: %1V").arg(voltageValue, 0, 'f', 1));
}
void MainWindow::updateTime()
{
QTime time = QTime::currentTime();
ui->TimeLabel->setText(time.toString("h:mmAP"));
}
void MainWindow::updateSetOutputStatus()
{
ui->outputStatusLabel->setText(QString("Set DC Current: %1A\nSet DC Voltage: %2V").arg(QString::number(setOutputCurrent), QString::number(setOutputVoltage)));
}
// maybe we don't need this?
void MainWindow::changeState(const QString &text)
{
m_client->publish(QMqttTopicName("/pebb/state"), text.toUtf8());
}
void MainWindow::changeFault(const QString &text)
{
m_client->publish(QMqttTopicName("/pebb/fault"), text.toUtf8());
}
void MainWindow::turnOnOff()
{
switch(ui->StateComboBox->currentIndex())
{
case 0: //ST_OFF
m_client->publish(QMqttTopicName("/pebb/power"), "on");
ui->StateComboBox->setItemData(0,QColor(Qt::blue),Qt::BackgroundRole);
ui->StateComboBox->setStyleSheet("QComboBox#StateComboBox::drop-down {border-width:0px;}QComboBox#StateComboBox::down-arrow {border-width:0px;image: none;} QComboBox#StateComboBox{background-color:green; color:#c2c7d5}");
break;
case 1: //ST_ON
m_client->publish(QMqttTopicName("/pebb/power"), "off");
ui->StateComboBox->setStyleSheet("QComboBox#StateComboBox::drop-down {border-width:0px;}QComboBox#StateComboBox::down-arrow {border-width:0px;image: none;} QComboBox#StateComboBox{background-color:yellow; color:#808080}");
break;
default:
break;
}
}
void MainWindow::updateOnMessageReceived(const QByteArray &message, const QMqttTopicName &topic)
{
if(topic.name().compare("/pebb/voltage") == 0)
{
setDCVoltageLabel(message.toDouble());
this->dataSource->addVoltage(message.toDouble());
// this->dataSource->addCurrent(message.toDouble());
}
else if (topic.name().compare("/pebb/current") ==0)
{
setDCCurrentLabel(message.toDouble());
this->dataSource->addCurrent(message.toDouble());
}
else if (topic.name() == "/pebb/state")
{
if(message.toStdString().compare("ST_OFF") == 0)
{
ui->StateComboBox->setCurrentIndex(0);
}
else if (message.toStdString().compare("ST_ON") == 0)
{
ui->StateComboBox->setCurrentIndex(1);
}
}
else if (topic.name() == "/pebb/fault")
{
if(message.toStdString().compare("NO_FAULT") == 0)
{
ui->FaultComboBox->setCurrentIndex(0);
ui->FaultComboBox->setStyleSheet("QComboBox#FaultComboBox{background-color: green;}");
}
else if (message.toStdString().compare("FAULT") == 0)
{
ui->FaultComboBox->setCurrentIndex(1);
ui->FaultComboBox->setStyleSheet("QComboBox#FaultComboBox{background-color: red;}");
}
}
}
void MainWindow::changeStateDropDownBGColor(QString backgroundColor)
{
QString templateSS = "QComboBox#StateComboBox::drop-down {border-width:0px;}QComboBox#StateComboBox::down-arrow {border-width:0px;image: none;} QComboBox#StateComboBox{background-color:%1; color:#c2c7d5}";
ui->StateComboBox->setStyleSheet(templateSS.arg(backgroundColor));
}
void MainWindow::on_OffButton_clicked()
{
}
void MainWindow::BatteryBar_Update()
{
QFile bCap("/sys/class/power_supply/BAT0/capacity");
bCap.open(QIODevice::ReadOnly | QIODevice::Text);
QString levelString = QString(bCap.readAll());
int level = levelString.toInt();
bCap.close();
if(level > 50){
ui->BatteryBar->setStyleSheet(QString::fromUtf8("QProgressBar#BatteryBar{color: black;}\n"
"\n"
"QProgressBar::chunk {\n"
" background-color: #02AA20;\n"
" width: 5px;\n"
" }"));
}
else if(level < 50 && level > 20){
ui->BatteryBar->setStyleSheet(QString::fromUtf8("QProgressBar#BatteryBar{color: black;}\n"
"\n"
"QProgressBar::chunk {\n"
" background-color: yellow;\n"
" width: 5px;\n"
" }"));
}
else if(level < 20){
ui->BatteryBar->setStyleSheet(QString::fromUtf8("QProgressBar#BatteryBar{color: black;}\n"
"\n"
"QProgressBar::chunk {\n"
" background-color: red;\n"
" width: 5px;\n"
" }"));
}
ui->BatteryBar->setValue(level);
}