Pretty display
This commit is contained in:
+42
-10
@@ -4,16 +4,33 @@
|
||||
#include "esphome/components/json/json_util.h"
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// Callback signature: void(device_id, cloud_alert, defrost_mode)
|
||||
using DisplayUpdateCallback = std::function<void(const std::string &device_id, bool cloud_alert, bool defrost_mode)>;
|
||||
struct MicrowaveData {
|
||||
std::string id;
|
||||
std::string name;
|
||||
int state;
|
||||
bool paused;
|
||||
int remaining_time;
|
||||
int progress;
|
||||
};
|
||||
|
||||
// Global states parsed from the API
|
||||
static std::vector<MicrowaveData> g_microwaves;
|
||||
static bool g_cloud_alert = false;
|
||||
static std::string g_update_time = "--:--";
|
||||
static bool g_received_update = false;
|
||||
|
||||
// Callback signature for refreshing UI
|
||||
using DisplayUpdateCallback = std::function<void()>;
|
||||
static DisplayUpdateCallback g_display_callback = nullptr;
|
||||
|
||||
class DisplayApiHandler : public esphome::web_server_idf::AsyncWebHandler {
|
||||
public:
|
||||
bool canHandle(esphome::web_server_idf::AsyncWebServerRequest *request) const override {
|
||||
return request->url_to() == "/api/display";
|
||||
// Provide the 513-byte buffer expected by ESPHome's new memory-safe API
|
||||
char url_buf[513];
|
||||
return request->url_to(url_buf) == "/api/display";
|
||||
}
|
||||
|
||||
void handleRequest(esphome::web_server_idf::AsyncWebServerRequest *request) override {
|
||||
@@ -24,16 +41,31 @@ class DisplayApiHandler : public esphome::web_server_idf::AsyncWebHandler {
|
||||
std::string body((char *)data, len);
|
||||
|
||||
esphome::json::parse_json(body, [](JsonObject root) -> bool {
|
||||
std::string device_id = root["device_id"] | "unknown";
|
||||
bool cloud_alert = root["cloud_alert"] | false;
|
||||
bool defrost_mode = root["defrost_mode"] | false;
|
||||
g_cloud_alert = root["cloud_alert"] | false;
|
||||
g_update_time = root["update_time"] | "--:--";
|
||||
|
||||
g_microwaves.clear();
|
||||
JsonArray mw_array = root["microwaves"];
|
||||
|
||||
for (JsonObject mw : mw_array) {
|
||||
MicrowaveData md;
|
||||
md.id = mw["id"] | "unknown";
|
||||
md.name = mw["name"] | "Microwave";
|
||||
md.state = mw["state"] | 4;
|
||||
md.paused = mw["paused"] | false;
|
||||
md.remaining_time = mw["remaining_time"] | 0;
|
||||
md.progress = mw["progress"] | 0;
|
||||
|
||||
g_microwaves.push_back(md);
|
||||
}
|
||||
|
||||
g_received_update = true;
|
||||
|
||||
ESP_LOGI("custom_api", "Received update for %s (Alert: %d, Defrost: %d)",
|
||||
device_id.c_str(), cloud_alert, defrost_mode);
|
||||
ESP_LOGI("custom_api", "Received update. Cloud Alert: %d, Microwaves: %d",
|
||||
g_cloud_alert, g_microwaves.size());
|
||||
|
||||
// Execute the callback passed from YAML
|
||||
if (g_display_callback != nullptr) {
|
||||
g_display_callback(device_id, cloud_alert, defrost_mode);
|
||||
g_display_callback();
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user