diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2025-10-20 22:02:00 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2025-10-21 23:15:29 +0200 |
| commit | aa1c7942422ce96ce8a47830ec78687932766a73 (patch) | |
| tree | 717fa430948042727d6fc83474f55771390bad9e /src/main.cc | |
| parent | d469db4fece0afb62ba58359d265a56712d5db64 (diff) | |
main: Add /api/v1/device/<address> for getting device data
Diffstat (limited to 'src/main.cc')
| -rw-r--r-- | src/main.cc | 22 |
1 files changed, 21 insertions, 1 deletions
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); |
