add more immich control options
This commit is contained in:
@@ -101,6 +101,14 @@ ImmichConfig ParseImmichConfigObject(QJsonObject immichJson) {
|
||||
if(albumIds.size() > 0)
|
||||
config.albumIds = albumIds;
|
||||
|
||||
std::string personId = ParseJSONString(immichJson, "personId");
|
||||
if(!personId.empty())
|
||||
config.personIds.push_back(personId);
|
||||
|
||||
std::vector<std::string> personIds = ParseJSONStrings(immichJson, "personIds");
|
||||
if(personIds.size() > 0)
|
||||
config.personIds = personIds;
|
||||
|
||||
SetJSONInt(config.pageSize, immichJson, "pageSize");
|
||||
SetJSONInt(config.maxAssets, immichJson, "maxAssets");
|
||||
SetJSONInt(config.cacheMaxMB, immichJson, "cacheMaxMB");
|
||||
@@ -123,6 +131,10 @@ MqttConfig ParseMqttConfigObject(QJsonObject mqttJson) {
|
||||
if(!topic.empty())
|
||||
config.topic = topic;
|
||||
|
||||
std::string immichTopic = ParseJSONString(mqttJson, "immichTopic");
|
||||
if(!immichTopic.empty())
|
||||
config.immichTopic = immichTopic;
|
||||
|
||||
std::string clientId = ParseJSONString(mqttJson, "clientId");
|
||||
if(!clientId.empty())
|
||||
config.clientId = clientId;
|
||||
@@ -139,6 +151,9 @@ MqttConfig ParseMqttConfigObject(QJsonObject mqttJson) {
|
||||
SetJSONInt(config.keepAlive, mqttJson, "keepAlive");
|
||||
SetJSONInt(config.qos, mqttJson, "qos");
|
||||
|
||||
if(config.immichTopic.empty() && !config.topic.empty())
|
||||
config.immichTopic = config.topic + "/immich";
|
||||
|
||||
if(!config.host.empty() && !config.topic.empty())
|
||||
config.enabled = true;
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ struct ImmichConfig {
|
||||
std::string url = "";
|
||||
std::string apiKey = "";
|
||||
std::vector<std::string> albumIds;
|
||||
std::vector<std::string> personIds;
|
||||
std::string size = "fullsize";
|
||||
std::string order = "desc";
|
||||
int pageSize = 200;
|
||||
@@ -40,6 +41,13 @@ struct ImmichConfig {
|
||||
if (albumIds[i] != b.albumIds[i])
|
||||
return false;
|
||||
}
|
||||
if (personIds.size() != b.personIds.size())
|
||||
return false;
|
||||
for (size_t i = 0; i < personIds.size(); ++i)
|
||||
{
|
||||
if (personIds[i] != b.personIds[i])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -54,6 +62,7 @@ struct MqttConfig {
|
||||
std::string host = "localhost";
|
||||
int port = 1883;
|
||||
std::string topic = "slide/control";
|
||||
std::string immichTopic = "";
|
||||
std::string clientId = "slide";
|
||||
std::string username = "";
|
||||
std::string password = "";
|
||||
@@ -66,6 +75,7 @@ struct MqttConfig {
|
||||
host == b.host &&
|
||||
port == b.port &&
|
||||
topic == b.topic &&
|
||||
immichTopic == b.immichTopic &&
|
||||
clientId == b.clientId &&
|
||||
username == b.username &&
|
||||
password == b.password &&
|
||||
|
||||
@@ -105,6 +105,14 @@ QVector<ImmichAsset> ImmichClient::fetchAssets()
|
||||
bool triedZero = false;
|
||||
int page = 1;
|
||||
|
||||
if (ShouldLog())
|
||||
{
|
||||
Log("Immich search: size=", config.size, ", order=", config.order,
|
||||
", pageSize=", pageSize, ", maxAssets=", maxAssets,
|
||||
", albumIds=", config.albumIds.size(),
|
||||
", personIds=", config.personIds.size());
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
QJsonObject body;
|
||||
@@ -121,6 +129,13 @@ QVector<ImmichAsset> ImmichClient::fetchAssets()
|
||||
ids.append(QString::fromStdString(id));
|
||||
body["albumIds"] = ids;
|
||||
}
|
||||
if (config.personIds.size() > 0)
|
||||
{
|
||||
QJsonArray ids;
|
||||
for (const auto &id : config.personIds)
|
||||
ids.append(QString::fromStdString(id));
|
||||
body["personIds"] = ids;
|
||||
}
|
||||
|
||||
QByteArray response = postJson(apiUrl("/search/metadata"), body, nullptr, kMetadataTimeoutMs);
|
||||
if (response.isEmpty())
|
||||
@@ -134,6 +149,7 @@ QVector<ImmichAsset> ImmichClient::fetchAssets()
|
||||
QJsonObject assetsObj = root["assets"].toObject();
|
||||
QJsonArray items = assetsObj["items"].toArray();
|
||||
int total = assetsObj["total"].toInt();
|
||||
Log("Immich page ", page, ": ", items.size(), " assets (total ", total, ")");
|
||||
if (items.isEmpty())
|
||||
{
|
||||
if (total > 0 && page == 1 && !triedZero)
|
||||
@@ -194,6 +210,7 @@ bool ImmichClient::downloadAsset(const QString &assetId, QByteArray &data, QStri
|
||||
url.setQuery(query);
|
||||
}
|
||||
|
||||
Log("Immich download asset ", assetId.toStdString(), " (", size.toStdString(), ")");
|
||||
QByteArray payload = getBytes(url, &contentType, kAssetTimeoutMs);
|
||||
if (payload.isEmpty())
|
||||
return false;
|
||||
@@ -297,7 +314,10 @@ QString ImmichAssetCache::getCachedPath(const QString &assetId, const QString &a
|
||||
|
||||
QString existing = findExisting(assetId);
|
||||
if (!existing.isEmpty())
|
||||
{
|
||||
Log("Immich cache hit: ", assetId.toStdString());
|
||||
return existing;
|
||||
}
|
||||
|
||||
QByteArray data;
|
||||
QString contentType;
|
||||
@@ -318,6 +338,8 @@ QString ImmichAssetCache::getCachedPath(const QString &assetId, const QString &a
|
||||
if (!file.commit())
|
||||
return "";
|
||||
|
||||
Log("Immich cached asset: ", assetId.toStdString(), " -> ", filePath.toStdString());
|
||||
|
||||
if (cacheMaxBytes > 0)
|
||||
{
|
||||
if (!cacheSizeKnown)
|
||||
|
||||
239
src/main.cpp
239
src/main.cpp
@@ -19,6 +19,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <memory>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
|
||||
void usage(std::string programName) {
|
||||
std::cerr << "Usage: " << programName << " [-t rotation_seconds] [-T transition_seconds] [-h/--overlay-color #rrggbb] [-a aspect('l','p','a', 'm')] [-o background_opacity(0..255)] [-b blur_radius] -p image_folder [-r] [-s] [-S] [-v] [--verbose] [--stretch] [-c config_file_path]" << std::endl;
|
||||
@@ -243,6 +246,216 @@ void ReloadConfigIfNeeded(AppConfig &appConfig, MainWindow &w, ImageSwitcher *sw
|
||||
}
|
||||
}
|
||||
|
||||
static bool ParseBooleanString(const QString &value, bool &outValue)
|
||||
{
|
||||
QString v = value.trimmed().toLower();
|
||||
if (v == "true" || v == "1" || v == "yes" || v == "on")
|
||||
{
|
||||
outValue = true;
|
||||
return true;
|
||||
}
|
||||
if (v == "false" || v == "0" || v == "no" || v == "off")
|
||||
{
|
||||
outValue = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static std::vector<std::string> SplitCsv(const QString &value)
|
||||
{
|
||||
std::vector<std::string> output;
|
||||
QStringList parts = value.split(',', Qt::SkipEmptyParts);
|
||||
for (const auto &part : parts)
|
||||
{
|
||||
QString trimmed = part.trimmed();
|
||||
if (!trimmed.isEmpty())
|
||||
output.push_back(trimmed.toStdString());
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
static bool ApplyImmichPayload(ImmichConfig &config, const QString &payload)
|
||||
{
|
||||
bool changed = false;
|
||||
QString trimmed = payload.trimmed();
|
||||
if (trimmed.isEmpty())
|
||||
return false;
|
||||
|
||||
if (trimmed.startsWith("{"))
|
||||
{
|
||||
QJsonDocument doc = QJsonDocument::fromJson(trimmed.toUtf8());
|
||||
if (!doc.isObject())
|
||||
return false;
|
||||
QJsonObject obj = doc.object();
|
||||
|
||||
if (obj.contains("albumId") && obj["albumId"].isString())
|
||||
{
|
||||
config.albumIds = { obj["albumId"].toString().toStdString() };
|
||||
changed = true;
|
||||
}
|
||||
if (obj.contains("albumIds") && obj["albumIds"].isArray())
|
||||
{
|
||||
config.albumIds.clear();
|
||||
QJsonArray arr = obj["albumIds"].toArray();
|
||||
for (const auto &value : arr)
|
||||
{
|
||||
if (value.isString())
|
||||
config.albumIds.push_back(value.toString().toStdString());
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
if (obj.contains("personId") && obj["personId"].isString())
|
||||
{
|
||||
config.personIds = { obj["personId"].toString().toStdString() };
|
||||
changed = true;
|
||||
}
|
||||
if (obj.contains("personIds") && obj["personIds"].isArray())
|
||||
{
|
||||
config.personIds.clear();
|
||||
QJsonArray arr = obj["personIds"].toArray();
|
||||
for (const auto &value : arr)
|
||||
{
|
||||
if (value.isString())
|
||||
config.personIds.push_back(value.toString().toStdString());
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
if (obj.contains("order") && obj["order"].isString())
|
||||
{
|
||||
config.order = obj["order"].toString().toStdString();
|
||||
changed = true;
|
||||
}
|
||||
if (obj.contains("size") && obj["size"].isString())
|
||||
{
|
||||
config.size = obj["size"].toString().toStdString();
|
||||
changed = true;
|
||||
}
|
||||
if (obj.contains("pageSize") && obj["pageSize"].isDouble())
|
||||
{
|
||||
config.pageSize = (int)obj["pageSize"].toDouble();
|
||||
changed = true;
|
||||
}
|
||||
if (obj.contains("maxAssets") && obj["maxAssets"].isDouble())
|
||||
{
|
||||
config.maxAssets = (int)obj["maxAssets"].toDouble();
|
||||
changed = true;
|
||||
}
|
||||
if (obj.contains("includeArchived"))
|
||||
{
|
||||
if (obj["includeArchived"].isBool())
|
||||
{
|
||||
config.includeArchived = obj["includeArchived"].toBool();
|
||||
changed = true;
|
||||
}
|
||||
else if (obj["includeArchived"].isString())
|
||||
{
|
||||
bool parsed = false;
|
||||
bool boolValue = false;
|
||||
parsed = ParseBooleanString(obj["includeArchived"].toString(), boolValue);
|
||||
if (parsed)
|
||||
{
|
||||
config.includeArchived = boolValue;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (obj.contains("reset") && obj["reset"].isBool() && obj["reset"].toBool())
|
||||
{
|
||||
config.albumIds.clear();
|
||||
config.personIds.clear();
|
||||
changed = true;
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
QString key;
|
||||
QString value;
|
||||
int idx = trimmed.indexOf('=');
|
||||
if (idx < 0)
|
||||
idx = trimmed.indexOf(':');
|
||||
if (idx < 0)
|
||||
idx = trimmed.indexOf(' ');
|
||||
|
||||
if (idx >= 0)
|
||||
{
|
||||
key = trimmed.left(idx).trimmed().toLower();
|
||||
value = trimmed.mid(idx + 1).trimmed();
|
||||
}
|
||||
else
|
||||
{
|
||||
key = trimmed.toLower();
|
||||
}
|
||||
|
||||
if (key == "reset" || key == "clear" || key == "all")
|
||||
{
|
||||
config.albumIds.clear();
|
||||
config.personIds.clear();
|
||||
return true;
|
||||
}
|
||||
if (key == "album" || key == "albumid")
|
||||
{
|
||||
config.albumIds = { value.toStdString() };
|
||||
return true;
|
||||
}
|
||||
if (key == "albums" || key == "albumids")
|
||||
{
|
||||
config.albumIds = SplitCsv(value);
|
||||
return true;
|
||||
}
|
||||
if (key == "person" || key == "personid")
|
||||
{
|
||||
config.personIds = { value.toStdString() };
|
||||
return true;
|
||||
}
|
||||
if (key == "persons" || key == "personids")
|
||||
{
|
||||
config.personIds = SplitCsv(value);
|
||||
return true;
|
||||
}
|
||||
if (key == "order")
|
||||
{
|
||||
config.order = value.toStdString();
|
||||
return true;
|
||||
}
|
||||
if (key == "size")
|
||||
{
|
||||
config.size = value.toStdString();
|
||||
return true;
|
||||
}
|
||||
if (key == "includearchived")
|
||||
{
|
||||
bool boolValue = false;
|
||||
if (ParseBooleanString(value, boolValue))
|
||||
{
|
||||
config.includeArchived = boolValue;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (key == "pagesize")
|
||||
{
|
||||
bool ok = false;
|
||||
int parsed = value.toInt(&ok);
|
||||
if (ok)
|
||||
{
|
||||
config.pageSize = parsed;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (key == "maxassets")
|
||||
{
|
||||
bool ok = false;
|
||||
int parsed = value.toInt(&ok);
|
||||
if (ok)
|
||||
{
|
||||
config.maxAssets = parsed;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
@@ -292,6 +505,32 @@ int main(int argc, char *argv[])
|
||||
std::unique_ptr<ImageSelector> newSelector = GetSelectorForApp(appConfig);
|
||||
switcher.restart(newSelector);
|
||||
});
|
||||
QObject::connect(mqttController.get(), &MqttController::immichControl, [&appConfig, &switcher](const QString &payload) {
|
||||
bool updated = false;
|
||||
for (int i = 0; i < appConfig.paths.count(); ++i)
|
||||
{
|
||||
if (!appConfig.paths[i].immich.enabled)
|
||||
continue;
|
||||
ImmichConfig newConfig = appConfig.paths[i].immich;
|
||||
if (ApplyImmichPayload(newConfig, payload))
|
||||
{
|
||||
appConfig.paths[i].immich = newConfig;
|
||||
updated = true;
|
||||
}
|
||||
}
|
||||
if (updated)
|
||||
{
|
||||
Log("MQTT immich update applied.");
|
||||
std::unique_ptr<ImageSelector> newSelector = GetSelectorForApp(appConfig);
|
||||
switcher.setImageSelector(newSelector);
|
||||
if (!switcher.isPaused())
|
||||
switcher.scheduleImageUpdate();
|
||||
}
|
||||
else
|
||||
{
|
||||
Log("MQTT immich update ignored: ", payload.toStdString());
|
||||
}
|
||||
});
|
||||
mqttController->start();
|
||||
}
|
||||
switcher.start();
|
||||
|
||||
@@ -18,6 +18,8 @@ MqttController::MqttController(const MqttConfig &configIn, QObject *parent)
|
||||
mosquitto_lib_init();
|
||||
g_mqttInitialized = true;
|
||||
}
|
||||
controlTopic = QString::fromStdString(config.topic);
|
||||
immichTopic = QString::fromStdString(config.immichTopic);
|
||||
}
|
||||
|
||||
MqttController::~MqttController()
|
||||
@@ -103,6 +105,18 @@ void MqttController::subscribe()
|
||||
{
|
||||
Log("MQTT subscribed to ", config.topic);
|
||||
}
|
||||
if (!config.immichTopic.empty() && config.immichTopic != config.topic)
|
||||
{
|
||||
rc = mosquitto_subscribe(client, nullptr, config.immichTopic.c_str(), config.qos);
|
||||
if (rc != MOSQ_ERR_SUCCESS)
|
||||
{
|
||||
Log("MQTT subscribe (immich) failed: ", mosquitto_strerror(rc));
|
||||
}
|
||||
else
|
||||
{
|
||||
Log("MQTT subscribed to ", config.immichTopic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MqttController::HandleMessage(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
|
||||
@@ -113,15 +127,26 @@ void MqttController::HandleMessage(struct mosquitto *mosq, void *userdata, const
|
||||
return;
|
||||
|
||||
QString payload = QString::fromUtf8(static_cast<const char *>(message->payload), message->payloadlen);
|
||||
QMetaObject::invokeMethod(self, "handleCommand", Qt::QueuedConnection, Q_ARG(QString, payload));
|
||||
QString topic = QString::fromUtf8(message->topic);
|
||||
QMetaObject::invokeMethod(self, "handleMessage", Qt::QueuedConnection,
|
||||
Q_ARG(QString, topic),
|
||||
Q_ARG(QString, payload));
|
||||
}
|
||||
|
||||
void MqttController::handleCommand(const QString &payload)
|
||||
void MqttController::handleMessage(const QString &topic, const QString &payload)
|
||||
{
|
||||
QString cmd = payload.trimmed().toLower();
|
||||
if (cmd.isEmpty())
|
||||
return;
|
||||
|
||||
Log("MQTT message on ", topic.toStdString(), ": ", cmd.toStdString());
|
||||
|
||||
if (!immichTopic.isEmpty() && topic == immichTopic)
|
||||
{
|
||||
emit immichControl(payload);
|
||||
return;
|
||||
}
|
||||
|
||||
if (cmd == "play" || cmd == "resume")
|
||||
{
|
||||
emit play();
|
||||
|
||||
@@ -24,9 +24,10 @@ signals:
|
||||
void nextImage();
|
||||
void nextFolder();
|
||||
void restart();
|
||||
void immichControl(const QString &payload);
|
||||
|
||||
private slots:
|
||||
void handleCommand(const QString &payload);
|
||||
void handleMessage(const QString &topic, const QString &payload);
|
||||
|
||||
private:
|
||||
static void HandleConnect(struct mosquitto *mosq, void *userdata, int rc);
|
||||
@@ -35,6 +36,8 @@ private:
|
||||
void subscribe();
|
||||
|
||||
MqttConfig config;
|
||||
QString controlTopic;
|
||||
QString immichTopic;
|
||||
struct mosquitto *client = nullptr;
|
||||
bool connected = false;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user