#ifndef THINQCLIENT_H #define THINQCLIENT_H #include #include #include #include #include #include #include #include #include "thinqjson.h" class Settings; class QNetworkReply; // Tutte le macchine dell'account, ognuna col suo stato. Una sola classe parla // col cloud LG: nessun ponte, nessun server di mezzo, nessun servizio. // // L'elenco si espone come DATO (QVariantList di mappe), non con funzioni di // lettura per indice: in QML un binding che chiama una funzione non registra // nessuna dipendenza e quindi non si aggiorna mai — le macchine si vedevano, ma // lo stato restava vuoto per sempre. class ThinQClient : public QObject { Q_OBJECT Q_PROPERTY(bool configured READ configured NOTIFY infoChanged) Q_PROPERTY(int deviceCount READ deviceCount NOTIFY devicesChanged) Q_PROPERTY(QVariantList devices READ devices NOTIFY devicesChanged) Q_PROPERTY(QVariantMap active READ active NOTIFY devicesChanged) 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(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; int deviceCount() const { return m_devices.size(); } QVariantList devices() const; QVariantMap active() 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; } int delayHours() const { return m_delayHours; } void setDelayHours(int hours); Q_INVOKABLE void refresh(); Q_INVOKABLE void discover(); 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(); // Il conto alla rovescia a schermo serve solo quando la pagina si vede: il // controllo invece continua anche con l'app in background. Q_INVOKABLE void setScreenVisible(bool visible); Q_INVOKABLE void clearNotice(); Q_INVOKABLE void reloadCredentials(); Q_INVOKABLE QString durationText(int minutes) const; signals: void infoChanged(); void stateChanged(); void busyChanged(); void pollingChanged(); 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 }; bool validIndex(int index) const; struct DeviceEntry { QString id; QString label; WasherState state; bool hasState = false; bool offline = false; bool stale = false; QString updated; // Quando lo stato e' stato letto davvero: serve a far scendere il tempo // rimanente a schermo fra un controllo e l'altro. QDateTime fetchedAt; }; void prepare(QNetworkRequest &request, bool command) const; 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, 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; QTimer m_tickTimer; // conto alla rovescia a schermo, senza rete QList m_devices; QList m_queue; // macchine da leggere, una richiesta per volta bool m_stateInFlight = false; int m_pollInterval = 1800000; // mezz'ora, e solo a schermata aperta int m_delayHours = 4; bool m_screenVisible = false; // pagina davanti agli occhi bool m_busy = false; QString m_notice; bool m_noticeIsError = false; }; #endif // THINQCLIENT_H