-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebServer.cpp
More file actions
255 lines (217 loc) · 9.54 KB
/
WebServer.cpp
File metadata and controls
255 lines (217 loc) · 9.54 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
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
#include "WebServer.h"
namespace crow {
// Any middleware requires following 3 members:
// struct context;
// storing data for the middleware; can be read from another middleware or handlers
// before_handle
// called before handling the request.
// if res.end() is called, the operation is halted.
// (still call after_handle of this middleware)
// 2 signatures:
// void before_handle(request& req, response& res, context& ctx)
// if you only need to access this middlewares context.
// template <typename AllContext>
// void before_handle(request& req, response& res, context& ctx, AllContext& all_ctx)
// you can access another middlewares' context by calling `all_ctx.template get<MW>()'
// ctx == all_ctx.template get<CurrentMiddleware>()
// after_handle
// called after handling the request.
// void after_handle(request& req, response& res, context& ctx)
// template <typename AllContext>
// void after_handle(request& req, response& res, context& ctx, AllContext& all_ctx)
struct CORS {
struct context {
};
void before_handle(request& req, response& res, context& ctx) {
// Handle CORS headers here
res.add_header("Access-Control-Allow-Origin", "*");
res.add_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
res.add_header("Access-Control-Allow-Headers", "Content-Type");
res.add_header("Access-Control-Max-Age", "86400"); // Optional: Specify max age for preflight requests
res.add_header("Content-Type", "application/json");
}
void after_handle(request& req, response& res, context& ctx) {
}
};
}
namespace PaulNovack {
class Navigation;
WebServer::WebServer() {
}
void WebServer::setNavigation(Navigation& navigation) {
_navigation = &navigation;
}
void WebServer::setDataStore(DataStore& ds) {
_ds = &ds;
}
void WebServer::Run() {
crow::App<crow::CORS> app;
app.loglevel(crow::LogLevel::ERROR);
CROW_ROUTE(app, "/getNavigationData")
.methods("GET"_method)
([this] {
crow::json::wvalue x({});
NavData navData = _navigation->getNavData();
x["destHeading"] = navData._destHeading;
x["destLatitude"] = navData._destLatitude;
x["destLongitude"] = navData._destLongitude;
x["goBackSecondsSpeedCalculation"] = navData._goBackSecondsSpeedCalculation;
x["hdop"] = navData._hdop;
x["heading"] = navData._heading;
x["headingDeviation"] = navData._headingDeviation;
x["lHTurn"] = navData._lHTurn;
x["latitude"] = navData._latitude;
x["longitude"] = navData._longitude;
x["milesToDestination"] = navData._milesToDestination;
x["pdop"] = navData._pdop;
x["rHTurn"] = navData._rHTurn;
x["speedMPH"] = navData._speedMPH;
x["stepsPerCorrection"] = navData._stepsPerCorrection;
x["turnDegrees"] = navData._turnDegrees;
return x;
});
CROW_ROUTE(app, "/setWaypoint")
.methods("POST"_method)
([this](const crow::request& req, crow::response & res) {
try {
// Parse the JSON data from the request body
json jsonData = json::parse(req.body);
string name = jsonData["name"];
string description = jsonData["description"];
float latitude = jsonData["latitude"];
float longitude = jsonData["longitude"];
// Create a JSON response with the extracted values
json response;
response["name"] = name;
response["description"] = description;
response["latitude"] = latitude;
response["longitude"] = longitude;
// Set the response body with the JSON data
res.body = response.dump();
// Set the response status to 200 OK
res.code = 200;
} catch (const std::exception& e) {
// Handle any parsing errors or other exceptions
res.code = 400;
res.body = "Error: " + std::string(e.what());
}
// Send the response
res.end();
});
CROW_ROUTE(app, "/setDestination")
.methods("POST"_method)
([this](const crow::request& req, crow::response & res) {
try {
// Parse the JSON data from the request body
json jsonData = json::parse(req.body);
float destinationLatitude = jsonData["destinationLatitude"];
float destinationLongitude = jsonData["destinationLongitude"];
json response;
response["destinationLatitude"] = destinationLatitude;
response["destinationLongitude"] = destinationLongitude;
_navigation->setDestination(destinationLatitude, destinationLongitude);
res.body = response.dump();
res.code = 200;
} catch (const std::exception& e) {
// Handle any parsing errors or other exceptions
res.code = 400;
res.body = "Error: " + std::string(e.what());
}
// Send the response
res.end();
});
CROW_ROUTE(app, "/getWaypoints")([this](const crow::request& req, crow::response & res) {
std::map<int, WayPoint> waypoints = _ds->getWayPoints();
json jsonResponse = json::array();
for (const auto& pair : waypoints) {
int id = pair.first;
const WayPoint& waypoint = pair.second;
json waypointJson;
waypointJson["id"] = id;
waypointJson["name"] = waypoint.name;
waypointJson["description"] = waypoint.description;
waypointJson["latitude"] = waypoint.latitude;
waypointJson["longitude"] = waypoint.longitude;
waypointJson["depth"] = waypoint.depth;
jsonResponse.push_back(waypointJson);
} // Set the response body with the JSON data
res.body = jsonResponse.dump();
// Set the response status to 200 OK
res.code = 200;
res.end();
});
CROW_ROUTE(app, "/insertWaypoint")
.methods("POST"_method)
([this](const crow::request& req, crow::response & res) {
crow::json::rvalue wayPoint = crow::json::load(req.body);
WayPoint wp;
// Extract values from the JSON and set variables
wp.depth = wayPoint["depth"].d();
wp.description = wayPoint["description"].s();
wp.latitude = wayPoint["latitude"].d();
wp.longitude = wayPoint["longitude"].d();
wp.name = wayPoint["name"].s();
wp.id = _ds->insertWayPoint(wp);
json waypointJson;
waypointJson["depth"] = wp.depth;
waypointJson["description"] = wp.description;
waypointJson["latitude"] = wp.latitude;
waypointJson["longitude"] = wp.longitude;
waypointJson["name"] = wp.name;
waypointJson["id"] = wp.id;
res.body = waypointJson.dump();
res.code = 200;
res.end();
});
CROW_ROUTE(app, "/updateWaypoint")
.methods("POST"_method)
([this](const crow::request& req, crow::response & res) {
crow::json::rvalue wayPoint = crow::json::load(req.body);
WayPoint wp;
// Extract values from the JSON and set variables
wp.depth = wayPoint["depth"].d();
wp.description = wayPoint["description"].s();
wp.latitude = wayPoint["latitude"].d();
wp.longitude = wayPoint["longitude"].d();
wp.name = wayPoint["name"].s();
wp.id = wayPoint["id"].i();
wp = _ds->updateWayPoint(wp);
json waypointJson;
waypointJson["depth"] = wp.depth;
waypointJson["description"] = wp.description;
waypointJson["latitude"] = wp.latitude;
waypointJson["longitude"] = wp.longitude;
waypointJson["name"] = wp.name;
waypointJson["id"] = wp.id;
res.body = waypointJson.dump();
res.code = 200;
res.end();
});
CROW_ROUTE(app, "/deleteWaypoint")
.methods("POST"_method)
([this](const crow::request& req, crow::response & res) {
crow::json::rvalue wayPoint = crow::json::load(req.body);
WayPoint wp;
// Extract values from the JSON and set variables
wp.depth = wayPoint["depth"].d();
wp.description = wayPoint["description"].s();
wp.latitude = wayPoint["latitude"].d();
wp.longitude = wayPoint["longitude"].d();
wp.name = wayPoint["name"].s();
wp.id = wayPoint["id"].i();
_ds->deleteWayPoint(wp);
json waypointJson;
res.body = waypointJson.dump();
res.code = 200;
res.end();
});
CROW_ROUTE(app, "/")([this]() {
return "AutoPilot Server. Nothing here routes return JSON for UI.";
});
app.port(18080).multithreaded().run_async();
}
WebServer::WebServer(const WebServer& orig) {
}
WebServer::~WebServer() {
}
}