- home: temperatura esterna e in casa/assente in alto, messaggio di stato, scheda impianto con temperatura stanza e acqua calda; comandi sotto - pagina Dati: calore prodotto, elettricita' usata, COP acqua calda e riscaldamento, risparmio, CO2 evitata, Smart Tariff Control (mese per mese) - pagina Impianto: stato, termostato a parete, versioni software, errori - bridge 0.4.0: rotta /stats (servizio statistiche del cloud) e blocco dispositivo in /summary (ID breve, versioni, termostato) - C++: parseStats, formatNumber, formatMoney, monthLabel (separatore e valuta secondo la lingua); 61/61 verifiche del nucleo - anteprime aggiornate (dati, impianto) in inglese e italiano
80 lines
3.0 KiB
C++
80 lines
3.0 KiB
C++
#ifndef APICLIENT_H
|
|
#define APICLIENT_H
|
|
|
|
#include <QNetworkAccessManager>
|
|
#include <QObject>
|
|
#include <QVariant>
|
|
#include <QVariantMap>
|
|
|
|
class QNetworkReply;
|
|
class Settings;
|
|
|
|
// Client HTTP verso il bridge sul server (~/workspace/Carlo/Aira/aira_bridge.py).
|
|
// Non parla con il cloud Aira: Qt 5.6 non ha HTTP/2, quindi il gRPC resta sul
|
|
// server e qui arrivano solo JSON.
|
|
class ApiClient : public QObject
|
|
{
|
|
Q_OBJECT
|
|
Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)
|
|
Q_PROPERTY(bool configured READ configured NOTIFY configuredChanged)
|
|
// Ultimo riassunto letto. Sta in C++ (context property) e non nel QML:
|
|
// le property dell'ApplicationWindow NON sono visibili alle pagine dello
|
|
// pageStack, quindi la dashboard e la copertina non potrebbero condividerle.
|
|
Q_PROPERTY(QVariantMap summary READ summary NOTIFY summaryChanged)
|
|
// Statistiche della pagina "Dati" (calore, elettricita', COP, risparmi).
|
|
// Separata dal riassunto: e' una chiamata piu' lenta, con cache lato bridge.
|
|
Q_PROPERTY(QVariantMap stats READ stats NOTIFY statsChanged)
|
|
|
|
public:
|
|
explicit ApiClient(Settings *settings, QObject *parent = 0);
|
|
|
|
bool busy() const { return m_busy; }
|
|
bool configured() const;
|
|
QVariantMap summary() const { return m_summary; }
|
|
QVariantMap stats() const { return m_stats; }
|
|
|
|
// Scarica il riassunto e lo emette con summaryChanged().
|
|
Q_INVOKABLE void refresh();
|
|
|
|
// Scarica le statistiche (GET /stats) e le emette con statsChanged().
|
|
Q_INVOKABLE void refreshStats();
|
|
|
|
// Prova GET /health (non richiede token) e risponde con connectionTested().
|
|
Q_INVOKABLE void testConnection();
|
|
|
|
// Invia un comando: sendCommand("set_hot_water_target", 55).
|
|
Q_INVOKABLE void sendCommand(const QString &id, const QVariant &value = QVariant());
|
|
|
|
// Formattazione per la UI: unica fonte di verita' in C++ (testata in
|
|
// tests/core_test.cpp), niente duplicazione di logica nel QML.
|
|
Q_INVOKABLE QString modeLabel(const QString &operatingStatus) const;
|
|
Q_INVOKABLE QString formatValue(const QVariant &value) const;
|
|
Q_INVOKABLE QString formatNumber(const QVariant &value, int decimals) const;
|
|
Q_INVOKABLE QString formatMoney(const QVariant &value, const QString ¤cyCode) const;
|
|
Q_INVOKABLE QString monthLabel(const QString &yearMonth) const;
|
|
|
|
signals:
|
|
void busyChanged();
|
|
void configuredChanged();
|
|
void summaryChanged(const QVariantMap &summary);
|
|
void statsChanged(const QVariantMap &stats);
|
|
void statsRefreshed(bool ok, const QString &message);
|
|
void commandFinished(bool ok, const QString &message);
|
|
void connectionTested(bool ok, const QString &message);
|
|
void errorOccurred(const QString &message);
|
|
|
|
private:
|
|
QNetworkReply *send(const QByteArray &verb, const QString &path,
|
|
const QByteArray &body, int timeoutSec);
|
|
void armTimeout(QNetworkReply *reply, int seconds);
|
|
void setBusy(bool value);
|
|
|
|
Settings *m_settings;
|
|
QNetworkAccessManager m_nam;
|
|
bool m_busy;
|
|
QVariantMap m_summary;
|
|
QVariantMap m_stats;
|
|
};
|
|
|
|
#endif // APICLIENT_H
|