-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
173 lines (149 loc) · 6.16 KB
/
mainwindow.cpp
File metadata and controls
173 lines (149 loc) · 6.16 KB
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QtEndian>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
tcpServer(new QTcpServer(this)),
isClientConnected(false),
socSetpoint(100)
//sendTimer(new QTimer(this))
{
ui->setupUi(this);
//connect(sendTimer, &QTimer::timeout, this, &MainWindow::sendData);
}
MainWindow::~MainWindow() {
tcpServer->close();
tcpServer->deleteLater();
delete ui;
}
/**
* @brief MainWindow::on_sendButton_clicked
*
* Send the data from the form (representing the car) to croonwolter & dros.
*/
void MainWindow::on_sendButton_clicked()
{
if (!isClientConnected){
QMessageBox::information(this, tr("STE-CroonwolterDros Interface test"),
tr("Client is not connected."));
return;
}
bool conversionOk;
int32_t soc = ui->carSoCEdit->text().toInt(&conversionOk);
if(!conversionOk || !(0 <= soc && soc <= 100)) {
QMessageBox::information(this, tr("STE-CroonwolterDros Interface test"),
tr("State of Charge should be an integer between 0 and 100."));
return;
}
int32_t power = ui->actualPowerEdit->text().toInt(&conversionOk);
if(!conversionOk || !(-15000 <= power && power <= 15000)) {
QMessageBox::information(this, tr("STE-CroonwolterDros Interface test"),
tr("Actual power should be an integer between -15000 and 15000."));
return;
}
bool connected = ui->coupledTrueRadioButton->isChecked();
bool local = ui->localControlTrueButton->isChecked();
vieData myData;
myData.connected = connected;
myData.local = local;
myData.power = qToBigEndian(power);
myData.setpoint = qToBigEndian(socSetpoint);
myData.soc = qToBigEndian(soc);
client->write(reinterpret_cast<char*>(&myData), sizeof(vieNetworkData));
client->flush();
}
/**
* @brief MainWindow::on_startButton_clicked
*
* The user clicked the start server button, handle this click by starting a server.
* A TCP server can only be created on valid ports (1 - 65535), on succesfully creating
* a server display this to the user on the UI.
*/
void MainWindow::on_startButton_clicked() {
ui->startButton->setEnabled(false);
QString port = ui->portEdit->text();
bool ok;
int portNr = port.toInt(&ok);
//port should be int and between 0 and 65535
if(!ok || !(0 < portNr && portNr < 65535)){
ui->startButton->setEnabled(true);
ui->connectedLabel->setText("Disconnected");
ui->connectedLabel->setStyleSheet("color: red");
QMessageBox::information(this, tr("STE-CroonwolterDros Interface test"),
tr("Port should be an integer between 1 and 65535"));
return;
}
if (!tcpServer->listen(QHostAddress::Any, portNr)){
ui->startButton->setEnabled(true);
ui->connectedLabel->setText("Disconnected");
ui->connectedLabel->setStyleSheet("color: red");
QMessageBox::information(this, tr("STE-CroonwolterDros Interface test"),
tr("Could not start the server: %1.").arg(tcpServer->errorString()));
return;
}
ui->connectedLabel->setText("Waiting for connection");
ui->connectedLabel->setStyleSheet("color: orange");
connect(tcpServer, &QTcpServer::newConnection, this, &MainWindow::handleNewConnection);
}
/**
* @brief MainWindow::handleNewConnection
* A new client is trying to connect to the server, we only allow one active connection
* for simplicity reasons. Hence drop the connection if there is already a client connected
* else accept the socket by connecting the signals with the slots of this application.
* And showing that a client was connected on the UI.
*/
void MainWindow::handleNewConnection(){
if (isClientConnected){
// We only accept one client in this setting, so disconnect the new client.
QTcpSocket *newClient = tcpServer->nextPendingConnection();
connect(newClient, &QAbstractSocket::disconnected, newClient, &QObject::deleteLater);
newClient->disconnectFromHost();
QMessageBox::information(this, tr("STE-CroonwolterDros Interface test"),
tr("A new client connected, while there was already a client connected. The new client has been disconnected."));
} else {
isClientConnected = true;
client = tcpServer->nextPendingConnection();
connect(client, &QAbstractSocket::disconnected, this, &MainWindow::handleClientDisconnection);
connect(client, &QAbstractSocket::disconnected, this, &QObject::deleteLater);
connect(client, &QIODevice::readyRead, this, &MainWindow::readResponse);
ui->connectedLabel->setText("Client connected");
ui->connectedLabel->setStyleSheet("color: green");
//sendTimer->start(500);
}
}
/**
* @brief MainWindow::handleClientDisconnection
*
* Handle the disconnection of the client by showing on the UI that the server
* is waiting for a connection.
*/
void MainWindow::handleClientDisconnection() {
ui->connectedLabel->setText("Waiting for connection");
ui->connectedLabel->setStyleSheet("color: orange");
//sendTimer->stop();
isClientConnected = false;
}
void MainWindow::readResponse() {
if (client->bytesAvailable() >= 5) {
// QByteArray data = client->read(5);
// qDebug() << data;
char networkBytes[5];
client->read(networkBytes, 5);
bool active = networkBytes[0];
socSetpoint = ((unsigned char)(networkBytes[1]) << 24) | ((unsigned char)(networkBytes[2]) << 16) | ((unsigned char)(networkBytes[3]) << 8) | ((unsigned char)(networkBytes[4]));
ui->currentSetpointLabel->setText(QString().number(socSetpoint));
ui->receiveControlLabel->setText((active ? QString("true") : QString("false")));
ui->receiveSetpointLabel->setText(QString().number(socSetpoint));
}
}
/*
void MainWindow::sendData() {
if (isClientConnected) {
} else {
qDebug() << "Warining: Timer fired after client was disconnected";
return;
}
}
*/