From aa1c7942422ce96ce8a47830ec78687932766a73 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Mon, 20 Oct 2025 22:02:00 +0200 Subject: main: Add /api/v1/device/
for getting device data --- src/main.cc | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.cc b/src/main.cc index d19a434..1f4bfd4 100644 --- a/src/main.cc +++ b/src/main.cc @@ -89,7 +89,7 @@ class HttpServerDelegate : public http::Server::Delegate { return http::Response::status(http::StatusCode::kMethodNotAllowed); } - auto* adapter = api_.adapter(); + auto const* adapter = api_.adapter(); json_writer_->clear(); write_adapter(adapter); @@ -109,6 +109,26 @@ class HttpServerDelegate : public http::Server::Delegate { return status_error("Bad state"); } + if (path.starts_with("device/")) { + if (request.method() != "GET") { + return http::Response::status(http::StatusCode::kMethodNotAllowed); + } + + std::string tmp; + auto address = uri::decode(path.substr(7), tmp); + auto const* adapter = api_.adapter(); + if (adapter) { + for (auto* device : adapter->devices()) { + if (device->address() == address) { + json_writer_->clear(); + write_device(*device); + return http::Response::content(json_tmp_, *json_mimetype_); + } + } + } + return http::Response::status(http::StatusCode::kNotFound); + } + if (path == "events") { if (request.method() != "GET") { return http::Response::status(http::StatusCode::kMethodNotAllowed); -- cgit v1.2.3-70-g09d2