Some restructuring. Added structure for image finding
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
// Required
|
// Required
|
||||||
"host": "localhost",
|
"host": "localhost",
|
||||||
"port": 0,
|
"port": 0,
|
||||||
|
"base_url": "https://trmnl.com/",
|
||||||
|
|
||||||
// Optional
|
// Optional
|
||||||
"devices_filename": "devices.json",
|
"devices_filename": "devices.json",
|
||||||
|
|||||||
81
main.cpp
81
main.cpp
@@ -3,7 +3,9 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
#include <set>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@@ -19,6 +21,8 @@ using nlohmann::json;
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
constexpr int API_KEY_LENGHT = 16;
|
constexpr int API_KEY_LENGHT = 16;
|
||||||
|
constexpr char DEFAULT_IMAGE_URL[] = "https://trmnl.com/images/setup/setup-logo.bmp";
|
||||||
|
constexpr char DEFAULT_IMAGE_FNAME[] = "2024-09-20T00:00:00";
|
||||||
|
|
||||||
string generate_api_key(int len = API_KEY_LENGHT)
|
string generate_api_key(int len = API_KEY_LENGHT)
|
||||||
{
|
{
|
||||||
@@ -68,13 +72,13 @@ string generate_api_key(int len = API_KEY_LENGHT)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void reload_container(TRMNLContainer& container, const string& filename)
|
bool reload_container(TRMNLContainer& container, const string& filename)
|
||||||
{
|
{
|
||||||
json j;
|
json j;
|
||||||
bool ok = read_file_json(j, filename, &cout);
|
bool ok = read_file_json(j, filename, &cout);
|
||||||
if (!ok)
|
if (!ok)
|
||||||
{
|
{
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
container.clear();
|
container.clear();
|
||||||
if (j.is_array())
|
if (j.is_array())
|
||||||
@@ -84,17 +88,35 @@ void reload_container(TRMNLContainer& container, const string& filename)
|
|||||||
container.add_device(j[i]);
|
container.add_device(j[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// First string is image filename for URL
|
||||||
|
// Second string is timestamp for TRMNL filename
|
||||||
|
pair<string, string> find_image_for_friendly(const string& folder_images, const string& friendly_id)
|
||||||
|
{
|
||||||
|
pair<string, string> result = {"", ""};
|
||||||
|
set<string> permitted_extensions = { ".bmp", ".png" };
|
||||||
|
|
||||||
|
// TODO: Implement
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
string config_filename = "config.json";
|
string config_filename = "config.json";
|
||||||
string devices_filename = "devices.json";
|
string devices_filename = "devices.json";
|
||||||
string host = "";
|
|
||||||
string folder_images = "images";
|
string folder_images = "images";
|
||||||
|
|
||||||
|
string host = "";
|
||||||
|
uint16_t port = 0;
|
||||||
|
string base_url = "";
|
||||||
|
|
||||||
string cert_file = "";
|
string cert_file = "";
|
||||||
string key_file = "";
|
string key_file = "";
|
||||||
uint16_t port = 0;
|
|
||||||
shared_ptr<httplib::Server> server = nullptr;
|
shared_ptr<httplib::Server> server = nullptr;
|
||||||
|
|
||||||
bool ok;
|
bool ok;
|
||||||
@@ -113,10 +135,14 @@ int main(int argc, char **argv)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
json_extract(cfg, "devices_filename", devices_filename);
|
|
||||||
json_extract(cfg, "host", host);
|
json_extract(cfg, "host", host);
|
||||||
json_extract(cfg, "port", port);
|
json_extract(cfg, "port", port);
|
||||||
|
json_extract(cfg, "base_url", base_url);
|
||||||
|
|
||||||
|
json_extract(cfg, "devices_filename", devices_filename);
|
||||||
json_extract(cfg, "folder_images", folder_images);
|
json_extract(cfg, "folder_images", folder_images);
|
||||||
|
|
||||||
|
// TODO: Extract when SSL is ready
|
||||||
// json_extract(cfg, "cert_file", cert_file);
|
// json_extract(cfg, "cert_file", cert_file);
|
||||||
// json_extract(cfg, "key_file", cert_file);
|
// json_extract(cfg, "key_file", cert_file);
|
||||||
|
|
||||||
@@ -132,6 +158,12 @@ int main(int argc, char **argv)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (base_url.empty())
|
||||||
|
{
|
||||||
|
cout << "base url not provided" << endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (folder_images.empty())
|
if (folder_images.empty())
|
||||||
{
|
{
|
||||||
cout << "folder for images is empty" << endl;
|
cout << "folder for images is empty" << endl;
|
||||||
@@ -147,14 +179,13 @@ int main(int argc, char **argv)
|
|||||||
server = make_shared<httplib::Server>();
|
server = make_shared<httplib::Server>();
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = read_file_json(devs, devices_filename, &cout);
|
ok = reload_container(container, devices_filename);
|
||||||
if (!ok)
|
if (!ok)
|
||||||
{
|
{
|
||||||
|
cout << "Could not read devices file" << endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
reload_container(container, devices_filename);
|
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
json response;
|
json response;
|
||||||
@@ -213,9 +244,8 @@ int main(int argc, char **argv)
|
|||||||
response["status"] = 200;
|
response["status"] = 200;
|
||||||
response["api_key"] = trmnl->api_key();
|
response["api_key"] = trmnl->api_key();
|
||||||
response["friendly_id"] = trmnl->friendly_id();
|
response["friendly_id"] = trmnl->friendly_id();
|
||||||
// TODO: Check for image in folder
|
response["image_url"] = DEFAULT_IMAGE_URL;
|
||||||
response["image_url"] = "https://trmnl.com/images/setup/setup-logo.bmp";
|
response["filename"] = DEFAULT_IMAGE_FNAME;
|
||||||
response["filename"] = "welcome";
|
|
||||||
|
|
||||||
res.set_header("Content-Type", "application/json");
|
res.set_header("Content-Type", "application/json");
|
||||||
res.body = response.dump();
|
res.body = response.dump();
|
||||||
@@ -226,7 +256,8 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
auto display_handler = [&container, &devices_filename](const httplib::Request& req, httplib::Response& res)
|
auto display_handler = [&container, &devices_filename, &folder_images, &base_url]
|
||||||
|
(const httplib::Request& req, httplib::Response& res)
|
||||||
{
|
{
|
||||||
json response;
|
json response;
|
||||||
|
|
||||||
@@ -277,19 +308,35 @@ int main(int argc, char **argv)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Check for image in folder
|
string image_fname;
|
||||||
// string image;
|
string image_timestamp;
|
||||||
|
tie(image_fname, image_timestamp) = find_image_for_friendly(folder_images, trmnl->friendly_id());
|
||||||
|
|
||||||
// The ID and api_key match here
|
// The ID and api_key match here
|
||||||
res.status = 200;
|
res.status = 200;
|
||||||
|
|
||||||
// From docs: will be 202 if no user_id is attached to device
|
// From docs: will be 202 if no user_id is attached to device
|
||||||
response["status"] = 0;
|
response["status"] = 0;
|
||||||
response["image_url"] = "https://trmnl.com/images/setup/setup-logo.bmp";
|
|
||||||
response["filename"] = "2024-09-20T00:00:00";
|
if (image_fname.empty())
|
||||||
|
{
|
||||||
|
response["image_url"] = DEFAULT_IMAGE_URL;
|
||||||
|
response["filename"] = DEFAULT_IMAGE_FNAME;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string full_url = base_url;
|
||||||
|
full_url += "/images/";
|
||||||
|
full_url += image_fname;
|
||||||
|
response["image_url"] = full_url;
|
||||||
|
response["filename"] = image_timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
response["refresh_rate"] = to_string(trmnl->refresh_rate());
|
||||||
|
|
||||||
|
// TODO: Handle firmware updating
|
||||||
response["update_firmware"] = false;
|
response["update_firmware"] = false;
|
||||||
response["firmware_url"] = nullptr;
|
response["firmware_url"] = nullptr;
|
||||||
response["refresh_rate"] = to_string(trmnl->refresh_rate());
|
|
||||||
response["reset_firmware"] = false;
|
response["reset_firmware"] = false;
|
||||||
|
|
||||||
res.set_header("Content-Type", "application/json");
|
res.set_header("Content-Type", "application/json");
|
||||||
|
|||||||
Reference in New Issue
Block a user