PATCH /elements/circuit/<id> : consigne et mode auto, avec signal de synchro vers l'Appli
This commit is contained in:
@@ -567,6 +567,65 @@ int main(int argc, char *argv[])
|
||||
return withCors(QHttpServerResponse(QJsonDocument(reponse).toJson()));
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// PATCH /elements/circuit/<id>
|
||||
// Écriture rapide (bouton d'accueil) - PAS le formulaire de config
|
||||
// complet (config_circuit.qml, avec orchestration esclave/maître,
|
||||
// §20.7) qui reste un futur endpoint séparé. Reproduit fidèlement
|
||||
// changement_consigne()/toggle_cdes_auto() (identiques dans circuit.qml
|
||||
// et Shipheart_bouton_circuit.qml).
|
||||
// ------------------------------------------------------------------
|
||||
server.route("/elements/circuit/<arg>", QHttpServerRequest::Method::Patch,
|
||||
[](int idCircuit, const QHttpServerRequest &request) {
|
||||
const QJsonDocument body = QJsonDocument::fromJson(request.body());
|
||||
if (!body.isObject()) {
|
||||
QJsonObject err;
|
||||
err["status"] = "erreur";
|
||||
err["message"] = "corps de requête JSON invalide";
|
||||
return withCors(QHttpServerResponse(QJsonDocument(err).toJson(), QHttpServerResponder::StatusCode::BadRequest));
|
||||
}
|
||||
const QJsonObject obj = body.object();
|
||||
bool ecritureEffectuee = false;
|
||||
|
||||
// Changement de consigne :
|
||||
if (obj.contains("consigne")) {
|
||||
int consigne = obj["consigne"].toInt();
|
||||
if (consigne < 0) consigne = 0;
|
||||
if (consigne > 100) consigne = 100;
|
||||
|
||||
QString req = QString("UPDATE TB_Circuits SET New_consigne=%1 ").arg(consigne);
|
||||
if (obj.contains("old_consigne")) {
|
||||
req += QString(", Old_consigne=%1 ").arg(obj["old_consigne"].toInt());
|
||||
}
|
||||
req += QString("WHERE Cle_primaire='%1'").arg(idCircuit);
|
||||
QSqlQuery q(req);
|
||||
ecritureEffectuee = true;
|
||||
}
|
||||
|
||||
// Bascule du mode "commandes automatiques" (Events) :
|
||||
if (obj.contains("validation_cdes_events")) {
|
||||
const int mode = obj["validation_cdes_events"].toInt() != 0 ? 1 : 0;
|
||||
QSqlQuery q(QString("UPDATE TB_Circuits SET Validation_cdes_events=%1 WHERE Cle_primaire='%2'").arg(mode).arg(idCircuit));
|
||||
ecritureEffectuee = true;
|
||||
}
|
||||
|
||||
if (!ecritureEffectuee) {
|
||||
QJsonObject err;
|
||||
err["status"] = "erreur";
|
||||
err["message"] = "aucun champ reconnu (attendu : consigne, validation_cdes_events)";
|
||||
return withCors(QHttpServerResponse(QJsonDocument(err).toJson(), QHttpServerResponder::StatusCode::BadRequest));
|
||||
}
|
||||
|
||||
// Signale à l'Appli qu'un changement est en attente, pour un
|
||||
// traitement rapide (100ms maxi) plutôt que son cycle normal
|
||||
// (cf synchro_bdd(shipheart_type_synchro_data) du natif) :
|
||||
QSqlQuery("UPDATE TB_Systeme SET Valeur=1 WHERE Nom=\"Synchro\"");
|
||||
|
||||
QJsonObject reponse;
|
||||
reponse["status"] = "ok";
|
||||
return withCors(QHttpServerResponse(QJsonDocument(reponse).toJson()));
|
||||
});
|
||||
|
||||
auto tcpServer = new QTcpServer();
|
||||
if (!tcpServer->listen(QHostAddress::Any, 8080) || !server.bind(tcpServer)) {
|
||||
qCritical() << "Impossible de démarrer le serveur HTTP sur le port 8080.";
|
||||
|
||||
Reference in New Issue
Block a user