Constat de banc du 2026-08-09 : un SetLoadConfig posant enabled: false sur une
charge alors au palier 3500 W détruisait l'adaptateur en laissant les TROIS RELAIS
FERMÉS, juste avant une intervention de câblage. Plus personne ne les commandait ;
ils y seraient restés indéfiniment.
applySafeState(now) est ajouté à ILoadAdapter, PURE VIRTUELLE : l'état sûr est
propre à chaque adaptateur et une formulation « tout couper » serait fausse.
- RelayRouter : tous relais ouverts
- EtmVariableLoadAdapter : consigne 0 W
- SgReadyAdapter : ÉTAT 2 (normal, mains off) — JAMAIS l'état 1. Bloquer
une PAC n'est pas la mettre en sécurité, c'est arrêter
le chauffage sans raison visible (SAFETY.md).
- EvAdapter : sans effet, il n'est pas construit depuis LoadConfig.
L'application passe par le chemin d'action NORMAL avec force = true, celui du
mode dégradé L2 : le mécanisme de contournement des verrous existait déjà.
ORDRE, et c'est le point qui dépendait d'ECS-410 : l'état sûr est appliqué AVANT
la destruction, et la destruction passe par deleteLater(). Les écritures d'ECS-410
sont asynchrones avec `this` en contexte de connexion — détruire immédiatement
couperait les acquittements en vol, et on ne saurait pas si la mise en sécurité a
abouti, précisément dans le cas où elle échoue.
PÉRIMÈTRE BORNÉ. Rien de tout cela à l'arrêt du plugin ni au redémarrage de
nymead : l'état doit y être CONSERVÉ, c'est ce qu'ECS-411 relit, et couper l'eau
chaude à chaque redémarrage de service serait une régression. La désactivation est
un acte délibéré de l'opérateur ; un redémarrage n'en est pas un. Les charges
CONSERVÉES par le rebuild incrémental ne passent pas par ce chemin.
Test testEcsDisableLeavesSafeState, avec son CAS NÉGATIF en premier : un rebuild
qui ne change que le rang ne coupe rien — sans lui, ECS-412 serait annulé et
chaque changement de priorité couperait la charge. Puis le cas positif :
désactivation, relais ouvert, et il le reste même sous surplus au cycle suivant.
Build amd64 0 erreur. Simulation : 17/17.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
150 lines
5.2 KiB
C++
150 lines
5.2 KiB
C++
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Copyright (C) 2025 - 2026, Patrick Schurig / ETM PowerSync
|
|
|
|
#include "etmvariableloadadapter.h"
|
|
#include "plugininfo.h"
|
|
|
|
#include <QDateTime>
|
|
#include <algorithm>
|
|
#include <integrations/thingmanager.h>
|
|
#include <integrations/thing.h>
|
|
#include <types/action.h>
|
|
#include <types/param.h>
|
|
|
|
EtmVariableLoadAdapter::EtmVariableLoadAdapter(ThingManager *thingManager,
|
|
const QString &id,
|
|
const QString &label,
|
|
const QList<int> &powerLevels,
|
|
int maxPowerW,
|
|
int priority,
|
|
const LoadNeeds &needs,
|
|
QObject *parent)
|
|
: QObject(parent)
|
|
, m_thingManager(thingManager)
|
|
, m_id(id)
|
|
, m_label(label)
|
|
, m_powerLevels(powerLevels)
|
|
, m_maxPowerW(maxPowerW)
|
|
, m_priority(priority)
|
|
, m_needs(needs)
|
|
{
|
|
// Re-tri par sécurité : l'UI émet trié, l'energymanager re-trie (contrat §6 cas limites).
|
|
std::sort(m_powerLevels.begin(), m_powerLevels.end());
|
|
// Contrat §2 : les paliers incluent 0. En *fixed* (liste non vide), 0 doit être présent.
|
|
Q_ASSERT(m_powerLevels.isEmpty() || m_powerLevels.first() == 0);
|
|
}
|
|
|
|
LoadDescriptor EtmVariableLoadAdapter::descriptor() const
|
|
{
|
|
LoadDescriptor d;
|
|
d.id = m_id;
|
|
d.label = m_label;
|
|
d.adapter = QStringLiteral("etmvariableload");
|
|
d.priority = m_priority;
|
|
|
|
d.declared.powerLevels = m_powerLevels;
|
|
d.declared.maxPowerW = m_maxPowerW;
|
|
d.needs = m_needs;
|
|
|
|
d.supportedKinds = { LoadAction::Setpoint };
|
|
return d;
|
|
}
|
|
|
|
LoadTelemetry EtmVariableLoadAdapter::telemetry() const
|
|
{
|
|
LoadTelemetry t;
|
|
Thing *thing = m_thingManager->findConfiguredThing(ThingId(m_id));
|
|
t.available = (thing != nullptr);
|
|
t.currentPowerW = readCurrentPowerW(); // juge runtime (contrat §4)
|
|
t.lastActionAt = m_lastActionAt;
|
|
return t;
|
|
}
|
|
|
|
LoadContext EtmVariableLoadAdapter::toLoadContext(const QDateTime &now) const
|
|
{
|
|
Q_UNUSED(now) // aucune fenêtre de verrou côté moteur : l'anti-rebond vit dans le thing.
|
|
|
|
LoadContext ctx;
|
|
ctx.id = m_id;
|
|
ctx.adapter = QStringLiteral("etmvariableload");
|
|
ctx.label = m_label;
|
|
ctx.priority = m_priority;
|
|
ctx.declared = descriptor().declared;
|
|
|
|
ctx.telemetry.currentPowerW = readCurrentPowerW();
|
|
return ctx;
|
|
}
|
|
|
|
LoadAction EtmVariableLoadAdapter::applyAction(const LoadAction &action, const QDateTime &now)
|
|
{
|
|
if (action.kind != LoadAction::Setpoint)
|
|
return action;
|
|
|
|
if (action.reason.isEmpty()) {
|
|
qCWarning(dcNymeaEnergy()) << "[EtmVariableLoadAdapter]" << m_label
|
|
<< "— LoadAction sans reason rejetée.";
|
|
return action;
|
|
}
|
|
|
|
// Second filet (invariant ILoadAdapter) : borner la consigne au plafond physique.
|
|
// L'arrondi au powerLevels (mode *fixed*) est fait par le scheduler (contrat §4, T3).
|
|
const double setpointW = qBound(0.0, action.powerW, static_cast<double>(m_maxPowerW));
|
|
|
|
qCInfo(dcNymeaEnergy()) << "[EtmVariableLoadAdapter]" << m_label
|
|
<< "→ setpoint" << qRound(setpointW) << "W"
|
|
<< (action.force ? "(force)" : "")
|
|
<< "|" << action.reason;
|
|
|
|
writeSetpoint(setpointW);
|
|
|
|
m_currentSetpointW = setpointW;
|
|
m_lastActionAt = now;
|
|
|
|
LoadAction applied = action;
|
|
applied.powerW = setpointW;
|
|
applied.estimatedPowerW = setpointW;
|
|
return applied;
|
|
}
|
|
|
|
void EtmVariableLoadAdapter::applySafeState(const QDateTime &now)
|
|
{
|
|
LoadAction sur;
|
|
sur.loadId = m_id;
|
|
sur.kind = LoadAction::Setpoint;
|
|
sur.powerW = 0;
|
|
sur.force = true;
|
|
sur.reason = QStringLiteral("Charge désactivée — mise en état sûr (0 W) avant retrait (ECS-413)");
|
|
applyAction(sur, now);
|
|
}
|
|
|
|
// ---- privé ---------------------------------------------------------------
|
|
|
|
double EtmVariableLoadAdapter::readCurrentPowerW() const
|
|
{
|
|
Thing *thing = m_thingManager->findConfiguredThing(ThingId(m_id));
|
|
if (!thing)
|
|
return 0.0;
|
|
if (thing->thingClass().stateTypes().findByName("currentPowerW").id().isNull())
|
|
return 0.0;
|
|
return thing->stateValue("currentPowerW").toDouble();
|
|
}
|
|
|
|
void EtmVariableLoadAdapter::writeSetpoint(double powerW)
|
|
{
|
|
Thing *thing = m_thingManager->findConfiguredThing(ThingId(m_id));
|
|
if (!thing) {
|
|
qCWarning(dcNymeaEnergy()) << "[EtmVariableLoadAdapter]" << m_label
|
|
<< "— thing non trouvé:" << m_id;
|
|
return;
|
|
}
|
|
|
|
StateType setpointStateType = thing->thingClass().stateTypes().findByName("powerSetpoint");
|
|
if (!setpointStateType.id().isNull()) {
|
|
Action setpointAction(setpointStateType.id(), thing->id(), Action::TriggeredByRule);
|
|
setpointAction.setParams(ParamList() << Param(setpointStateType.id(), powerW));
|
|
m_thingManager->executeAction(setpointAction);
|
|
} else {
|
|
thing->setStateValue("powerSetpoint", powerW); // repli mock
|
|
}
|
|
}
|