Schermata principale con tutte le macchine, avviso di fine lavaggio, IT/EN

- la chiave si chiede una volta sola: poi la schermata principale mostra
  ogni macchina dell'account col suo stato e il suo comando
- avviso di sistema a fine lavaggio, anche se il ciclo e' finito mentre
  l'app era chiusa (si annota il numero di ciclo gia' avvisato)
- comandi per singola macchina: avvio/stop, spegnimento, risveglio,
  stop ritardato (nel menu della macchina, tocco prolungato sul nome)
- le macchine non raggiungibili vengono segnate come non collegate,
  senza riempire la barra dei messaggi ad ogni aggiornamento
- multilingua italiano/inglese, catalogo verificato dal banco di prova
  (52 controlli)
This commit is contained in:
kaneda committed 2026-10-12 12:38:10 +02:00
1 parent fe961b438c
commit e2c6f28613
14 files changed
+1015 -541

No files matched your search

+2 -2
View File
@@ -21,7 +21,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
app->setOrganizationName(QStringLiteral("harbour"));
app->setApplicationName(QStringLiteral("thinq"));
app->setApplicationVersion(QStringLiteral("0.1.2"));
app->setApplicationVersion(QStringLiteral("0.2.0"));
Settings settings;
ThinQClient api(&settings);
@@ -34,7 +34,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
QQuickView *view = SailfishApp::createView();
// Prima riga del log: dice se il device sta girando la build nuova o una stale.
qDebug() << "harbour-thinq v0.1.2 build" << __DATE__ << __TIME__;
qDebug() << "harbour-thinq v0.2.0 build" << __DATE__ << __TIME__;
view->rootContext()->setContextProperty(QStringLiteral("appSettings"), &settings);
view->rootContext()->setContextProperty(QStringLiteral("api"), &api);
+15
View File
@@ -104,3 +104,18 @@ void Settings::setLanguage(const QString &value)
m_settings.sync();
emit languageChanged();
}
int Settings::notifiedCycleFor(const QString &deviceId) const
{
if (deviceId.isEmpty())
return 0;
return m_settings.value(QStringLiteral("notified/") + deviceId, 0).toInt();
}
void Settings::setNotifiedCycleFor(const QString &deviceId, int cycle)
{
if (deviceId.isEmpty() || cycle <= 0)
return;
m_settings.setValue(QStringLiteral("notified/") + deviceId, cycle);
m_settings.sync();
}
+6
View File
@@ -41,6 +41,12 @@ public:
QString language() const { return m_language; }
void setLanguage(const QString &value);
// Ciclo gia' avvisato per ciascuna macchina: serve a non ripetere l'avviso
// di fine lavaggio, e a recuperarlo se l'app era chiusa quando la macchina
// ha finito.
int notifiedCycleFor(const QString &deviceId) const;
void setNotifiedCycleFor(const QString &deviceId, int cycle);
signals:
void tokenChanged();
void countryChanged();
+281 -99
View File
@@ -3,7 +3,6 @@
#include "settings.h"
#include <QCoreApplication>
#include <QDateTime>
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
@@ -27,72 +26,145 @@ ThinQClient::ThinQClient(Settings *settings, QObject *parent)
, m_settings(settings)
{
m_pollTimer.setSingleShot(true);
// Una richiesta per volta: il timer riparte dopo ogni risposta (vedi scheduleNextPoll).
// Una macchina per volta: ogni risposta fa partire la richiesta successiva
// (pumpQueue), cosi' l'app non bombarda il cloud LG.
connect(&m_pollTimer, &QTimer::timeout, this, [this]() { refresh(); });
connect(m_settings, &Settings::tokenChanged, this, [this]() {
m_state = WasherState();
m_stale = false;
m_updated.clear();
emit infoChanged();
m_devices.clear();
m_queue.clear();
m_stateInFlight = false;
setBusy(false);
emit devicesChanged();
emit stateChanged();
});
connect(m_settings, &Settings::deviceChanged, this, [this]() {
m_state = WasherState();
emit infoChanged();
emit stateChanged();
refresh();
});
}
bool ThinQClient::configured() const
{
return !m_settings->token().isEmpty() && !m_settings->deviceId().isEmpty();
return !m_settings->token().isEmpty();
}
QString ThinQClient::alias() const
int ThinQClient::activeIndex() const
{
const QString a = m_settings->deviceAlias();
return a.isEmpty() ? QCoreApplication::translate("ThinQ", "Washing machine") : a;
for (int i = 0; i < m_devices.size(); ++i) {
if (ThinQJson::statusLevel(m_devices.at(i).state) == 1)
return i;
}
for (int i = 0; i < m_devices.size(); ++i) {
if (m_devices.at(i).hasState)
return i;
}
return m_devices.isEmpty() ? -1 : 0;
}
QString ThinQClient::phaseText() const
bool ThinQClient::validIndex(int index) const
{
if (!m_state.valid)
return index >= 0 && index < m_devices.size();
}
QString ThinQClient::labelAt(int index) const
{
if (!validIndex(index))
return QString();
const QString label = m_devices.at(index).label;
return label.isEmpty()
? QCoreApplication::translate("ThinQ", "Washing machine")
: label;
}
bool ThinQClient::hasStateAt(int index) const
{
return validIndex(index) && m_devices.at(index).hasState;
}
bool ThinQClient::offlineAt(int index) const
{
return validIndex(index) && m_devices.at(index).offline;
}
bool ThinQClient::staleAt(int index) const
{
return validIndex(index) && m_devices.at(index).stale;
}
QString ThinQClient::phaseTextAt(int index) const
{
if (!hasStateAt(index))
return QCoreApplication::translate("ThinQ", "No data");
return ThinQJson::phaseLabel(m_state);
return ThinQJson::phaseLabel(m_devices.at(index).state);
}
int ThinQClient::statusLevel() const
int ThinQClient::statusLevelAt(int index) const
{
return ThinQJson::statusLevel(m_state);
if (!validIndex(index))
return -1;
return ThinQJson::statusLevel(m_devices.at(index).state);
}
QString ThinQClient::remainText() const
QString ThinQClient::remainTextAt(int index) const
{
return ThinQJson::durationText(m_state.remainMinutes);
if (!validIndex(index))
return QString();
return ThinQJson::durationText(m_devices.at(index).state.remainMinutes);
}
QString ThinQClient::totalText() const
QString ThinQClient::totalTextAt(int index) const
{
return ThinQJson::durationText(m_state.totalMinutes);
if (!validIndex(index))
return QString();
return ThinQJson::durationText(m_devices.at(index).state.totalMinutes);
}
QString ThinQClient::primaryLabel() const
QString ThinQClient::totalLineAt(int index) const
{
if (!m_state.valid)
const QString total = totalTextAt(index);
if (total.isEmpty())
return QString();
return QCoreApplication::translate("ThinQ", "of %1").arg(total);
}
int ThinQClient::cycleCountAt(int index) const
{
if (!validIndex(index))
return 0;
return m_devices.at(index).state.cycleCount;
}
bool ThinQClient::remoteEnabledAt(int index) const
{
if (!hasStateAt(index))
return false;
return m_devices.at(index).state.remoteEnabled;
}
QString ThinQClient::primaryLabelAt(int index) const
{
if (!hasStateAt(index))
return QCoreApplication::translate("ThinQ", "Start");
return ThinQJson::primaryActionLabel(m_state);
return ThinQJson::primaryActionLabel(m_devices.at(index).state);
}
bool ThinQClient::canCommand() const
bool ThinQClient::canCommandAt(int index) const
{
return configured() && m_state.valid && m_state.remoteEnabled;
if (!hasStateAt(index))
return false;
const DeviceEntry &e = m_devices.at(index);
return !e.offline && e.state.remoteEnabled;
}
QString ThinQClient::updatedText() const
QString ThinQClient::updatedTextAt(int index) const
{
return m_updated;
if (!validIndex(index))
return QString();
return m_devices.at(index).updated;
}
QString ThinQClient::deviceIdAt(int index) const
{
if (!validIndex(index))
return QString();
return m_devices.at(index).id;
}
void ThinQClient::setDelayHours(int hours)
@@ -143,7 +215,11 @@ void ThinQClient::startPolling()
{
if (!configured())
return;
refresh();
// Primo avvio con la chiave gia' salvata: prima le macchine, poi lo stato.
if (m_devices.isEmpty())
discover();
else
refresh();
if (!m_pollTimer.isActive()) {
m_pollTimer.start(m_pollInterval);
emit pollingChanged();
@@ -162,61 +238,90 @@ void ThinQClient::scheduleNextPoll()
m_pollTimer.start(m_pollInterval);
}
void ThinQClient::queueAllStates()
{
m_queue.clear();
for (int i = 0; i < m_devices.size(); ++i)
m_queue << i;
}
void ThinQClient::pumpQueue()
{
if (m_stateInFlight || !configured())
return;
if (m_devices.isEmpty()) {
scheduleNextPoll();
return;
}
if (m_queue.isEmpty()) {
scheduleNextPoll();
return;
}
const int index = m_queue.takeFirst();
if (!validIndex(index)) {
pumpQueue();
return;
}
m_stateInFlight = true;
setBusy(true);
get(QStringLiteral("devices/%1/state").arg(m_devices.at(index).id), KindState, index);
}
void ThinQClient::refresh()
{
if (!configured())
return;
get(QStringLiteral("devices/%1/state").arg(m_settings->deviceId()), KindState);
if (m_devices.isEmpty()) {
discover();
return;
}
queueAllStates();
pumpQueue();
}
void ThinQClient::discover()
{
if (m_settings->token().isEmpty()) {
if (!configured()) {
setNotice(QCoreApplication::translate("ThinQ", "Enter the access token first"), true);
return;
}
get(QStringLiteral("devices"), KindDevices);
}
void ThinQClient::chooseDevice(int index)
void ThinQClient::runPrimaryAt(int index)
{
if (index < 0 || index >= m_deviceIds.size())
return;
const QString id = m_deviceIds.at(index);
const QString label = m_deviceLabels.at(index);
m_selected = index;
m_settings->setDeviceAlias(label);
m_settings->setDeviceId(id);
setNotice(QCoreApplication::translate("ThinQ", "Machine selected"), false);
emit devicesChanged();
}
void ThinQClient::runPrimary()
{
if (!m_state.valid) {
if (!hasStateAt(index)) {
setNotice(QCoreApplication::translate("ThinQ", "No data from the machine yet"), true);
return;
}
post(QStringLiteral("devices/%1/control").arg(m_settings->deviceId()),
ThinQJson::operationPayload(ThinQJson::primaryAction(m_state)));
post(QStringLiteral("devices/%1/control").arg(m_devices.at(index).id),
ThinQJson::operationPayload(ThinQJson::primaryAction(m_devices.at(index).state)), index);
}
void ThinQClient::powerOff()
void ThinQClient::powerOffAt(int index)
{
post(QStringLiteral("devices/%1/control").arg(m_settings->deviceId()),
ThinQJson::operationPayload(QStringLiteral("POWER_OFF")));
if (!validIndex(index))
return;
post(QStringLiteral("devices/%1/control").arg(m_devices.at(index).id),
ThinQJson::operationPayload(QStringLiteral("POWER_OFF")), index);
}
void ThinQClient::wakeUp()
void ThinQClient::wakeUpAt(int index)
{
post(QStringLiteral("devices/%1/control").arg(m_settings->deviceId()),
ThinQJson::operationPayload(QStringLiteral("WAKE_UP")));
if (!validIndex(index))
return;
post(QStringLiteral("devices/%1/control").arg(m_devices.at(index).id),
ThinQJson::operationPayload(QStringLiteral("WAKE_UP")), index);
}
void ThinQClient::sendDelay()
void ThinQClient::sendDelayAt(int index)
{
post(QStringLiteral("devices/%1/control").arg(m_settings->deviceId()),
ThinQJson::delayPayload(m_delayHours));
if (!validIndex(index))
return;
post(QStringLiteral("devices/%1/control").arg(m_devices.at(index).id),
ThinQJson::delayPayload(m_delayHours), index);
}
void ThinQClient::prepare(QNetworkRequest &request, bool command) const
@@ -237,13 +342,13 @@ void ThinQClient::prepare(QNetworkRequest &request, bool command) const
request.setRawHeader("x-conditional-control", "true");
}
void ThinQClient::get(const QString &endpoint, RequestKind kind)
void ThinQClient::get(const QString &endpoint, RequestKind kind, int deviceIndex)
{
const QUrl url(ThinQJson::apiBase(m_settings->country()) + QLatin1Char('/') + endpoint);
QNetworkRequest request(url);
prepare(request, /*command=*/false);
if (kind == KindState || kind == KindCommand)
if (kind == KindDevices)
setBusy(true);
QNetworkReply *reply = m_nam.get(request);
@@ -255,18 +360,23 @@ void ThinQClient::get(const QString &endpoint, RequestKind kind)
connect(guard, &QTimer::timeout, reply, &QNetworkReply::abort);
guard->start(kRequestTimeoutMs);
connect(reply, &QNetworkReply::finished, this, [this, reply, kind]() {
connect(reply, &QNetworkReply::finished, this, [this, reply, kind, deviceIndex]() {
reply->deleteLater();
const QByteArray body = reply->readAll();
const int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (kind == KindState)
setBusy(false);
m_stateInFlight = false;
if (reply->error() != QNetworkReply::NoError || status < 200 || status >= 300) {
const QString netErr = reply->error() == QNetworkReply::NoError
? QString() : reply->errorString();
handleFailure(status, body, netErr, kind == KindCommand);
setBusy(false);
handleFailure(status, body, netErr, kind == KindCommand, deviceIndex);
if (kind == KindState)
pumpQueue(); // una macchina muta non blocca le altre
else
scheduleNextPoll();
return;
}
@@ -276,8 +386,8 @@ void ThinQClient::get(const QString &endpoint, RequestKind kind)
: QJsonValue(doc.isArray() ? QJsonValue(doc.array()) : QJsonValue());
if (kind == KindDevices) {
m_deviceLabels.clear();
m_deviceIds.clear();
setBusy(false);
m_devices.clear();
const QJsonArray arr = response.toArray();
for (int i = 0; i < arr.size(); ++i) {
const QJsonObject dev = arr.at(i).toObject();
@@ -287,43 +397,61 @@ void ThinQClient::get(const QString &endpoint, RequestKind kind)
&& !type.startsWith(QLatin1String("DEVICE_WASHCOMBO"))
&& !type.startsWith(QLatin1String("DEVICE_WASHTOWER"))
&& !type.startsWith(QLatin1String("DEVICE_DRYER"))) {
continue; // l'app controlla lavatrici e lavasciughe
continue; // questa app comanda lavatrici e lavasciughe
}
QString label = info.value(QStringLiteral("alias")).toString();
if (label.isEmpty())
label = info.value(QStringLiteral("modelName")).toString();
m_deviceLabels << label;
m_deviceIds << dev.value(QStringLiteral("deviceId")).toString();
DeviceEntry entry;
entry.id = dev.value(QStringLiteral("deviceId")).toString();
entry.label = info.value(QStringLiteral("alias")).toString();
if (entry.label.isEmpty())
entry.label = info.value(QStringLiteral("modelName")).toString();
m_devices << entry;
}
m_selected = m_deviceIds.indexOf(m_settings->deviceId());
emit devicesChanged();
if (m_deviceLabels.isEmpty())
if (m_devices.isEmpty()) {
setNotice(QCoreApplication::translate("ThinQ", "No washing machine found on this account"), true);
else
setNotice(QCoreApplication::translate("ThinQ", "Machines found: %1").arg(m_deviceLabels.size()), false);
scheduleNextPoll();
} else {
setNotice(QCoreApplication::translate("ThinQ", "Machines found: %1").arg(m_devices.size()), false);
queueAllStates();
pumpQueue();
}
return;
}
if (kind == KindState) {
setBusy(false);
if (!validIndex(deviceIndex)) {
pumpQueue();
return;
}
const bool hadState = m_devices.at(deviceIndex).hasState;
const int previousLevel = hadState
? ThinQJson::statusLevel(m_devices.at(deviceIndex).state) : -1;
const QByteArray payload = QJsonDocument(response.toArray()).toJson(QJsonDocument::Compact);
applyState(ThinQJson::parseState(payload));
applyState(deviceIndex, hadState, previousLevel, ThinQJson::parseState(payload));
pumpQueue();
return;
}
// kind == KindCommand
m_pendingCommand = false;
setBusy(false);
setNotice(QCoreApplication::translate("ThinQ", "Command sent"), false);
refresh();
});
}
void ThinQClient::post(const QString &endpoint, const QString &payload)
void ThinQClient::post(const QString &endpoint, const QString &payload, int deviceIndex)
{
if (!configured()) {
setNotice(QCoreApplication::translate("ThinQ", "Set the access token and the machine first"), true);
setNotice(QCoreApplication::translate("ThinQ", "The access token is missing"), true);
return;
}
if (!m_state.valid || !m_state.remoteEnabled) {
if (!validIndex(deviceIndex)) {
setNotice(QCoreApplication::translate("ThinQ", "No machine to command"), true);
return;
}
const DeviceEntry &entry = m_devices.at(deviceIndex);
if (!entry.hasState || !entry.state.remoteEnabled) {
setNotice(QCoreApplication::translate("ThinQ", "Remote start is off on the machine: switch it on from its panel"), true);
return;
}
@@ -332,7 +460,6 @@ void ThinQClient::post(const QString &endpoint, const QString &payload)
QNetworkRequest request(url);
prepare(request, /*command=*/true);
m_pendingCommand = true;
setBusy(true);
QNetworkReply *reply = m_nam.post(request, payload.toUtf8());
@@ -341,7 +468,7 @@ void ThinQClient::post(const QString &endpoint, const QString &payload)
connect(guard, &QTimer::timeout, reply, &QNetworkReply::abort);
guard->start(kRequestTimeoutMs);
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
connect(reply, &QNetworkReply::finished, this, [this, reply, deviceIndex]() {
reply->deleteLater();
const QByteArray body = reply->readAll();
const int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
@@ -350,7 +477,7 @@ void ThinQClient::post(const QString &endpoint, const QString &payload)
if (reply->error() != QNetworkReply::NoError || status < 200 || status >= 300) {
const QString netErr = reply->error() == QNetworkReply::NoError
? QString() : reply->errorString();
handleFailure(status, body, netErr, true);
handleFailure(status, body, netErr, true, deviceIndex);
return;
}
setNotice(QCoreApplication::translate("ThinQ", "Command sent"), false);
@@ -358,20 +485,63 @@ void ThinQClient::post(const QString &endpoint, const QString &payload)
});
}
void ThinQClient::applyState(const WasherState &state)
void ThinQClient::applyState(int deviceIndex, bool hadState, int previousLevel,
const WasherState &state)
{
m_state = state;
m_stale = false;
m_updated = QCoreApplication::translate("ThinQ", "Updated at %1").arg(
if (!validIndex(deviceIndex))
return;
DeviceEntry &entry = m_devices[deviceIndex];
entry.state = state;
entry.hasState = state.valid;
entry.offline = false;
entry.stale = false;
entry.updated = QCoreApplication::translate("ThinQ", "Updated at %1").arg(
QLocale().toString(QTime::currentTime(), QLocale::ShortFormat));
if (m_pollInterval != kPollFastMs)
m_pollInterval = kPollFastMs;
emit stateChanged();
scheduleNextPoll();
noteFinished(deviceIndex, hadState, previousLevel);
}
void ThinQClient::noteFinished(int deviceIndex, bool hadState, int previousLevel)
{
if (!validIndex(deviceIndex))
return;
const DeviceEntry &entry = m_devices.at(deviceIndex);
const int level = ThinQJson::statusLevel(entry.state);
const int saved = m_settings->notifiedCycleFor(entry.id);
if (level == 3) {
// Appena finito sotto i nostri occhi, oppure finito mentre l'app era
// chiusa (il numero di ciclo e' avanzato rispetto all'ultimo avvisato).
const bool justEnded = hadState && (previousLevel == 1 || previousLevel == 2);
const bool missedWhileClosed = saved > 0 && entry.state.cycleCount > saved;
if (justEnded || missedWhileClosed) {
m_settings->setNotifiedCycleFor(entry.id, entry.state.cycleCount);
emit cycleFinished(entry.label, entry.state.cycleCount);
} else if (saved == 0) {
// Prima volta che vediamo questa macchina: si annota il ciclo senza
// avvisare, altrimenti al primo avvio scatterebbe un avviso per un
// lavaggio finito giorni fa.
m_settings->setNotifiedCycleFor(entry.id, entry.state.cycleCount);
}
return;
}
// Ciclo in corso: si annota il numero, cosi' l'avviso scattera' solo se il
// ciclo finisce davvero.
if (saved == 0 && entry.state.cycleCount > 0)
m_settings->setNotifiedCycleFor(entry.id, entry.state.cycleCount);
}
void ThinQClient::handleFailure(int httpStatus, const QByteArray &body,
const QString &networkError, bool commandRequest)
const QString &networkError, bool commandRequest,
int deviceIndex)
{
QString code;
QString message;
@@ -385,13 +555,28 @@ void ThinQClient::handleFailure(int httpStatus, const QByteArray &body,
qDebug() << "[thinq] errore LG" << httpStatus << code << message
<< (networkError.isEmpty() ? QString() : networkError);
// Macchina spenta o senza rete: si annota SULLA macchina, non sulla barra
// dei messaggi, altrimenti ogni giro di aggiornamento ripeterebbe lo
// stesso avviso.
if (!commandRequest && validIndex(deviceIndex)
&& (code == QLatin1String("1222") || code == QLatin1String("1223")
|| code == QLatin1String("1231") || code == QLatin1String("1209"))) {
DeviceEntry &entry = m_devices[deviceIndex];
entry.offline = true;
entry.hasState = false;
entry.updated.clear();
entry.stale = false;
emit stateChanged();
return;
}
if (code == QLatin1String("1103") || code == QLatin1String("1218")
|| code == QLatin1String("1302")) {
|| code == QLatin1String("1302") || httpStatus == 401) {
setNotice(QCoreApplication::translate("ThinQ", "The access token is no longer valid: enter a new one in Settings"), true);
} else if (code == QLatin1String("1306")) {
m_pollInterval = kPollSlowMs;
setNotice(QCoreApplication::translate("ThinQ", "Too many requests to LG: slowing down"), true);
} else if (code == QLatin1String("1222")) {
} else if (code == QLatin1String("1222") || code == QLatin1String("1223")) {
setNotice(QCoreApplication::translate("ThinQ", "The washing machine is not connected (switched off or offline)"), true);
} else if (code == QLatin1String("2301")) {
setNotice(QCoreApplication::translate("ThinQ", "Remote start is off on the machine: switch it on from its panel"), true);
@@ -405,13 +590,10 @@ void ThinQClient::handleFailure(int httpStatus, const QByteArray &body,
setNotice(QCoreApplication::translate("ThinQ", "The LG cloud refused the request (HTTP %1)").arg(httpStatus), true);
}
if (commandRequest)
m_pendingCommand = false;
// I dati a schermo non sono piu' freschi: dirlo, invece di mostrare i
// numeri vecchi come se fossero attuali.
if (m_state.valid) {
m_stale = true;
if (validIndex(deviceIndex) && m_devices.at(deviceIndex).hasState) {
m_devices[deviceIndex].stale = true;
emit stateChanged();
}
scheduleNextPoll();
+58 -49
View File
@@ -1,6 +1,7 @@
#ifndef THINQCLIENT_H
#define THINQCLIENT_H
#include <QList>
#include <QNetworkAccessManager>
#include <QObject>
#include <QStringList>
@@ -11,66 +12,62 @@
class Settings;
class QNetworkReply;
// Parla direttamente con il cloud LG ThinQ Connect: nessun ponte, nessun
// server di mezzo. Il token di accesso sta nelle impostazioni dell'app.
// Tutte le macchine dell'account, ognuna col suo stato. Una sola classe parla
// col cloud LG: nessun ponte, nessun server di mezzo, nessun servizio.
//
// Il QML legge per indice (labelAt/phaseTextAt/...): l'elenco cambia raramente e
// cosi' le stesse funzioni servono la schermata principale e la copertina.
class ThinQClient : public QObject
{
Q_OBJECT
Q_PROPERTY(bool configured READ configured NOTIFY infoChanged)
Q_PROPERTY(QString alias READ alias NOTIFY infoChanged)
Q_PROPERTY(bool hasState READ hasState NOTIFY stateChanged)
Q_PROPERTY(QString phaseText READ phaseText NOTIFY stateChanged)
Q_PROPERTY(int statusLevel READ statusLevel NOTIFY stateChanged)
Q_PROPERTY(QString remainText READ remainText NOTIFY stateChanged)
Q_PROPERTY(QString totalText READ totalText NOTIFY stateChanged)
Q_PROPERTY(int cycleCount READ cycleCount NOTIFY stateChanged)
Q_PROPERTY(bool remoteEnabled READ remoteEnabled NOTIFY stateChanged)
Q_PROPERTY(QString primaryLabel READ primaryLabel NOTIFY stateChanged)
Q_PROPERTY(bool canCommand READ canCommand NOTIFY stateChanged)
Q_PROPERTY(QString updatedText READ updatedText NOTIFY stateChanged)
Q_PROPERTY(bool stale READ stale NOTIFY stateChanged)
Q_PROPERTY(int deviceCount READ deviceCount NOTIFY devicesChanged)
Q_PROPERTY(int activeIndex READ activeIndex NOTIFY stateChanged)
Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)
Q_PROPERTY(bool polling READ polling NOTIFY pollingChanged)
Q_PROPERTY(QString notice READ notice NOTIFY noticeChanged)
Q_PROPERTY(bool noticeIsError READ noticeIsError NOTIFY noticeChanged)
Q_PROPERTY(QStringList deviceLabels READ deviceLabels NOTIFY devicesChanged)
Q_PROPERTY(int deviceCount READ deviceCount NOTIFY devicesChanged)
Q_PROPERTY(int selectedDevice READ selectedDevice NOTIFY devicesChanged)
Q_PROPERTY(int delayHours READ delayHours WRITE setDelayHours NOTIFY delayHoursChanged)
public:
explicit ThinQClient(Settings *settings, QObject *parent = nullptr);
// La chiave di accesso c'e': la schermata principale mostra i dispositivi
// invece di chiedere le credenziali.
bool configured() const;
QString alias() const;
bool hasState() const { return m_state.valid; }
QString phaseText() const;
int statusLevel() const;
QString remainText() const;
QString totalText() const;
int cycleCount() const { return m_state.cycleCount; }
bool remoteEnabled() const { return m_state.remoteEnabled; }
QString primaryLabel() const;
bool canCommand() const;
QString updatedText() const;
bool stale() const { return m_stale; }
int deviceCount() const { return m_devices.size(); }
// La macchina che sta lavorando (per la copertina), altrimenti la prima.
int activeIndex() const;
bool busy() const { return m_busy; }
bool polling() const { return m_pollTimer.isActive(); }
QString notice() const { return m_notice; }
bool noticeIsError() const { return m_noticeIsError; }
QStringList deviceLabels() const { return m_deviceLabels; }
int deviceCount() const { return m_deviceLabels.size(); }
int selectedDevice() const { return m_selected; }
int delayHours() const { return m_delayHours; }
void setDelayHours(int hours);
Q_INVOKABLE QString labelAt(int index) const;
Q_INVOKABLE bool hasStateAt(int index) const;
Q_INVOKABLE bool offlineAt(int index) const;
Q_INVOKABLE bool staleAt(int index) const;
Q_INVOKABLE QString phaseTextAt(int index) const;
Q_INVOKABLE int statusLevelAt(int index) const;
Q_INVOKABLE QString remainTextAt(int index) const;
Q_INVOKABLE QString totalTextAt(int index) const;
Q_INVOKABLE QString totalLineAt(int index) const;
Q_INVOKABLE int cycleCountAt(int index) const;
Q_INVOKABLE bool remoteEnabledAt(int index) const;
Q_INVOKABLE QString primaryLabelAt(int index) const;
Q_INVOKABLE bool canCommandAt(int index) const;
Q_INVOKABLE QString updatedTextAt(int index) const;
Q_INVOKABLE QString deviceIdAt(int index) const;
Q_INVOKABLE bool validIndex(int index) const;
Q_INVOKABLE void refresh();
Q_INVOKABLE void discover();
Q_INVOKABLE void chooseDevice(int index);
Q_INVOKABLE void runPrimary();
Q_INVOKABLE void powerOff();
Q_INVOKABLE void wakeUp();
Q_INVOKABLE void sendDelay();
Q_INVOKABLE void runPrimaryAt(int index);
Q_INVOKABLE void powerOffAt(int index);
Q_INVOKABLE void wakeUpAt(int index);
Q_INVOKABLE void sendDelayAt(int index);
Q_INVOKABLE void startPolling();
Q_INVOKABLE void stopPolling();
Q_INVOKABLE void clearNotice();
@@ -85,35 +82,47 @@ signals:
void noticeChanged();
void devicesChanged();
void delayHoursChanged();
// Fine di un lavaggio: la macchina e' arrivata a END. Il QML lo trasforma in
// una notifica di sistema.
void cycleFinished(const QString &label, int cycleCount);
private:
enum RequestKind { KindDevices, KindState, KindCommand };
struct DeviceEntry {
QString id;
QString label;
WasherState state;
bool hasState = false;
bool offline = false;
bool stale = false;
QString updated;
};
void prepare(QNetworkRequest &request, bool command) const;
void get(const QString &endpoint, RequestKind kind);
void post(const QString &endpoint, const QString &payload);
void applyState(const WasherState &state);
void get(const QString &endpoint, RequestKind kind, int deviceIndex = -1);
void post(const QString &endpoint, const QString &payload, int deviceIndex);
void applyState(int deviceIndex, bool hadState, int previousLevel, const WasherState &state);
void handleFailure(int httpStatus, const QByteArray &body, const QString &networkError,
bool commandRequest);
bool commandRequest, int deviceIndex);
void setNotice(const QString &text, bool isError);
void setBusy(bool busy);
void scheduleNextPoll();
void queueAllStates();
void pumpQueue();
void noteFinished(int deviceIndex, bool hadState, int previousLevel);
Settings *m_settings;
QNetworkAccessManager m_nam;
QTimer m_pollTimer;
WasherState m_state;
QStringList m_deviceLabels;
QStringList m_deviceIds;
int m_selected = -1;
int m_pollInterval = 45000;
QList<DeviceEntry> m_devices;
QList<int> m_queue; // macchine da leggere, una richiesta per volta
bool m_stateInFlight = false;
int m_pollInterval = 45000; // a schermata aperta
int m_delayHours = 4;
bool m_busy = false;
bool m_stale = false;
bool m_pendingCommand = false;
QString m_notice;
bool m_noticeIsError = false;
QString m_updated;
};
#endif // THINQCLIENT_H