Updated uri handling on server

This commit is contained in:
Pax1601
2023-03-06 20:17:18 +01:00
parent 0fa29284c8
commit b2429ba256
12 changed files with 84 additions and 65 deletions

View File

@@ -83,16 +83,24 @@ json::value Unit::json(bool fullRefresh)
{
auto json = json::value::object();
/********** Base data **********/
json[L"AI"] = AI;
json[L"name"] = json::value::string(name);
json[L"unitName"] = json::value::string(unitName);
json[L"groupName"] = json::value::string(groupName);
json[L"alive"] = alive;
json[L"category"] = json::value::string(getCategory());
/********** Flight data **********/
json[L"flightData"] = json::value::object();
json[L"flightData"][L"latitude"] = latitude;
json[L"flightData"][L"longitude"] = longitude;
json[L"flightData"][L"altitude"] = altitude;
json[L"flightData"][L"speed"] = speed;
json[L"flightData"][L"heading"] = heading;
if (fullRefresh)
{
/********** Base data **********/
json[L"AI"] = AI;
json[L"name"] = json::value::string(name);
json[L"unitName"] = json::value::string(unitName);
json[L"groupName"] = json::value::string(groupName);
json[L"alive"] = alive;
json[L"category"] = json::value::string(getCategory());
/********** Mission data **********/
json[L"missionData"] = json::value::object();
json[L"missionData"][L"fuel"] = fuel;
@@ -145,14 +153,6 @@ json::value Unit::json(bool fullRefresh)
json[L"optionsData"][L"reactionToThreat"] = json::value::string(reactionToThreat);
}
/********** Flight data **********/
json[L"flightData"] = json::value::object();
json[L"flightData"][L"latitude"] = latitude;
json[L"flightData"][L"longitude"] = longitude;
json[L"flightData"][L"altitude"] = altitude;
json[L"flightData"][L"speed"] = speed;
json[L"flightData"][L"heading"] = heading;
return json;
}

View File

@@ -12,7 +12,7 @@ UnitsManager* unitsManager = nullptr;
Server* server = nullptr;
Scheduler* scheduler = nullptr;
json::value airbasesData;
json::value bullseyeData;
json::value bullseyesData;
mutex mutexLock;
bool initialized = false;
@@ -91,7 +91,7 @@ extern "C" DllExport int coreMissionData(lua_State * L)
if (missionData.has_object_field(L"airbases"))
airbasesData = missionData[L"airbases"];
if (missionData.has_object_field(L"bullseye"))
bullseyeData = missionData[L"bullseye"];
bullseyesData = missionData[L"bullseyes"];
return(0);
}

View File

@@ -10,7 +10,7 @@
extern UnitsManager* unitsManager;
extern Scheduler* scheduler;
extern json::value airbasesData;
extern json::value bullseyeData;
extern json::value bullseyesData;
extern mutex mutexLock;
void handle_eptr(std::exception_ptr eptr)
@@ -71,28 +71,29 @@ void Server::handle_get(http_request request)
std::exception_ptr eptr;
try {
auto answer = json::value::object();
wstring requestUri = request.request_uri().to_string();
log(requestUri);
if (requestUri.compare(L"/" + wstring(REST_URI) + L"/" + wstring(UNITS_URI) + L"/" + wstring(PARTIAL_REFRESH_URI)) == 0)
unitsManager->updateAnswer(answer, false);
auto path = uri::split_path(uri::decode(request.relative_uri().path()));
if (requestUri.compare(L"/" + wstring(REST_URI) + L"/" + wstring(UNITS_URI) + L"/" + wstring(FULL_REFRESH_URI)) == 0)
unitsManager->updateAnswer(answer, true);
/* Get the logs from the logger */
if (requestUri.compare(L"/" + wstring(REST_URI) + L"/" + wstring(LOGS_URI)) == 0)
if (path.size() > 0)
{
auto logs = json::value::object();
getLogsJSON(logs); // By reference, for thread safety
answer[L"logs"] = logs;
if (path[0] == UNITS_URI && path.size() > 1)
{
if (path[1] == UPDATE_URI)
unitsManager->updateAnswer(answer, false);
else if (path[1] == REFRESH_URI)
unitsManager->updateAnswer(answer, true);
}
else if (path[0] == LOGS_URI)
{
auto logs = json::value::object();
getLogsJSON(logs, 100); // By reference, for thread safety. Get the last 100 log entries
answer[L"logs"] = logs;
}
else if (path[0] == AIRBASES_URI)
answer[L"airbases"] = airbasesData;
else if (path[0] == BULLSEYE_URI)
answer[L"bullseyes"] = bullseyesData;
}
if (requestUri.compare(L"/" + wstring(REST_URI) + L"/" + wstring(AIRBASES_URI)) == 0)
answer[L"airbases"] = airbasesData;
if (requestUri.compare(L"/" + wstring(REST_URI) + L"/" + wstring(BULLSEYE_URI)) == 0)
answer[L"bullseye"] = bullseyeData;
response.set_body(answer);
}
catch (...) {