diff --git a/nymea-app/main.cpp b/nymea-app/main.cpp index ceead76b..2b9590c3 100644 --- a/nymea-app/main.cpp +++ b/nymea-app/main.cpp @@ -36,6 +36,7 @@ #include #include #include +#include "utils/qhashqml.h" #include "libnymea-app-core.h" #include "libnymea-app-airconditioning.h" @@ -183,6 +184,7 @@ int main(int argc, char *argv[]) qmlRegisterUncreatableType("Nymea", 1, 0, "DashboardWebViewItem", ""); qmlRegisterSingletonType("NymeaApp.Utils", 1, 0, "PrivacyPolicyHelper", PrivacyPolicyHelper::qmlProvider); + qmlRegisterType("NymeaApp.Utils", 1, 0, "QHash"); qmlRegisterType("Nymea", 1, 0, "MouseObserver"); diff --git a/nymea-app/nymea-app.pro b/nymea-app/nymea-app.pro index 173e6334..05cf702b 100644 --- a/nymea-app/nymea-app.pro +++ b/nymea-app/nymea-app.pro @@ -42,7 +42,8 @@ HEADERS += \ pushnotifications.h \ platformhelper.h \ ruletemplates/messages.h \ - utils/privacypolicyhelper.h + utils/privacypolicyhelper.h \ + utils/qhashqml.h SOURCES += main.cpp \ configuredhostsmodel.cpp \ @@ -56,7 +57,8 @@ SOURCES += main.cpp \ pushnotifications.cpp \ platformhelper.cpp \ platformintegration/generic/screenhelper.cpp \ - utils/privacypolicyhelper.cpp + utils/privacypolicyhelper.cpp \ + utils/qhashqml.cpp RESOURCES += resources.qrc \ ruletemplates.qrc \ diff --git a/nymea-app/ui/mainviews/energy/ConsumersHistory.qml b/nymea-app/ui/mainviews/energy/ConsumersHistory.qml index 3f3ed05c..c233cc98 100644 --- a/nymea-app/ui/mainviews/energy/ConsumersHistory.qml +++ b/nymea-app/ui/mainviews/energy/ConsumersHistory.qml @@ -375,6 +375,8 @@ Item { readonly property Thing thing: consumers.get(index) property AreaSeries series: null + property QHash valueCache: QHash {} + function calculateBaseValue(timestamp) { if (index > 0) { return consumersRepeater.itemAt(index - 1).calculateValue(timestamp) @@ -383,6 +385,12 @@ Item { } function calculateValue(timestamp) { + var cached = valueCache.value(timestamp) +// print("ached:", cached) + if (cached !== undefined) { + return cached//valueCache.value(timestamp) + } + var ret = calculateBaseValue(timestamp) var entry = logs.find(timestamp) @@ -390,6 +398,7 @@ Item { ret += entry.currentPower; } + valueCache.insert(timestamp, ret); // print("calculating value for", thing.name, timestamp, ret) return ret } @@ -408,11 +417,21 @@ Item { series.lowerSeries.removePoints(0, 1); series.upperSeries.removePoints(0, 1); + var oldestTimestamp = null + var newestTimestamp = null + for (var i = 0; i < count; i++) { var entry = logs.get(index + i) // print("got thing entry", thing.name, entry.timestamp, entry.currentPower, index + i) - zeroSeries.ensureValue(entry.timestamp) +// zeroSeries.ensureValue(entry.timestamp) + if (oldestTimestamp == null || entry.timestamp < oldestTimestamp) { + oldestTimestamp = entry.timestamp; + } + if (newestTimestamp == null || entry.timestamp > newestTimestamp) { + newestTimestamp = entry.timestamp; + } + valueAxis.adjustMax(entry.currentPower) insertEntry(index + i, entry) @@ -421,6 +440,9 @@ Item { } } + zeroSeries.ensureValue(oldestTimestamp) + zeroSeries.ensureValue(newestTimestamp) + // Add the leading 0-value entry back series.lowerSeries.insert(0, series.upperSeries.at(0).x, 0) series.upperSeries.insert(0, series.upperSeries.at(0).x, 0) diff --git a/nymea-app/utils/qhashqml.cpp b/nymea-app/utils/qhashqml.cpp new file mode 100644 index 00000000..64d200a0 --- /dev/null +++ b/nymea-app/utils/qhashqml.cpp @@ -0,0 +1,22 @@ +#include "qhashqml.h" + +QHashQml::QHashQml(QObject *parent) + : QObject{parent} +{ + +} + +void QHashQml::insert(int key, const QVariant &value) +{ + m_hash.insert(key, value); +} + +bool QHashQml::contains(int key) +{ + return m_hash.contains(key); +} + +QVariant QHashQml::value(int key) +{ + return m_hash.value(key); +} diff --git a/nymea-app/utils/qhashqml.h b/nymea-app/utils/qhashqml.h new file mode 100644 index 00000000..b46a2821 --- /dev/null +++ b/nymea-app/utils/qhashqml.h @@ -0,0 +1,23 @@ +#ifndef QHASHQML_H +#define QHASHQML_H + +#include +#include +#include + +class QHashQml : public QObject +{ + Q_OBJECT +public: + explicit QHashQml(QObject *parent = nullptr); + + Q_INVOKABLE void insert(int key, const QVariant &value); + Q_INVOKABLE bool contains(int key); + Q_INVOKABLE QVariant value(int key); + + +private: + QHash m_hash; +}; + +#endif // QHASHQML_H