-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
131 lines (109 loc) · 4.22 KB
/
main.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
#include "mavlink/mavlinkudp.h"
#include "qjsonobject.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QSslSocket>
#include <mavlink/planecontroller.h>
#include <utils/httpclient.h>
#include <utils/notificationcenter.h>
#include <utils/teknofestproperties.h>
#include <QJsonDocument>
#include <video/videoitem.h>
#include <QList>
#include <QString>
#include <QDir>
#include <QMetaType>
#include <horizon/linearindicator.h>
bool isSerialPort(const QString &filename) {
return filename.startsWith("ttyUSB") || filename.startsWith("ttyACM");
}
QStringList get_available_ports() {
//std::vector<std::string> ports;
QStringList ports;
// Directory where serial port files are located
const QString devDir = "/sys/class/tty";
QDir dir(devDir);
// Get the list of files in the directory
QStringList fileList = dir.entryList();
// Iterate through all files in the directory
for (const QString &entry : fileList) {
QString filePath = dir.absoluteFilePath(entry);
QFileInfo fileInfo(filePath);
if (isSerialPort(entry)) {
qDebug() << fileInfo;
std::string str = filePath.toStdString();
str.erase(0, 15);
ports << QString::fromStdString(str);
}
}
return ports;
}
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
VideoItem videoItem;
qmlRegisterType<VideoItem>("CustomTypes", 1, 0, "VideoItem");
qmlRegisterType<LinearIndicator>("io.smth", 1, 0, "LinearIndicator");
qmlRegisterType<ColorSegment>("io.smth", 1, 0, "ColorSegment");
NotificationCenter notificationCenter;
MavLinkProperties mavlinkProperties;
PlaneController planeController(¬ificationCenter);
HttpClient client;
TeknofestProperties teknofestProperties;
teknofestProperties.setQrLongitude(-122.25);
mavlinkProperties.setPorts(get_available_ports());
MavLinkUDP mavLink(&mavlinkProperties, &planeController, &teknofestProperties, &client, ¬ificationCenter, &app);
QQmlApplicationEngine engine;
teknofestProperties.setSimMode(false);
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(
&engine,
&QQmlApplicationEngine::objectCreated,
&app,
[url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
},
Qt::QueuedConnection);
engine.rootContext()->setContextProperty("mavlinkHandler", &mavLink);
engine.rootContext()->setContextProperty("mavlinkProperties", &mavlinkProperties);
engine.rootContext()->setContextProperty("teknofestProperties", &teknofestProperties);
engine.rootContext()->setContextProperty("stringList", QVariant::fromValue(mavlinkProperties.ports()));
engine.rootContext()->setContextProperty("NotificationCenter", ¬ificationCenter);
QString ipString = "127.0.0.1"; // Example IP address
int port = 14550;
int result = mavLink.initialize(ipString, port);
if (result < 0) {
// Try mavproxy
result = mavLink.initialize(ipString, 3131);
if(result < 0){
qDebug() << "Initialization failed. Error code:" << result;
}
// Handle initialization failure
//return -1;
}else {
//mavLink.initTeknofest();
}
planeController.setQmlContext(engine.rootContext());
if(mavlinkProperties.connected()){
qDebug() << "Connected to mavlink.";
/*
* This can not be used because they need to specify.
* if(teknofestProperties.simMode()){
//plane* plane = mavLink.createDummyPlane();
plane* plane = planeController.findMainPlane(teknofestProperties.takimid());
QJsonObject json = plane->toJson();
//qDebug() << "------";
//qDebug() << json;
//qDebug() << "------";
QString serverUrl = "http://replica.neostellar.net/api/telemetri_gonder"; // Example server URL
client.sendLocationData(&teknofestProperties, &planeController, json);
} */
}
engine.load(url);
return app.exec();
}