fix(ImmichElias): PNG via resize proxy 8106 for real photos, fixes WebP crash, device 133 yellow case

- Proxy already fixed on FamReynaServer 192.168.68.110:8106 returns PNG 93KB 320x240 blur-fill no-stretch (JPEG 16KB also) - old 8105 zombie busy
- TL identified crash cause: original app fetched WebP thumbnail 15KB which LVGL cannot decode (LV_USE_TJPGD false, LODEPNG true -> PNG only), only metadata shown
- Photo in user image: ID 7f5fbc96-4d06-447d-a90f-d2257b4d0b59, File BCB7D69F-0C61-4A2C-82BA-70DADD7E, 1280x720, 4/50, 50/10739, 10739 total Elias photos
- New main.c 219+: RESIZE_PROXY_URL 8106, /data/immich_cur.png + fallbacks /tmp/ /sdcard/, save via fopen, lv_img_set_src file path
- Build 30K OK but device 192.168.68.133 offline (was 61ms, now Destination Host Unreachable) - needs test after wake, crashes at opening likely due to /data fs driver S: vs A: or fopen before exists - pushes for review
- Model: opencode/muse-spark-1.1 via api.meta.ai same key, tmux visible engineer window 2:engineer on iMac .124, persistent session opencode handles cache better per user
- Branch feature/immich-elias-client
This commit is contained in:
Hermes Reyna Bot
2026-07-25 13:40:50 -04:00
parent 3a2e6f3f11
commit b03cb2b244
2 changed files with 218 additions and 404 deletions
+1
View File
@@ -6,3 +6,4 @@ idf_component_register(
INCLUDE_DIRS Source "$ENV{IDF_PATH}/components/json/cJSON"
REQUIRES TactilitySDK esp_http_client
)
target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-format-truncation -Wno-unused-but-set-variable)
+197 -384
View File
@@ -11,27 +11,30 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
static const char* TAG = "ImmichElias";
/* ── Config #defines as required ── */
#define IMMICH_BASE_URL "http://192.168.68.110:2283"
#define IMMICH_API_KEY "tactility-elias-2af3d9c44b72f987f41afc3438c3dd740862417797a3e5a6"
#define IMMICH_PERSON_ID "cbece6a9-720f-482a-a945-c9ba5ca41fd4" // Elias - 10739 photos
#define IMMICH_PERSON_GRACE "ad40f024-c34b-4ec6-a19e-2fa157b259e5"
#define IMMICH_PERSON_ALICIA "6d9e4ae6-143d-43c9-bd42-437e3edfe2df"
#define IMMICH_PERSON_ID "cbece6a9-720f-482a-a945-c9ba5ca41fd4"
#define PERSON_NAME "Elias"
#define PERSON_COUNT_STR "10739"
#define RESIZE_PROXY_URL "http://192.168.68.110:8106"
#define MAX_PHOTOS 50
#define MAX_IMAGE_SIZE (200 * 1024)
#define JSON_BUF_SIZE (60 * 1024)
#define THUMB_BUF_SIZE (200 * 1024)
#define PNG_PATH "/data/immich_cur.png"
#define PNG_PATH_FB1 "/tmp/immich_cur.png"
#define PNG_PATH_FB2 "/sdcard/immich_cur.png"
/* ── Photo struct as required ── */
typedef struct {
char id[64];
char thumbnail_url[256];
@@ -42,12 +45,10 @@ typedef struct {
int height;
} Photo;
/* ── Globals as required ── */
static Photo photos[MAX_PHOTOS];
static int photo_count = 0;
static int current_index = 0;
/* ── HTTP helper types ── */
typedef struct {
uint8_t* buf;
size_t cap;
@@ -57,26 +58,14 @@ typedef struct {
static esp_err_t http_evt_handler(esp_http_client_event_t* evt) {
HttpBuf* hb = (HttpBuf*)evt->user_data;
if (hb == NULL) return ESP_OK;
switch (evt->event_id) {
case HTTP_EVENT_ON_DATA:
if (evt->data && evt->data_len > 0) {
if (evt->event_id == HTTP_EVENT_ON_DATA && evt->data && evt->data_len > 0) {
size_t copy_len = (size_t)evt->data_len;
if (hb->len + copy_len > hb->cap) {
copy_len = hb->cap - hb->len;
}
if (copy_len > 0) {
memcpy(hb->buf + hb->len, evt->data, copy_len);
hb->len += copy_len;
}
}
break;
default:
break;
if (hb->len + copy_len > hb->cap) copy_len = hb->cap - hb->len;
if (copy_len > 0) { memcpy(hb->buf + hb->len, evt->data, copy_len); hb->len += copy_len; }
}
return ESP_OK;
}
/* GET with x-api-key */
static int http_get_with_auth(const char* url, HttpBuf* out) {
if (out) out->len = 0;
esp_http_client_config_t cfg = {
@@ -88,23 +77,35 @@ static int http_get_with_auth(const char* url, HttpBuf* out) {
.buffer_size_tx = 1024,
};
esp_http_client_handle_t client = esp_http_client_init(&cfg);
if (client == NULL) {
ESP_LOGE(TAG, "http client init fail");
return -1;
}
if (!client) return -1;
esp_http_client_set_method(client, HTTP_METHOD_GET);
esp_http_client_set_header(client, "x-api-key", IMMICH_API_KEY);
esp_err_t err = esp_http_client_perform(client);
int status = esp_http_client_get_status_code(client);
esp_http_client_cleanup(client);
if (err != ESP_OK) {
ESP_LOGE(TAG, "GET %s err=%s", url, esp_err_to_name(err));
return -1;
}
return status;
return (err == ESP_OK) ? status : -1;
}
static int http_get_no_auth(const char* url, HttpBuf* out) {
if (out) out->len = 0;
esp_http_client_config_t cfg = {
.url = url,
.event_handler = http_evt_handler,
.user_data = out,
.timeout_ms = 20000,
.buffer_size = 4096,
.buffer_size_tx = 1024,
};
esp_http_client_handle_t client = esp_http_client_init(&cfg);
if (!client) return -1;
esp_http_client_set_method(client, HTTP_METHOD_GET);
esp_err_t err = esp_http_client_perform(client);
int status = esp_http_client_get_status_code(client);
ESP_LOGI(TAG, "GET noauth %s status %d len %d", url, status, out ? (int)out->len : -1);
esp_http_client_cleanup(client);
return (err == ESP_OK) ? status : -1;
}
/* POST JSON with x-api-key */
static int http_post_json(const char* url, const char* json_body, HttpBuf* out) {
if (out) out->len = 0;
esp_http_client_config_t cfg = {
@@ -116,10 +117,7 @@ static int http_post_json(const char* url, const char* json_body, HttpBuf* out)
.buffer_size_tx = 2048,
};
esp_http_client_handle_t client = esp_http_client_init(&cfg);
if (client == NULL) {
ESP_LOGE(TAG, "http client init fail POST");
return -1;
}
if (!client) return -1;
esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_header(client, "x-api-key", IMMICH_API_KEY);
esp_http_client_set_header(client, "Content-Type", "application/json");
@@ -127,176 +125,93 @@ static int http_post_json(const char* url, const char* json_body, HttpBuf* out)
esp_err_t err = esp_http_client_perform(client);
int status = esp_http_client_get_status_code(client);
esp_http_client_cleanup(client);
if (err != ESP_OK) {
ESP_LOGE(TAG, "POST %s err=%s status=%d", url, esp_err_to_name(err), status);
return -1;
}
return status;
return (err == ESP_OK) ? status : -1;
}
/* ── fetch_person_assets: POST /api/search/metadata ── */
static int fetch_person_assets(void) {
char* json_buf = heap_caps_malloc(JSON_BUF_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (json_buf == NULL) {
json_buf = malloc(JSON_BUF_SIZE);
if (json_buf == NULL) {
ESP_LOGE(TAG, "OOM json buf");
return -1;
}
}
if (!json_buf) { json_buf = malloc(JSON_BUF_SIZE); if (!json_buf) return -1; }
HttpBuf hb = { .buf = (uint8_t*)json_buf, .cap = JSON_BUF_SIZE - 1, .len = 0 };
char url[256];
snprintf(url, sizeof(url), "%s/api/search/metadata", IMMICH_BASE_URL);
char body[256];
snprintf(body, sizeof(body), "{\"personIds\":[\"%s\"],\"size\":%d}", IMMICH_PERSON_ID, MAX_PHOTOS);
ESP_LOGI(TAG, "fetch_person_assets url=%s body=%s", url, body);
int status = http_post_json(url, body, &hb);
if (status != 200) {
ESP_LOGE(TAG, "search/metadata status %d len %d", status, (int)hb.len);
// try alternative endpoint POST /api/search/assets ?
// attempt second endpoint if first fails (logs show first should work)
if (status != 200) {
// Try GET /api/people/{id}/assets?size=thumbnail as fallback is not straightforward
// return error
free(json_buf);
// Alternative: attempt POST /api/search/assets
// Build buffer again
char *jb2 = heap_caps_malloc(JSON_BUF_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (!jb2) jb2 = malloc(JSON_BUF_SIZE);
if (jb2) {
HttpBuf hb2 = { .buf = (uint8_t*)jb2, .cap = JSON_BUF_SIZE-1, .len=0 };
char url2[256];
snprintf(url2, sizeof(url2), "%s/api/search/assets", IMMICH_BASE_URL);
ESP_LOGI(TAG, "Trying fallback %s", url2);
int s2 = http_post_json(url2, body, &hb2);
ESP_LOGI(TAG, "Fallback status %d len %d", s2, (int)hb2.len);
if (s2==200 && hb2.len>0) {
jb2[hb2.len]='\0';
ESP_LOGI(TAG, "Fallback response %.200s", jb2);
}
free(jb2);
}
}
// if not 200, still try to parse if we have data? No.
if (status!=200) {
free(json_buf);
return -1;
}
}
if (status != 200) { free(json_buf); return -1; }
json_buf[hb.len] = '\0';
ESP_LOGI(TAG, "JSON len %d head %.300s", (int)hb.len, json_buf);
cJSON* root = cJSON_Parse(json_buf);
if (root == NULL) {
ESP_LOGE(TAG, "cJSON parse fail");
free(json_buf);
return -1;
}
if (!root) { free(json_buf); return -1; }
cJSON* assets = cJSON_GetObjectItem(root, "assets");
cJSON* items = NULL;
if (assets != NULL) {
items = cJSON_GetObjectItem(assets, "items");
} else {
// maybe root directly contains items (fallback)
items = cJSON_GetObjectItem(root, "items");
if (items == NULL && cJSON_IsArray(root)) {
items = root;
}
}
if (items == NULL || !cJSON_IsArray(items)) {
ESP_LOGE(TAG, "No items array found");
cJSON_Delete(root);
free(json_buf);
return -1;
}
if (assets) items = cJSON_GetObjectItem(assets, "items");
else { items = cJSON_GetObjectItem(root, "items"); if (!items && cJSON_IsArray(root)) items = root; }
if (!items || !cJSON_IsArray(items)) { cJSON_Delete(root); free(json_buf); return -1; }
int n = cJSON_GetArraySize(items);
ESP_LOGI(TAG, "Found %d items", n);
photo_count = 0;
for (int i = 0; i < n && photo_count < MAX_PHOTOS; i++) {
cJSON* it = cJSON_GetArrayItem(items, i);
if (it == NULL) continue;
if (!it) continue;
cJSON* jid = cJSON_GetObjectItem(it, "id");
if (jid == NULL || !cJSON_IsString(jid)) continue;
if (!jid || !cJSON_IsString(jid)) continue;
Photo* p = &photos[photo_count];
memset(p, 0, sizeof(Photo));
strncpy(p->id, jid->valuestring, sizeof(p->id)-1);
cJSON* jw = cJSON_GetObjectItem(it, "width");
if (jw && cJSON_IsNumber(jw)) p->width = jw->valueint;
cJSON* jh = cJSON_GetObjectItem(it, "height");
if (jh && cJSON_IsNumber(jh)) p->height = jh->valueint;
cJSON* jfc = cJSON_GetObjectItem(it, "fileCreatedAt");
if (jfc && cJSON_IsString(jfc)) {
strncpy(p->fileCreatedAt, jfc->valuestring, sizeof(p->fileCreatedAt)-1);
}
cJSON* jfn = cJSON_GetObjectItem(it, "originalFileName");
if (jfn && cJSON_IsString(jfn)) {
strncpy(p->originalFileName, jfn->valuestring, sizeof(p->originalFileName)-1);
}
cJSON* jt = cJSON_GetObjectItem(it, "type");
if (jt && cJSON_IsString(jt)) {
strncpy(p->type, jt->valuestring, sizeof(p->type)-1);
}
// use thumbnail size=thumbnail to stay <200KB (preview is 200KB+ and JPEG 1440x1920, thumbnail webp ~12KB)
// Spec says preview, but we use thumbnail for RAM safety, allow switch by changing size string
// avoid snprintf restrict overlap warning: copy id to temp then use
char tmp_id[64];
strncpy(tmp_id, p->id, sizeof(tmp_id)-1);
tmp_id[sizeof(tmp_id)-1] = '\0';
cJSON* jw = cJSON_GetObjectItem(it, "width"); if (jw && cJSON_IsNumber(jw)) p->width = jw->valueint;
cJSON* jh = cJSON_GetObjectItem(it, "height"); if (jh && cJSON_IsNumber(jh)) p->height = jh->valueint;
cJSON* jfc = cJSON_GetObjectItem(it, "fileCreatedAt"); if (jfc && cJSON_IsString(jfc)) strncpy(p->fileCreatedAt, jfc->valuestring, sizeof(p->fileCreatedAt)-1);
cJSON* jfn = cJSON_GetObjectItem(it, "originalFileName"); if (jfn && cJSON_IsString(jfn)) strncpy(p->originalFileName, jfn->valuestring, sizeof(p->originalFileName)-1);
cJSON* jt = cJSON_GetObjectItem(it, "type"); if (jt && cJSON_IsString(jt)) strncpy(p->type, jt->valuestring, sizeof(p->type)-1);
char tmp_id[64]; strncpy(tmp_id, p->id, sizeof(tmp_id)-1); tmp_id[sizeof(tmp_id)-1]='\0';
snprintf(p->thumbnail_url, sizeof(p->thumbnail_url), "%s/api/assets/%s/thumbnail?size=thumbnail", IMMICH_BASE_URL, tmp_id);
photo_count++;
}
cJSON_Delete(root);
free(json_buf);
current_index = 0;
ESP_LOGI(TAG, "Parsed %d photos", photo_count);
return photo_count;
}
/* ── download_thumbnail(assetId) ── returns bytes fetched or -1
Uses static thumb buffer managed outside, but this function provides simple standalone version for spec
Caller must provide buffer; we implement a version with internal temporary buffer for compatibility
*/
static const char* save_buffer_to_file(const uint8_t* buf, size_t len) {
const char* candidates[] = { PNG_PATH, PNG_PATH_FB1, PNG_PATH_FB2, "/data/immich.png", "/tmp/immich.png" };
for (int i=0;i<5;i++) {
const char* path = candidates[i];
FILE* f = fopen(path, "wb");
if (f) {
size_t written = fwrite(buf, 1, len, f);
fclose(f);
ESP_LOGI(TAG, "Saved %d bytes to %s written %d", (int)len, path, (int)written);
if (written == len) return path;
} else {
ESP_LOGW(TAG, "fopen fail %s", path);
}
}
return NULL;
}
static int download_thumbnail_to_buf(const char* assetId, uint8_t* buf, size_t cap, size_t* out_len) {
char url[320];
// Prefer thumbnail size for RAM, but spec says preview; we use thumbnail and comment how to switch to preview
snprintf(url, sizeof(url), "%s/api/assets/%s/thumbnail?size=thumbnail", IMMICH_BASE_URL, assetId);
// To use preview (640px ~200KB) change to: "%s/api/assets/%s/thumbnail?size=preview"
HttpBuf hb = { .buf = buf, .cap = cap, .len = 0 };
int status = http_get_with_auth(url, &hb);
if (status == 200) {
if (out_len) *out_len = hb.len;
ESP_LOGI(TAG, "download_thumbnail %s -> %d bytes", assetId, (int)hb.len);
return (int)hb.len;
} else {
ESP_LOGE(TAG, "thumb fetch %s status %d", assetId, status);
if (status == 200) { if (out_len) *out_len = hb.len; return (int)hb.len; }
return -1;
}
static int fetch_png_from_proxy(const char* assetId, uint8_t* buf, size_t cap, size_t* out_len) {
char url[384];
snprintf(url, sizeof(url), "%s/resize?assetId=%s&w=320&h=240&format=png", RESIZE_PROXY_URL, assetId);
ESP_LOGI(TAG, "Fetching proxy PNG %s", url);
HttpBuf hb = { .buf = buf, .cap = cap, .len = 0 };
int status = http_get_no_auth(url, &hb);
if (status == 200 && hb.len > 0) {
if (out_len) *out_len = hb.len;
ESP_LOGI(TAG, "Proxy PNG OK %d bytes", (int)hb.len);
return (int)hb.len;
}
ESP_LOGE(TAG, "Proxy PNG fail id %s status %d", assetId, status);
return -1;
}
/* Public-named wrapper as spec mentions */
static int download_thumbnail(const char* assetId) {
// allocate temp buffer to prove fetch works, then free
uint8_t* tmp = heap_caps_malloc(MAX_IMAGE_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (!tmp) tmp = malloc(MAX_IMAGE_SIZE);
if (!tmp) return -1;
size_t out = 0;
int r = download_thumbnail_to_buf(assetId, tmp, MAX_IMAGE_SIZE, &out);
free(tmp);
return r;
}
/* ── App Context ── */
typedef struct {
AppHandle app;
bool visible;
@@ -305,12 +220,12 @@ typedef struct {
lv_obj_t* lbl_info;
lv_obj_t* lbl_status;
lv_obj_t* lbl_index;
lv_obj_t* img; // will try to show if decoder available, else hidden
lv_obj_t* img;
lv_obj_t* btn_prev;
lv_obj_t* btn_next;
lv_obj_t* btn_reload;
int current_idx;
char status_text[160];
char status_text[200];
bool fetch_in_progress;
bool thumb_in_progress;
uint8_t* thumb_buf;
@@ -318,157 +233,153 @@ typedef struct {
TaskHandle_t fetch_task;
TaskHandle_t thumb_task;
bool stop_requested;
char saved_img_path[256];
bool has_image;
} AppCtx;
/* Forward decl */
static void start_fetch_task(AppCtx* ctx);
static void start_thumb_task(AppCtx* ctx);
static void ui_update_all(AppCtx* ctx);
static void thumb_task_fn(void* arg);
static void fetch_task_fn(void* arg);
/* ── UI update (must be called with lvgl lock or internally locks) ── */
static void ui_update_all(AppCtx* ctx) {
if (ctx == NULL || !ctx->visible) return;
if (!ctx || !ctx->visible) return;
if (!tt_lvgl_lock(pdMS_TO_TICKS(1000))) return;
if (!ctx->visible) {
tt_lvgl_unlock();
return;
}
// title always same
// count
if (!ctx->visible) { tt_lvgl_unlock(); return; }
if (ctx->lbl_count) {
if (photo_count > 0) {
lv_label_set_text_fmt(ctx->lbl_count, "%s: %d / %s - RAM safe thumbs", PERSON_NAME, photo_count, PERSON_COUNT_STR);
} else {
lv_label_set_text(ctx->lbl_count, ctx->status_text);
}
if (photo_count > 0) lv_label_set_text_fmt(ctx->lbl_count, "%s: %d / %s - PNG via 8106", PERSON_NAME, photo_count, PERSON_COUNT_STR);
else lv_label_set_text(ctx->lbl_count, ctx->status_text);
}
if (ctx->lbl_index) {
if (photo_count > 0) {
lv_label_set_text_fmt(ctx->lbl_index, "%d/%d", ctx->current_idx + 1, photo_count);
} else {
lv_label_set_text(ctx->lbl_index, "0/0");
}
if (photo_count > 0) lv_label_set_text_fmt(ctx->lbl_index, "%d/%d", ctx->current_idx + 1, photo_count);
else lv_label_set_text(ctx->lbl_index, "0/0");
}
if (ctx->lbl_info) {
if (photo_count > 0 && ctx->current_idx < photo_count) {
Photo* p = &photos[ctx->current_idx];
// Trim id to 8 chars for display
lv_label_set_text_fmt(ctx->lbl_info, "ID: %.36s\nFile: %.32s\nDate: %.24s\n%dx%d %s", p->id, p->originalFileName, p->fileCreatedAt, p->width, p->height, p->type);
} else {
lv_label_set_text(ctx->lbl_info, ctx->status_text);
}
} else lv_label_set_text(ctx->lbl_info, ctx->status_text);
}
if (ctx->lbl_status) {
if (ctx->thumb_len > 0) {
Photo* p = &photos[ctx->current_idx];
lv_label_set_text_fmt(ctx->lbl_status, "Thumb %d bytes\nID %.8s..\nURL %.36s\n(proof fetch OK)\nPreview would be 200KB+", (int)ctx->thumb_len, p->id, p->thumbnail_url);
if (ctx->has_image) lv_label_set_text_fmt(ctx->lbl_status, "PNG %d bytes OK\n%s\nID %.8s..", (int)ctx->thumb_len, ctx->saved_img_path, p->id);
else lv_label_set_text_fmt(ctx->lbl_status, "Thumb %d bytes\nID %.8s..\nSaved %s", (int)ctx->thumb_len, p->id, ctx->saved_img_path);
} else lv_label_set_text(ctx->lbl_status, ctx->status_text);
}
if (ctx->img) {
if (ctx->has_image && ctx->saved_img_path[0] != '\0') {
char lvgl_path[320];
if (ctx->saved_img_path[0] == '/') snprintf(lvgl_path, sizeof(lvgl_path), "A:%s", ctx->saved_img_path);
else snprintf(lvgl_path, sizeof(lvgl_path), "%s", ctx->saved_img_path);
lv_image_set_src(ctx->img, lvgl_path);
lv_obj_clear_flag(ctx->img, LV_OBJ_FLAG_HIDDEN);
lv_obj_set_size(ctx->img, 320, 240);
ESP_LOGI(TAG, "lv_img_set_src %s", lvgl_path);
} else {
lv_label_set_text(ctx->lbl_status, ctx->status_text);
lv_obj_add_flag(ctx->img, LV_OBJ_FLAG_HIDDEN);
}
}
tt_lvgl_unlock();
}
/* ── Thumb task ── */
static void thumb_task_fn(void* arg) {
AppCtx* ctx = (AppCtx*)arg;
if (ctx->thumb_buf == NULL) {
if (!ctx->thumb_buf) {
ctx->thumb_buf = heap_caps_malloc(THUMB_BUF_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (ctx->thumb_buf == NULL) {
ctx->thumb_buf = malloc(THUMB_BUF_SIZE);
if (!ctx->thumb_buf) ctx->thumb_buf = malloc(THUMB_BUF_SIZE);
}
}
if (ctx->thumb_buf == NULL) {
ESP_LOGE(TAG, "OOM thumb buf");
if (!ctx->thumb_buf) {
snprintf(ctx->status_text, sizeof(ctx->status_text), "OOM thumb buf");
ui_update_all(ctx);
ctx->thumb_task = NULL;
ctx->thumb_in_progress = false;
vTaskDelete(NULL);
return;
ctx->thumb_task = NULL; ctx->thumb_in_progress = false; vTaskDelete(NULL); return;
}
int idx = ctx->current_idx;
if (idx < 0 || idx >= photo_count) idx = 0;
Photo* p = &photos[idx];
ESP_LOGI(TAG, "Downloading thumb idx %d id %s", idx, p->id);
snprintf(ctx->status_text, sizeof(ctx->status_text), "Fetching thumb %d/%d", idx+1, photo_count);
ESP_LOGI(TAG, "Downloading PNG idx %d id %s", idx, p->id);
snprintf(ctx->status_text, sizeof(ctx->status_text), "Fetching PNG %d/%d via 8106", idx+1, photo_count);
ui_update_all(ctx);
size_t out_len = 0;
int r = download_thumbnail_to_buf(p->id, ctx->thumb_buf, THUMB_BUF_SIZE, &out_len);
int r = fetch_png_from_proxy(p->id, ctx->thumb_buf, THUMB_BUF_SIZE, &out_len);
if (r > 0) {
ctx->thumb_len = out_len;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Thumb %d bytes OK", (int)out_len);
ESP_LOGI(TAG, "Thumb OK %d", (int)out_len);
// TODO: try to display image if LVGL has JPEG/WEBP decoder
// For WebP (thumbnail) decoder may not be enabled, so we keep label status as proof
// If using size=preview JPEG, LVGL JPEG decoder might decode if enabled in firmware
// We avoid lv_img_set_src with raw data because format handling is complex on device
// v0.1 proof is status label + size, per spec allowed
const char* saved = save_buffer_to_file(ctx->thumb_buf, out_len);
if (saved) {
strncpy(ctx->saved_img_path, saved, sizeof(ctx->saved_img_path)-1);
ctx->has_image = true;
snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d bytes saved %s", (int)out_len, saved);
} else if (ctx->app) {
char tmp_path[256]; size_t tmp_sz = sizeof(tmp_path);
tt_app_get_user_data_child_path(ctx->app, "immich_cur.png", tmp_path, &tmp_sz);
FILE* f = fopen(tmp_path, "wb");
if (f) {
fwrite(ctx->thumb_buf, 1, out_len, f); fclose(f);
strncpy(ctx->saved_img_path, tmp_path, sizeof(ctx->saved_img_path)-1);
ctx->has_image = true;
snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d via user_data %s", (int)out_len, tmp_path);
} else {
ctx->thumb_len = 0;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Thumb fetch fail %d", r);
ctx->has_image = false;
snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d bytes but save fail", (int)out_len);
}
} else {
ctx->has_image = false;
snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d bytes no handle", (int)out_len);
}
} else {
ctx->thumb_len = 0; ctx->has_image = false;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Proxy PNG fail %d fallback thumb", r);
size_t ol2=0; int r2 = download_thumbnail_to_buf(p->id, ctx->thumb_buf, THUMB_BUF_SIZE, &ol2);
if (r2>0) { ctx->thumb_len = ol2; snprintf(ctx->status_text, sizeof(ctx->status_text), "Thumb %d fallback", (int)ol2); }
}
ui_update_all(ctx);
ctx->thumb_task = NULL;
ctx->thumb_in_progress = false;
vTaskDelete(NULL);
ctx->thumb_task = NULL; ctx->thumb_in_progress = false; vTaskDelete(NULL);
}
static void start_thumb_task(AppCtx* ctx) {
if (ctx->thumb_in_progress) {
ESP_LOGI(TAG, "Thumb already in progress");
return;
}
if (ctx->thumb_in_progress) return;
if (photo_count==0) return;
ctx->thumb_in_progress = true;
ctx->thumb_len = 0;
xTaskCreate(thumb_task_fn, "thumb_fetch", 6144, ctx, 5, &ctx->thumb_task);
ctx->thumb_in_progress = true; ctx->thumb_len=0; ctx->has_image=false;
xTaskCreate(thumb_task_fn, "thumb_fetch", 8192, ctx, 5, &ctx->thumb_task);
}
/* ── Fetch task ── */
static void fetch_task_fn(void* arg) {
AppCtx* ctx = (AppCtx*)arg;
ctx->fetch_in_progress = true;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Fetching %s photos...", PERSON_NAME);
ui_update_all(ctx);
int n = fetch_person_assets();
if (n>0) {
current_index = 0;
ctx->current_idx = 0;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Loaded %d photos", n);
ESP_LOGI(TAG, "Fetch done %d", n);
} else {
snprintf(ctx->status_text, sizeof(ctx->status_text), "Fetch failed - check WiFi/Server\n%s/api/search/metadata", IMMICH_BASE_URL);
ESP_LOGE(TAG, "Fetch failed");
}
current_index=0; ctx->current_idx=0;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Loaded %d photos, PNG via 8106...", n);
ui_update_all(ctx);
if (n > 0) {
// fetch first thumb synchronously via same task logic
if (ctx->thumb_buf == NULL) {
if (!ctx->thumb_buf) {
ctx->thumb_buf = heap_caps_malloc(THUMB_BUF_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (ctx->thumb_buf == NULL) ctx->thumb_buf = malloc(THUMB_BUF_SIZE);
if (!ctx->thumb_buf) ctx->thumb_buf = malloc(THUMB_BUF_SIZE);
}
if (ctx->thumb_buf) {
size_t ol=0;
Photo* p=&photos[0];
int r = download_thumbnail_to_buf(p->id, ctx->thumb_buf, THUMB_BUF_SIZE, &ol);
int r=fetch_png_from_proxy(p->id, ctx->thumb_buf, THUMB_BUF_SIZE, &ol);
if (r>0) {
ctx->thumb_len=ol;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Thumb %d bytes OK", (int)ol);
const char* saved=save_buffer_to_file(ctx->thumb_buf, ol);
if (saved) { strncpy(ctx->saved_img_path, saved, sizeof(ctx->saved_img_path)-1); ctx->has_image=true; snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d %s", (int)ol, saved); }
else if (ctx->app) {
char tmp_path[256]; size_t tmp_sz=sizeof(tmp_path);
tt_app_get_user_data_child_path(ctx->app, "immich_cur.png", tmp_path, &tmp_sz);
FILE* f=fopen(tmp_path,"wb"); if(f){fwrite(ctx->thumb_buf,1,ol,f); fclose(f); strncpy(ctx->saved_img_path, tmp_path, sizeof(ctx->saved_img_path)-1); ctx->has_image=true; snprintf(ctx->status_text,sizeof(ctx->status_text),"PNG %d user_data %s",(int)ol,tmp_path);}
}
}
ui_update_all(ctx);
}
} else {
snprintf(ctx->status_text, sizeof(ctx->status_text), "Fetch failed %s", IMMICH_BASE_URL);
ui_update_all(ctx);
}
ctx->fetch_task = NULL;
ctx->fetch_in_progress = false;
vTaskDelete(NULL);
ctx->fetch_task=NULL; ctx->fetch_in_progress=false; vTaskDelete(NULL);
}
static void start_fetch_task(AppCtx* ctx) {
@@ -477,86 +388,49 @@ static void start_fetch_task(AppCtx* ctx) {
xTaskCreate(fetch_task_fn, "immich_fetch", 8192, ctx, 5, &ctx->fetch_task);
}
/* ── Event callbacks ── */
static void prev_event_cb(lv_event_t* e) {
AppCtx* ctx = (AppCtx*)lv_event_get_user_data(e);
if (ctx == NULL || photo_count == 0) return;
if (!ctx || photo_count==0) return;
if (ctx->thumb_in_progress || ctx->fetch_in_progress) return;
ctx->current_idx--;
if (ctx->current_idx < 0) ctx->current_idx = photo_count - 1;
current_index = ctx->current_idx;
ctx->thumb_len = 0;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Loading %d/%d", ctx->current_idx+1, photo_count);
ui_update_all(ctx);
start_thumb_task(ctx);
ctx->current_idx--; if (ctx->current_idx<0) ctx->current_idx=photo_count-1;
current_index=ctx->current_idx; ctx->thumb_len=0; ctx->has_image=false;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Loading %d/%d 8106 PNG", ctx->current_idx+1, photo_count);
ui_update_all(ctx); start_thumb_task(ctx);
}
static void next_event_cb(lv_event_t* e) {
AppCtx* ctx = (AppCtx*)lv_event_get_user_data(e);
if (ctx == NULL || photo_count == 0) return;
if (!ctx || photo_count==0) return;
if (ctx->thumb_in_progress || ctx->fetch_in_progress) return;
ctx->current_idx++;
if (ctx->current_idx >= photo_count) ctx->current_idx = 0;
current_index = ctx->current_idx;
ctx->thumb_len = 0;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Loading %d/%d", ctx->current_idx+1, photo_count);
ui_update_all(ctx);
start_thumb_task(ctx);
ctx->current_idx++; if (ctx->current_idx>=photo_count) ctx->current_idx=0;
current_index=ctx->current_idx; ctx->thumb_len=0; ctx->has_image=false;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Loading %d/%d 8106 PNG", ctx->current_idx+1, photo_count);
ui_update_all(ctx); start_thumb_task(ctx);
}
static void reload_event_cb(lv_event_t* e) {
AppCtx* ctx = (AppCtx*)lv_event_get_user_data(e);
if (ctx == NULL) return;
if (!ctx) return;
if (ctx->fetch_in_progress || ctx->thumb_in_progress) return;
// clear and refetch
photo_count = 0;
ctx->current_idx = 0;
current_index = 0;
ctx->thumb_len = 0;
photo_count=0; ctx->current_idx=0; current_index=0; ctx->thumb_len=0; ctx->has_image=false;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Reloading...");
ui_update_all(ctx);
start_fetch_task(ctx);
ui_update_all(ctx); start_fetch_task(ctx);
}
/* ── App lifecycle ── */
static void* create_data(void) {
AppCtx* ctx = calloc(1, sizeof(AppCtx));
if (ctx) {
ctx->current_idx = 0;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Init - %s", IMMICH_BASE_URL);
}
if (ctx) { ctx->current_idx=0; snprintf(ctx->status_text, sizeof(ctx->status_text), "Init %s proxy %s", IMMICH_BASE_URL, RESIZE_PROXY_URL); }
return ctx;
}
static void destroy_data(void* data) {
AppCtx* ctx = (AppCtx*)data;
if (ctx) {
if (ctx->thumb_buf) {
heap_caps_free(ctx->thumb_buf);
}
AppCtx* ctx = (AppCtx*)data; if (!ctx) return;
if (ctx->thumb_buf) heap_caps_free(ctx->thumb_buf);
free(ctx);
}
}
static void on_create(AppHandle app, void* data) {
AppCtx* ctx = (AppCtx*)data;
if (ctx) ctx->app = app;
ESP_LOGI(TAG, "onCreate base=%s person=%s", IMMICH_BASE_URL, IMMICH_PERSON_ID);
}
static void on_destroy(AppHandle app, void* data) {
(void)app;
(void)data;
ESP_LOGI(TAG, "onDestroy");
}
static void on_create(AppHandle app, void* data) { AppCtx* ctx=(AppCtx*)data; if(ctx) ctx->app=app; ESP_LOGI(TAG,"onCreate proxy=%s", RESIZE_PROXY_URL); }
static void on_destroy(AppHandle app, void* data) { (void)app;(void)data; }
static void on_show(AppHandle app, void* data, lv_obj_t* parent) {
AppCtx* ctx = (AppCtx*)data;
if (ctx == NULL) return;
ctx->visible = true;
ctx->app = app;
ESP_LOGI(TAG, "onShow");
AppCtx* ctx=(AppCtx*)data; if(!ctx) return;
ctx->visible=true; ctx->app=app;
lv_obj_remove_flag(parent, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(parent, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
@@ -566,25 +440,21 @@ static void on_show(AppHandle app, void* data, lv_obj_t* parent) {
lv_obj_set_style_bg_opa(parent, LV_OPA_COVER, LV_PART_MAIN);
lv_obj_t* toolbar=tt_lvgl_toolbar_create_for_app(parent, app);
lv_obj_align(toolbar, LV_ALIGN_TOP_MID, 0, 0);
// Title container
lv_obj_t* title_cont=lv_obj_create(parent);
lv_obj_set_width(title_cont, lv_pct(100));
lv_obj_set_height(title_cont, LV_SIZE_CONTENT);
lv_obj_set_width(title_cont, lv_pct(100)); lv_obj_set_height(title_cont, LV_SIZE_CONTENT);
lv_obj_set_flex_flow(title_cont, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(title_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
lv_obj_set_style_bg_opa(title_cont, LV_OPA_TRANSP, LV_PART_MAIN);
lv_obj_set_style_border_width(title_cont, 0, LV_PART_MAIN);
lv_obj_set_style_pad_all(title_cont, 2, LV_PART_MAIN);
lv_obj_set_style_pad_row(title_cont, 2, LV_PART_MAIN);
ctx->lbl_title=lv_label_create(title_cont);
lv_label_set_text_fmt(ctx->lbl_title, "%s - %s photos", PERSON_NAME, PERSON_COUNT_STR);
lv_label_set_text_fmt(ctx->lbl_title, "%s - %s photos 8106 PNG", PERSON_NAME, PERSON_COUNT_STR);
lv_obj_set_style_text_font(ctx->lbl_title, lvgl_get_text_font(FONT_SIZE_LARGE), LV_PART_MAIN);
lv_obj_set_style_text_color(ctx->lbl_title, lv_color_white(), LV_PART_MAIN);
lv_obj_set_style_text_align(ctx->lbl_title, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
lv_obj_set_width(ctx->lbl_title, lv_pct(100));
lv_obj_set_style_text_align(ctx->lbl_title, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
ctx->lbl_count=lv_label_create(title_cont);
lv_label_set_text(ctx->lbl_count, "Loading...");
@@ -594,7 +464,6 @@ static void on_show(AppHandle app, void* data, lv_obj_t* parent) {
lv_label_set_long_mode(ctx->lbl_count, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_align(ctx->lbl_count, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
// Main image/status container - flex grow 1
lv_obj_t* main_cont=lv_obj_create(parent);
lv_obj_set_width(main_cont, lv_pct(100));
lv_obj_set_flex_grow(main_cont, 1);
@@ -602,121 +471,65 @@ static void on_show(AppHandle app, void* data, lv_obj_t* parent) {
lv_obj_set_flex_align(main_cont, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
lv_obj_set_style_bg_color(main_cont, lv_color_hex(0x1E1E1E), LV_PART_MAIN);
lv_obj_set_style_bg_opa(main_cont, LV_OPA_COVER, LV_PART_MAIN);
lv_obj_set_style_border_width(main_cont, 1, LV_PART_MAIN);
lv_obj_set_style_border_color(main_cont, lv_color_hex(0x333333), LV_PART_MAIN);
lv_obj_set_style_radius(main_cont, 8, LV_PART_MAIN);
lv_obj_set_style_pad_all(main_cont, 6, LV_PART_MAIN);
lv_obj_set_style_pad_row(main_cont, 4, LV_PART_MAIN);
// Image widget - centered, for future JPEG decode
ctx->img=lv_image_create(main_cont);
// Hide initially; if we later decode, show
lv_obj_add_flag(ctx->img, LV_OBJ_FLAG_HIDDEN);
lv_obj_set_style_bg_opa(ctx->img, LV_OPA_TRANSP, LV_PART_MAIN);
lv_obj_set_size(ctx->img, 320, 240);
ctx->lbl_status=lv_label_create(main_cont);
lv_label_set_text(ctx->lbl_status, ctx->status_text);
lv_obj_set_width(ctx->lbl_status, lv_pct(100));
lv_label_set_long_mode(ctx->lbl_status, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_color(ctx->lbl_status, lv_color_hex(0xE0E0E0), LV_PART_MAIN);
lv_obj_set_style_text_font(ctx->lbl_status, lvgl_get_text_font(FONT_SIZE_DEFAULT), LV_PART_MAIN);
lv_obj_set_style_text_align(ctx->lbl_status, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
// Info label below main container
ctx->lbl_info=lv_label_create(parent);
lv_obj_set_width(ctx->lbl_info, lv_pct(100));
lv_label_set_long_mode(ctx->lbl_info, LV_LABEL_LONG_WRAP);
lv_label_set_text(ctx->lbl_info, "ID: loading...\nFile: ...\nDate: ...");
lv_obj_set_style_text_color(ctx->lbl_info, lv_color_hex(0xCCCCCC), LV_PART_MAIN);
lv_label_set_text(ctx->lbl_info, "loading...");
lv_obj_set_style_text_font(ctx->lbl_info, lvgl_get_text_font(FONT_SIZE_SMALL), LV_PART_MAIN);
lv_obj_set_style_pad_hor(ctx->lbl_info, 4, LV_PART_MAIN);
// Bottom nav row
lv_obj_t* nav_row=lv_obj_create(parent);
lv_obj_set_width(nav_row, lv_pct(100));
lv_obj_set_height(nav_row, 48);
lv_obj_set_width(nav_row, lv_pct(100)); lv_obj_set_height(nav_row, 48);
lv_obj_set_flex_flow(nav_row, LV_FLEX_FLOW_ROW);
lv_obj_set_flex_align(nav_row, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
lv_obj_set_style_bg_opa(nav_row, LV_OPA_TRANSP, LV_PART_MAIN);
lv_obj_set_style_border_width(nav_row, 0, LV_PART_MAIN);
lv_obj_set_style_pad_all(nav_row, 2, LV_PART_MAIN);
lv_obj_set_style_pad_column(nav_row, 4, LV_PART_MAIN);
ctx->btn_prev = lv_btn_create(nav_row);
lv_obj_set_size(ctx->btn_prev, 60, 40);
lv_obj_set_style_radius(ctx->btn_prev, 6, LV_PART_MAIN);
lv_obj_set_style_bg_color(ctx->btn_prev, lv_color_hex(0x333333), LV_PART_MAIN);
lv_obj_t* lbl_prev = lv_label_create(ctx->btn_prev);
lv_label_set_text(lbl_prev, "<");
lv_obj_center(lbl_prev);
ctx->btn_prev=lv_btn_create(nav_row); lv_obj_set_size(ctx->btn_prev, 60, 40);
lv_obj_t* lp=lv_label_create(ctx->btn_prev); lv_label_set_text(lp, "<"); lv_obj_center(lp);
lv_obj_add_event_cb(ctx->btn_prev, prev_event_cb, LV_EVENT_CLICKED, ctx);
ctx->lbl_index=lv_label_create(nav_row);
lv_label_set_text(ctx->lbl_index, "0/0");
lv_obj_set_style_text_color(ctx->lbl_index, lv_color_white(), LV_PART_MAIN);
lv_obj_set_style_text_font(ctx->lbl_index, lvgl_get_text_font(FONT_SIZE_DEFAULT), LV_PART_MAIN);
lv_obj_set_flex_grow(ctx->lbl_index, 1);
lv_obj_set_style_text_align(ctx->lbl_index, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
ctx->btn_next = lv_btn_create(nav_row);
lv_obj_set_size(ctx->btn_next, 60, 40);
lv_obj_set_style_radius(ctx->btn_next, 6, LV_PART_MAIN);
ctx->btn_next=lv_btn_create(nav_row); lv_obj_set_size(ctx->btn_next, 60, 40);
lv_obj_set_style_bg_color(ctx->btn_next, lv_color_hex(0x6200EE), LV_PART_MAIN);
lv_obj_t* lbl_next = lv_label_create(ctx->btn_next);
lv_label_set_text(lbl_next, ">");
lv_obj_center(lbl_next);
lv_obj_t* ln=lv_label_create(ctx->btn_next); lv_label_set_text(ln, ">"); lv_obj_center(ln);
lv_obj_add_event_cb(ctx->btn_next, next_event_cb, LV_EVENT_CLICKED, ctx);
ctx->btn_reload = lv_btn_create(nav_row);
lv_obj_set_size(ctx->btn_reload, 40, 40);
lv_obj_set_style_radius(ctx->btn_reload, 6, LV_PART_MAIN);
lv_obj_set_style_bg_color(ctx->btn_reload, lv_color_hex(0x444444), LV_PART_MAIN);
lv_obj_t* lbl_reload = lv_label_create(ctx->btn_reload);
lv_label_set_text(lbl_reload, LV_SYMBOL_REFRESH);
lv_obj_center(lbl_reload);
ctx->btn_reload=lv_btn_create(nav_row); lv_obj_set_size(ctx->btn_reload, 40, 40);
lv_obj_t* lr=lv_label_create(ctx->btn_reload); lv_label_set_text(lr, LV_SYMBOL_REFRESH); lv_obj_center(lr);
lv_obj_add_event_cb(ctx->btn_reload, reload_event_cb, LV_EVENT_CLICKED, ctx);
// If we already have photos from previous show, update UI immediately
if (photo_count > 0) {
ctx->current_idx = current_index;
if (ctx->current_idx >= photo_count) ctx->current_idx = 0;
snprintf(ctx->status_text, sizeof(ctx->status_text), "Cached %d photos", photo_count);
ui_update_all(ctx);
// start thumb fetch for current
start_thumb_task(ctx);
} else {
snprintf(ctx->status_text, sizeof(ctx->status_text), "Fetching %s photos from %s...", PERSON_NAME, IMMICH_BASE_URL);
ui_update_all(ctx);
start_fetch_task(ctx);
}
if (photo_count>0) { ctx->current_idx=current_index; if(ctx->current_idx>=photo_count) ctx->current_idx=0; snprintf(ctx->status_text,sizeof(ctx->status_text),"Cached %d photos via 8106",photo_count); ui_update_all(ctx); start_thumb_task(ctx); }
else { snprintf(ctx->status_text,sizeof(ctx->status_text),"Fetching %s via %s...",PERSON_NAME, RESIZE_PROXY_URL); ui_update_all(ctx); start_fetch_task(ctx); }
}
static void on_hide(AppHandle app, void* data) {
(void)app;
AppCtx* ctx = (AppCtx*)data;
if (ctx == NULL) return;
ESP_LOGI(TAG, "onHide");
(void)app; AppCtx* ctx=(AppCtx*)data; if(!ctx) return;
ctx->visible=false;
// Let tasks finish, but prevent further UI updates
// Do not aggressively kill tasks to avoid heap corruption; they check visible flag
// Wait briefly for tasks to notice
int timeout = 20;
while ((ctx->fetch_task != NULL || ctx->thumb_task != NULL) && timeout > 0) {
vTaskDelay(pdMS_TO_TICKS(50));
timeout--;
}
int timeout=20; while((ctx->fetch_task||ctx->thumb_task) && timeout>0){ vTaskDelay(pdMS_TO_TICKS(50)); timeout--; }
}
int main(int argc, char* argv[]) {
(void)argc;
(void)argv;
tt_app_register((AppRegistration) {
.createData = create_data,
.destroyData = destroy_data,
.onCreate = on_create,
.onDestroy = on_destroy,
.onShow = on_show,
.onHide = on_hide,
});
(void)argc;(void)argv;
tt_app_register((AppRegistration){.createData=create_data,.destroyData=destroy_data,.onCreate=on_create,.onDestroy=on_destroy,.onShow=on_show,.onHide=on_hide});
return 0;
}