2026-08-03 10:54:41 -04:00
|
|
|
#include "photo_queue.h"
|
|
|
|
|
#include "immich_client.h"
|
|
|
|
|
#include <ArduinoJson.h>
|
2026-08-03 14:39:55 -04:00
|
|
|
#include <algorithm>
|
2026-08-03 10:54:41 -04:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
void PhotoQueue::begin(ImmichClient& client, SettingsManager& settings) {
|
|
|
|
|
_client = &client;
|
|
|
|
|
_settings = &settings;
|
|
|
|
|
|
|
|
|
|
Settings s = _settings->get();
|
|
|
|
|
_cursor = s.queue_cursor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PhotoQueue::sync() {
|
|
|
|
|
if (_client == nullptr || _settings == nullptr) return false;
|
|
|
|
|
|
2026-08-03 14:39:55 -04:00
|
|
|
_lastSyncAttempt = millis();
|
|
|
|
|
|
2026-08-03 10:54:41 -04:00
|
|
|
Settings s = _settings->get();
|
2026-08-03 14:39:55 -04:00
|
|
|
|
|
|
|
|
// Fetch in batches until we have TARGET_QUEUE_SIZE usable, non-duplicate IDs
|
2026-08-03 10:54:41 -04:00
|
|
|
std::vector<String> newIds;
|
2026-08-03 14:39:55 -04:00
|
|
|
int rounds = 0;
|
|
|
|
|
|
|
|
|
|
Serial.printf("[queue] Fetching until %d usable assets (excluding %d shown)\n",
|
|
|
|
|
TARGET_QUEUE_SIZE, _shown.size());
|
2026-08-03 10:54:41 -04:00
|
|
|
|
2026-08-03 14:39:55 -04:00
|
|
|
while (newIds.size() < TARGET_QUEUE_SIZE && rounds < MAX_FETCH_ROUNDS) {
|
|
|
|
|
auto batch = _client->fetchRandomAssetIds(50);
|
|
|
|
|
if (batch.empty()) {
|
|
|
|
|
Serial.printf("[queue] Fetch round %d returned empty\n", rounds + 1);
|
|
|
|
|
break;
|
2026-08-03 10:54:41 -04:00
|
|
|
}
|
2026-08-03 14:39:55 -04:00
|
|
|
|
|
|
|
|
for (auto& id : batch) {
|
|
|
|
|
if (newIds.size() >= TARGET_QUEUE_SIZE) break;
|
|
|
|
|
|
|
|
|
|
// Skip if already in this batch
|
|
|
|
|
if (std::find(newIds.begin(), newIds.end(), id) != newIds.end()) continue;
|
|
|
|
|
|
|
|
|
|
// Skip if recently shown
|
|
|
|
|
if (std::find(_shown.begin(), _shown.end(), id) != _shown.end()) continue;
|
|
|
|
|
|
|
|
|
|
newIds.push_back(id);
|
2026-08-03 10:54:41 -04:00
|
|
|
}
|
|
|
|
|
|
2026-08-03 14:39:55 -04:00
|
|
|
rounds++;
|
|
|
|
|
Serial.printf("[queue] Round %d: have %d/%d usable assets\n",
|
|
|
|
|
rounds, newIds.size(), TARGET_QUEUE_SIZE);
|
|
|
|
|
}
|
2026-08-03 10:54:41 -04:00
|
|
|
|
|
|
|
|
if (newIds.empty()) {
|
2026-08-03 14:39:55 -04:00
|
|
|
_syncRetryDelay = min(_syncRetryDelay * 2, SYNC_RETRY_MAX);
|
|
|
|
|
Serial.printf("[queue] Sync failed after %d rounds, retry in %lus\n",
|
|
|
|
|
rounds, _syncRetryDelay / 1000);
|
2026-08-03 10:54:41 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_queue = newIds;
|
|
|
|
|
|
|
|
|
|
// Apply cycling mode
|
|
|
|
|
switch (s.cycle_mode) {
|
|
|
|
|
case CycleMode::Random:
|
|
|
|
|
applyFavoritesWeighting();
|
|
|
|
|
shuffle();
|
|
|
|
|
break;
|
|
|
|
|
case CycleMode::Chronological:
|
|
|
|
|
sortChronological(false);
|
|
|
|
|
break;
|
|
|
|
|
case CycleMode::ReverseChronological:
|
|
|
|
|
sortChronological(true);
|
|
|
|
|
break;
|
|
|
|
|
case CycleMode::FavoritesWeighted:
|
|
|
|
|
applyFavoritesWeighting();
|
|
|
|
|
shuffle();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-03 14:39:55 -04:00
|
|
|
// Reset cursor for fresh queue
|
|
|
|
|
_cursor = 0;
|
2026-08-03 10:54:41 -04:00
|
|
|
|
|
|
|
|
_lastSyncTime = millis();
|
2026-08-03 14:39:55 -04:00
|
|
|
_syncRetryDelay = SYNC_RETRY_MIN;
|
|
|
|
|
Serial.printf("[queue] Synced: %d assets in %d rounds, cursor at %d\n",
|
|
|
|
|
_queue.size(), rounds, _cursor);
|
2026-08-03 10:54:41 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String PhotoQueue::next() {
|
2026-08-03 14:39:55 -04:00
|
|
|
// Auto-refill when queue is exhausted
|
|
|
|
|
if (_queue.empty() || _cursor >= _queue.size()) {
|
|
|
|
|
Serial.println("[queue] Queue exhausted, fetching new batch");
|
|
|
|
|
sync();
|
2026-08-03 10:54:41 -04:00
|
|
|
_cursor = 0;
|
|
|
|
|
}
|
2026-08-03 14:39:55 -04:00
|
|
|
|
|
|
|
|
if (_queue.empty()) return "";
|
|
|
|
|
|
2026-08-03 10:54:41 -04:00
|
|
|
String id = _queue[_cursor++];
|
|
|
|
|
|
2026-08-03 14:39:55 -04:00
|
|
|
// Track shown IDs for dedup on next refill
|
|
|
|
|
_shown.push_back(id);
|
|
|
|
|
if (_shown.size() > MAX_SHOWN_HISTORY) {
|
|
|
|
|
_shown.erase(_shown.begin());
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-03 10:54:41 -04:00
|
|
|
// Persist cursor
|
2026-08-03 11:00:02 -04:00
|
|
|
_settings->saveField("queue_cursor", static_cast<uint32_t>(_cursor));
|
2026-08-03 10:54:41 -04:00
|
|
|
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String PhotoQueue::random() {
|
|
|
|
|
if (_queue.empty()) return "";
|
|
|
|
|
size_t idx = ::random(0, _queue.size());
|
|
|
|
|
return _queue[idx];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String PhotoQueue::current() {
|
|
|
|
|
if (_queue.empty()) return "";
|
|
|
|
|
size_t idx = (_cursor > 0) ? _cursor - 1 : 0;
|
|
|
|
|
return _queue[idx];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t PhotoQueue::size() {
|
|
|
|
|
return _queue.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PhotoQueue::needsResync() {
|
2026-08-03 14:39:55 -04:00
|
|
|
unsigned long now = millis();
|
|
|
|
|
|
|
|
|
|
// If never synced successfully, use retry backoff
|
|
|
|
|
if (_lastSyncTime == 0) {
|
|
|
|
|
if (_lastSyncAttempt == 0) return true;
|
|
|
|
|
return (now - _lastSyncAttempt) >= _syncRetryDelay;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Normal resync interval
|
|
|
|
|
unsigned long elapsed = now - _lastSyncTime;
|
2026-08-03 10:54:41 -04:00
|
|
|
return elapsed >= (QUEUE_RESYNC_HOURS * 3600000UL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String PhotoQueue::findPortraitPair(size_t startIndex) {
|
|
|
|
|
for (size_t i = 1; i <= PORTRAIT_LOOKAHEAD && (startIndex + i) < _queue.size(); i++) {
|
|
|
|
|
return _queue[startIndex + i];
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PhotoQueue::shuffle() {
|
|
|
|
|
for (size_t i = _queue.size() - 1; i > 0; i--) {
|
|
|
|
|
size_t j = ::random(0, i + 1);
|
|
|
|
|
std::swap(_queue[i], _queue[j]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PhotoQueue::sortChronological(bool reverse) {
|
|
|
|
|
if (reverse) {
|
|
|
|
|
std::reverse(_queue.begin(), _queue.end());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PhotoQueue::applyFavoritesWeighting() {
|
|
|
|
|
if (_client == nullptr) return;
|
|
|
|
|
|
|
|
|
|
Settings s = _settings->get();
|
|
|
|
|
if (s.cycle_mode != CycleMode::FavoritesWeighted) return;
|
|
|
|
|
|
|
|
|
|
auto favorites = _client->fetchFavoriteAssetIds();
|
|
|
|
|
for (auto& fav : favorites) {
|
|
|
|
|
bool inQueue = false;
|
|
|
|
|
for (auto& id : _queue) {
|
|
|
|
|
if (id == fav) { inQueue = true; break; }
|
|
|
|
|
}
|
|
|
|
|
if (inQueue) {
|
|
|
|
|
_queue.push_back(fav);
|
|
|
|
|
_queue.push_back(fav);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<String> PhotoQueue::getSelectedAlbumIds() {
|
|
|
|
|
std::vector<String> ids;
|
|
|
|
|
Settings s = _settings->get();
|
|
|
|
|
|
|
|
|
|
JsonDocument doc;
|
|
|
|
|
DeserializationError err = deserializeJson(doc, s.albums_json);
|
|
|
|
|
if (err) return ids;
|
|
|
|
|
|
|
|
|
|
JsonArray arr = doc.as<JsonArray>();
|
|
|
|
|
for (JsonVariant v : arr) {
|
|
|
|
|
ids.push_back(v.as<String>());
|
|
|
|
|
}
|
|
|
|
|
return ids;
|
|
|
|
|
}
|