Line data Source code
1 : #include "../include/mainwindow.h"
2 :
3 : //adding layouts and widgets to main window
4 7 : MainWindow::MainWindow(QWidget *parent)
5 7 : : QMainWindow(parent), client(new QMqttClient(this))
6 : {
7 7 : setStyleSheet("background-color: rgb(0, 0, 20);");
8 7 : left_dial = new Speed(this);
9 7 : right_dial = new Battery(this);
10 7 : object = new Object(this);
11 7 : QHBoxLayout* layout = new QHBoxLayout();
12 7 : layout->addWidget(left_dial, 1, Qt::AlignTop | Qt::AlignLeft);
13 :
14 7 : center_dial = new Lane(this);
15 7 : QVBoxLayout* centerLayout = new QVBoxLayout();
16 7 : centerLayout->addWidget(center_dial, 0, Qt::AlignCenter);
17 7 : layout->addLayout(centerLayout, 1);
18 :
19 7 : layout->addWidget(right_dial, 1, Qt::AlignTop | Qt::AlignRight);
20 7 : QVBoxLayout* mainlayout = new QVBoxLayout();
21 :
22 7 : QHBoxLayout* objectLayout = new QHBoxLayout();
23 7 : objectLayout->addStretch(); // Push the widget to the right
24 7 : object->setFixedSize(60, 60); // or any size that fits your image
25 7 : objectLayout->addWidget(object, 0, Qt::AlignTop | Qt::AlignRight);
26 7 : mainlayout->addLayout(objectLayout, 0);
27 7 : mainlayout->addLayout(layout, 2);
28 :
29 7 : temp = new Temperature(this);
30 7 : autonomy = new Autonomy(this);
31 7 : QHBoxLayout* layoutbar = new QHBoxLayout();
32 7 : layoutbar->setSpacing(width() / 20);
33 7 : layoutbar->addWidget(temp, 0, Qt::AlignBottom | Qt::AlignRight);
34 7 : layoutbar->addWidget(autonomy, 0, Qt::AlignBottom | Qt::AlignLeft);
35 7 : mainlayout->addLayout(layoutbar, 1);
36 7 : QWidget* centralWidget = new QWidget(this);
37 7 : centralWidget->setLayout(mainlayout);
38 7 : setCentralWidget(centralWidget);
39 7 : init_mqtt();
40 7 : }
41 :
42 : //close main window at destruction
43 14 : MainWindow::~MainWindow()
44 : {
45 7 : std::cout << "Remove Window" << std::endl;
46 7 : close();
47 14 : }
48 :
49 : //connecting to mqtt via cloud or localhost or to jetracer via network
50 7 : void MainWindow::init_mqtt()
51 : {
52 7 : client->setHostname("972e24210b544ba49bfb9c1d3164d02b.s1.eu.hivemq.cloud"); //cloud
53 7 : client->setPort(8883);
54 7 : QString user = qgetenv("user");
55 7 : client->setUsername(user);
56 7 : QString pass = qgetenv("password");
57 7 : client->setPassword(pass);
58 : // client->setHostname("10.21.221.67"); //when on the same network
59 : // client->setPort(1883); //cross compiling
60 : // client->setHostname("127.0.0.1"); //when cross-compiling with jetracer
61 :
62 7 : connect(client, &QMqttClient::connected, this, &MainWindow::connected);
63 7 : connect(client, &QMqttClient::messageReceived, this, &MainWindow::message_received);
64 7 : connect(client, &QMqttClient::errorChanged, this, [](QMqttClient::ClientError error) {
65 0 : qDebug() << "MQTT Client error:" << error;
66 0 : });
67 7 : client->connectToHostEncrypted(); //for cloud needs to be encrypted, for jetracer network or localhost its not encrypted
68 14 : }
69 :
70 : //subscribing to topic of mqtt
71 3 : void MainWindow::connected()
72 : {
73 3 : QMqttTopicFilter topic("jetracer/speed");
74 3 : auto speed_sub = client->subscribe(topic);
75 3 : QMqttTopicFilter battery("jetracer/battery");
76 3 : auto bat_sub = client->subscribe(battery);
77 3 : QMqttTopicFilter temp("jetracer/temperature");
78 3 : auto temp_sub = client->subscribe(temp);
79 3 : QMqttTopicFilter autono("jetracer/autonomy");
80 3 : auto autono_sub = client->subscribe(autono);
81 3 : QMqttTopicFilter lane("jetracer/lane_touch");
82 3 : auto lane_sub = client->subscribe(lane);
83 : // QMqttTopicFilter obj("jetracer/object");
84 : // auto object = client->subscribe(obj);
85 3 : if (!speed_sub || !bat_sub | !autono_sub || !temp_sub || !lane_sub) { // || !object
86 0 : qDebug() << "Failed to subscribe to topic";
87 : }
88 6 : }
89 :
90 : //receiving message and updating current
91 8 : void MainWindow::message_received(const QByteArray &message, const QMqttTopicName &topic)
92 : {
93 8 : qDebug() << topic.name() << ":" << message;
94 8 : bool ok;
95 8 : double msg = message.toDouble(&ok);
96 8 : if (ok) {
97 8 : if (topic.name() == "jetracer/speed") {
98 8 : QMetaObject::invokeMethod(this, [this, msg]() {
99 8 : left_dial->set_current(static_cast<float>(msg));
100 8 : }, Qt::AutoConnection);
101 : }
102 0 : else if (topic.name() == "jetracer/battery") {
103 0 : QMetaObject::invokeMethod(this, [this, msg]() {
104 0 : right_dial->set_current(msg);
105 0 : }, Qt::AutoConnection);
106 : }
107 0 : else if (topic.name() == "jetracer/temperature") {
108 0 : QMetaObject::invokeMethod(this, [this, msg]() {
109 0 : temp->set_temperature(msg);
110 0 : }, Qt::AutoConnection);
111 : }
112 0 : else if (topic.name() == "jetracer/autonomy") {
113 0 : QMetaObject::invokeMethod(this, [this, msg]() {
114 0 : autonomy->set_autonomy(msg);
115 0 : }, Qt::AutoConnection);
116 : }
117 0 : else if (topic.name() == "jetracer/lane_touch") {
118 0 : QMetaObject::invokeMethod(this, [this, msg]() {
119 0 : center_dial->set_lane(msg);
120 0 : }, Qt::AutoConnection);
121 : }
122 : // else if (topic.name() == "jetracer/object") {
123 : // QMetaObject::invokeMethod(this, [this, msg]() {
124 : // center_dial->set_object(msg);
125 : // }, Qt::AutoConnection);
126 : // }
127 : } else {
128 0 : qDebug() << "Invalid data received";
129 : }
130 8 : }
131 :
132 2 : QMqttClient* MainWindow::get_client()
133 : {
134 2 : return client;
135 : }
136 :
137 1 : Battery* MainWindow::get_battery()
138 : {
139 1 : return right_dial;
140 : }
141 :
142 1 : Autonomy* MainWindow::get_autonomy()
143 : {
144 1 : std::cout << "Getting autonomy\n";
145 1 : return autonomy;
146 : }
147 :
148 1 : Temperature* MainWindow::get_temperature()
149 : {
150 1 : return temp;
151 : }
152 :
153 3 : Lane* MainWindow::get_lane()
154 : {
155 3 : return center_dial;
156 : }
|