Prima versione di ThinQ: stato e comandi della lavasciuga via cloud LG
- elenco delle macchine e stato in lettura, fasi tradotte in italiano - avvio, stop, spegnimento, ripresa e stop ritardato da 3 a 19 ore - chiave di accesso custodita solo sul telefono, nel file privato dell'app - 44 verifiche automatiche sul nucleo (scripts/test-core.sh) - interfaccia italiana o inglese secondo la lingua del telefono
This commit is contained in:
commit
b5f6e79146
32 files changed
+2859
No files matched your search
+180
@@ -0,0 +1,180 @@
|
||||
// Banco di prova del nucleo: gira su desktop con Qt 5.15, senza kit Sailfish.
|
||||
// Verifica il parsing sullo stato VERO di una lavasciuga LG e la coerenza dei
|
||||
// comandi. Le asserzioni sulle stringhe sono scritte in inglese (lingua
|
||||
// sorgente); con il catalogo italiano caricato si verifica anche la traduzione.
|
||||
#include <QCoreApplication>
|
||||
#include <QTranslator>
|
||||
#include <QDebug>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
|
||||
#include "thinqjson.h"
|
||||
|
||||
static int failures = 0;
|
||||
static int checks = 0;
|
||||
|
||||
static void check(bool condition, const QString &what, const QString &detail = QString())
|
||||
{
|
||||
++checks;
|
||||
if (condition) {
|
||||
qInfo().noquote() << " ok " << what;
|
||||
} else {
|
||||
++failures;
|
||||
qWarning().noquote() << " FAIL " << what << (detail.isEmpty() ? QString() : "-> " + detail);
|
||||
}
|
||||
}
|
||||
|
||||
static void checkEq(const QString &got, const QString &expected, const QString &what)
|
||||
{
|
||||
check(got == expected, what, "got: " + got + " | atteso: " + expected);
|
||||
}
|
||||
|
||||
// Stato reale letto dalla lavasciuga (response di GET /devices/{id}/state).
|
||||
static QByteArray realState()
|
||||
{
|
||||
const QString json = QStringLiteral(
|
||||
"[{\"runState\":{\"currentState\":\"DRYING\"},"
|
||||
"\"remoteControlEnable\":{\"remoteControlEnabled\":true},"
|
||||
"\"timer\":{\"remainHour\":1,\"remainMinute\":54,\"relativeHourToStop\":0,"
|
||||
"\"relativeMinuteToStop\":0,\"totalHour\":4,\"totalMinute\":47},"
|
||||
"\"cycle\":{\"cycleCount\":4},"
|
||||
"\"location\":{\"locationName\":\"MAIN\"}}]");
|
||||
return json.toUtf8();
|
||||
}
|
||||
|
||||
static void testParsing()
|
||||
{
|
||||
qInfo() << "== parsing dello stato reale ==";
|
||||
const WasherState s = ThinQJson::parseState(realState());
|
||||
check(s.valid, "stato valido");
|
||||
checkEq(s.phase, "DRYING", "fase");
|
||||
check(s.remainMinutes == 114, "tempo rimanente 114 min", QString::number(s.remainMinutes));
|
||||
check(s.totalMinutes == 287, "tempo totale 287 min", QString::number(s.totalMinutes));
|
||||
check(s.cycleCount == 4, "quarto ciclo", QString::number(s.cycleCount));
|
||||
check(s.remoteEnabled, "comando a distanza attivo");
|
||||
check(s.running(), "considerata in corso");
|
||||
|
||||
qInfo() << "== casi limite ==";
|
||||
const WasherState empty = ThinQJson::parseState("[]");
|
||||
check(!empty.valid, "risposta vuota -> nessun dato");
|
||||
const WasherState junk = ThinQJson::parseState("{\"errore\":true}");
|
||||
check(!junk.valid, "json non riconosciuto -> nessun dato");
|
||||
|
||||
// La posizione MAIN va scelta anche quando non e' la prima della lista.
|
||||
const QByteArray twoLocations = QStringLiteral(
|
||||
"[{\"runState\":{\"currentState\":\"END\"},\"location\":{\"locationName\":\"MINI\"}},"
|
||||
"{\"runState\":{\"currentState\":\"RUNNING\"},\"location\":{\"locationName\":\"MAIN\"}}]")
|
||||
.toUtf8();
|
||||
checkEq(ThinQJson::parseState(twoLocations).phase, "RUNNING", "sceglie la posizione MAIN");
|
||||
}
|
||||
|
||||
static void testCommands()
|
||||
{
|
||||
qInfo() << "== comandi ==";
|
||||
WasherState s;
|
||||
s.valid = true;
|
||||
|
||||
s.phase = QStringLiteral("DRYING");
|
||||
checkEq(ThinQJson::primaryAction(s), "STOP", "in asciugatura si ferma");
|
||||
check(ThinQJson::statusLevel(s) == 1, "livello 1 (in corso)");
|
||||
checkEq(ThinQJson::primaryActionLabel(s), "Stop", "etichetta Stop");
|
||||
|
||||
s.phase = QStringLiteral("END");
|
||||
checkEq(ThinQJson::primaryAction(s), "START", "a fine ciclo si avvia");
|
||||
check(ThinQJson::statusLevel(s) == 3, "livello 3 (finita)");
|
||||
|
||||
s.phase = QStringLiteral("PAUSE");
|
||||
checkEq(ThinQJson::primaryActionLabel(s), "Resume", "in pausa si riprende");
|
||||
check(ThinQJson::statusLevel(s) == 2, "livello 2 (in pausa)");
|
||||
|
||||
s.phase = QStringLiteral("ERROR");
|
||||
checkEq(ThinQJson::primaryAction(s), "POWER_OFF", "in errore si spegne");
|
||||
check(ThinQJson::statusLevel(s) == 4, "livello 4 (errore)");
|
||||
|
||||
s.phase = QStringLiteral("POWER_OFF");
|
||||
check(ThinQJson::statusLevel(s) == 0, "livello 0 (spenta)");
|
||||
|
||||
checkEq(ThinQJson::operationPayload("START"),
|
||||
"{\"operation\":{\"washerOperationMode\":\"START\"}}", "corpo del comando avvio");
|
||||
checkEq(ThinQJson::operationPayload("POWER_OFF"),
|
||||
"{\"operation\":{\"washerOperationMode\":\"POWER_OFF\"}}", "corpo dello spegnimento");
|
||||
checkEq(ThinQJson::delayPayload(5), "{\"timer\":{\"relativeHourToStop\":5}}",
|
||||
"corpo dello stop ritardato");
|
||||
}
|
||||
|
||||
static void testCloud()
|
||||
{
|
||||
qInfo() << "== indirizzi del cloud ==";
|
||||
checkEq(ThinQJson::regionFor("IT"), "eic", "Italia -> eic");
|
||||
checkEq(ThinQJson::regionFor("it"), "eic", "paese minuscolo");
|
||||
checkEq(ThinQJson::regionFor("US"), "aic", "Stati Uniti -> aic");
|
||||
checkEq(ThinQJson::regionFor("KR"), "kic", "Corea -> kic");
|
||||
checkEq(ThinQJson::regionFor("XX"), "kic", "paese sconosciuto -> kic");
|
||||
checkEq(ThinQJson::regionFor(""), "eic", "paese vuoto -> eic (Europa)");
|
||||
checkEq(ThinQJson::apiBase("IT"), "https://api-eic.lgthinq.com", "indirizzo per l'Italia");
|
||||
|
||||
const QString id = ThinQJson::messageId();
|
||||
check(id.length() == 22, "message-id di 22 caratteri", QString::number(id.length()));
|
||||
check(!id.contains(QLatin1Char('=')), "message-id senza riempimento");
|
||||
check(id != ThinQJson::messageId(), "message-id diversi ad ogni chiamata");
|
||||
|
||||
// Senza queste due intestazioni il cloud LG risponde 401 con corpo vuoto:
|
||||
// e' stato il difetto che impediva all'app di trovare le macchine.
|
||||
checkEq(ThinQJson::servicePhase(), "OP", "x-service-phase");
|
||||
check(ThinQJson::apiKey().length() == 40, "x-api-key di 40 caratteri",
|
||||
QString::number(ThinQJson::apiKey().length()));
|
||||
check(!ThinQJson::apiKey().contains(QLatin1Char(' ')), "x-api-key senza spazi");
|
||||
}
|
||||
|
||||
static void testDurations()
|
||||
{
|
||||
qInfo() << "== durate ==";
|
||||
checkEq(ThinQJson::durationText(114), "1 h 54 min", "1 h 54 min");
|
||||
checkEq(ThinQJson::durationText(60), "1 h", "un'ora esatta");
|
||||
checkEq(ThinQJson::durationText(45), "45 min", "solo minuti");
|
||||
checkEq(ThinQJson::durationText(0), "", "zero -> vuoto");
|
||||
}
|
||||
|
||||
static void testItalian(const QString &catalog)
|
||||
{
|
||||
qInfo() << "== catalogo italiano ==";
|
||||
if (catalog.isEmpty()) {
|
||||
qWarning() << " SALTO: nessun .qm passato come argomento";
|
||||
return;
|
||||
}
|
||||
QTranslator tr;
|
||||
if (!tr.load(catalog)) {
|
||||
++failures;
|
||||
qWarning() << " FAIL impossibile caricare" << catalog;
|
||||
return;
|
||||
}
|
||||
QCoreApplication::installTranslator(&tr);
|
||||
|
||||
WasherState s;
|
||||
s.valid = true;
|
||||
s.phase = QStringLiteral("DRYING");
|
||||
checkEq(ThinQJson::phaseLabel(s), "In asciugatura", "fase tradotta");
|
||||
s.phase = QStringLiteral("END");
|
||||
checkEq(ThinQJson::phaseLabel(s), "Fine", "fine tradotta");
|
||||
checkEq(ThinQJson::primaryActionLabel(s), "Avvia", "tasto tradotto");
|
||||
s.phase = QStringLiteral("PAUSE");
|
||||
checkEq(ThinQJson::primaryActionLabel(s), "Riprendi", "riprendi tradotto");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication app(argc, argv);
|
||||
const QString catalog = argc > 1 ? QString::fromLocal8Bit(argv[1]) : QString();
|
||||
|
||||
testParsing();
|
||||
testCommands();
|
||||
testCloud();
|
||||
testDurations();
|
||||
testItalian(catalog);
|
||||
|
||||
qInfo().noquote() << QString("\n%1 verifiche, %2 fallite").arg(checks).arg(failures);
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
Reference in new issue
Block a user