// 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 .
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef LOADADAPTERREGISTRY_H
#define LOADADAPTERREGISTRY_H
#include
#include
#include
#include
#include
#include
#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 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 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 ¶ms);
// 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 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 ¶ms);
RoleStatus buildRoleStatus(LoadRole role) const;
ThingManager *m_thingManager;
QHash m_entries;
};
#endif // LOADADAPTERREGISTRY_H