4#include <pico/cyw43_arch.h>
5#include <hardware/adc.h>
18 printf(
"[DashboardController] Initializing routes...\n");
23 static const char* uploadHtml = R
"rawliteral(
27 <meta charset="UTF-8">
31 <h1>Upload a File</h1>
32 <form method="POST" action="/api/v1/upload" enctype="multipart/form-data">
33 <input type="file" name="file" />
34 <button type="submit">Upload</button>
36 <p>Try opening the file at <code>/uploads/filename.jpg</code> after uploading.</p>
42 res.
send(uploadHtml); });
43 router.
addRoute(
"GET",
"/api/v1/temperature", [
this](
auto &req,
auto &res,
const auto &match)
46 router.
addRoute(
"GET",
"/api/v1/led", [
this](
auto &req,
auto &res,
const auto &match)
49 router.
addRoute(
"POST",
"/api/v1/led/{value}", [
this](
auto &req,
auto &res,
const auto &match)
52 router.
addRoute(
"POST",
"/api/v1/upload", [
this](
auto &req,
auto &res,
const auto &)
55 router.
addRoute(
"DELETE",
"/uploads/{file}", [
this](
auto &req,
auto &res,
const auto &match)
59 { res.
sendFile(
"/uploads/pico_gpios.html"); });
82 res.
json({{
"temperature", tempC}});
88 res.
json({{
"state", isOn ? 1 : 0}});
93 int value = std::stoi(match.
getParam(
"value").value_or(
"0"));
95 res.
json({{
"state", value}});
106 if (!fs->isMounted() && !fs->mount())
108 res.
sendError(500,
"mount_failed",
"Failed to mount filesystem");
112 std::string path =
"/uploads/" + match.
getParam(
"file").value_or(
"");
113 if (fs->exists(path))
Abstract interface for file and directory storage backends.
static constexpr std::uintptr_t getTypeKey()
void getLedState(HttpRequest &req, HttpResponse &res, const RouteMatch &match)
void deleteFile(HttpRequest &req, HttpResponse &res, const RouteMatch &match)
DashboardController(Router &r, PicoModel &pico)
void uploadHandler(HttpRequest &req, HttpResponse &res, const RouteMatch &match)
void initRoutes() override
Initialize routes for this controller.
void setLedState(HttpRequest &req, HttpResponse &res, const RouteMatch &match)
void getTemperature(HttpRequest &req, HttpResponse &res, const RouteMatch &match)
Base class for event-driven control logic in embedded applications.
Router & router
Handles path-to-handler mapping - reference to shared Router instance.
Forward declaration for potential routing needs.
int handle_multipart(HttpResponse &res)
Handle multipart/form-data uploads.
Represents an HTTP response object.
HttpResponse & setContentType(const std::string &content_type)
Set the Content-Type header.
HttpResponse & json(const std::string &jsonString)
Send a JSON string/object with correct content type.
void send(const std::string &body)
Send a full response including headers and body.
bool sendFile(const std::string &path)
Sends the specified file from mounted storage to the client.
HttpResponse & sendSuccess(const nlohmann::json &data={}, const std::string &message="")
HttpResponse & sendError(int statusCode, const std::string &code, const std::string &message)
void setLedState(bool state)
The central router for handling HTTP requests and middleware.
void addCatchAllGetRoute(RouteHandler handler, std::vector< Middleware > middleware={})
Register a catch-all route with optional middleware.
void serveStatic(HttpRequest &req, HttpResponse &res, const RouteMatch &match)
Serve static files from the internal HttpFileserver.
void listDirectory(HttpRequest &req, HttpResponse &res, const RouteMatch &match)
Convenience method to list directory from the internal HttpFileserver.
void addRoute(const std::string &method, const std::string &path, RouteHandler handler, std::vector< Middleware > middleware={})
Register a route with optional middleware.
Represents a match of a route against an incoming HTTP request.
std::optional< std::string > getParam(const std::string &name) const