// 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 . * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include "rootmeter.h" #include "integrations/thing.h" #include 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; // [upstream-fix] La tension est LOCALE À CHAQUE PHASE. Elle était déclarée une seule fois // ici et mutée par le bloc de la phase B, si bien que la phase C — quand elle ne publie pas // sa propre tension — était calculée avec celle de B. Démontré : B à 200 V et C à 4 000 W // donnaient 5,0 A de marge au lieu de 7,6. // // Le défaut n'était pas atteignable depuis la phase A : l'interface `energymeter` rend // `currentPhaseA` et `voltagePhaseA` OBLIGATOIRES, donc la branche « puissance ÷ tension » // de A est morte par construction. Seules B et C sont optionnelles, et la fuite allait donc // de B vers C — plus étroit que « la tension de A sert à calculer B », mais bien réel. auto tensionDe = [this](const char *nomTension) { return m_thing->hasState(nomTension) ? m_thing->stateValue(nomTension).toDouble() : 230.0; }; 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")) { allowance = qMin(allowance, 1.0 * phasePowerLimit - (m_thing->stateValue("currentPowerPhaseA").toDouble() / tensionDe("voltagePhaseA"))); } 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")) { allowance = qMin(allowance, 1.0 * phasePowerLimit - (m_thing->stateValue("currentPowerPhaseB").toDouble() / tensionDe("voltagePhaseB"))); } 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")) { allowance = qMin(allowance, 1.0 * phasePowerLimit - (m_thing->stateValue("currentPowerPhaseC").toDouble() / tensionDe("voltagePhaseC"))); } else { qCDebug(dcNymeaEnergy()) << "Asked to calculate allowence for Phase C on a single phase root meter."; } } return allowance; }