Proper display endpoint

This commit is contained in:
2026-06-23 16:14:24 +03:00
parent 2e7899c701
commit 84bc7f5d40
2 changed files with 139 additions and 65 deletions

View File

@@ -20,5 +20,5 @@ Use file named `devices.json` in working directory or supply your own via config
* From: https://github.com/yhirose/cpp-httplib * From: https://github.com/yhirose/cpp-httplib
* Version: 0.47.0 * Version: 0.47.0
# Info on API # Info on TRMNL API
* https://github.com/usetrmnl/trmnl-firmware * https://github.com/usetrmnl/trmnl-firmware

124
main.cpp
View File

@@ -18,6 +18,18 @@ using nlohmann::json;
using namespace std; using namespace std;
void reload_container(TRMNLContainer& container, const string& filename)
{
json j;
bool ok = read_file_json(j, filename, &cout);
if (!ok)
{
return;
}
container.clear();
container = j;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
string config_filename = "config.json"; string config_filename = "config.json";
@@ -89,24 +101,30 @@ int main(int argc, char **argv)
container = devs; container = devs;
auto setup_handler = [&container, &devices_filename](const httplib::Request& req, httplib::Response& res) auto setup_handler = [&container, &devices_filename](const httplib::Request& req, httplib::Response& res)
{
if (req.has_header("ID"))
{ {
json response; json response;
if (!req.has_header("ID"))
{
// Bad Request - No ID header
res.status = 400;
response["status"] = 400;
response["error"] = "No ID header";
res.set_header("Content-Type", "application/json");
res.body = response.dump();
return;
}
// Refresh data from file
// Someone might have put a new device in
reload_container(container, devices_filename);
string id = req.get_header_value("ID"); string id = req.get_header_value("ID");
TRMNL* trmnl = container.get_device_by_id(id); TRMNL* trmnl = container.get_device_by_id(id);
// Refresh date from file
// Someone might have put the new device in
if (nullptr == trmnl)
{
json j;
read_file_json(j, devices_filename, &cout);
container.clear();
container = j;
trmnl = container.get_device_by_id(id);
}
if (nullptr == trmnl) if (nullptr == trmnl)
{ {
res.status = 404; res.status = 404;
@@ -117,10 +135,11 @@ int main(int argc, char **argv)
response["image_url"] = nullptr; response["image_url"] = nullptr;
response["filename"] = nullptr; response["filename"] = nullptr;
res.set_header("Content-Type", "application/json");
res.body = response.dump(); res.body = response.dump();
return;
} }
else
{
bool should_dump = false; bool should_dump = false;
res.status = 200; res.status = 200;
@@ -133,6 +152,7 @@ int main(int argc, char **argv)
if (trmnl->api_key().empty()) if (trmnl->api_key().empty())
{ {
should_dump = true; should_dump = true;
// TODO: Randomly generate key here
trmnl->api_key("nullptr"); trmnl->api_key("nullptr");
} }
@@ -143,29 +163,83 @@ int main(int argc, char **argv)
response["image_url"] = "https://trmnl.com/images/setup/setup-logo.bmp"; response["image_url"] = "https://trmnl.com/images/setup/setup-logo.bmp";
response["filename"] = "welcome"; response["filename"] = "welcome";
res.set_header("Content-Type", "application/json");
res.body = response.dump(); res.body = response.dump();
if (should_dump) if (should_dump)
{ {
write_file_json(container, devices_filename, &cout); write_file_json(container, devices_filename, &cout);
} }
}
}
else
{
// Bad Request - No ID header
res.status = 400;
}
}; };
auto display_handler = [&container, &devices_filename](const httplib::Request& req, httplib::Response& res) auto display_handler = [&container, &devices_filename](const httplib::Request& req, httplib::Response& res)
{ {
res.status = 500; json response;
for (auto header : res.headers) if (!req.has_header("ID") || !req.has_header("Access-Token"))
{ {
cout << header.first << ": " << header.second << endl; // Bad Request
res.status = 400;
response["status"] = 400;
response["error"] = "No ID and Access-Token headers set";
res.body = response.dump();
res.set_header("Content-Type", "application/json");
return;
} }
// Refresh data from file
// Someone might have put a new device in
reload_container(container, devices_filename);
string id = req.get_header_value("ID");
string api_key = req.get_header_value("Access-Token");
TRMNL* trmnl = container.get_device_by_id(id);
if (nullptr == trmnl)
{
// Not Found
res.status = 404;
response["status"] = 404;
response["error"] = "Device not found";
res.set_header("Content-Type", "application/json");
res.body = response.dump();
return;
}
if (trmnl->api_key() != api_key)
{
// Forbidden
res.status = 403;
response["status"] = 403;
response["error"] = "Wrong credentials for this device";
res.set_header("Content-Type", "application/json");
res.body = response.dump();
return;
}
// TODO: Check for image in folder
// string image;
// The ID and api_key match here
res.status = 200;
// From docs: will be 202 if no user_id is attached to device
response["status"] = 0;
response["image_url"] = "https://trmnl.com/images/setup/setup-logo.bmp";
response["filename"] = "2024-09-20T00:00:00";
response["update_firmware"] = false;
response["firmware_url"] = nullptr;
response["refresh_rate"] = to_string(trmnl->refresh_rate());
response["reset_firmware"] = false;
res.set_header("Content-Type", "application/json");
res.body = response.dump();
}; };
auto log_handler = [](const httplib::Request& req, httplib::Response& res) auto log_handler = [](const httplib::Request& req, httplib::Response& res)