harbour-server 0.1.0: schermata parametri server (schema status/1)
Prima release dell'app Sailfish: schermata unica con CPU, memoria, disco, container, backup, servizi, SSH, aggiornamenti e VPN, letti da un endpoint read-only /v1/status. Soglie giallo/rosso, aggiornamento ogni 60 s, copertina, impostazioni con indirizzo e token.
This commit is contained in:
commit
24e1f3b376
20 files changed
+1112
No files matched your search
@@ -0,0 +1,32 @@
|
||||
#include <QGuiApplication>
|
||||
#include <QQmlContext>
|
||||
#include <QQuickView>
|
||||
#include <QDebug>
|
||||
#include <sailfishapp.h>
|
||||
|
||||
#include "settings.h"
|
||||
#include "statusclient.h"
|
||||
|
||||
Q_DECL_EXPORT int main(int argc, char *argv[])
|
||||
{
|
||||
QGuiApplication *app = SailfishApp::application(argc, argv);
|
||||
|
||||
// Identita' allineata al profilo X-Sailjail del desktop file:
|
||||
// AppConfigLocation = ~/.config/harbour/server (zona persistente).
|
||||
app->setOrganizationName(QStringLiteral("harbour"));
|
||||
app->setApplicationName(QStringLiteral("server"));
|
||||
app->setApplicationVersion(QStringLiteral("0.1.0"));
|
||||
|
||||
qDebug() << "harbour-server v0.1.0 build" << __DATE__ << __TIME__;
|
||||
|
||||
Settings settings;
|
||||
StatusClient status(&settings);
|
||||
|
||||
QQuickView *view = SailfishApp::createView();
|
||||
view->rootContext()->setContextProperty(QStringLiteral("appSettings"), &settings);
|
||||
view->rootContext()->setContextProperty(QStringLiteral("statusClient"), &status);
|
||||
view->setSource(SailfishApp::pathTo(QStringLiteral("qml/harbour-server.qml")));
|
||||
view->show();
|
||||
|
||||
return app->exec();
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
#include "settings.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QStandardPaths>
|
||||
|
||||
static const char *kKeyBaseUrl = "server/baseUrl";
|
||||
static const char *kKeyToken = "server/token";
|
||||
|
||||
QString Settings::configPath()
|
||||
{
|
||||
// AppConfigLocation = ~/.config/harbour/server con org=harbour,
|
||||
// app=server (vedi main.cpp e X-Sailjail nel desktop file). Il QSettings
|
||||
// "di default" scriverebbe in ~/.config/harbour/server.conf, FUORI dalla
|
||||
// directory persistente della sandbox: le impostazioni andrebbero perse a
|
||||
// ogni riavvio (bug gia' visto in harbour-hermes).
|
||||
const QString dir = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
|
||||
QDir().mkpath(dir);
|
||||
return dir + QStringLiteral("/server.conf");
|
||||
}
|
||||
|
||||
Settings::Settings(QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_settings(configPath(), QSettings::IniFormat)
|
||||
{
|
||||
m_baseUrl = m_settings.value(QLatin1String(kKeyBaseUrl)).toString();
|
||||
m_token = m_settings.value(QLatin1String(kKeyToken)).toString();
|
||||
qDebug() << "Settings file:" << m_settings.fileName();
|
||||
qDebug() << "Settings baseUrl=[" << m_baseUrl << "] token set:" << !m_token.isEmpty();
|
||||
}
|
||||
|
||||
QString Settings::baseUrl() const
|
||||
{
|
||||
return m_baseUrl;
|
||||
}
|
||||
|
||||
QString Settings::token() const
|
||||
{
|
||||
return m_token;
|
||||
}
|
||||
|
||||
void Settings::setBaseUrl(const QString &value)
|
||||
{
|
||||
QString v = value.trimmed();
|
||||
while (v.endsWith(QLatin1Char('/')))
|
||||
v.chop(1);
|
||||
if (v == m_baseUrl)
|
||||
return;
|
||||
m_baseUrl = v;
|
||||
m_settings.setValue(QLatin1String(kKeyBaseUrl), m_baseUrl);
|
||||
m_settings.sync();
|
||||
emit baseUrlChanged();
|
||||
}
|
||||
|
||||
void Settings::setToken(const QString &value)
|
||||
{
|
||||
const QString v = value.trimmed();
|
||||
if (v == m_token)
|
||||
return;
|
||||
m_token = v;
|
||||
m_settings.setValue(QLatin1String(kKeyToken), m_token);
|
||||
m_settings.sync();
|
||||
emit tokenChanged();
|
||||
}
|
||||
|
||||
void Settings::save(const QString &baseUrl, const QString &token)
|
||||
{
|
||||
setBaseUrl(baseUrl);
|
||||
setToken(token);
|
||||
m_settings.sync();
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#ifndef SETTINGS_H
|
||||
#define SETTINGS_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QSettings>
|
||||
#include <QString>
|
||||
|
||||
// Impostazioni dell'app. Nessun indirizzo compilato nel binario: il server si
|
||||
// configura la prima volta che si apre l'app.
|
||||
//
|
||||
// Il file vive in AppConfigLocation (~/.config/harbour/server/server.conf),
|
||||
// l'unica zona che la sandbox SailJail rende persistente quando org/app sono
|
||||
// dichiarati sia nel main.cpp sia in X-Sailjail del desktop file.
|
||||
class Settings : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString baseUrl READ baseUrl WRITE setBaseUrl NOTIFY baseUrlChanged)
|
||||
Q_PROPERTY(QString token READ token WRITE setToken NOTIFY tokenChanged)
|
||||
|
||||
public:
|
||||
explicit Settings(QObject *parent = nullptr);
|
||||
|
||||
QString baseUrl() const;
|
||||
QString token() const;
|
||||
|
||||
void setBaseUrl(const QString &value);
|
||||
void setToken(const QString &value);
|
||||
|
||||
Q_INVOKABLE void save(const QString &baseUrl, const QString &token);
|
||||
|
||||
signals:
|
||||
void baseUrlChanged();
|
||||
void tokenChanged();
|
||||
|
||||
private:
|
||||
static QString configPath();
|
||||
|
||||
QSettings m_settings;
|
||||
QString m_baseUrl;
|
||||
QString m_token;
|
||||
};
|
||||
|
||||
#endif // SETTINGS_H
|
||||
@@ -0,0 +1,189 @@
|
||||
#include "statusclient.h"
|
||||
#include "settings.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QDebug>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonParseError>
|
||||
#include <QLocale>
|
||||
#include <QNetworkRequest>
|
||||
#include <QUrl>
|
||||
|
||||
static const int kTimeoutMs = 8000;
|
||||
|
||||
StatusClient::StatusClient(Settings *settings, QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_settings(settings)
|
||||
, m_busy(false)
|
||||
{
|
||||
m_watchdog.setSingleShot(true);
|
||||
connect(&m_watchdog, SIGNAL(timeout()), this, SLOT(onTimeout()));
|
||||
}
|
||||
|
||||
QVariantMap StatusClient::data() const { return m_data; }
|
||||
bool StatusClient::busy() const { return m_busy; }
|
||||
QString StatusClient::error() const { return m_error; }
|
||||
QString StatusClient::fetchedAt() const { return m_fetchedAt; }
|
||||
|
||||
bool StatusClient::configured() const
|
||||
{
|
||||
return !m_settings->baseUrl().trimmed().isEmpty();
|
||||
}
|
||||
|
||||
void StatusClient::setBusy(bool value)
|
||||
{
|
||||
if (m_busy == value)
|
||||
return;
|
||||
m_busy = value;
|
||||
emit busyChanged();
|
||||
}
|
||||
|
||||
void StatusClient::setError(const QString &message)
|
||||
{
|
||||
if (m_error == message)
|
||||
return;
|
||||
m_error = message;
|
||||
emit errorChanged();
|
||||
emit changed();
|
||||
}
|
||||
|
||||
void StatusClient::clearReply()
|
||||
{
|
||||
if (m_reply) {
|
||||
m_reply->disconnect(this);
|
||||
m_reply->deleteLater();
|
||||
m_reply = nullptr;
|
||||
}
|
||||
m_watchdog.stop();
|
||||
}
|
||||
|
||||
void StatusClient::refresh()
|
||||
{
|
||||
if (m_busy)
|
||||
return;
|
||||
|
||||
const QString base = m_settings->baseUrl().trimmed();
|
||||
if (base.isEmpty()) {
|
||||
setError(tr("Indica l'indirizzo del server nelle impostazioni"));
|
||||
return;
|
||||
}
|
||||
|
||||
const QUrl url(base + QStringLiteral("/v1/status"));
|
||||
if (!url.isValid() || url.scheme().isEmpty() || url.host().isEmpty()) {
|
||||
setError(tr("Indirizzo non valido: serve schema e host (es. http://192.168.3.21:8792)"));
|
||||
return;
|
||||
}
|
||||
|
||||
QNetworkRequest request(url);
|
||||
request.setRawHeader("Accept", "application/json");
|
||||
const QString token = m_settings->token().trimmed();
|
||||
if (!token.isEmpty())
|
||||
request.setRawHeader("X-Status-Token", token.toUtf8());
|
||||
|
||||
setError(QString());
|
||||
setBusy(true);
|
||||
|
||||
m_reply = m_nam.get(request);
|
||||
connect(m_reply, SIGNAL(finished()), this, SLOT(onFinished()));
|
||||
m_watchdog.start(kTimeoutMs);
|
||||
qDebug() << "[status] GET" << url.toString();
|
||||
}
|
||||
|
||||
void StatusClient::onTimeout()
|
||||
{
|
||||
if (!m_reply)
|
||||
return;
|
||||
qDebug() << "[status] timeout";
|
||||
clearReply();
|
||||
setBusy(false);
|
||||
setError(tr("Server non raggiungibile (timeout)"));
|
||||
}
|
||||
|
||||
void StatusClient::onFinished()
|
||||
{
|
||||
if (!m_reply)
|
||||
return;
|
||||
|
||||
QNetworkReply *reply = m_reply;
|
||||
m_watchdog.stop();
|
||||
m_reply = nullptr;
|
||||
reply->disconnect(this);
|
||||
reply->deleteLater();
|
||||
|
||||
const int httpStatus =
|
||||
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
setBusy(false);
|
||||
if (httpStatus == 401)
|
||||
setError(tr("Token non valido: controlla le impostazioni"));
|
||||
else if (httpStatus == 0)
|
||||
setError(tr("Server non raggiungibile: %1").arg(reply->errorString()));
|
||||
else
|
||||
setError(tr("Errore dal server (HTTP %1)").arg(httpStatus));
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonParseError parseError;
|
||||
const QJsonDocument doc = QJsonDocument::fromJson(reply->readAll(), &parseError);
|
||||
if (parseError.error != QJsonParseError::NoError || !doc.isObject()) {
|
||||
setBusy(false);
|
||||
setError(tr("Risposta non leggibile dal server"));
|
||||
return;
|
||||
}
|
||||
|
||||
const QJsonObject obj = doc.object();
|
||||
const QString schema = obj.value(QStringLiteral("schema")).toString();
|
||||
if (!schema.startsWith(QStringLiteral("status/"))) {
|
||||
setBusy(false);
|
||||
setError(tr("Il server non parla il formato status (schema assente)"));
|
||||
return;
|
||||
}
|
||||
|
||||
m_data = obj.toVariantMap();
|
||||
m_fetchedAt = QLocale::system().toString(QDateTime::currentDateTime(), QStringLiteral("HH:mm:ss"));
|
||||
setBusy(false);
|
||||
emit changed();
|
||||
emit busyChanged();
|
||||
qDebug() << "[status] ok, schema" << schema;
|
||||
}
|
||||
|
||||
QString StatusClient::fmtBytes(qint64 bytes) const
|
||||
{
|
||||
const double gb = double(bytes) / (1024.0 * 1024.0 * 1024.0);
|
||||
if (gb >= 1.0)
|
||||
return QLocale::system().toString(gb, 'f', 1) + QStringLiteral(" GB");
|
||||
const double mb = double(bytes) / (1024.0 * 1024.0);
|
||||
return QLocale::system().toString(mb, 'f', 0) + QStringLiteral(" MB");
|
||||
}
|
||||
|
||||
QString StatusClient::fmtUptime(qint64 seconds) const
|
||||
{
|
||||
if (seconds <= 0)
|
||||
return QString();
|
||||
const qint64 days = seconds / 86400;
|
||||
const qint64 hours = (seconds % 86400) / 3600;
|
||||
const qint64 minutes = (seconds % 3600) / 60;
|
||||
if (days > 0)
|
||||
return tr("%1 g %2 h").arg(days).arg(hours);
|
||||
if (hours > 0)
|
||||
return tr("%1 h %2 min").arg(hours).arg(minutes);
|
||||
return tr("%1 min").arg(minutes);
|
||||
}
|
||||
|
||||
QString StatusClient::fmtAge(double hours) const
|
||||
{
|
||||
if (hours < 0)
|
||||
return QString();
|
||||
if (hours < 1.0)
|
||||
return tr("%1 min fa").arg(qMax(1, int(hours * 60.0)));
|
||||
if (hours < 48.0)
|
||||
return tr("%1 h fa").arg(QLocale::system().toString(hours, 'f', 1));
|
||||
return tr("%1 giorni fa").arg(QLocale::system().toString(hours / 24.0, 'f', 1));
|
||||
}
|
||||
|
||||
QString StatusClient::fmtNum(double value, int decimals) const
|
||||
{
|
||||
return QLocale::system().toString(value, 'f', decimals);
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
#ifndef STATUSCLIENT_H
|
||||
#define STATUSCLIENT_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QPointer>
|
||||
#include <QString>
|
||||
#include <QTimer>
|
||||
#include <QVariantMap>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
|
||||
class Settings;
|
||||
|
||||
// Scarica lo snapshot `status/1` dal collector e lo espone al QML.
|
||||
//
|
||||
// QNetworkAccessManager non applica timeout: una richiesta senza risposta
|
||||
// lascerebbe la UI in attesa per sempre, quindi c'e' un watchdog che aborta
|
||||
// dopo kTimeoutMs e riporta un errore visibile.
|
||||
class StatusClient : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QVariantMap data READ data NOTIFY changed)
|
||||
Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)
|
||||
Q_PROPERTY(QString error READ error NOTIFY errorChanged)
|
||||
Q_PROPERTY(QString fetchedAt READ fetchedAt NOTIFY changed)
|
||||
Q_PROPERTY(bool configured READ configured NOTIFY changed)
|
||||
|
||||
public:
|
||||
explicit StatusClient(Settings *settings, QObject *parent = nullptr);
|
||||
|
||||
QVariantMap data() const;
|
||||
bool busy() const;
|
||||
QString error() const;
|
||||
QString fetchedAt() const;
|
||||
bool configured() const;
|
||||
|
||||
Q_INVOKABLE void refresh();
|
||||
|
||||
// Formattazione lato C++: lo JS del QML di Qt 5.6 e' ES5 e le locale
|
||||
// numeriche italiane (virgola decimale) qui sono affidabili.
|
||||
Q_INVOKABLE QString fmtBytes(qint64 bytes) const;
|
||||
Q_INVOKABLE QString fmtUptime(qint64 seconds) const;
|
||||
Q_INVOKABLE QString fmtAge(double hours) const;
|
||||
Q_INVOKABLE QString fmtNum(double value, int decimals) const;
|
||||
|
||||
signals:
|
||||
void changed();
|
||||
void busyChanged();
|
||||
void errorChanged();
|
||||
|
||||
private slots:
|
||||
void onFinished();
|
||||
void onTimeout();
|
||||
|
||||
private:
|
||||
void setBusy(bool value);
|
||||
void setError(const QString &message);
|
||||
void clearReply();
|
||||
|
||||
Settings *m_settings;
|
||||
QNetworkAccessManager m_nam;
|
||||
QPointer<QNetworkReply> m_reply;
|
||||
QTimer m_watchdog;
|
||||
QVariantMap m_data;
|
||||
QString m_error;
|
||||
QString m_fetchedAt;
|
||||
bool m_busy;
|
||||
};
|
||||
|
||||
#endif // STATUSCLIENT_H
|
||||
Reference in new issue
Block a user