From 312f13854f087b8ef7e5e91ad6b397c9223d42ab Mon Sep 17 00:00:00 2001 From: viper4gh Date: Wed, 5 Jan 2022 13:03:17 +0100 Subject: [PATCH] Added timestamp to data (in milliseconds + 1 digit) Updated response codes and included AMS2 --- CRESTServer.cpp | 2 +- HttpMessageHandler.cpp | 4 ++-- SharedMemoryRenderer.cpp | 9 +++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CRESTServer.cpp b/CRESTServer.cpp index 7c2c0e4..e8a55a9 100644 --- a/CRESTServer.cpp +++ b/CRESTServer.cpp @@ -8,7 +8,7 @@ #include // Configuration properties -#define CREST2_VERSION "v0.4.0" +#define CREST2_VERSION "v0.4.1" #define POLL_TIME_IN_MILLIS 17 // fossa definition: maximum number of milliseconds to sleep for ns_mgr_poll/ fossa calls the winsock select() function with it, where it is a timeout: // The select function returns the total number of socket handles that are ready and contained in the fd_set structures, zero if the time limit expired. diff --git a/HttpMessageHandler.cpp b/HttpMessageHandler.cpp index 387d5bf..5fa6686 100644 --- a/HttpMessageHandler.cpp +++ b/HttpMessageHandler.cpp @@ -10,8 +10,8 @@ // Constants #define MAP_OBJECT_NAME "$pcars2$" -#define HTTP_RESPONSE_503 "{\r\n \"status\": \"503 Service unavailable, is Project CARS running and is Shared Memory enabled?\"\r\n}" -#define HTTP_RESPONSE_409 "{\r\n \"status\": \"409 Conflict, are CREST and Project CARS both at the latest version?\"\r\n}" +#define HTTP_RESPONSE_503 "{\r\n \"status\": \"503 Service unavailable, is PCARS2 or AMS2 running and is Shared Memory enabled?\"\r\n}" +#define HTTP_RESPONSE_409 "{\r\n \"status\": \"409 Conflict, are CREST and PCARS2 or AMS2 both at the latest version?\"\r\n}" #define GZIP_THRESHOLD 128 static SharedMemoryRenderer sharedMemoryRenderer = SharedMemoryRenderer(); diff --git a/SharedMemoryRenderer.cpp b/SharedMemoryRenderer.cpp index 89aa27d..20ec428 100644 --- a/SharedMemoryRenderer.cpp +++ b/SharedMemoryRenderer.cpp @@ -258,6 +258,14 @@ std::string SharedMemoryRenderer::render(const SharedMemory* sharedData, std::st std::regex target("formatted=true"); std::string replacement = ""; + //Get current system UTC time for adding a timestamp to the data + struct timespec ts; + if (timespec_get(&ts, TIME_UTC) != TIME_UTC) + { + printf("timespec_get failed!\n"); + } + uint64_t cur_time = 1000000000 * ts.tv_sec + ts.tv_nsec; + // if the URL parameter includes "formatted=true" then generate a formatted browser output if (Utils::contains(queryString, "formatted=true")) { tab = TAB; @@ -342,6 +350,7 @@ std::string SharedMemoryRenderer::render(const SharedMemory* sharedData, std::st renderWeather(ss, sharedData); skipSeparator = false; } + ss << "," << nl << tab << "\"timestamp\":" << cur_time / 100000; // curtime in milliseconds + 1 digit - for milliseconds divide it by 10 (division of integers truncates the value instead of rounding, in result 1 digit more for precision) ss << nl << "}"; return ss.str();