harbour-aira 0.1.0: client Sailfish per pompa di calore Aira (bridge HTTP + stato e comandi ACS)
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
#include "airajson.h"
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
#include <QUrl>
|
||||
|
||||
namespace AiraJson {
|
||||
|
||||
QString normalizeBaseUrl(const QString &raw)
|
||||
{
|
||||
QString s = raw.trimmed();
|
||||
if (s.isEmpty())
|
||||
return QString();
|
||||
|
||||
if (!s.startsWith(QLatin1String("http://")) && !s.startsWith(QLatin1String("https://")))
|
||||
s.prepend(QLatin1String("http://"));
|
||||
|
||||
while (s.endsWith(QLatin1Char('/')))
|
||||
s.chop(1);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
QVariantMap parseSummary(const QByteArray &body, bool *ok)
|
||||
{
|
||||
if (ok)
|
||||
*ok = false;
|
||||
|
||||
QJsonParseError err;
|
||||
const QJsonDocument doc = QJsonDocument::fromJson(body, &err);
|
||||
if (err.error != QJsonParseError::NoError || !doc.isObject())
|
||||
return QVariantMap();
|
||||
|
||||
const QJsonObject root = doc.object();
|
||||
if (root.value(QStringLiteral("ok")).toBool() == false)
|
||||
return QVariantMap();
|
||||
|
||||
const QJsonObject summary = root.value(QStringLiteral("summary")).toObject();
|
||||
if (summary.isEmpty())
|
||||
return QVariantMap();
|
||||
|
||||
if (ok)
|
||||
*ok = true;
|
||||
return summary.toVariantMap();
|
||||
}
|
||||
|
||||
QString modeLabel(const QString &operatingStatus)
|
||||
{
|
||||
const QString s = operatingStatus;
|
||||
if (s == QLatin1String("OPERATING_STATUS_AUTO"))
|
||||
return QStringLiteral("Automatico");
|
||||
if (s == QLatin1String("OPERATING_STATUS_HEATING"))
|
||||
return QStringLiteral("Riscaldamento");
|
||||
if (s == QLatin1String("OPERATING_STATUS_COOLING"))
|
||||
return QStringLiteral("Raffrescamento");
|
||||
if (s == QLatin1String("OPERATING_STATUS_HOT_WATER"))
|
||||
return QStringLiteral("Acqua calda");
|
||||
if (s == QLatin1String("OPERATING_STATUS_STANDBY"))
|
||||
return QStringLiteral("Stand-by");
|
||||
if (s == QLatin1String("OPERATING_STATUS_OFF"))
|
||||
return QStringLiteral("Spento");
|
||||
if (s.isEmpty())
|
||||
return QStringLiteral("—");
|
||||
// Sconosciuto: togliamo il prefisso e mostriamo il resto, minuscolo.
|
||||
QString rest = s;
|
||||
rest.remove(QLatin1String("OPERATING_STATUS_"));
|
||||
return rest.toLower();
|
||||
}
|
||||
|
||||
QString formatValue(const QVariant &value)
|
||||
{
|
||||
if (!value.isValid() || value.isNull())
|
||||
return QStringLiteral("—");
|
||||
|
||||
bool isNumber = false;
|
||||
const double d = value.toDouble(&isNumber);
|
||||
if (isNumber)
|
||||
return QString::number(d, 'f', 1);
|
||||
|
||||
const QString s = value.toString();
|
||||
return s.isEmpty() ? QStringLiteral("—") : s;
|
||||
}
|
||||
|
||||
QByteArray commandPayload(const QString &id, const QVariant &value)
|
||||
{
|
||||
QJsonObject o;
|
||||
o.insert(QStringLiteral("id"), id);
|
||||
if (value.isValid() && !value.isNull())
|
||||
o.insert(QStringLiteral("value"), QJsonValue::fromVariant(value));
|
||||
return QJsonDocument(o).toJson(QJsonDocument::Compact);
|
||||
}
|
||||
|
||||
QString errorMessage(const QByteArray &body, int httpStatus)
|
||||
{
|
||||
QJsonParseError err;
|
||||
const QJsonDocument doc = QJsonDocument::fromJson(body, &err);
|
||||
if (err.error == QJsonParseError::NoError && doc.isObject()) {
|
||||
const QString msg = doc.object().value(QStringLiteral("error")).toString();
|
||||
if (!msg.isEmpty())
|
||||
return msg;
|
||||
}
|
||||
if (httpStatus == 0)
|
||||
return QStringLiteral("server non raggiungibile");
|
||||
return QStringLiteral("errore HTTP %1").arg(httpStatus);
|
||||
}
|
||||
|
||||
} // namespace AiraJson
|
||||
Reference in New Issue
Block a user