-
Notifications
You must be signed in to change notification settings - Fork 0
/
flightrequestcreator.cpp
138 lines (127 loc) · 5.69 KB
/
flightrequestcreator.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
#include "flightrequestcreator.h"
#include <QGeoPolygon>
#include <QGeoRectangle>
#include <QGeoCoordinate>
#include "Vehicles/Vehicles.h"
#include "Vehicles/Vehicle.h"
#include "Mission/VehicleMission.h"
#include "Mission/Waypoint.h"
#include "App/AppLog.h"
FlightRequestCreator::FlightRequestCreator(Fact *parent):
HttpApiBase(parent, "create_request", "Create", "", Fact::Group, "plus-circle-outline")
{
f_drones = new Fact(this, "drones", "Drone", "Choose your drone", Fact::Enum, "airplane");
f_startDateTime = new DateTimeFact(this, "start_date_time", "Start date time (UTC)", "", "airplane-takeoff", 60);
f_endDateTime = new DateTimeFact(this, "end_date_time", "End date time (UTC)", "", "airplane-landing", 60 + 60 * 60 * 4);
f_missionSize = new Fact(this, "mission_size", "Mission size", "", Fact::NoFlags, "earth");
f_missionSize->setValue(0);
f_createButton = new Fact(this, "create_button", "Create", "", Fact::Apply | Fact::Action);
f_createStatus = new Fact(this, "create_status", "", "", Fact::NoFlags);
auto mission = Vehicles::instance()->current()->f_mission;
connect(this, &Fact::triggered, this, &FlightRequestCreator::onTriggered);
connect(f_createButton, &Fact::triggered, this, &FlightRequestCreator::onCreateTriggered);
connect(&m_drones, &Drones::dronesReceived, this, &FlightRequestCreator::onDronesReceived);
connect(mission, &VehicleMission::missionSizeChanged, this, &FlightRequestCreator::onMissionSizeChanged);
}
void FlightRequestCreator::onJsonReceived(const QJsonDocument &doc)
{
QString flightRequetsUuid = doc.object()["data"].toString();
if(flightRequetsUuid.isEmpty()) {
apxMsgW() << "Diginavis: " << QString::fromUtf8(doc.toJson());
f_createStatus->setTitle("FAIL");
} else
f_createStatus->setTitle("Success. Please wait for moderator response.");
}
void FlightRequestCreator::onTriggered()
{
for(auto f: facts())
if(f != f_createStatus)
f->setVisible(true);
else
f->setVisible(false);
f_createButton->setVisible(true);
m_drones.setBearerToken(getBearerToken());
m_drones.request();
}
void FlightRequestCreator::onCreateTriggered()
{
QJsonObject root;
root["flightDescription"] = "";
QJsonObject flightplan;
QJsonObject geometry1;
geometry1["type"] = "Feature";
QJsonObject geometry2;
geometry2["type"] = "Polygon";
QJsonArray coordinates;
QJsonArray coordinates2;
QGeoPolygon polygon;
auto mission = Vehicles::instance()->current()->f_mission;
auto waypoints = mission->f_waypoints;
float maxAltitude = 0;
for(int i = 0; i < waypoints->size(); i++) {
auto waypoint = dynamic_cast<Waypoint *>(waypoints->child(i));
if(waypoint) {
auto altitude = waypoint->f_altitude->value().toFloat();
if(altitude > maxAltitude)
maxAltitude = altitude;
polygon.addCoordinate(QGeoCoordinate(waypoint->f_longitude->value().toFloat(),
waypoint->f_latitude->value().toFloat()));
}
}
auto geoRect = polygon.boundingGeoRectangle();
geoRect.setTopLeft(geoRect.topLeft().atDistanceAndAzimuth(1000, 315));
geoRect.setBottomRight(geoRect.bottomRight().atDistanceAndAzimuth(1000, 135));
coordinates2.append(QJsonArray({geoRect.topLeft().latitude(), geoRect.topLeft().longitude()}));
coordinates2.append(QJsonArray({geoRect.topRight().latitude(), geoRect.topRight().longitude()}));
coordinates2.append(QJsonArray({geoRect.bottomRight().latitude(), geoRect.bottomRight().longitude()}));
coordinates2.append(QJsonArray({geoRect.bottomLeft().latitude(), geoRect.bottomLeft().longitude()}));
coordinates2.append(QJsonArray({geoRect.topLeft().latitude(), geoRect.topLeft().longitude()}));
coordinates.append(coordinates2);
geometry2["coordinates"] = coordinates;
geometry1["geometry"] = geometry2;
flightplan["geometry"] = geometry1;
flightplan["maxAltitudeAgl"] = maxAltitude;
flightplan["zonalAreaCode"] = "A54618";
auto startDateTime = f_startDateTime->getDateTime().toSecsSinceEpoch();
auto endDateTime = f_endDateTime->getDateTime().toSecsSinceEpoch();
flightplan["scheduledDateTimeStart"] = startDateTime;
flightplan["scheduledDateTimeEnd"] = endDateTime;
flightplan["reserveDateTimeStart"] = startDateTime;
flightplan["reserveDateTimeEnd"] = endDateTime;
flightplan["location"] = "Russia";
flightplan["supervisorName"] = "Dispatcher";
QJsonObject pilot;
pilot["phoneNumber"] = "+79482647223";
pilot["email"] = "[email protected]";
pilot["firstName"] = "Владимир";
pilot["secondName"] = "Галинский";
pilot["patronymicName"] = "Александрович";
flightplan["pilot"] = pilot;
QJsonArray uavs;
uavs.append(m_drones.f_drones[f_drones->value().toInt()]->property("uuid").toString());
flightplan["uavs"] = uavs;
root["flightPlan"] = flightplan;
QJsonDocument doc(root);
auto request = makeRequest(URL);
m_network.post(request, doc.toJson());
f_createStatus->setTitle("Please wait...");
f_createButton->setVisible(false);
for(auto f: facts())
if(f != f_createStatus)
f->setVisible(false);
else
f->setVisible(true);
}
void FlightRequestCreator::onDronesReceived()
{
QList<QString> enumStrings;
for(auto f: m_drones.f_drones) {
enumStrings.append(QString("%1 (%2)").arg(f->title(), f->descr()));
}
f_drones->setEnumStrings(enumStrings);
}
void FlightRequestCreator::onMissionSizeChanged()
{
auto mission = Vehicles::instance()->current()->f_mission;
f_missionSize->setValue(QString("%1").arg(mission->missionSize()));
}