67 lines
2.3 KiB
C++
67 lines
2.3 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)
|
|
|
|
public:
|
|
explicit ApiClient(Settings *settings, QObject *parent = 0);
|
|
|
|
bool busy() const { return m_busy; }
|
|
bool configured() const;
|
|
QVariantMap summary() const { return m_summary; }
|
|
|
|
// Scarica il riassunto e lo emette con summaryChanged().
|
|
Q_INVOKABLE void refresh();
|
|
|
|
// 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;
|
|
|
|
signals:
|
|
void busyChanged();
|
|
void configuredChanged();
|
|
void summaryChanged(const QVariantMap &summary);
|
|
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;
|
|
};
|
|
|
|
#endif // APICLIENT_H
|