Add energy manager class
This commit is contained in:
parent
ef12ee5937
commit
6f5850f7c6
108
libnymea-app/energy/energymanager.cpp
Normal file
108
libnymea-app/energy/energymanager.cpp
Normal file
@ -0,0 +1,108 @@
|
||||
#include "energymanager.h"
|
||||
|
||||
#include "engine.h"
|
||||
|
||||
#include "logging.h"
|
||||
|
||||
NYMEA_LOGGING_CATEGORY(dcEnergyExperience, "EnergyExperience")
|
||||
|
||||
EnergyManager::EnergyManager(QObject *parent) : QObject(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
EnergyManager::~EnergyManager()
|
||||
{
|
||||
if (m_engine) {
|
||||
m_engine->jsonRpcClient()->unregisterNotificationHandler(this);
|
||||
}
|
||||
}
|
||||
|
||||
Engine *EnergyManager::engine() const
|
||||
{
|
||||
return m_engine;
|
||||
}
|
||||
|
||||
void EnergyManager::setEngine(Engine *engine)
|
||||
{
|
||||
if (m_engine != engine) {
|
||||
if (m_engine) {
|
||||
m_engine->jsonRpcClient()->unregisterNotificationHandler(this);
|
||||
}
|
||||
|
||||
m_engine = engine;
|
||||
emit engineChanged();
|
||||
|
||||
if (m_engine) {
|
||||
m_engine->jsonRpcClient()->registerNotificationHandler(this, "Energy", "notificationReceived");
|
||||
m_engine->jsonRpcClient()->sendCommand("Energy.GetRootMeter", QVariantMap(), this, "getRootMeterResponse");
|
||||
m_engine->jsonRpcClient()->sendCommand("Energy.GetPowerBalance", QVariantMap(), this, "getPowerBalanceResponse");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QUuid EnergyManager::rootMeterId() const
|
||||
{
|
||||
return m_rootMeterId;
|
||||
}
|
||||
|
||||
int EnergyManager::setRootMeterId(const QUuid &rootMeterId)
|
||||
{
|
||||
if (!m_engine) {
|
||||
return -1;
|
||||
}
|
||||
QVariantMap params;
|
||||
params.insert("rootMeterThingId", rootMeterId);
|
||||
return m_engine->jsonRpcClient()->sendCommand("Energy.SetRootMeter", params);
|
||||
}
|
||||
|
||||
double EnergyManager::currentPowerConsumption() const
|
||||
{
|
||||
return m_currentPowerConsumption;
|
||||
}
|
||||
|
||||
double EnergyManager::currentPowerProduction() const
|
||||
{
|
||||
return m_currentPowerProduction;
|
||||
}
|
||||
|
||||
double EnergyManager::currentPowerAcquisition() const
|
||||
{
|
||||
return m_currentPowerAcquisition;
|
||||
}
|
||||
|
||||
void EnergyManager::notificationReceived(const QVariantMap &data)
|
||||
{
|
||||
qCDebug(dcEnergyExperience()) << "Energy notification received" << data;
|
||||
QString notification = data.value("notification").toString();
|
||||
QVariantMap params = data.value("params").toMap();
|
||||
if (notification == "Energy.RootMeterChanged") {
|
||||
m_rootMeterId = params.value("rootMeterThingId").toUuid();
|
||||
emit rootMeterIdChanged();
|
||||
}
|
||||
if (notification == "Energy.PowerBalanceChanged") {
|
||||
m_currentPowerConsumption = params.value("currentPowerConsumption").toDouble();
|
||||
m_currentPowerProduction = params.value("currentPowerProduction").toDouble();
|
||||
m_currentPowerAcquisition = params.value("currentPowerAcquisition").toDouble();
|
||||
emit powerBalanceChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void EnergyManager::getRootMeterResponse(int commandId, const QVariantMap ¶ms)
|
||||
{
|
||||
Q_UNUSED(commandId)
|
||||
qCDebug(dcEnergyExperience) << "RootMeter response:" << params;
|
||||
m_rootMeterId = params.value("rootMeterThingId").toUuid();
|
||||
emit rootMeterIdChanged();
|
||||
}
|
||||
|
||||
void EnergyManager::getPowerBalanceResponse(int commandId, const QVariantMap ¶ms)
|
||||
{
|
||||
Q_UNUSED(commandId)
|
||||
qCDebug(dcEnergyExperience()) << "Power balance response:" << params;
|
||||
m_currentPowerConsumption = params.value("currentPowerConsumption").toDouble();
|
||||
m_currentPowerProduction = params.value("currentPowerProduction").toDouble();
|
||||
m_currentPowerAcquisition = params.value("currentPowerAcquisition").toDouble();
|
||||
emit powerBalanceChanged();
|
||||
}
|
||||
|
||||
53
libnymea-app/energy/energymanager.h
Normal file
53
libnymea-app/energy/energymanager.h
Normal file
@ -0,0 +1,53 @@
|
||||
#ifndef ENERGYMANAGER_H
|
||||
#define ENERGYMANAGER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QUuid>
|
||||
|
||||
class Engine;
|
||||
|
||||
class EnergyManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(Engine* engine READ engine WRITE setEngine NOTIFY engineChanged)
|
||||
Q_PROPERTY(QUuid rootMeterId READ rootMeterId NOTIFY rootMeterIdChanged)
|
||||
|
||||
Q_PROPERTY(double currentPowerConsumption READ currentPowerConsumption NOTIFY powerBalanceChanged)
|
||||
Q_PROPERTY(double currentPowerProduction READ currentPowerProduction NOTIFY powerBalanceChanged)
|
||||
Q_PROPERTY(double currentPowerAcquisition READ currentPowerAcquisition NOTIFY powerBalanceChanged)
|
||||
|
||||
public:
|
||||
explicit EnergyManager(QObject *parent = nullptr);
|
||||
~EnergyManager();
|
||||
|
||||
Engine* engine() const;
|
||||
void setEngine(Engine *engine);
|
||||
|
||||
QUuid rootMeterId() const;
|
||||
Q_INVOKABLE int setRootMeterId(const QUuid &rootMeterId);
|
||||
|
||||
double currentPowerConsumption() const;
|
||||
double currentPowerProduction() const;
|
||||
double currentPowerAcquisition() const;
|
||||
|
||||
signals:
|
||||
void engineChanged();
|
||||
void rootMeterIdChanged();
|
||||
void powerBalanceChanged();
|
||||
|
||||
private slots:
|
||||
void notificationReceived(const QVariantMap &data);
|
||||
void getRootMeterResponse(int commandId, const QVariantMap ¶ms);
|
||||
void getPowerBalanceResponse(int commandId, const QVariantMap ¶ms);
|
||||
|
||||
private:
|
||||
Engine *m_engine = nullptr;
|
||||
QUuid m_rootMeterId;
|
||||
|
||||
double m_currentPowerConsumption = 0;
|
||||
double m_currentPowerProduction = 0;
|
||||
double m_currentPowerAcquisition = 0;
|
||||
|
||||
};
|
||||
|
||||
#endif // ENERGYMANAGER_H
|
||||
@ -74,8 +74,8 @@ JsonRpcClient::JsonRpcClient(QObject *parent) :
|
||||
|
||||
void JsonRpcClient::registerNotificationHandler(QObject *handler, const QString &nameSpace, const QString &method)
|
||||
{
|
||||
if (m_notificationHandlerMethods.contains(handler)) {
|
||||
qWarning() << "Notification handler" << handler << " already registered";
|
||||
if (m_notificationHandlers.key(handler) == nameSpace) {
|
||||
qWarning() << "Notification handler" << handler << " already registered for namespace" << nameSpace;
|
||||
return;
|
||||
}
|
||||
m_notificationHandlers.insert(nameSpace, handler);
|
||||
|
||||
@ -135,6 +135,7 @@
|
||||
#include "modbus/modbusrtumanager.h"
|
||||
#include "modbus/modbusrtumasters.h"
|
||||
#include "types/serialportsproxy.h"
|
||||
#include "energy/energymanager.h"
|
||||
|
||||
#include <QtQml/qqml.h>
|
||||
|
||||
@ -357,6 +358,8 @@ void registerQmlTypes() {
|
||||
|
||||
qmlRegisterType<AppData>(uri, 1, 0, "AppData");
|
||||
|
||||
qmlRegisterType<EnergyManager>(uri, 1, 0, "EnergyManager");
|
||||
|
||||
qmlRegisterType<SortFilterProxyModel>(uri, 1, 0, "SortFilterProxyModel");
|
||||
}
|
||||
|
||||
|
||||
@ -21,6 +21,7 @@ INCLUDEPATH += \
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/appdata.cpp \
|
||||
$$PWD/energy/energymanager.cpp \
|
||||
$$PWD/models/scriptsproxymodel.cpp \
|
||||
$$PWD/tagwatcher.cpp \
|
||||
$$PWD/zigbee/zigbeenode.cpp \
|
||||
@ -177,6 +178,7 @@ SOURCES += \
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/appdata.h \
|
||||
$$PWD/energy/energymanager.h \
|
||||
$$PWD/models/scriptsproxymodel.h \
|
||||
$$PWD/tagwatcher.h \
|
||||
$$PWD/zigbee/zigbeenode.h \
|
||||
|
||||
@ -58,7 +58,9 @@ ThingPageBase {
|
||||
id: background
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
Layout.margins: Style.hugeMargins
|
||||
Layout.leftMargin: Style.hugeMargins
|
||||
Layout.rightMargin: Style.hugeMargins
|
||||
Layout.topMargin: Style.hugeMargins
|
||||
// iconSource: "ev-charger"
|
||||
onColor: app.interfaceToColor("evcharger")
|
||||
// on: (actionQueue.pendingValue || powerState.value) === true
|
||||
|
||||
Reference in New Issue
Block a user