Constaté au banc à 14:41:58 : l'avertissement « has a model 160 but no StCha/StDisCha block » a été émis une fois, au démarrage, sur une machine dont les deux blocs existent et dont le currentPower a ensuite suivi la batterie correctement en charge, en décharge et au repos. Cause. libnymea-sunspec crée les blocs répétés d'un modèle depuis `initFinished`, émis pendant la PREMIÈRE lecture, juste avant `blockUpdated`. Sur ce premier signal les blocs existent mais n'ont jamais été remplis : tous les `IDStr` sont vides, rien ne se classe, et `hasBatteryBlocks()` répond faux. Cette réponse est exacte et hors sujet — la question n'était pas « pas de batterie » mais « pas encore lu ». Ce que le faux positif coûtait. Ce n'est pas du bruit : le README dit à l'exploitation de chercher cette ligne EN PREMIER pour distinguer une batterie cassée d'une batterie non mesurable. Une ligne qui sort à chaque démarrage sain ne distingue plus rien, et le diagnostic que ce lot venait d'installer était mort-né. Les deux questions sont maintenant posées séparément : `blocksAreIdentified()` avant `hasBatteryBlocks()`. Sans identification, on se tait et on attend le tour suivant — `readBlockData()` passe à chaque rafraîchissement de toute façon. Le garde en double au moment du binding disparaît aussi : les deux updaters se gardent eux-mêmes, un seul endroit à tenir à jour plutôt que deux qui peuvent diverger. Deux tests figent la distinction, dont un sur le cas où l'avertissement DOIT sortir — un GEN24 sans batterie, lu, identifié, sans bloc batterie. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Y3gzsuwENJfJtvuNDT3xGn
234 lines
7.4 KiB
C++
234 lines
7.4 KiB
C++
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
*
|
|
* Copyright (C) 2026, ETM-PowerSync
|
|
*
|
|
* This file is part of nymea.
|
|
*
|
|
* nymea is 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 is 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. If not, see <https://www.gnu.org/licenses/>.
|
|
*
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
#include "froniusrules.h"
|
|
|
|
using namespace SunSpecStorageLogic;
|
|
|
|
namespace FroniusRules {
|
|
|
|
bool matchesManufacturer(const QStringList &manufacturers)
|
|
{
|
|
foreach (const QString &name, manufacturers) {
|
|
if (name.contains(QStringLiteral("fronius"), Qt::CaseInsensitive))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
double meterPowerSign(bool froniusMeter)
|
|
{
|
|
/* The Fronius meter is already in the nymea convention, so the generic
|
|
* negation has to be undone for it, and only for it. */
|
|
return froniusMeter ? 1.0 : -1.0;
|
|
}
|
|
|
|
bool hasBattery(double batteryVoltage, double availableCharge)
|
|
{
|
|
/* Strictly positive only: zero is the no-battery reading, and a negative
|
|
* value is the library's way of surfacing a not-implemented point rather
|
|
* than a real measurement. */
|
|
return batteryVoltage > 0.0 || availableCharge > 0.0;
|
|
}
|
|
|
|
SunSpecDataPoint::ByteOrder byteOrder()
|
|
{
|
|
/* Big endian is what the hardware emits, and what SunSpec specifies. The
|
|
* library's default is the opposite; see the header for the measurement. */
|
|
return SunSpecDataPoint::ByteOrderBigEndian;
|
|
}
|
|
|
|
MeterConnectionDecision decideMeterConnection(int meterSlaveId, int inverterSlaveId)
|
|
{
|
|
if (meterSlaveId == 0)
|
|
return MeterConnectionDisabled;
|
|
|
|
if (meterSlaveId < 0 || meterSlaveId > MaximumSlaveId)
|
|
return MeterConnectionInvalidSlaveId;
|
|
|
|
/* Not a theoretical case: the slave ID of the thing is written by the
|
|
* discovery from the slave ID it was asked to scan, so an inverter found
|
|
* with a discovery slave ID of 200 ends up with both values at 200. */
|
|
if (meterSlaveId == inverterSlaveId)
|
|
return MeterConnectionCollidesWithInverter;
|
|
|
|
return MeterConnectionCreate;
|
|
}
|
|
|
|
int clampUpdateInterval(int requestedSeconds)
|
|
{
|
|
return qMax(requestedSeconds, MinimumUpdateIntervalSeconds);
|
|
}
|
|
|
|
bool isWriteForbidden(const QString ®isterName)
|
|
{
|
|
static const QStringList forbidden = {
|
|
QStringLiteral("WChaMax"),
|
|
QStringLiteral("WChaGra"),
|
|
QStringLiteral("WDisChaGra"),
|
|
QStringLiteral("InOutWRte_WinTms"),
|
|
QStringLiteral("InOutWRte_RmpTms"),
|
|
/* VAChaMax is doubly unusable: the write is ignored and its scale
|
|
* factor is not supported either. */
|
|
QStringLiteral("VAChaMax")
|
|
};
|
|
|
|
foreach (const QString &name, forbidden) {
|
|
if (registerName.compare(name, Qt::CaseInsensitive) == 0)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
SetpointPlan planBatteryMode(BatteryMode mode, double chargeLimit, double dischargeLimit, quint16 revertTime)
|
|
{
|
|
/* StorCtl_Mod is a bitfield: bit 0 CHARGE, bit 1 DISCHARGE. ChaGriSet is
|
|
* 0 PV, 1 GRID. A register absent from the vector below is deliberately not
|
|
* written for that mode.
|
|
*
|
|
* planSetpoint() puts these in the mandated order and appends the revert
|
|
* timer, so the order they are listed in here does not matter. */
|
|
QVector<Write> setpoints;
|
|
|
|
switch (mode) {
|
|
case BatteryModeNormal:
|
|
setpoints.append({RegisterStorCtlMod, 0});
|
|
setpoints.append({RegisterOutWRte, 100});
|
|
break;
|
|
|
|
case BatteryModeNoDischarge:
|
|
setpoints.append({RegisterStorCtlMod, 2});
|
|
setpoints.append({RegisterOutWRte, 0});
|
|
break;
|
|
|
|
case BatteryModeNoCharge:
|
|
setpoints.append({RegisterStorCtlMod, 1});
|
|
setpoints.append({RegisterInWRte, 0});
|
|
break;
|
|
|
|
case BatteryModeFrozen:
|
|
setpoints.append({RegisterStorCtlMod, 3});
|
|
setpoints.append({RegisterInWRte, 0});
|
|
setpoints.append({RegisterOutWRte, 0});
|
|
break;
|
|
|
|
case BatteryModeForceCharge:
|
|
/* Charging from the grid has to be allowed on the inverter as well;
|
|
* ChaGriSet alone is ANDed with a setting of the component
|
|
* configuration and fails silently if that one is off. */
|
|
setpoints.append({RegisterChaGriSet, 1});
|
|
setpoints.append({RegisterStorCtlMod, 2});
|
|
/* Negative is charging. */
|
|
setpoints.append({RegisterOutWRte, -chargeLimit});
|
|
break;
|
|
|
|
case BatteryModeLimit:
|
|
setpoints.append({RegisterStorCtlMod, 3});
|
|
setpoints.append({RegisterInWRte, chargeLimit});
|
|
setpoints.append({RegisterOutWRte, dischargeLimit});
|
|
break;
|
|
}
|
|
|
|
return planSetpoint(setpoints, revertTime);
|
|
}
|
|
|
|
BlockRole classifyBlock(const QString &inputIdString)
|
|
{
|
|
const QString id = inputIdString.trimmed();
|
|
if (id.isEmpty())
|
|
return BlockRoleUnknown;
|
|
|
|
/* Prefix match, not contains: the role is decided by the string, never by
|
|
* the block index. StDisCha is checked first so that no cascade can ever
|
|
* swallow it into the charge case. */
|
|
if (id.startsWith(QStringLiteral("StDisCha"), Qt::CaseInsensitive))
|
|
return BlockRoleBatteryDischarge;
|
|
|
|
if (id.startsWith(QStringLiteral("StCha"), Qt::CaseInsensitive))
|
|
return BlockRoleBatteryCharge;
|
|
|
|
if (id.startsWith(QStringLiteral("MPPT"), Qt::CaseInsensitive))
|
|
return BlockRolePvString;
|
|
|
|
return BlockRoleUnknown;
|
|
}
|
|
|
|
double pvPower(const QList<ModuleReading> &readings)
|
|
{
|
|
double total = 0;
|
|
foreach (const ModuleReading &reading, readings) {
|
|
if (classifyBlock(reading.inputIdString) == BlockRolePvString)
|
|
total += reading.dcPower;
|
|
}
|
|
return total;
|
|
}
|
|
|
|
double storageCurrentPower(const QList<ModuleReading> &readings)
|
|
{
|
|
double charging = 0;
|
|
double discharging = 0;
|
|
|
|
foreach (const ModuleReading &reading, readings) {
|
|
switch (classifyBlock(reading.inputIdString)) {
|
|
case BlockRoleBatteryCharge:
|
|
charging += reading.dcPower;
|
|
break;
|
|
case BlockRoleBatteryDischarge:
|
|
discharging += reading.dcPower;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
return charging - discharging;
|
|
}
|
|
|
|
double batteryPower(const QList<ModuleReading> &readings)
|
|
{
|
|
/* Written as the negation rather than as a second sum: two independent
|
|
* implementations of the same subtraction are two places for the sign to
|
|
* rot, and only one of them has a test watching it. */
|
|
return -storageCurrentPower(readings);
|
|
}
|
|
|
|
bool blocksAreIdentified(const QList<ModuleReading> &readings)
|
|
{
|
|
foreach (const ModuleReading &reading, readings) {
|
|
if (!reading.inputIdString.trimmed().isEmpty())
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool hasBatteryBlocks(const QList<ModuleReading> &readings)
|
|
{
|
|
foreach (const ModuleReading &reading, readings) {
|
|
const BlockRole role = classifyBlock(reading.inputIdString);
|
|
if (role == BlockRoleBatteryCharge || role == BlockRoleBatteryDischarge)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|