Patrick Schurig 13d24e10df [upstream-fix] rootmeter: lire l'état qu'on vient de tester (phases B et C)
Les branches des phases B et C testaient `currentPhaseB`/`currentPhaseC`
— des ampères — puis lisaient `currentPowerPhaseB`/`currentPowerPhaseC`
divisés par 230 — des watts. La phase A, elle, lit bien l'état qu'elle
teste. L'asymétrie n'était pas cosmétique : un compteur publiant les
courants par phase SANS les puissances par phase entrait dans la première
branche, y lisait 0, et se voyait accorder une place libre égale au plafond
entier. La protection de surcharge était AVEUGLE sur B et C, sans rien
signaler.

Le correctif change le comportement, et dans le bon sens : des bornes
seront écrêtées qui ne l'étaient pas, parce que la protection redevient
effective. Sur une triphasée déséquilibrée, le disjoncteur était jusqu'ici
le seul rempart — L0 employé comme régulateur au lieu de dernier recours.

Aucun test de ce dépôt ne pouvait le trouver : le mock pose currentPhaseB =
currentPowerPhaseB / 230, donc les deux états y sont cohérents par
construction et lire l'un ou l'autre y donne le même nombre. Le test
désaccorde les états à la main, ce qu'aucun chemin normal du mock ne
permet ; sans le correctif il annonce 25 A de place libre sur une phase qui
en tire 20.

L'angle mort est nommé comme une CLASSE dans SAFETY.md, et non comme un
incident : l'installation de référence n'exerce pas le cas, une suite verte
ne dit alors rien sur lui, et `stateValue()` d'un état absent rend 0 —
valeur qui, dans un calcul de marge, se confond avec « rien ne consomme ».

Un défaut voisin de la même fonction est consigné sans être corrigé :
`voltage` est déclarée hors des trois blocs et mutée par celui de la phase
A. Il aura son propre correctif isolé.

Fait avant l'étape 3 délibérément : c'est la couche de sécurité, c'est le
chemin qu'on étend, et brancher la marge dessus l'aurait rendu plus
difficile à isoler.

Suite complète : simulation 51/51, charging 48/48 identique à la référence
ligne à ligne, loadmodel 21/21, spotmarket 32/32, doxygen 0 avertissement.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015F7G5VeaPVSMeVNjiGj36p
2026-08-30 12:11:43 +02:00

135 lines
5.4 KiB
C++

// SPDX-License-Identifier: GPL-3.0-or-later
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright (C) 2013 - 2024, nymea GmbH
* Copyright (C) 2024 - 2025, chargebyte austria GmbH
*
* This file is part of nymea-energy-plugin-nymea.
*
* nymea-energy-plugin-nymea.s free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nymea-energy-plugin-nymea.s distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with nymea-energy-plugin-nymea. If not, see <https://www.gnu.org/licenses/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "rootmeter.h"
#include "integrations/thing.h"
#include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(dcNymeaEnergy)
RootMeter::RootMeter(Thing *parent)
: QObject{parent},
m_thing(parent)
{
connect(m_thing, &Thing::stateValueChanged, this, [=](const StateTypeId &stateTypeId, const QVariant &value){
if (m_thing->thingClass().getStateType(stateTypeId).name() == "currentPower") {
emit currentPowerChanged(value.toDouble());
}
});
}
double RootMeter::currentPower() const
{
return m_thing->stateValue("currentPower").toDouble();
}
double RootMeter::currentPowerPhaseA() const
{
return m_thing->stateValue("currentPowerPhaseA").toDouble();
}
double RootMeter::currentPowerPhaseB() const
{
return m_thing->stateValue("currentPowerPhaseB").toDouble();
}
double RootMeter::currentPowerPhaseC() const
{
return m_thing->stateValue("currentPowerPhaseC").toDouble();
}
double RootMeter::currentPhaseA() const
{
return m_thing->stateValue("currentPhaseA").toDouble();
}
double RootMeter::currentPhaseB() const
{
return m_thing->stateValue("currentPhaseB").toDouble();
}
double RootMeter::currentPhaseC() const
{
return m_thing->stateValue("currentPhaseC").toDouble();
}
double RootMeter::calculateAllowanceAmpere(Electricity::Phases phases, uint phasePowerLimit)
{
double allowance = phasePowerLimit;
double voltage = 230;
if (phases.testFlag(Electricity::PhaseA) || phases == Electricity::PhaseNone) {
if (m_thing->hasState("currentPhaseA")) {
allowance = qMin(allowance, 1.0 * phasePowerLimit - m_thing->stateValue("currentPhaseA").toDouble());
} else if (m_thing->hasState("currentPowerPhaseA")) {
if (m_thing->hasState("voltagePhaseA"))
voltage = m_thing->stateValue("voltagePhaseA").toDouble();
allowance = qMin(allowance, 1.0 * phasePowerLimit - (m_thing->stateValue("currentPowerPhaseA").toDouble() / voltage));
} else {
// No currentPhaseA state... Assume single phase and deduct from currentPower
double current = m_thing->stateValue("currentPower").toDouble() / 230;
allowance = qMin(allowance, 1.0 * phasePowerLimit - current);
}
}
if (phases.testFlag(Electricity::PhaseB) || phases == Electricity::PhaseNone) {
if (m_thing->hasState("currentPhaseB")) {
// [upstream-fix] Lire l'état qu'on vient de tester. Cette branche testait
// currentPhaseB (ampères) et lisait currentPowerPhaseB/230 (watts) — asymétrique
// avec la phase A, et surtout AVEUGLE sur un compteur qui publie les courants
// sans les puissances par phase : la lecture y rendait 0, donc la place libre
// entière, donc aucun écrêtage.
allowance = qMin(allowance, 1.0 * phasePowerLimit - m_thing->stateValue("currentPhaseB").toDouble());
} else if (m_thing->hasState("currentPowerPhaseB")) {
if (m_thing->hasState("voltagePhaseB"))
voltage = m_thing->stateValue("voltagePhaseB").toDouble();
allowance = qMin(allowance, 1.0 * phasePowerLimit - (m_thing->stateValue("currentPowerPhaseB").toDouble() / voltage));
} else {
qCDebug(dcNymeaEnergy()) << "Asked to calculate allowence for Phase B on a single phase root meter.";
}
}
if (phases.testFlag(Electricity::PhaseC) || phases == Electricity::PhaseNone) {
if (m_thing->hasState("currentPhaseC")) {
// [upstream-fix] Lire l'état qu'on vient de tester. Cette branche testait
// currentPhaseC (ampères) et lisait currentPowerPhaseC/230 (watts) — asymétrique
// avec la phase A, et surtout AVEUGLE sur un compteur qui publie les courants
// sans les puissances par phase : la lecture y rendait 0, donc la place libre
// entière, donc aucun écrêtage.
allowance = qMin(allowance, 1.0 * phasePowerLimit - m_thing->stateValue("currentPhaseC").toDouble());
} else if (m_thing->hasState("currentPowerPhaseC")) {
if (m_thing->hasState("voltagePhaseC"))
voltage = m_thing->stateValue("voltagePhaseC").toDouble();
allowance = qMin(allowance, 1.0 * phasePowerLimit - (m_thing->stateValue("currentPowerPhaseC").toDouble() / voltage));
} else {
qCDebug(dcNymeaEnergy()) << "Asked to calculate allowence for Phase C on a single phase root meter.";
}
}
return allowance;
}