pakutz79 67836b7234 feat: Installer Setup — LoadAdapterRegistry + adapter layer (v12)
Adds a thin adapter layer that maps installer-visible roles (EVCharger,
DHW, HeatPump, Battery, SolarMeter, GridMeter) to nymea Things and
applies Scheduler slot decisions to real hardware.

New files:
- energyplugin/types/loadrole.h — LoadRole enum + compat interface map
- energyplugin/adapters/iloadapter.h — abstract ILoadAdapter interface
- energyplugin/adapters/relayadapter.h/.cpp — binary on/off relay (DHW/HP)
- energyplugin/adapters/sgreadyadapter.h/.cpp — SG-Ready 2-relay HP (SG1–SG4)
- energyplugin/adapters/evchargeradapter.h/.cpp — EV charger W→A adapter
- energyplugin/adapters/batteryadapter.h/.cpp — battery charge/discharge/standby
- energyplugin/adapters/loadadapterregistry.h/.cpp — role→Thing→adapter registry
- energyplugin/adapters/adaptersettings.h/.cpp — persists to adapters.conf (INI)

Modified:
- SchedulerManager: optional LoadAdapterRegistry param; applyCurrentSlot()
  now calls adapter->applyPower() for DHW/HP/Battery roles
- NymeaEnergyJsonHandler: 6 new v12 methods (GetSetupStatus,
  GetCompatibleThings, AssignThingToRole, UnassignRole, SetRoleEnabled,
  TestRoleConnection) + 3 push notifications
- energypluginnymea.cpp: wires AdapterSettings + LoadAdapterRegistry,
  bumps JSON-RPC version to 12
- energyplugin.pri: adds 7 headers + 6 sources
- testscheduler: 4 new tests (detectAdapterType, AdapterSettings round-trip,
  null-registry no-crash, LoadRole serialization) — 15/15 pass
- doc.md: section 12 Installer Setup

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-24 09:15:06 +01:00

152 lines
5.3 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/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef LOADADAPTERREGISTRY_H
#define LOADADAPTERREGISTRY_H
#include <QObject>
#include <QHash>
#include <QList>
#include <QVariantMap>
#include <QString>
#include <integrations/thingmanager.h>
#include "types/loadrole.h"
#include "adapters/iloadapter.h"
class AdapterSettings;
// Summarises a nymea Thing that is compatible with a given role.
struct ThingInfo {
ThingId thingId;
QString displayName;
QString pluginName;
QStringList interfaces;
};
// Per-role status, serialisable to JSON-RPC.
struct RoleStatus {
LoadRole role;
bool enabled = false;
bool assigned = false;
ThingId assignedThingId;
QString assignedThingName;
bool reachable = false;
QString adapterType;
QString lastError;
};
// Aggregated setup status across all roles.
struct SetupStatus {
QList<RoleStatus> roles;
bool allEnabledRolesOk = false;
int configuredCount = 0;
int errorCount = 0;
};
// LoadAdapterRegistry maps installer-visible roles to nymea Things and
// creates the appropriate ILoadAdapter for each assignment.
//
// Usage:
// LoadAdapterRegistry *reg = new LoadAdapterRegistry(thingManager, this);
// reg->loadFromSettings(adapterSettings);
// reg->assignThing(LoadRole::DHW, dhwThingId, {});
// reg->adapterForRole(LoadRole::DHW)->applyPower(2000);
class LoadAdapterRegistry : public QObject
{
Q_OBJECT
public:
explicit LoadAdapterRegistry(ThingManager *thingManager, QObject *parent = nullptr);
// Load saved assignments from AdapterSettings.
// Call once after construction; safe to call with nullptr (no-op).
void loadFromSettings(AdapterSettings *settings);
// List all configured Things compatible with the given role.
// Filters by the required interfaces for the role.
QList<ThingInfo> compatibleThings(LoadRole role) const;
// Assign a Thing to a role.
// 'params' is adapter-specific:
// relay: { nominalPowerW: double }
// sgready: { relay1ThingId: uuid, relay2ThingId: uuid, normalPowerW: double }
// evcharger:{ phases: int, minA: int, maxA: int }
// battery: { capacityKwh: double, maxChargeW: double, maxDischargeW: double }
// Returns the detected adapter type, or an error string prefixed with "error:".
QString assignThing(LoadRole role, const ThingId &thingId, const QVariantMap &params);
// Unassign a role; destroys the underlying adapter.
void unassignRole(LoadRole role);
// Return the adapter for a role, or nullptr if not assigned.
ILoadAdapter *adapterForRole(LoadRole role) const;
bool isRoleAssigned(LoadRole role) const;
bool isRoleEnabled(LoadRole role) const;
void setRoleEnabled(LoadRole role, bool enabled);
// Initiate a connection test for the role's adapter.
// Result is delivered via connectionTestResult() signal.
void testConnection(LoadRole role);
// Current setup status across all roles.
SetupStatus setupStatus() const;
// Raw assignment maps for persistence (one map per assigned role).
QList<QVariantMap> rawAssignments() const;
// Static: determine adapter type from role + Thing interfaces (no ThingManager needed).
// Returns "relay", "sgready", "evcharger", "battery", or "readonly".
static QString detectAdapterType(LoadRole role, const QStringList &interfaces);
signals:
void roleAssigned(LoadRole role, ThingId thingId);
void roleUnassigned(LoadRole role);
void roleEnabledChanged(LoadRole role, bool enabled);
void connectionTestResult(LoadRole role, bool success, const QString &message);
void setupStatusChanged(const SetupStatus &status);
void thingBecameCompatible(LoadRole role, const ThingInfo &info);
private:
struct RoleEntry {
bool enabled = false;
ILoadAdapter *adapter = nullptr;
QVariantMap rawParams;
QString lastError;
};
void onThingAdded(Thing *thing);
ILoadAdapter *createAdapter(LoadRole role,
const ThingId &thingId,
const QString &adapterType,
const QVariantMap &params);
RoleStatus buildRoleStatus(LoadRole role) const;
ThingManager *m_thingManager;
QHash<LoadRole, RoleEntry> m_entries;
};
#endif // LOADADAPTERREGISTRY_H