Fix loading of previous values at startup

master
Michael Zanetti 2021-11-26 16:43:04 +01:00
parent f63e9bcaf0
commit fd1fd490df
3 changed files with 38 additions and 6 deletions

View File

@ -87,6 +87,7 @@ void EnergyLogger::logThingPower(const ThingId &thingId, double currentPower, do
m_thingsPowerLiveLogs[thingId].removeLast(); m_thingsPowerLiveLogs[thingId].removeLast();
} }
} }
PowerBalanceLogEntries EnergyLogger::powerBalanceLogs(SampleRate sampleRate, const QDateTime &from, const QDateTime &to) const PowerBalanceLogEntries EnergyLogger::powerBalanceLogs(SampleRate sampleRate, const QDateTime &from, const QDateTime &to) const
{ {
PowerBalanceLogEntries result; PowerBalanceLogEntries result;
@ -196,10 +197,27 @@ PowerBalanceLogEntry EnergyLogger::latestLogEntry(SampleRate sampleRate)
qCDebug(dcEnergyExperience()) << "No power balance log entry in DB for sample rate:" << sampleRate; qCDebug(dcEnergyExperience()) << "No power balance log entry in DB for sample rate:" << sampleRate;
return PowerBalanceLogEntry(); return PowerBalanceLogEntry();
} }
qCDebug(dcEnergyExperience()) << "Loaded latest log entry:" << query.record();
return queryResultToBalanceLogEntry(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) void EnergyLogger::removeThingLogs(const ThingId &thingId)
{ {
QSqlQuery query(m_db); QSqlQuery query(m_db);
@ -729,3 +747,12 @@ PowerBalanceLogEntry EnergyLogger::queryResultToBalanceLogEntry(const QSqlRecord
record.value("totalReturn").toDouble()); 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());
}

View File

@ -25,6 +25,7 @@ public:
ThingPowerLogEntries thingPowerLogs(SampleRate sampleRate, const QList<ThingId> &thingIds, const QDateTime &from = QDateTime(), const QDateTime &to = QDateTime()) const override; ThingPowerLogEntries thingPowerLogs(SampleRate sampleRate, const QList<ThingId> &thingIds, const QDateTime &from = QDateTime(), const QDateTime &to = QDateTime()) const override;
PowerBalanceLogEntry latestLogEntry(SampleRate sampleRate); PowerBalanceLogEntry latestLogEntry(SampleRate sampleRate);
ThingPowerLogEntry latestLogEntry(SampleRate sampleRate, const ThingId &thingId);
void removeThingLogs(const ThingId &thingId); void removeThingLogs(const ThingId &thingId);
QList<ThingId> loggedThings() const; QList<ThingId> loggedThings() const;
@ -54,6 +55,7 @@ private:
void trimThingPower(const ThingId &thingId, SampleRate sampleRate, const QDateTime &beforeTime); void trimThingPower(const ThingId &thingId, SampleRate sampleRate, const QDateTime &beforeTime);
PowerBalanceLogEntry queryResultToBalanceLogEntry(const QSqlRecord &record) const; PowerBalanceLogEntry queryResultToBalanceLogEntry(const QSqlRecord &record) const;
ThingPowerLogEntry queryResultToThingPowerLogEntry(const QSqlRecord &record) const;
private: private:
struct SampleConfig { struct SampleConfig {

View File

@ -142,8 +142,10 @@ void EnergyManagerImpl::watchThing(Thing *thing)
|| thing->thingClass().interfaces().contains("smartmeterproducer") || thing->thingClass().interfaces().contains("smartmeterproducer")
|| thing->thingClass().interfaces().contains("energystorage")) { || thing->thingClass().interfaces().contains("energystorage")) {
m_totalEnergyConsumedCache[thing] = thing->stateValue("totalEnergyConsumed").toDouble(); ThingPowerLogEntry entry = m_logger->latestLogEntry(EnergyLogs::SampleRate1Min, {thing->id()});
m_totalEnergyProducedCache[thing] = thing->stateValue("totalEnergyProduced").toDouble(); 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){ connect(thing, &Thing::stateValueChanged, this, [=](const StateTypeId &stateTypeId, const QVariant &value){
if (thing->thingClass().getStateType(stateTypeId).name() == "currentPower") { if (thing->thingClass().getStateType(stateTypeId).name() == "currentPower") {
@ -171,13 +173,13 @@ void EnergyManagerImpl::updatePowerBalance()
double oldAcquisition = m_totalEnergyConsumedCache.value(m_rootMeter); double oldAcquisition = m_totalEnergyConsumedCache.value(m_rootMeter);
double newAcquisition = m_rootMeter->stateValue("totalEnergyConsumed").toDouble(); 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_totalAcquisition += newAcquisition - oldAcquisition;
m_totalEnergyConsumedCache[m_rootMeter] = newAcquisition; m_totalEnergyConsumedCache[m_rootMeter] = newAcquisition;
double oldReturn = m_totalEnergyProducedCache.value(m_rootMeter); double oldReturn = m_totalEnergyProducedCache.value(m_rootMeter);
double newReturn = m_rootMeter->stateValue("totalEnergyProduced").toDouble(); 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_totalReturn += newReturn - oldReturn;
m_totalEnergyProducedCache[m_rootMeter] = newReturn; m_totalEnergyProducedCache[m_rootMeter] = newReturn;
} }
@ -187,7 +189,7 @@ void EnergyManagerImpl::updatePowerBalance()
currentPowerProduction += thing->stateValue("currentPower").toDouble(); currentPowerProduction += thing->stateValue("currentPower").toDouble();
double oldProduction = m_totalEnergyProducedCache.value(thing); double oldProduction = m_totalEnergyProducedCache.value(thing);
double newProduction = thing->stateValue("totalEnergyProduced").toDouble(); 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_totalProduction += newProduction - oldProduction;
m_totalEnergyProducedCache[thing] = newProduction; m_totalEnergyProducedCache[thing] = newProduction;
} }
@ -198,6 +200,7 @@ void EnergyManagerImpl::updatePowerBalance()
currentPowerStorage += thing->stateValue("currentPower").toDouble(); currentPowerStorage += thing->stateValue("currentPower").toDouble();
double oldProduction = m_totalEnergyProducedCache.value(thing); double oldProduction = m_totalEnergyProducedCache.value(thing);
double newProduction = thing->stateValue("totalEnergyProduced").toDouble(); 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; totalFromStorage += newProduction - oldProduction;
m_totalEnergyProducedCache[thing] = newProduction; m_totalEnergyProducedCache[thing] = newProduction;
} }