From fd1fd490df04a68cfbeed997477d3c4854a9c0be Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Fri, 26 Nov 2021 16:43:04 +0100 Subject: [PATCH] Fix loading of previous values at startup --- plugin/energylogger.cpp | 29 ++++++++++++++++++++++++++++- plugin/energylogger.h | 2 ++ plugin/energymanagerimpl.cpp | 13 ++++++++----- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/plugin/energylogger.cpp b/plugin/energylogger.cpp index ccf39f2..3b70433 100644 --- a/plugin/energylogger.cpp +++ b/plugin/energylogger.cpp @@ -87,6 +87,7 @@ void EnergyLogger::logThingPower(const ThingId &thingId, double currentPower, do m_thingsPowerLiveLogs[thingId].removeLast(); } } + PowerBalanceLogEntries EnergyLogger::powerBalanceLogs(SampleRate sampleRate, const QDateTime &from, const QDateTime &to) const { PowerBalanceLogEntries result; @@ -196,10 +197,27 @@ PowerBalanceLogEntry EnergyLogger::latestLogEntry(SampleRate sampleRate) qCDebug(dcEnergyExperience()) << "No power balance log entry in DB for sample rate:" << sampleRate; return PowerBalanceLogEntry(); } - qCDebug(dcEnergyExperience()) << "Loaded latest log entry:" << query.record(); return queryResultToBalanceLogEntry(query.record()); } +ThingPowerLogEntry EnergyLogger::latestLogEntry(SampleRate sampleRate, const ThingId &thingId) +{ + QSqlQuery query(m_db); + query.prepare("SELECT MAX(timestamp), currentPower, totalConsumption, totalProduction from thingPower WHERE sampleRate = ? AND thingId = ?;"); + query.addBindValue(sampleRate); + query.addBindValue(thingId); + if (!query.exec()) { + qCWarning(dcEnergyExperience()) << "Error fetching latest thing log entry from DB:" << query.lastError() << query.executedQuery(); + return ThingPowerLogEntry(); + } + if (!query.next()) { + qCDebug(dcEnergyExperience()) << "No thing power log entry in DB for sample rate:" << sampleRate; + return ThingPowerLogEntry(); + } + return queryResultToThingPowerLogEntry(query.record()); + +} + void EnergyLogger::removeThingLogs(const ThingId &thingId) { QSqlQuery query(m_db); @@ -729,3 +747,12 @@ PowerBalanceLogEntry EnergyLogger::queryResultToBalanceLogEntry(const QSqlRecord record.value("totalReturn").toDouble()); } + +ThingPowerLogEntry EnergyLogger::queryResultToThingPowerLogEntry(const QSqlRecord &record) const +{ + return ThingPowerLogEntry(QDateTime::fromMSecsSinceEpoch(record.value("timestamp").toULongLong()), + record.value("thingId").toUuid(), + record.value("currentPower").toDouble(), + record.value("totalConsumption").toDouble(), + record.value("totalProduction").toDouble()); +} diff --git a/plugin/energylogger.h b/plugin/energylogger.h index dcf7a3c..f90ed18 100644 --- a/plugin/energylogger.h +++ b/plugin/energylogger.h @@ -25,6 +25,7 @@ public: ThingPowerLogEntries thingPowerLogs(SampleRate sampleRate, const QList &thingIds, const QDateTime &from = QDateTime(), const QDateTime &to = QDateTime()) const override; PowerBalanceLogEntry latestLogEntry(SampleRate sampleRate); + ThingPowerLogEntry latestLogEntry(SampleRate sampleRate, const ThingId &thingId); void removeThingLogs(const ThingId &thingId); QList loggedThings() const; @@ -54,6 +55,7 @@ private: void trimThingPower(const ThingId &thingId, SampleRate sampleRate, const QDateTime &beforeTime); PowerBalanceLogEntry queryResultToBalanceLogEntry(const QSqlRecord &record) const; + ThingPowerLogEntry queryResultToThingPowerLogEntry(const QSqlRecord &record) const; private: struct SampleConfig { diff --git a/plugin/energymanagerimpl.cpp b/plugin/energymanagerimpl.cpp index ea3eef5..56e80d4 100644 --- a/plugin/energymanagerimpl.cpp +++ b/plugin/energymanagerimpl.cpp @@ -142,8 +142,10 @@ void EnergyManagerImpl::watchThing(Thing *thing) || thing->thingClass().interfaces().contains("smartmeterproducer") || thing->thingClass().interfaces().contains("energystorage")) { - m_totalEnergyConsumedCache[thing] = thing->stateValue("totalEnergyConsumed").toDouble(); - m_totalEnergyProducedCache[thing] = thing->stateValue("totalEnergyProduced").toDouble(); + ThingPowerLogEntry entry = m_logger->latestLogEntry(EnergyLogs::SampleRate1Min, {thing->id()}); + m_totalEnergyConsumedCache[thing] = entry.totalConsumption(); + m_totalEnergyProducedCache[thing] = entry.totalProduction(); + qCDebug(dcEnergyExperience()) << "Loaded thing power totals for" << thing->name() << "Consumption:" << entry.totalConsumption() << "Production:" << entry.totalProduction(); connect(thing, &Thing::stateValueChanged, this, [=](const StateTypeId &stateTypeId, const QVariant &value){ if (thing->thingClass().getStateType(stateTypeId).name() == "currentPower") { @@ -171,13 +173,13 @@ void EnergyManagerImpl::updatePowerBalance() double oldAcquisition = m_totalEnergyConsumedCache.value(m_rootMeter); double newAcquisition = m_rootMeter->stateValue("totalEnergyConsumed").toDouble(); - qCDebug(dcEnergyExperience()) << "Root meteter total consumption: Previous value:" << oldAcquisition << "New value:" << newAcquisition << "Diff:" << (newAcquisition -oldAcquisition); + qCDebug(dcEnergyExperience()) << "Root meter total consumption: Previous value:" << oldAcquisition << "New value:" << newAcquisition << "Diff:" << (newAcquisition -oldAcquisition); m_totalAcquisition += newAcquisition - oldAcquisition; m_totalEnergyConsumedCache[m_rootMeter] = newAcquisition; double oldReturn = m_totalEnergyProducedCache.value(m_rootMeter); double newReturn = m_rootMeter->stateValue("totalEnergyProduced").toDouble(); - qCDebug(dcEnergyExperience()) << "Root meteter total return diff" << "old" << oldReturn << " new" << newReturn << (newReturn - oldReturn); + qCDebug(dcEnergyExperience()) << "Root meter total production: Previous value:" << oldReturn << "New value:" << newReturn << "Diff:" << (newReturn - oldReturn); m_totalReturn += newReturn - oldReturn; m_totalEnergyProducedCache[m_rootMeter] = newReturn; } @@ -187,7 +189,7 @@ void EnergyManagerImpl::updatePowerBalance() currentPowerProduction += thing->stateValue("currentPower").toDouble(); double oldProduction = m_totalEnergyProducedCache.value(thing); double newProduction = thing->stateValue("totalEnergyProduced").toDouble(); - qCDebug(dcEnergyExperience()) << "inverter total production diff" << "old" << oldProduction << " new" << newProduction << (newProduction - oldProduction); + qCDebug(dcEnergyExperience()) << "Producer" << thing->name() << "total production: Previous value:" << oldProduction << "New value:" << newProduction << "Diff:" << (newProduction - oldProduction); m_totalProduction += newProduction - oldProduction; m_totalEnergyProducedCache[thing] = newProduction; } @@ -198,6 +200,7 @@ void EnergyManagerImpl::updatePowerBalance() currentPowerStorage += thing->stateValue("currentPower").toDouble(); double oldProduction = m_totalEnergyProducedCache.value(thing); double newProduction = thing->stateValue("totalEnergyProduced").toDouble(); + qCDebug(dcEnergyExperience()) << "Storage" << thing->name() << "total storage: Previous value:" << oldProduction << "New value:" << newProduction << "Diff:" << (newProduction - oldProduction); totalFromStorage += newProduction - oldProduction; m_totalEnergyProducedCache[thing] = newProduction; }