Morceaux 0-2 implémentés et compilés (0 erreur / 0 warning) : - M0 : LoadAction.force=false (bypass verrous anti-rebond sécurité) - M1 : EcsRelayAdapter (.h+.cpp) — N paliers powerswitch, anti-rebond, etm.pri - M2 : buildContext() — SurplusMeter brut, loads EV+ECS, registerEcsAdapter() AGENTS.md : section PLAN 3C ajoutée avec corrections A+B intégrées. Corrections A (déduction EV unique dans scheduler) et B (recrédit conso propre anti-clignotement) documentées avant implémentation morceau 3. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
192 lines
6.5 KiB
C++
192 lines
6.5 KiB
C++
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Copyright (C) 2025 - 2026, Patrick Schurig / ETM PowerSync
|
|
|
|
#include "ecsrelayadapter.h"
|
|
#include "plugininfo.h"
|
|
|
|
#include <QDateTime>
|
|
#include <integrations/thingmanager.h>
|
|
#include <integrations/thing.h>
|
|
#include <types/action.h>
|
|
#include <types/param.h>
|
|
|
|
EcsRelayAdapter::EcsRelayAdapter(ThingManager *thingManager,
|
|
const QString &id,
|
|
const QString &label,
|
|
const QList<int> &stages,
|
|
const QList<QList<QString>> &relayMapping,
|
|
int minOnS,
|
|
int minOffS,
|
|
int priority,
|
|
QObject *parent)
|
|
: QObject(parent)
|
|
, m_thingManager(thingManager)
|
|
, m_id(id)
|
|
, m_label(label)
|
|
, m_stages(stages)
|
|
, m_relayMapping(relayMapping)
|
|
, m_minOnS(minOnS)
|
|
, m_minOffS(minOffS)
|
|
, m_priority(priority)
|
|
{
|
|
Q_ASSERT(!m_stages.isEmpty() && m_stages.first() == 0);
|
|
Q_ASSERT(m_relayMapping.size() == m_stages.size());
|
|
}
|
|
|
|
LoadDescriptor EcsRelayAdapter::descriptor() const
|
|
{
|
|
LoadDescriptor d;
|
|
d.id = m_id;
|
|
d.label = m_label;
|
|
d.adapter = QStringLiteral("relay-stages");
|
|
d.priority = m_priority;
|
|
|
|
d.declared.stages = m_stages;
|
|
d.limits.minOnS = m_minOnS;
|
|
d.limits.minOffS = m_minOffS;
|
|
|
|
d.supportedKinds = { LoadAction::Stage };
|
|
return d;
|
|
}
|
|
|
|
LoadTelemetry EcsRelayAdapter::telemetry() const
|
|
{
|
|
LoadTelemetry t;
|
|
t.available = true;
|
|
t.lastActionAt = m_lastActionAt;
|
|
|
|
// Puissance mesurée = somme des relais actifs pour le stage courant
|
|
double power = 0;
|
|
const QList<QString> &activeRelays = m_currentStage < m_relayMapping.size()
|
|
? m_relayMapping.at(m_currentStage)
|
|
: QList<QString>();
|
|
for (const QString &thingId : activeRelays) {
|
|
Thing *t2 = m_thingManager->findConfiguredThing(ThingId(thingId));
|
|
if (t2)
|
|
power += t2->stateValue("currentPower").toDouble();
|
|
}
|
|
// Si pas de powermetering dans le mock, on estime depuis les stages déclarés
|
|
if (power == 0 && m_currentStage > 0 && m_currentStage < m_stages.size())
|
|
power = m_stages.at(m_currentStage);
|
|
|
|
t.currentPowerW = power;
|
|
return t;
|
|
}
|
|
|
|
LoadContext EcsRelayAdapter::toLoadContext() const
|
|
{
|
|
LoadContext ctx;
|
|
ctx.id = m_id;
|
|
ctx.adapter = QStringLiteral("relay-stages");
|
|
ctx.label = m_label;
|
|
ctx.priority = m_priority;
|
|
ctx.declared = descriptor().declared;
|
|
ctx.limits = descriptor().limits;
|
|
|
|
const LoadTelemetry tel = telemetry();
|
|
ctx.telemetry.currentPowerW = tel.currentPowerW;
|
|
ctx.telemetry.stage = m_currentStage;
|
|
ctx.telemetry.lastSwitch = m_lastSwitch;
|
|
return ctx;
|
|
}
|
|
|
|
LoadAction EcsRelayAdapter::applyAction(const LoadAction &action)
|
|
{
|
|
if (action.kind != LoadAction::Stage)
|
|
return action;
|
|
|
|
if (action.reason.isEmpty()) {
|
|
qCWarning(dcNymeaEnergy()) << "[EcsRelayAdapter]" << m_label
|
|
<< "— LoadAction sans reason rejetée.";
|
|
return action;
|
|
}
|
|
|
|
const int newStage = qBound(0, action.stage, m_stages.size() - 1);
|
|
|
|
if (newStage == m_currentStage)
|
|
return action; // Aucun changement → idempotent
|
|
|
|
// Verrous anti-rebond — bypassés si force == true (L2 watchdog)
|
|
if (!action.force && lockActive(newStage)) {
|
|
qCDebug(dcNymeaEnergy()) << "[EcsRelayAdapter]" << m_label
|
|
<< "— verrou anti-rebond actif, stage" << newStage << "ignoré.";
|
|
return action;
|
|
}
|
|
|
|
qCDebug(dcNymeaEnergy()) << "[EcsRelayAdapter]" << m_label
|
|
<< "→ stage" << newStage
|
|
<< "(" << (m_currentStage < m_stages.size() ? m_stages.at(m_currentStage) : 0) << "W"
|
|
<< "→" << m_stages.at(newStage) << "W)"
|
|
<< "|" << action.reason;
|
|
|
|
applyRelayStage(newStage);
|
|
|
|
m_currentStage = newStage;
|
|
m_lastSwitch = QDateTime::currentDateTime();
|
|
m_lastActionAt = m_lastSwitch;
|
|
|
|
LoadAction applied = action;
|
|
applied.stage = newStage;
|
|
applied.estimatedPowerW = m_stages.at(newStage);
|
|
return applied;
|
|
}
|
|
|
|
// ---- privé ---------------------------------------------------------------
|
|
|
|
bool EcsRelayAdapter::lockActive(int newStage) const
|
|
{
|
|
if (!m_lastSwitch.isValid())
|
|
return false;
|
|
|
|
const int elapsed = static_cast<int>(m_lastSwitch.secsTo(QDateTime::currentDateTime()));
|
|
|
|
if (newStage > m_currentStage) {
|
|
// Passage à un palier supérieur : minOffS si l'on quitte l'état OFF (stage 0→n)
|
|
// ou simplement le délai de stabilisation
|
|
if (m_currentStage == 0 && elapsed < m_minOffS)
|
|
return true;
|
|
} else {
|
|
// Réduction de palier : minOnS depuis le dernier ON
|
|
if (m_currentStage > 0 && elapsed < m_minOnS)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void EcsRelayAdapter::applyRelayStage(int stage)
|
|
{
|
|
// Ensemble des relais ON pour le nouveau stage
|
|
const QSet<QString> wantOn = [&]() {
|
|
QSet<QString> s;
|
|
if (stage < m_relayMapping.size())
|
|
for (const QString &id : m_relayMapping.at(stage))
|
|
s.insert(id);
|
|
return s;
|
|
}();
|
|
|
|
// Union de tous les relais connus
|
|
QSet<QString> allRelays;
|
|
for (const auto &list : m_relayMapping)
|
|
for (const QString &id : list)
|
|
allRelays.insert(id);
|
|
|
|
for (const QString &thingId : allRelays) {
|
|
Thing *relay = m_thingManager->findConfiguredThing(ThingId(thingId));
|
|
if (!relay) {
|
|
qCWarning(dcNymeaEnergy()) << "[EcsRelayAdapter]" << m_label
|
|
<< "— relais non trouvé:" << thingId;
|
|
continue;
|
|
}
|
|
const bool targetOn = wantOn.contains(thingId);
|
|
StateType powerStateType = relay->thingClass().stateTypes().findByName("power");
|
|
if (!powerStateType.id().isNull()) {
|
|
Action powerAction(powerStateType.id(), relay->id(), Action::TriggeredByRule);
|
|
powerAction.setParams(ParamList() << Param(powerStateType.id(), targetOn));
|
|
m_thingManager->executeAction(powerAction);
|
|
} else {
|
|
// Fallback mock : setStateValue direct (Things sans actionType "power")
|
|
relay->setStateValue("power", targetOn);
|
|
}
|
|
}
|
|
}
|