-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to configure system time from HTTP API
- Loading branch information
Showing
14 changed files
with
268 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2024, Open Control Systems authors | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
#include "ocs_algo/time_ops.h" | ||
#include "ocs_algo/uri_ops.h" | ||
#include "ocs_pipeline/httpserver/time_handler.h" | ||
|
||
namespace ocs { | ||
namespace pipeline { | ||
namespace httpserver { | ||
|
||
TimeHandler::TimeHandler(http::Server& server, time_t start_point) { | ||
server.add_GET("/api/v1/system/time", [start_point](httpd_req_t* req) { | ||
const auto values = algo::UriOps::parse_query(req->uri); | ||
const auto it = values.find("value"); | ||
|
||
if (it == values.end()) { | ||
const auto timestamp = algo::TimeOps::get_time(); | ||
if (!timestamp) { | ||
return status::StatusCode::Error; | ||
} | ||
|
||
auto err = httpd_resp_set_type(req, HTTPD_TYPE_TEXT); | ||
if (err != ESP_OK) { | ||
return status::StatusCode::Error; | ||
} | ||
|
||
err = httpd_resp_send(req, std::to_string(*timestamp).c_str(), | ||
HTTPD_RESP_USE_STRLEN); | ||
if (err != ESP_OK) { | ||
return status::StatusCode::Error; | ||
} | ||
|
||
return status::StatusCode::OK; | ||
} | ||
|
||
char buf[it->second.size() + 1]; | ||
memset(buf, 0, sizeof(buf)); | ||
memcpy(buf, it->second.data(), it->second.size()); | ||
|
||
const auto code = algo::TimeOps::set_time(buf, start_point); | ||
if (code != status::StatusCode::OK) { | ||
return code; | ||
} | ||
|
||
auto err = httpd_resp_set_type(req, HTTPD_TYPE_TEXT); | ||
if (err != ESP_OK) { | ||
return status::StatusCode::Error; | ||
} | ||
|
||
err = httpd_resp_send(req, "OK", HTTPD_RESP_USE_STRLEN); | ||
if (err != ESP_OK) { | ||
return status::StatusCode::Error; | ||
} | ||
|
||
return status::StatusCode::OK; | ||
}); | ||
} | ||
|
||
} // namespace httpserver | ||
} // namespace pipeline | ||
} // namespace ocs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2024, Open Control Systems authors | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <ctime> | ||
|
||
#include "ocs_core/noncopyable.h" | ||
#include "ocs_http/server.h" | ||
|
||
namespace ocs { | ||
namespace pipeline { | ||
namespace httpserver { | ||
|
||
class TimeHandler : public core::NonCopyable<> { | ||
public: | ||
//! Initialize. | ||
//! | ||
//! @params | ||
//! - @p server to register HTTP endpoint. | ||
//! - @p start_point - time set via the HTTP API should be greater than this value. | ||
TimeHandler(http::Server& server, time_t start_point); | ||
}; | ||
|
||
} // namespace httpserver | ||
} // namespace pipeline | ||
} // namespace ocs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2024, Open Control Systems authors | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
#include "freertos/FreeRTOSConfig.h" | ||
|
||
#include "ocs_fmt/json/time_formatter.h" | ||
#include "ocs_pipeline/httpserver/time_pipeline.h" | ||
|
||
namespace ocs { | ||
namespace pipeline { | ||
namespace httpserver { | ||
|
||
TimePipeline::TimePipeline(http::Server& server, | ||
fmt::json::FanoutFormatter& telemetry_formatter, | ||
fmt::json::FanoutFormatter& registration_formatter, | ||
time_t start_point) { | ||
formatter_.reset(new (std::nothrow) fmt::json::TimeFormatter(start_point)); | ||
configASSERT(formatter_); | ||
|
||
telemetry_formatter.add(*formatter_); | ||
registration_formatter.add(*formatter_); | ||
|
||
handler_.reset(new (std::nothrow) TimeHandler(server, start_point)); | ||
configASSERT(handler_); | ||
} | ||
|
||
} // namespace httpserver | ||
} // namespace pipeline | ||
} // namespace ocs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (c) 2024, Open Control Systems authors | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <ctime> | ||
#include <memory> | ||
|
||
#include "ocs_core/noncopyable.h" | ||
#include "ocs_fmt/json/fanout_formatter.h" | ||
#include "ocs_http/server.h" | ||
#include "ocs_pipeline/httpserver/time_handler.h" | ||
|
||
namespace ocs { | ||
namespace pipeline { | ||
namespace httpserver { | ||
|
||
class TimePipeline : public core::NonCopyable<> { | ||
public: | ||
//! Initialize. | ||
TimePipeline(http::Server& server, | ||
fmt::json::FanoutFormatter& telemetry_formatter, | ||
fmt::json::FanoutFormatter& registration_formatter, | ||
time_t start_point); | ||
|
||
private: | ||
std::unique_ptr<fmt::json::IFormatter> formatter_; | ||
std::unique_ptr<TimeHandler> handler_; | ||
}; | ||
|
||
} // namespace httpserver | ||
} // namespace pipeline | ||
} // namespace ocs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.